blob: 14460a0a9aba48d9de8520be6f99a07155b0a5e9 [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));
498static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000511static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000513static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000519static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000527static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000528static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000531static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000552static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000558static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000574static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000575static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000576static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000580#ifdef vim_mkdir
581static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
582#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000586static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000588static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000589static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000591static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000592static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000605static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000607static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000614static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000615static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000616static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000618static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000622static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000626static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627#ifdef HAVE_STRFTIME
628static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
630static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000642static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000643static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000644static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000645static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000646static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000648static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000662static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000665static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000667static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
668static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static int get_env_len __ARGS((char_u **arg));
670static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000671static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000672static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
673#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
674#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
675 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000676static 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 +0000677static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000678static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000679static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
680static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static typval_T *alloc_tv __ARGS((void));
682static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void init_tv __ARGS((typval_T *varp));
684static long get_tv_number __ARGS((typval_T *varp));
685static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000686static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static char_u *get_tv_string __ARGS((typval_T *varp));
688static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000691static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
693static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
694static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
695static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
696static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
697static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
698static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000699static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000701static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
703static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
704static int eval_fname_script __ARGS((char_u *p));
705static int eval_fname_sid __ARGS((char_u *p));
706static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000707static ufunc_T *find_func __ARGS((char_u *name));
708static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000709static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000710#ifdef FEAT_PROFILE
711static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000712static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
713static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
714static int
715# ifdef __BORLANDC__
716 _RTLENTRYF
717# endif
718 prof_total_cmp __ARGS((const void *s1, const void *s2));
719static int
720# ifdef __BORLANDC__
721 _RTLENTRYF
722# endif
723 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000724#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000725static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000726static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000727static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void func_free __ARGS((ufunc_T *fp));
729static void func_unref __ARGS((char_u *name));
730static void func_ref __ARGS((char_u *name));
731static 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));
732static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000733static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
734static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000735static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000736static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000737static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000738
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000739/* Character used as separated in autoload function/variable names. */
740#define AUTOLOAD_CHAR '#'
741
Bram Moolenaar33570922005-01-25 22:26:29 +0000742/*
743 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000744 */
745 void
746eval_init()
747{
Bram Moolenaar33570922005-01-25 22:26:29 +0000748 int i;
749 struct vimvar *p;
750
751 init_var_dict(&globvardict, &globvars_var);
752 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000753 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000754 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000755
756 for (i = 0; i < VV_LEN; ++i)
757 {
758 p = &vimvars[i];
759 STRCPY(p->vv_di.di_key, p->vv_name);
760 if (p->vv_flags & VV_RO)
761 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
762 else if (p->vv_flags & VV_RO_SBX)
763 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
764 else
765 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000766
767 /* add to v: scope dict, unless the value is not always available */
768 if (p->vv_type != VAR_UNKNOWN)
769 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000771 /* add to compat scope dict */
772 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000773 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000774}
775
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000776#if defined(EXITFREE) || defined(PROTO)
777 void
778eval_clear()
779{
780 int i;
781 struct vimvar *p;
782
783 for (i = 0; i < VV_LEN; ++i)
784 {
785 p = &vimvars[i];
786 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000787 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000788 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000789 p->vv_di.di_tv.vval.v_string = NULL;
790 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000791 }
792 hash_clear(&vimvarht);
793 hash_clear(&compat_hashtab);
794
795 /* script-local variables */
796 for (i = 1; i <= ga_scripts.ga_len; ++i)
797 vars_clear(&SCRIPT_VARS(i));
798 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000799 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000800
801 /* global variables */
802 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000803
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000804 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000805 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000806 hash_clear(&func_hashtab);
807
808 /* unreferenced lists and dicts */
809 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000810}
811#endif
812
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 * Return the name of the executed function.
815 */
816 char_u *
817func_name(cookie)
818 void *cookie;
819{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
823/*
824 * Return the address holding the next breakpoint line for a funccall cookie.
825 */
826 linenr_T *
827func_breakpoint(cookie)
828 void *cookie;
829{
Bram Moolenaar33570922005-01-25 22:26:29 +0000830 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831}
832
833/*
834 * Return the address holding the debug tick for a funccall cookie.
835 */
836 int *
837func_dbg_tick(cookie)
838 void *cookie;
839{
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
843/*
844 * Return the nesting level for a funccall cookie.
845 */
846 int
847func_level(cookie)
848 void *cookie;
849{
Bram Moolenaar33570922005-01-25 22:26:29 +0000850 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851}
852
853/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000854funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856/*
857 * Return TRUE when a function was ended by a ":return" command.
858 */
859 int
860current_func_returned()
861{
862 return current_funccal->returned;
863}
864
865
866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 * Set an internal variable to a string value. Creates the variable if it does
868 * not already exist.
869 */
870 void
871set_internal_string_var(name, value)
872 char_u *name;
873 char_u *value;
874{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000875 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
878 val = vim_strsave(value);
879 if (val != NULL)
880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000881 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000882 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000884 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000885 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 }
888}
889
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000890static lval_T *redir_lval = NULL;
891static char_u *redir_endp = NULL;
892static char_u *redir_varname = NULL;
893
894/*
895 * Start recording command output to a variable
896 * Returns OK if successfully completed the setup. FAIL otherwise.
897 */
898 int
899var_redir_start(name, append)
900 char_u *name;
901 int append; /* append to an existing variable */
902{
903 int save_emsg;
904 int err;
905 typval_T tv;
906
907 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000908 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000909 {
910 EMSG(_(e_invarg));
911 return FAIL;
912 }
913
914 redir_varname = vim_strsave(name);
915 if (redir_varname == NULL)
916 return FAIL;
917
918 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
919 if (redir_lval == NULL)
920 {
921 var_redir_stop();
922 return FAIL;
923 }
924
925 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000926 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
927 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000928 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
929 {
930 if (redir_endp != NULL && *redir_endp != NUL)
931 /* Trailing characters are present after the variable name */
932 EMSG(_(e_trailing));
933 else
934 EMSG(_(e_invarg));
935 var_redir_stop();
936 return FAIL;
937 }
938
939 /* check if we can write to the variable: set it to or append an empty
940 * string */
941 save_emsg = did_emsg;
942 did_emsg = FALSE;
943 tv.v_type = VAR_STRING;
944 tv.vval.v_string = (char_u *)"";
945 if (append)
946 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
947 else
948 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
949 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000950 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000951 if (err)
952 {
953 var_redir_stop();
954 return FAIL;
955 }
956 if (redir_lval->ll_newkey != NULL)
957 {
958 /* Dictionary item was created, don't do it again. */
959 vim_free(redir_lval->ll_newkey);
960 redir_lval->ll_newkey = NULL;
961 }
962
963 return OK;
964}
965
966/*
967 * Append "value[len]" to the variable set by var_redir_start().
968 */
969 void
970var_redir_str(value, len)
971 char_u *value;
972 int len;
973{
974 char_u *val;
975 typval_T tv;
976 int save_emsg;
977 int err;
978
979 if (redir_lval == NULL)
980 return;
981
982 if (len == -1)
983 /* Append the entire string */
984 val = vim_strsave(value);
985 else
986 /* Append only the specified number of characters */
987 val = vim_strnsave(value, len);
988 if (val == NULL)
989 return;
990
991 tv.v_type = VAR_STRING;
992 tv.vval.v_string = val;
993
994 save_emsg = did_emsg;
995 did_emsg = FALSE;
996 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
997 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000998 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 if (err)
1000 var_redir_stop();
1001
1002 vim_free(tv.vval.v_string);
1003}
1004
1005/*
1006 * Stop redirecting command output to a variable.
1007 */
1008 void
1009var_redir_stop()
1010{
1011 if (redir_lval != NULL)
1012 {
1013 clear_lval(redir_lval);
1014 vim_free(redir_lval);
1015 redir_lval = NULL;
1016 }
1017 vim_free(redir_varname);
1018 redir_varname = NULL;
1019}
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021# if defined(FEAT_MBYTE) || defined(PROTO)
1022 int
1023eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1024 char_u *enc_from;
1025 char_u *enc_to;
1026 char_u *fname_from;
1027 char_u *fname_to;
1028{
1029 int err = FALSE;
1030
1031 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1032 set_vim_var_string(VV_CC_TO, enc_to, -1);
1033 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1034 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1035 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1036 err = TRUE;
1037 set_vim_var_string(VV_CC_FROM, NULL, -1);
1038 set_vim_var_string(VV_CC_TO, NULL, -1);
1039 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1040 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1041
1042 if (err)
1043 return FAIL;
1044 return OK;
1045}
1046# endif
1047
1048# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1049 int
1050eval_printexpr(fname, args)
1051 char_u *fname;
1052 char_u *args;
1053{
1054 int err = FALSE;
1055
1056 set_vim_var_string(VV_FNAME_IN, fname, -1);
1057 set_vim_var_string(VV_CMDARG, args, -1);
1058 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1059 err = TRUE;
1060 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1061 set_vim_var_string(VV_CMDARG, NULL, -1);
1062
1063 if (err)
1064 {
1065 mch_remove(fname);
1066 return FAIL;
1067 }
1068 return OK;
1069}
1070# endif
1071
1072# if defined(FEAT_DIFF) || defined(PROTO)
1073 void
1074eval_diff(origfile, newfile, outfile)
1075 char_u *origfile;
1076 char_u *newfile;
1077 char_u *outfile;
1078{
1079 int err = FALSE;
1080
1081 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1082 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1083 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1084 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1085 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1086 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1087 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1088}
1089
1090 void
1091eval_patch(origfile, difffile, outfile)
1092 char_u *origfile;
1093 char_u *difffile;
1094 char_u *outfile;
1095{
1096 int err;
1097
1098 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1099 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1100 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1101 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1102 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1103 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1104 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1105}
1106# endif
1107
1108/*
1109 * Top level evaluation function, returning a boolean.
1110 * Sets "error" to TRUE if there was an error.
1111 * Return TRUE or FALSE.
1112 */
1113 int
1114eval_to_bool(arg, error, nextcmd, skip)
1115 char_u *arg;
1116 int *error;
1117 char_u **nextcmd;
1118 int skip; /* only parse, don't execute */
1119{
Bram Moolenaar33570922005-01-25 22:26:29 +00001120 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 int retval = FALSE;
1122
1123 if (skip)
1124 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 else
1128 {
1129 *error = FALSE;
1130 if (!skip)
1131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001132 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001133 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 }
1136 if (skip)
1137 --emsg_skip;
1138
1139 return retval;
1140}
1141
1142/*
1143 * Top level evaluation function, returning a string. If "skip" is TRUE,
1144 * only parsing to "nextcmd" is done, without reporting errors. Return
1145 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1146 */
1147 char_u *
1148eval_to_string_skip(arg, nextcmd, skip)
1149 char_u *arg;
1150 char_u **nextcmd;
1151 int skip; /* only parse, don't execute */
1152{
Bram Moolenaar33570922005-01-25 22:26:29 +00001153 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 char_u *retval;
1155
1156 if (skip)
1157 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 retval = NULL;
1160 else
1161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001162 retval = vim_strsave(get_tv_string(&tv));
1163 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 if (skip)
1166 --emsg_skip;
1167
1168 return retval;
1169}
1170
1171/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001172 * Skip over an expression at "*pp".
1173 * Return FAIL for an error, OK otherwise.
1174 */
1175 int
1176skip_expr(pp)
1177 char_u **pp;
1178{
Bram Moolenaar33570922005-01-25 22:26:29 +00001179 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001180
1181 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001183}
1184
1185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 * Top level evaluation function, returning a string.
1187 * Return pointer to allocated memory, or NULL for failure.
1188 */
1189 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001190eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 char_u *arg;
1192 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001193 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
Bram Moolenaar33570922005-01-25 22:26:29 +00001195 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001197 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001199 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 retval = NULL;
1201 else
1202 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001203 if (dolist && tv.v_type == VAR_LIST)
1204 {
1205 ga_init2(&ga, (int)sizeof(char), 80);
1206 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1207 ga_append(&ga, NUL);
1208 retval = (char_u *)ga.ga_data;
1209 }
1210 else
1211 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001212 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
1214
1215 return retval;
1216}
1217
1218/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001219 * Call eval_to_string() without using current local variables and using
1220 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 */
1222 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001223eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 char_u *arg;
1225 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001226 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227{
1228 char_u *retval;
1229 void *save_funccalp;
1230
1231 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001232 if (use_sandbox)
1233 ++sandbox;
1234 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001235 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001236 if (use_sandbox)
1237 --sandbox;
1238 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 restore_funccal(save_funccalp);
1240 return retval;
1241}
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243/*
1244 * Top level evaluation function, returning a number.
1245 * Evaluates "expr" silently.
1246 * Returns -1 for an error.
1247 */
1248 int
1249eval_to_number(expr)
1250 char_u *expr;
1251{
Bram Moolenaar33570922005-01-25 22:26:29 +00001252 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001254 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256 ++emsg_off;
1257
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 retval = -1;
1260 else
1261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001262 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265 --emsg_off;
1266
1267 return retval;
1268}
1269
Bram Moolenaara40058a2005-07-11 22:42:07 +00001270/*
1271 * Prepare v: variable "idx" to be used.
1272 * Save the current typeval in "save_tv".
1273 * When not used yet add the variable to the v: hashtable.
1274 */
1275 static void
1276prepare_vimvar(idx, save_tv)
1277 int idx;
1278 typval_T *save_tv;
1279{
1280 *save_tv = vimvars[idx].vv_tv;
1281 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1282 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1283}
1284
1285/*
1286 * Restore v: variable "idx" to typeval "save_tv".
1287 * When no longer defined, remove the variable from the v: hashtable.
1288 */
1289 static void
1290restore_vimvar(idx, save_tv)
1291 int idx;
1292 typval_T *save_tv;
1293{
1294 hashitem_T *hi;
1295
1296 clear_tv(&vimvars[idx].vv_tv);
1297 vimvars[idx].vv_tv = *save_tv;
1298 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1299 {
1300 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1301 if (HASHITEM_EMPTY(hi))
1302 EMSG2(_(e_intern2), "restore_vimvar()");
1303 else
1304 hash_remove(&vimvarht, hi);
1305 }
1306}
1307
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001308#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001309/*
1310 * Evaluate an expression to a list with suggestions.
1311 * For the "expr:" part of 'spellsuggest'.
1312 */
1313 list_T *
1314eval_spell_expr(badword, expr)
1315 char_u *badword;
1316 char_u *expr;
1317{
1318 typval_T save_val;
1319 typval_T rettv;
1320 list_T *list = NULL;
1321 char_u *p = skipwhite(expr);
1322
1323 /* Set "v:val" to the bad word. */
1324 prepare_vimvar(VV_VAL, &save_val);
1325 vimvars[VV_VAL].vv_type = VAR_STRING;
1326 vimvars[VV_VAL].vv_str = badword;
1327 if (p_verbose == 0)
1328 ++emsg_off;
1329
1330 if (eval1(&p, &rettv, TRUE) == OK)
1331 {
1332 if (rettv.v_type != VAR_LIST)
1333 clear_tv(&rettv);
1334 else
1335 list = rettv.vval.v_list;
1336 }
1337
1338 if (p_verbose == 0)
1339 --emsg_off;
1340 vimvars[VV_VAL].vv_str = NULL;
1341 restore_vimvar(VV_VAL, &save_val);
1342
1343 return list;
1344}
1345
1346/*
1347 * "list" is supposed to contain two items: a word and a number. Return the
1348 * word in "pp" and the number as the return value.
1349 * Return -1 if anything isn't right.
1350 * Used to get the good word and score from the eval_spell_expr() result.
1351 */
1352 int
1353get_spellword(list, pp)
1354 list_T *list;
1355 char_u **pp;
1356{
1357 listitem_T *li;
1358
1359 li = list->lv_first;
1360 if (li == NULL)
1361 return -1;
1362 *pp = get_tv_string(&li->li_tv);
1363
1364 li = li->li_next;
1365 if (li == NULL)
1366 return -1;
1367 return get_tv_number(&li->li_tv);
1368}
1369#endif
1370
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001371/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001372 * Top level evaluation function.
1373 * Returns an allocated typval_T with the result.
1374 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001375 */
1376 typval_T *
1377eval_expr(arg, nextcmd)
1378 char_u *arg;
1379 char_u **nextcmd;
1380{
1381 typval_T *tv;
1382
1383 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001384 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001385 {
1386 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001387 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001388 }
1389
1390 return tv;
1391}
1392
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1395/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001396 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001398 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001400 static int
1401call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 char_u *func;
1403 int argc;
1404 char_u **argv;
1405 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaar33570922005-01-25 22:26:29 +00001408 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 long n;
1410 int len;
1411 int i;
1412 int doesrange;
1413 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001416 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 for (i = 0; i < argc; i++)
1421 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001422 /* Pass a NULL or empty argument as an empty string */
1423 if (argv[i] == NULL || *argv[i] == NUL)
1424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001425 argvars[i].v_type = VAR_STRING;
1426 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001427 continue;
1428 }
1429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 /* Recognize a number argument, the others must be strings. */
1431 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1432 if (len != 0 && len == (int)STRLEN(argv[i]))
1433 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001434 argvars[i].v_type = VAR_NUMBER;
1435 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437 else
1438 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001439 argvars[i].v_type = VAR_STRING;
1440 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442 }
1443
1444 if (safe)
1445 {
1446 save_funccalp = save_funccal();
1447 ++sandbox;
1448 }
1449
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1451 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001453 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (safe)
1455 {
1456 --sandbox;
1457 restore_funccal(save_funccalp);
1458 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459 vim_free(argvars);
1460
1461 if (ret == FAIL)
1462 clear_tv(rettv);
1463
1464 return ret;
1465}
1466
1467/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001468 * Call vimL function "func" and return the result as a string.
1469 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470 * Uses argv[argc] for the function arguments.
1471 */
1472 void *
1473call_func_retstr(func, argc, argv, safe)
1474 char_u *func;
1475 int argc;
1476 char_u **argv;
1477 int safe; /* use the sandbox */
1478{
1479 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001480 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001481
1482 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1483 return NULL;
1484
1485 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 return retval;
1488}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001489
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001490#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001491/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001492 * Call vimL function "func" and return the result as a number.
1493 * Returns -1 when calling the function fails.
1494 * Uses argv[argc] for the function arguments.
1495 */
1496 long
1497call_func_retnr(func, argc, argv, safe)
1498 char_u *func;
1499 int argc;
1500 char_u **argv;
1501 int safe; /* use the sandbox */
1502{
1503 typval_T rettv;
1504 long retval;
1505
1506 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1507 return -1;
1508
1509 retval = get_tv_number_chk(&rettv, NULL);
1510 clear_tv(&rettv);
1511 return retval;
1512}
1513#endif
1514
1515/*
1516 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001517 * Uses argv[argc] for the function arguments.
1518 */
1519 void *
1520call_func_retlist(func, argc, argv, safe)
1521 char_u *func;
1522 int argc;
1523 char_u **argv;
1524 int safe; /* use the sandbox */
1525{
1526 typval_T rettv;
1527
1528 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1529 return NULL;
1530
1531 if (rettv.v_type != VAR_LIST)
1532 {
1533 clear_tv(&rettv);
1534 return NULL;
1535 }
1536
1537 return rettv.vval.v_list;
1538}
1539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
1541
1542/*
1543 * Save the current function call pointer, and set it to NULL.
1544 * Used when executing autocommands and for ":source".
1545 */
1546 void *
1547save_funccal()
1548{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001549 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 current_funccal = NULL;
1552 return (void *)fc;
1553}
1554
1555 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001556restore_funccal(vfc)
1557 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001559 funccall_T *fc = (funccall_T *)vfc;
1560
1561 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562}
1563
Bram Moolenaar05159a02005-02-26 23:04:13 +00001564#if defined(FEAT_PROFILE) || defined(PROTO)
1565/*
1566 * Prepare profiling for entering a child or something else that is not
1567 * counted for the script/function itself.
1568 * Should always be called in pair with prof_child_exit().
1569 */
1570 void
1571prof_child_enter(tm)
1572 proftime_T *tm; /* place to store waittime */
1573{
1574 funccall_T *fc = current_funccal;
1575
1576 if (fc != NULL && fc->func->uf_profiling)
1577 profile_start(&fc->prof_child);
1578 script_prof_save(tm);
1579}
1580
1581/*
1582 * Take care of time spent in a child.
1583 * Should always be called after prof_child_enter().
1584 */
1585 void
1586prof_child_exit(tm)
1587 proftime_T *tm; /* where waittime was stored */
1588{
1589 funccall_T *fc = current_funccal;
1590
1591 if (fc != NULL && fc->func->uf_profiling)
1592 {
1593 profile_end(&fc->prof_child);
1594 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1595 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1596 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1597 }
1598 script_prof_restore(tm);
1599}
1600#endif
1601
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603#ifdef FEAT_FOLDING
1604/*
1605 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1606 * it in "*cp". Doesn't give error messages.
1607 */
1608 int
1609eval_foldexpr(arg, cp)
1610 char_u *arg;
1611 int *cp;
1612{
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 int retval;
1615 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001616 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1617 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001620 if (use_sandbox)
1621 ++sandbox;
1622 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 retval = 0;
1626 else
1627 {
1628 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001629 if (tv.v_type == VAR_NUMBER)
1630 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001631 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 retval = 0;
1633 else
1634 {
1635 /* If the result is a string, check if there is a non-digit before
1636 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001637 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (!VIM_ISDIGIT(*s) && *s != '-')
1639 *cp = *s++;
1640 retval = atol((char *)s);
1641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001642 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001645 if (use_sandbox)
1646 --sandbox;
1647 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 return retval;
1650}
1651#endif
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 * ":let" list all variable values
1655 * ":let var1 var2" list variable values
1656 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001657 * ":let var += expr" assignment command.
1658 * ":let var -= expr" assignment command.
1659 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
1662 void
1663ex_let(eap)
1664 exarg_T *eap;
1665{
1666 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001667 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001668 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 int var_count = 0;
1671 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001673 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaardb552d602006-03-23 22:59:57 +00001675 argend = skip_var_list(arg, &var_count, &semicolon);
1676 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001678 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1679 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001680 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001683 /*
1684 * ":let" without "=": list variables
1685 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 if (*arg == '[')
1687 EMSG(_(e_invarg));
1688 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 /* ":let var1 var2" */
1690 arg = list_arg_vars(eap, arg);
1691 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001693 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001694 list_glob_vars();
1695 list_buf_vars();
1696 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001697#ifdef FEAT_WINDOWS
1698 list_tab_vars();
1699#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001700 list_script_vars();
1701 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001702 list_vim_vars();
1703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 eap->nextcmd = check_nextcmd(arg);
1705 }
1706 else
1707 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001708 op[0] = '=';
1709 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001710 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001711 {
1712 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1713 op[0] = expr[-1]; /* +=, -= or .= */
1714 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (eap->skip)
1718 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (eap->skip)
1721 {
1722 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001723 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 --emsg_skip;
1725 }
1726 else if (i != FAIL)
1727 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001729 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001730 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
1732 }
1733}
1734
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735/*
1736 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1737 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 * When "nextchars" is not NULL it points to a string with characters that
1739 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1740 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 * Returns OK or FAIL;
1742 */
1743 static int
1744ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1745 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001746 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 int copy; /* copy values from "tv", don't move */
1748 int semicolon; /* from skip_var_list() */
1749 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001750 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751{
1752 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001753 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001755 listitem_T *item;
1756 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757
1758 if (*arg != '[')
1759 {
1760 /*
1761 * ":let var = expr" or ":for var in list"
1762 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001763 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 return FAIL;
1765 return OK;
1766 }
1767
1768 /*
1769 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1770 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001771 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 {
1773 EMSG(_(e_listreq));
1774 return FAIL;
1775 }
1776
1777 i = list_len(l);
1778 if (semicolon == 0 && var_count < i)
1779 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001780 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 return FAIL;
1782 }
1783 if (var_count - semicolon > i)
1784 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001785 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 return FAIL;
1787 }
1788
1789 item = l->lv_first;
1790 while (*arg != ']')
1791 {
1792 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 item = item->li_next;
1795 if (arg == NULL)
1796 return FAIL;
1797
1798 arg = skipwhite(arg);
1799 if (*arg == ';')
1800 {
1801 /* Put the rest of the list (may be empty) in the var after ';'.
1802 * Create a new list for this. */
1803 l = list_alloc();
1804 if (l == NULL)
1805 return FAIL;
1806 while (item != NULL)
1807 {
1808 list_append_tv(l, &item->li_tv);
1809 item = item->li_next;
1810 }
1811
1812 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001813 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 ltv.vval.v_list = l;
1815 l->lv_refcount = 1;
1816
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001817 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1818 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 clear_tv(&ltv);
1820 if (arg == NULL)
1821 return FAIL;
1822 break;
1823 }
1824 else if (*arg != ',' && *arg != ']')
1825 {
1826 EMSG2(_(e_intern2), "ex_let_vars()");
1827 return FAIL;
1828 }
1829 }
1830
1831 return OK;
1832}
1833
1834/*
1835 * Skip over assignable variable "var" or list of variables "[var, var]".
1836 * Used for ":let varvar = expr" and ":for varvar in expr".
1837 * For "[var, var]" increment "*var_count" for each variable.
1838 * for "[var, var; var]" set "semicolon".
1839 * Return NULL for an error.
1840 */
1841 static char_u *
1842skip_var_list(arg, var_count, semicolon)
1843 char_u *arg;
1844 int *var_count;
1845 int *semicolon;
1846{
1847 char_u *p, *s;
1848
1849 if (*arg == '[')
1850 {
1851 /* "[var, var]": find the matching ']'. */
1852 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001853 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 {
1855 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1856 s = skip_var_one(p);
1857 if (s == p)
1858 {
1859 EMSG2(_(e_invarg2), p);
1860 return NULL;
1861 }
1862 ++*var_count;
1863
1864 p = skipwhite(s);
1865 if (*p == ']')
1866 break;
1867 else if (*p == ';')
1868 {
1869 if (*semicolon == 1)
1870 {
1871 EMSG(_("Double ; in list of variables"));
1872 return NULL;
1873 }
1874 *semicolon = 1;
1875 }
1876 else if (*p != ',')
1877 {
1878 EMSG2(_(e_invarg2), p);
1879 return NULL;
1880 }
1881 }
1882 return p + 1;
1883 }
1884 else
1885 return skip_var_one(arg);
1886}
1887
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001888/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001889 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1890 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001891 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 static char_u *
1893skip_var_one(arg)
1894 char_u *arg;
1895{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001896 if (*arg == '@' && arg[1] != NUL)
1897 return arg + 2;
1898 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1899 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900}
1901
Bram Moolenaara7043832005-01-21 11:56:39 +00001902/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 * List variables for hashtab "ht" with prefix "prefix".
1904 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001905 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001907list_hashtable_vars(ht, prefix, empty)
1908 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001911{
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 hashitem_T *hi;
1913 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001914 int todo;
1915
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001916 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1918 {
1919 if (!HASHITEM_EMPTY(hi))
1920 {
1921 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 di = HI2DI(hi);
1923 if (empty || di->di_tv.v_type != VAR_STRING
1924 || di->di_tv.vval.v_string != NULL)
1925 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 }
1927 }
1928}
1929
1930/*
1931 * List global variables.
1932 */
1933 static void
1934list_glob_vars()
1935{
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937}
1938
1939/*
1940 * List buffer variables.
1941 */
1942 static void
1943list_buf_vars()
1944{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001945 char_u numbuf[NUMBUFLEN];
1946
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001948
1949 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1950 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001951}
1952
1953/*
1954 * List window variables.
1955 */
1956 static void
1957list_win_vars()
1958{
Bram Moolenaar33570922005-01-25 22:26:29 +00001959 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001960}
1961
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001962#ifdef FEAT_WINDOWS
1963/*
1964 * List tab page variables.
1965 */
1966 static void
1967list_tab_vars()
1968{
1969 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1970}
1971#endif
1972
Bram Moolenaara7043832005-01-21 11:56:39 +00001973/*
1974 * List Vim variables.
1975 */
1976 static void
1977list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001978{
Bram Moolenaar33570922005-01-25 22:26:29 +00001979 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980}
1981
1982/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001983 * List script-local variables, if there is a script.
1984 */
1985 static void
1986list_script_vars()
1987{
1988 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1989 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1990}
1991
1992/*
1993 * List function variables, if there is a function.
1994 */
1995 static void
1996list_func_vars()
1997{
1998 if (current_funccal != NULL)
1999 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2000 (char_u *)"l:", FALSE);
2001}
2002
2003/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 * List variables in "arg".
2005 */
2006 static char_u *
2007list_arg_vars(eap, arg)
2008 exarg_T *eap;
2009 char_u *arg;
2010{
2011 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002012 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002014 char_u *name_start;
2015 char_u *arg_subsc;
2016 char_u *tofree;
2017 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018
2019 while (!ends_excmd(*arg) && !got_int)
2020 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002021 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002022 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002023 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002024 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2025 {
2026 emsg_severe = TRUE;
2027 EMSG(_(e_trailing));
2028 break;
2029 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 /* get_name_len() takes care of expanding curly braces */
2034 name_start = name = arg;
2035 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2036 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002038 /* This is mainly to keep test 49 working: when expanding
2039 * curly braces fails overrule the exception error message. */
2040 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002042 emsg_severe = TRUE;
2043 EMSG2(_(e_invarg2), arg);
2044 break;
2045 }
2046 error = TRUE;
2047 }
2048 else
2049 {
2050 if (tofree != NULL)
2051 name = tofree;
2052 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002053 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002054 else
2055 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 /* handle d.key, l[idx], f(expr) */
2057 arg_subsc = arg;
2058 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002059 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 case 'g': list_glob_vars(); break;
2067 case 'b': list_buf_vars(); break;
2068 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002069#ifdef FEAT_WINDOWS
2070 case 't': list_tab_vars(); break;
2071#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002073 case 's': list_script_vars(); break;
2074 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 default:
2076 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002078 }
2079 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002080 {
2081 char_u numbuf[NUMBUFLEN];
2082 char_u *tf;
2083 int c;
2084 char_u *s;
2085
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002086 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002087 c = *arg;
2088 *arg = NUL;
2089 list_one_var_a((char_u *)"",
2090 arg == arg_subsc ? name : name_start,
2091 tv.v_type, s == NULL ? (char_u *)"" : s);
2092 *arg = c;
2093 vim_free(tf);
2094 }
2095 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 }
2098 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002099
2100 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002102
2103 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 }
2105
2106 return arg;
2107}
2108
2109/*
2110 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2111 * Returns a pointer to the char just after the var name.
2112 * Returns NULL if there is an error.
2113 */
2114 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002115ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002120 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121{
2122 int c1;
2123 char_u *name;
2124 char_u *p;
2125 char_u *arg_end = NULL;
2126 int len;
2127 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129
2130 /*
2131 * ":let $VAR = expr": Set environment variable.
2132 */
2133 if (*arg == '$')
2134 {
2135 /* Find the end of the name. */
2136 ++arg;
2137 name = arg;
2138 len = get_env_len(&arg);
2139 if (len == 0)
2140 EMSG2(_(e_invarg2), name - 1);
2141 else
2142 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002143 if (op != NULL && (*op == '+' || *op == '-'))
2144 EMSG2(_(e_letwrong), op);
2145 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 EMSG(_(e_letunexp));
2148 else
2149 {
2150 c1 = name[len];
2151 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 p = get_tv_string_chk(tv);
2153 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002154 {
2155 int mustfree = FALSE;
2156 char_u *s = vim_getenv(name, &mustfree);
2157
2158 if (s != NULL)
2159 {
2160 p = tofree = concat_str(s, p);
2161 if (mustfree)
2162 vim_free(s);
2163 }
2164 }
2165 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002166 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 if (STRICMP(name, "HOME") == 0)
2169 init_homedir();
2170 else if (didset_vim && STRICMP(name, "VIM") == 0)
2171 didset_vim = FALSE;
2172 else if (didset_vimruntime
2173 && STRICMP(name, "VIMRUNTIME") == 0)
2174 didset_vimruntime = FALSE;
2175 arg_end = arg;
2176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 }
2180 }
2181 }
2182
2183 /*
2184 * ":let &option = expr": Set option value.
2185 * ":let &l:option = expr": Set local option value.
2186 * ":let &g:option = expr": Set global option value.
2187 */
2188 else if (*arg == '&')
2189 {
2190 /* Find the end of the name. */
2191 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002192 if (p == NULL || (endchars != NULL
2193 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 EMSG(_(e_letunexp));
2195 else
2196 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002197 long n;
2198 int opt_type;
2199 long numval;
2200 char_u *stringval = NULL;
2201 char_u *s;
2202
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 c1 = *p;
2204 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205
2206 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002207 s = get_tv_string_chk(tv); /* != NULL if number or string */
2208 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002209 {
2210 opt_type = get_option_value(arg, &numval,
2211 &stringval, opt_flags);
2212 if ((opt_type == 1 && *op == '.')
2213 || (opt_type == 0 && *op != '.'))
2214 EMSG2(_(e_letwrong), op);
2215 else
2216 {
2217 if (opt_type == 1) /* number */
2218 {
2219 if (*op == '+')
2220 n = numval + n;
2221 else
2222 n = numval - n;
2223 }
2224 else if (opt_type == 0 && stringval != NULL) /* string */
2225 {
2226 s = concat_str(stringval, s);
2227 vim_free(stringval);
2228 stringval = s;
2229 }
2230 }
2231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002232 if (s != NULL)
2233 {
2234 set_option_value(arg, n, s, opt_flags);
2235 arg_end = p;
2236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002237 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002238 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 }
2240 }
2241
2242 /*
2243 * ":let @r = expr": Set register contents.
2244 */
2245 else if (*arg == '@')
2246 {
2247 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002248 if (op != NULL && (*op == '+' || *op == '-'))
2249 EMSG2(_(e_letwrong), op);
2250 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002251 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 EMSG(_(e_letunexp));
2253 else
2254 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002255 char_u *tofree = NULL;
2256 char_u *s;
2257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002258 p = get_tv_string_chk(tv);
2259 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002260 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002261 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002262 if (s != NULL)
2263 {
2264 p = tofree = concat_str(s, p);
2265 vim_free(s);
2266 }
2267 }
2268 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002270 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002271 arg_end = arg + 1;
2272 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002273 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 }
2275 }
2276
2277 /*
2278 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002281 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002283 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002285 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2289 EMSG(_(e_letunexp));
2290 else
2291 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002292 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002293 arg_end = p;
2294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002296 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298
2299 else
2300 EMSG2(_(e_invarg2), arg);
2301
2302 return arg_end;
2303}
2304
2305/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002306 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2307 */
2308 static int
2309check_changedtick(arg)
2310 char_u *arg;
2311{
2312 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2313 {
2314 EMSG2(_(e_readonlyvar), arg);
2315 return TRUE;
2316 }
2317 return FALSE;
2318}
2319
2320/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 * Get an lval: variable, Dict item or List item that can be assigned a value
2322 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2323 * "name.key", "name.key[expr]" etc.
2324 * Indexing only works if "name" is an existing List or Dictionary.
2325 * "name" points to the start of the name.
2326 * If "rettv" is not NULL it points to the value to be assigned.
2327 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2328 * wrong; must end in space or cmd separator.
2329 *
2330 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 */
2334 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002335get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002337 typval_T *rettv;
2338 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002339 int unlet;
2340 int skip;
2341 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002342 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 char_u *expr_start, *expr_end;
2346 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002347 dictitem_T *v;
2348 typval_T var1;
2349 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002351 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002352 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002354 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002357 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358
2359 if (skip)
2360 {
2361 /* When skipping just find the end of the name. */
2362 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002363 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 }
2365
2366 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002367 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002368 if (expr_start != NULL)
2369 {
2370 /* Don't expand the name when we already know there is an error. */
2371 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2372 && *p != '[' && *p != '.')
2373 {
2374 EMSG(_(e_trailing));
2375 return NULL;
2376 }
2377
2378 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2379 if (lp->ll_exp_name == NULL)
2380 {
2381 /* Report an invalid expression in braces, unless the
2382 * expression evaluation has been cancelled due to an
2383 * aborting error, an interrupt, or an exception. */
2384 if (!aborting() && !quiet)
2385 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002386 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 EMSG2(_(e_invarg2), name);
2388 return NULL;
2389 }
2390 }
2391 lp->ll_name = lp->ll_exp_name;
2392 }
2393 else
2394 lp->ll_name = name;
2395
2396 /* Without [idx] or .key we are done. */
2397 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2398 return p;
2399
2400 cc = *p;
2401 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002402 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 if (v == NULL && !quiet)
2404 EMSG2(_(e_undefvar), lp->ll_name);
2405 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 if (v == NULL)
2407 return NULL;
2408
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 /*
2410 * Loop until no more [idx] or .key is following.
2411 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002412 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2416 && !(lp->ll_tv->v_type == VAR_DICT
2417 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 if (!quiet)
2420 EMSG(_("E689: Can only index a List or Dictionary"));
2421 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (!quiet)
2426 EMSG(_("E708: [:] must come last"));
2427 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002429
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 len = -1;
2431 if (*p == '.')
2432 {
2433 key = p + 1;
2434 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2435 ;
2436 if (len == 0)
2437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (!quiet)
2439 EMSG(_(e_emptykey));
2440 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 }
2442 p = key + len;
2443 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002444 else
2445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002447 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 if (*p == ':')
2449 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002450 else
2451 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 empty1 = FALSE;
2453 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 if (get_tv_string_chk(&var1) == NULL)
2456 {
2457 /* not a number or string */
2458 clear_tv(&var1);
2459 return NULL;
2460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002461 }
2462
2463 /* Optionally get the second index [ :expr]. */
2464 if (*p == ':')
2465 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002470 if (!empty1)
2471 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (rettv != NULL && (rettv->v_type != VAR_LIST
2475 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 if (!quiet)
2478 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002479 if (!empty1)
2480 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 }
2483 p = skipwhite(p + 1);
2484 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 else
2487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002489 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 if (!empty1)
2492 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 if (get_tv_string_chk(&var2) == NULL)
2496 {
2497 /* not a number or string */
2498 if (!empty1)
2499 clear_tv(&var1);
2500 clear_tv(&var2);
2501 return NULL;
2502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002505 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002508
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002510 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (!quiet)
2512 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513 if (!empty1)
2514 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 }
2519
2520 /* Skip to past ']'. */
2521 ++p;
2522 }
2523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 {
2526 if (len == -1)
2527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002529 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 if (*key == NUL)
2531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (!quiet)
2533 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 }
2537 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 lp->ll_list = NULL;
2539 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002540 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002543 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548 if (len == -1)
2549 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 }
2552 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 if (len == -1)
2557 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 p = NULL;
2560 break;
2561 }
2562 if (len == -1)
2563 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002565 }
2566 else
2567 {
2568 /*
2569 * Get the number and item for the only or first index of the List.
2570 */
2571 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 else
2574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002575 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002576 clear_tv(&var1);
2577 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002578 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 lp->ll_list = lp->ll_tv->vval.v_list;
2580 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2581 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (!quiet)
2584 EMSGN(_(e_listidx), lp->ll_n1);
2585 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 }
2589
2590 /*
2591 * May need to find the item or absolute index for the second
2592 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 * When no index given: "lp->ll_empty2" is TRUE.
2594 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002598 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 if (ni == NULL)
2604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSGN(_(e_listidx), lp->ll_n2);
2607 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 }
2611
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2613 if (lp->ll_n1 < 0)
2614 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2615 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (!quiet)
2618 EMSGN(_(e_listidx), lp->ll_n2);
2619 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002621 }
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002625 }
2626
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 return p;
2628}
2629
2630/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 */
2633 static void
2634clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636{
2637 vim_free(lp->ll_exp_name);
2638 vim_free(lp->ll_newkey);
2639}
2640
2641/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002642 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002644 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 */
2646 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002652 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653{
2654 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002655 listitem_T *ri;
2656 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657
2658 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 cc = *endp;
2663 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664 if (op != NULL && *op != '=')
2665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002666 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002668 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002669 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002670 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 {
2672 if (tv_op(&tv, rettv, op) == OK)
2673 set_var(lp->ll_name, &tv, FALSE);
2674 clear_tv(&tv);
2675 }
2676 }
2677 else
2678 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002682 else if (tv_check_lock(lp->ll_newkey == NULL
2683 ? lp->ll_tv->v_lock
2684 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2685 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 else if (lp->ll_range)
2687 {
2688 /*
2689 * Assign the List values to the list items.
2690 */
2691 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 if (op != NULL && *op != '=')
2694 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2695 else
2696 {
2697 clear_tv(&lp->ll_li->li_tv);
2698 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 ri = ri->li_next;
2701 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2702 break;
2703 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002706 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 ri = NULL;
2709 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_li = lp->ll_li->li_next;
2713 ++lp->ll_n1;
2714 }
2715 if (ri != NULL)
2716 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717 else if (lp->ll_empty2
2718 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 : lp->ll_n1 != lp->ll_n2)
2720 EMSG(_("E711: List value has not enough items"));
2721 }
2722 else
2723 {
2724 /*
2725 * Assign to a List or Dictionary item.
2726 */
2727 if (lp->ll_newkey != NULL)
2728 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002729 if (op != NULL && *op != '=')
2730 {
2731 EMSG2(_(e_letwrong), op);
2732 return;
2733 }
2734
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002736 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (di == NULL)
2738 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2740 {
2741 vim_free(di);
2742 return;
2743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 else if (op != NULL && *op != '=')
2747 {
2748 tv_op(lp->ll_tv, rettv, op);
2749 return;
2750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 /*
2755 * Assign the value to the variable or list item.
2756 */
2757 if (copy)
2758 copy_tv(rettv, lp->ll_tv);
2759 else
2760 {
2761 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002762 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002764 }
2765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002766}
2767
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2770 * Returns OK or FAIL.
2771 */
2772 static int
2773tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 typval_T *tv1;
2775 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 char_u *op;
2777{
2778 long n;
2779 char_u numbuf[NUMBUFLEN];
2780 char_u *s;
2781
2782 /* Can't do anything with a Funcref or a Dict on the right. */
2783 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2784 {
2785 switch (tv1->v_type)
2786 {
2787 case VAR_DICT:
2788 case VAR_FUNC:
2789 break;
2790
2791 case VAR_LIST:
2792 if (*op != '+' || tv2->v_type != VAR_LIST)
2793 break;
2794 /* List += List */
2795 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2796 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2797 return OK;
2798
2799 case VAR_NUMBER:
2800 case VAR_STRING:
2801 if (tv2->v_type == VAR_LIST)
2802 break;
2803 if (*op == '+' || *op == '-')
2804 {
2805 /* nr += nr or nr -= nr*/
2806 n = get_tv_number(tv1);
2807 if (*op == '+')
2808 n += get_tv_number(tv2);
2809 else
2810 n -= get_tv_number(tv2);
2811 clear_tv(tv1);
2812 tv1->v_type = VAR_NUMBER;
2813 tv1->vval.v_number = n;
2814 }
2815 else
2816 {
2817 /* str .= str */
2818 s = get_tv_string(tv1);
2819 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2820 clear_tv(tv1);
2821 tv1->v_type = VAR_STRING;
2822 tv1->vval.v_string = s;
2823 }
2824 return OK;
2825 }
2826 }
2827
2828 EMSG2(_(e_letwrong), op);
2829 return FAIL;
2830}
2831
2832/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 * Add a watcher to a list.
2834 */
2835 static void
2836list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002837 list_T *l;
2838 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839{
2840 lw->lw_next = l->lv_watch;
2841 l->lv_watch = lw;
2842}
2843
2844/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002845 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846 * No warning when it isn't found...
2847 */
2848 static void
2849list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 list_T *l;
2851 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852{
Bram Moolenaar33570922005-01-25 22:26:29 +00002853 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854
2855 lwp = &l->lv_watch;
2856 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2857 {
2858 if (lw == lwrem)
2859 {
2860 *lwp = lw->lw_next;
2861 break;
2862 }
2863 lwp = &lw->lw_next;
2864 }
2865}
2866
2867/*
2868 * Just before removing an item from a list: advance watchers to the next
2869 * item.
2870 */
2871 static void
2872list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 list_T *l;
2874 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875{
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877
2878 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2879 if (lw->lw_item == item)
2880 lw->lw_item = item->li_next;
2881}
2882
2883/*
2884 * Evaluate the expression used in a ":for var in expr" command.
2885 * "arg" points to "var".
2886 * Set "*errp" to TRUE for an error, FALSE otherwise;
2887 * Return a pointer that holds the info. Null when there is an error.
2888 */
2889 void *
2890eval_for_line(arg, errp, nextcmdp, skip)
2891 char_u *arg;
2892 int *errp;
2893 char_u **nextcmdp;
2894 int skip;
2895{
Bram Moolenaar33570922005-01-25 22:26:29 +00002896 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 typval_T tv;
2899 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900
2901 *errp = TRUE; /* default: there is an error */
2902
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 if (fi == NULL)
2905 return NULL;
2906
2907 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2908 if (expr == NULL)
2909 return fi;
2910
2911 expr = skipwhite(expr);
2912 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2913 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002914 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 return fi;
2916 }
2917
2918 if (skip)
2919 ++emsg_skip;
2920 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2921 {
2922 *errp = FALSE;
2923 if (!skip)
2924 {
2925 l = tv.vval.v_list;
2926 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002927 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002929 clear_tv(&tv);
2930 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 else
2932 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002933 /* No need to increment the refcount, it's already set for the
2934 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 fi->fi_list = l;
2936 list_add_watch(l, &fi->fi_lw);
2937 fi->fi_lw.lw_item = l->lv_first;
2938 }
2939 }
2940 }
2941 if (skip)
2942 --emsg_skip;
2943
2944 return fi;
2945}
2946
2947/*
2948 * Use the first item in a ":for" list. Advance to the next.
2949 * Assign the values to the variable (list). "arg" points to the first one.
2950 * Return TRUE when a valid item was found, FALSE when at end of list or
2951 * something wrong.
2952 */
2953 int
2954next_for_item(fi_void, arg)
2955 void *fi_void;
2956 char_u *arg;
2957{
Bram Moolenaar33570922005-01-25 22:26:29 +00002958 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002961
2962 item = fi->fi_lw.lw_item;
2963 if (item == NULL)
2964 result = FALSE;
2965 else
2966 {
2967 fi->fi_lw.lw_item = item->li_next;
2968 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2969 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2970 }
2971 return result;
2972}
2973
2974/*
2975 * Free the structure used to store info used by ":for".
2976 */
2977 void
2978free_for_info(fi_void)
2979 void *fi_void;
2980{
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002983 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002984 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002986 list_unref(fi->fi_list);
2987 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002988 vim_free(fi);
2989}
2990
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2992
2993 void
2994set_context_for_expression(xp, arg, cmdidx)
2995 expand_T *xp;
2996 char_u *arg;
2997 cmdidx_T cmdidx;
2998{
2999 int got_eq = FALSE;
3000 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 if (cmdidx == CMD_let)
3004 {
3005 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003006 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 {
3008 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003009 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010 {
3011 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 if (vim_iswhite(*p))
3014 break;
3015 }
3016 return;
3017 }
3018 }
3019 else
3020 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3021 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 while ((xp->xp_pattern = vim_strpbrk(arg,
3023 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3024 {
3025 c = *xp->xp_pattern;
3026 if (c == '&')
3027 {
3028 c = xp->xp_pattern[1];
3029 if (c == '&')
3030 {
3031 ++xp->xp_pattern;
3032 xp->xp_context = cmdidx != CMD_let || got_eq
3033 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3034 }
3035 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003038 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3039 xp->xp_pattern += 2;
3040
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 else if (c == '$')
3044 {
3045 /* environment variable */
3046 xp->xp_context = EXPAND_ENV_VARS;
3047 }
3048 else if (c == '=')
3049 {
3050 got_eq = TRUE;
3051 xp->xp_context = EXPAND_EXPRESSION;
3052 }
3053 else if (c == '<'
3054 && xp->xp_context == EXPAND_FUNCTIONS
3055 && vim_strchr(xp->xp_pattern, '(') == NULL)
3056 {
3057 /* Function name can start with "<SNR>" */
3058 break;
3059 }
3060 else if (cmdidx != CMD_let || got_eq)
3061 {
3062 if (c == '"') /* string */
3063 {
3064 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3065 if (c == '\\' && xp->xp_pattern[1] != NUL)
3066 ++xp->xp_pattern;
3067 xp->xp_context = EXPAND_NOTHING;
3068 }
3069 else if (c == '\'') /* literal string */
3070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003071 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3073 /* skip */ ;
3074 xp->xp_context = EXPAND_NOTHING;
3075 }
3076 else if (c == '|')
3077 {
3078 if (xp->xp_pattern[1] == '|')
3079 {
3080 ++xp->xp_pattern;
3081 xp->xp_context = EXPAND_EXPRESSION;
3082 }
3083 else
3084 xp->xp_context = EXPAND_COMMANDS;
3085 }
3086 else
3087 xp->xp_context = EXPAND_EXPRESSION;
3088 }
3089 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090 /* Doesn't look like something valid, expand as an expression
3091 * anyway. */
3092 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 arg = xp->xp_pattern;
3094 if (*arg != NUL)
3095 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3096 /* skip */ ;
3097 }
3098 xp->xp_pattern = arg;
3099}
3100
3101#endif /* FEAT_CMDL_COMPL */
3102
3103/*
3104 * ":1,25call func(arg1, arg2)" function call.
3105 */
3106 void
3107ex_call(eap)
3108 exarg_T *eap;
3109{
3110 char_u *arg = eap->arg;
3111 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 linenr_T lnum;
3117 int doesrange;
3118 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003121 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3122 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 if (tofree == NULL)
3124 return;
3125
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003126 /* Increase refcount on dictionary, it could get deleted when evaluating
3127 * the arguments. */
3128 if (fudi.fd_dict != NULL)
3129 ++fudi.fd_dict->dv_refcount;
3130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003132 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
Bram Moolenaar532c7802005-01-27 14:44:31 +00003135 /* Skip white space to allow ":call func ()". Not good, but required for
3136 * backward compatibility. */
3137 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003138 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 if (*startarg != '(')
3141 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003142 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 goto end;
3144 }
3145
3146 /*
3147 * When skipping, evaluate the function once, to find the end of the
3148 * arguments.
3149 * When the function takes a range, this is discovered after the first
3150 * call, and the loop is broken.
3151 */
3152 if (eap->skip)
3153 {
3154 ++emsg_skip;
3155 lnum = eap->line2; /* do it once, also with an invalid range */
3156 }
3157 else
3158 lnum = eap->line1;
3159 for ( ; lnum <= eap->line2; ++lnum)
3160 {
3161 if (!eap->skip && eap->addr_count > 0)
3162 {
3163 curwin->w_cursor.lnum = lnum;
3164 curwin->w_cursor.col = 0;
3165 }
3166 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003167 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003168 eap->line1, eap->line2, &doesrange,
3169 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
3171 failed = TRUE;
3172 break;
3173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003174 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (doesrange || eap->skip)
3176 break;
3177 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003178 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003179 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003180 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (aborting())
3182 break;
3183 }
3184 if (eap->skip)
3185 --emsg_skip;
3186
3187 if (!failed)
3188 {
3189 /* Check for trailing illegal characters and a following command. */
3190 if (!ends_excmd(*arg))
3191 {
3192 emsg_severe = TRUE;
3193 EMSG(_(e_trailing));
3194 }
3195 else
3196 eap->nextcmd = check_nextcmd(arg);
3197 }
3198
3199end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003200 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003201 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202}
3203
3204/*
3205 * ":unlet[!] var1 ... " command.
3206 */
3207 void
3208ex_unlet(eap)
3209 exarg_T *eap;
3210{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003211 ex_unletlock(eap, eap->arg, 0);
3212}
3213
3214/*
3215 * ":lockvar" and ":unlockvar" commands
3216 */
3217 void
3218ex_lockvar(eap)
3219 exarg_T *eap;
3220{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003222 int deep = 2;
3223
3224 if (eap->forceit)
3225 deep = -1;
3226 else if (vim_isdigit(*arg))
3227 {
3228 deep = getdigits(&arg);
3229 arg = skipwhite(arg);
3230 }
3231
3232 ex_unletlock(eap, arg, deep);
3233}
3234
3235/*
3236 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3237 */
3238 static void
3239ex_unletlock(eap, argstart, deep)
3240 exarg_T *eap;
3241 char_u *argstart;
3242 int deep;
3243{
3244 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003247 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 do
3250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003252 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3253 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 if (lv.ll_name == NULL)
3255 error = TRUE; /* error but continue parsing */
3256 if (name_end == NULL || (!vim_iswhite(*name_end)
3257 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 if (name_end != NULL)
3260 {
3261 emsg_severe = TRUE;
3262 EMSG(_(e_trailing));
3263 }
3264 if (!(eap->skip || error))
3265 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 break;
3267 }
3268
3269 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270 {
3271 if (eap->cmdidx == CMD_unlet)
3272 {
3273 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3274 error = TRUE;
3275 }
3276 else
3277 {
3278 if (do_lock_var(&lv, name_end, deep,
3279 eap->cmdidx == CMD_lockvar) == FAIL)
3280 error = TRUE;
3281 }
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003284 if (!eap->skip)
3285 clear_lval(&lv);
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 arg = skipwhite(name_end);
3288 } while (!ends_excmd(*arg));
3289
3290 eap->nextcmd = check_nextcmd(arg);
3291}
3292
Bram Moolenaar8c711452005-01-14 21:53:12 +00003293 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003294do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003296 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003297 int forceit;
3298{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003299 int ret = OK;
3300 int cc;
3301
3302 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003303 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003304 cc = *name_end;
3305 *name_end = NUL;
3306
3307 /* Normal name or expanded name. */
3308 if (check_changedtick(lp->ll_name))
3309 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003310 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003312 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003313 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003314 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3315 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316 else if (lp->ll_range)
3317 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003319
3320 /* Delete a range of List items. */
3321 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3322 {
3323 li = lp->ll_li->li_next;
3324 listitem_remove(lp->ll_list, lp->ll_li);
3325 lp->ll_li = li;
3326 ++lp->ll_n1;
3327 }
3328 }
3329 else
3330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003331 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003332 /* unlet a List item. */
3333 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003334 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003335 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003336 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003337 }
3338
3339 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003340}
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342/*
3343 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003344 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
3346 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003347do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003349 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 hashtab_T *ht;
3352 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003353 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaar33570922005-01-25 22:26:29 +00003355 ht = find_var_ht(name, &varname);
3356 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 hi = hash_find(ht, varname);
3359 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003360 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003361 if (var_check_ro(HI2DI(hi)->di_flags, name))
3362 return FAIL;
3363 delete_var(ht, hi);
3364 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003367 if (forceit)
3368 return OK;
3369 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 return FAIL;
3371}
3372
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003373/*
3374 * Lock or unlock variable indicated by "lp".
3375 * "deep" is the levels to go (-1 for unlimited);
3376 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3377 */
3378 static int
3379do_lock_var(lp, name_end, deep, lock)
3380 lval_T *lp;
3381 char_u *name_end;
3382 int deep;
3383 int lock;
3384{
3385 int ret = OK;
3386 int cc;
3387 dictitem_T *di;
3388
3389 if (deep == 0) /* nothing to do */
3390 return OK;
3391
3392 if (lp->ll_tv == NULL)
3393 {
3394 cc = *name_end;
3395 *name_end = NUL;
3396
3397 /* Normal name or expanded name. */
3398 if (check_changedtick(lp->ll_name))
3399 ret = FAIL;
3400 else
3401 {
3402 di = find_var(lp->ll_name, NULL);
3403 if (di == NULL)
3404 ret = FAIL;
3405 else
3406 {
3407 if (lock)
3408 di->di_flags |= DI_FLAGS_LOCK;
3409 else
3410 di->di_flags &= ~DI_FLAGS_LOCK;
3411 item_lock(&di->di_tv, deep, lock);
3412 }
3413 }
3414 *name_end = cc;
3415 }
3416 else if (lp->ll_range)
3417 {
3418 listitem_T *li = lp->ll_li;
3419
3420 /* (un)lock a range of List items. */
3421 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3422 {
3423 item_lock(&li->li_tv, deep, lock);
3424 li = li->li_next;
3425 ++lp->ll_n1;
3426 }
3427 }
3428 else if (lp->ll_list != NULL)
3429 /* (un)lock a List item. */
3430 item_lock(&lp->ll_li->li_tv, deep, lock);
3431 else
3432 /* un(lock) a Dictionary item. */
3433 item_lock(&lp->ll_di->di_tv, deep, lock);
3434
3435 return ret;
3436}
3437
3438/*
3439 * Lock or unlock an item. "deep" is nr of levels to go.
3440 */
3441 static void
3442item_lock(tv, deep, lock)
3443 typval_T *tv;
3444 int deep;
3445 int lock;
3446{
3447 static int recurse = 0;
3448 list_T *l;
3449 listitem_T *li;
3450 dict_T *d;
3451 hashitem_T *hi;
3452 int todo;
3453
3454 if (recurse >= DICT_MAXNEST)
3455 {
3456 EMSG(_("E743: variable nested too deep for (un)lock"));
3457 return;
3458 }
3459 if (deep == 0)
3460 return;
3461 ++recurse;
3462
3463 /* lock/unlock the item itself */
3464 if (lock)
3465 tv->v_lock |= VAR_LOCKED;
3466 else
3467 tv->v_lock &= ~VAR_LOCKED;
3468
3469 switch (tv->v_type)
3470 {
3471 case VAR_LIST:
3472 if ((l = tv->vval.v_list) != NULL)
3473 {
3474 if (lock)
3475 l->lv_lock |= VAR_LOCKED;
3476 else
3477 l->lv_lock &= ~VAR_LOCKED;
3478 if (deep < 0 || deep > 1)
3479 /* recursive: lock/unlock the items the List contains */
3480 for (li = l->lv_first; li != NULL; li = li->li_next)
3481 item_lock(&li->li_tv, deep - 1, lock);
3482 }
3483 break;
3484 case VAR_DICT:
3485 if ((d = tv->vval.v_dict) != NULL)
3486 {
3487 if (lock)
3488 d->dv_lock |= VAR_LOCKED;
3489 else
3490 d->dv_lock &= ~VAR_LOCKED;
3491 if (deep < 0 || deep > 1)
3492 {
3493 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003494 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003495 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3496 {
3497 if (!HASHITEM_EMPTY(hi))
3498 {
3499 --todo;
3500 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3501 }
3502 }
3503 }
3504 }
3505 }
3506 --recurse;
3507}
3508
Bram Moolenaara40058a2005-07-11 22:42:07 +00003509/*
3510 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3511 * it refers to a List or Dictionary that is locked.
3512 */
3513 static int
3514tv_islocked(tv)
3515 typval_T *tv;
3516{
3517 return (tv->v_lock & VAR_LOCKED)
3518 || (tv->v_type == VAR_LIST
3519 && tv->vval.v_list != NULL
3520 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3521 || (tv->v_type == VAR_DICT
3522 && tv->vval.v_dict != NULL
3523 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3524}
3525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3527/*
3528 * Delete all "menutrans_" variables.
3529 */
3530 void
3531del_menutrans_vars()
3532{
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 {
3540 if (!HASHITEM_EMPTY(hi))
3541 {
3542 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3544 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 }
3546 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
3549#endif
3550
3551#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3552
3553/*
3554 * Local string buffer for the next two functions to store a variable name
3555 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3556 * get_user_var_name().
3557 */
3558
3559static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3560
3561static char_u *varnamebuf = NULL;
3562static int varnamebuflen = 0;
3563
3564/*
3565 * Function to concatenate a prefix and a variable name.
3566 */
3567 static char_u *
3568cat_prefix_varname(prefix, name)
3569 int prefix;
3570 char_u *name;
3571{
3572 int len;
3573
3574 len = (int)STRLEN(name) + 3;
3575 if (len > varnamebuflen)
3576 {
3577 vim_free(varnamebuf);
3578 len += 10; /* some additional space */
3579 varnamebuf = alloc(len);
3580 if (varnamebuf == NULL)
3581 {
3582 varnamebuflen = 0;
3583 return NULL;
3584 }
3585 varnamebuflen = len;
3586 }
3587 *varnamebuf = prefix;
3588 varnamebuf[1] = ':';
3589 STRCPY(varnamebuf + 2, name);
3590 return varnamebuf;
3591}
3592
3593/*
3594 * Function given to ExpandGeneric() to obtain the list of user defined
3595 * (global/buffer/window/built-in) variable names.
3596 */
3597/*ARGSUSED*/
3598 char_u *
3599get_user_var_name(xp, idx)
3600 expand_T *xp;
3601 int idx;
3602{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003603 static long_u gdone;
3604 static long_u bdone;
3605 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003606#ifdef FEAT_WINDOWS
3607 static long_u tdone;
3608#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003609 static int vidx;
3610 static hashitem_T *hi;
3611 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003614 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003615 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003616#ifdef FEAT_WINDOWS
3617 tdone = 0;
3618#endif
3619 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003620
3621 /* Global variables */
3622 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003624 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003626 else
3627 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003628 while (HASHITEM_EMPTY(hi))
3629 ++hi;
3630 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3631 return cat_prefix_varname('g', hi->hi_key);
3632 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003634
3635 /* b: variables */
3636 ht = &curbuf->b_vars.dv_hashtab;
3637 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003639 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003640 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003641 else
3642 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003643 while (HASHITEM_EMPTY(hi))
3644 ++hi;
3645 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return (char_u *)"b:changedtick";
3651 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003652
3653 /* w: variables */
3654 ht = &curwin->w_vars.dv_hashtab;
3655 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003657 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003659 else
3660 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003661 while (HASHITEM_EMPTY(hi))
3662 ++hi;
3663 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003665
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003666#ifdef FEAT_WINDOWS
3667 /* t: variables */
3668 ht = &curtab->tp_vars.dv_hashtab;
3669 if (tdone < ht->ht_used)
3670 {
3671 if (tdone++ == 0)
3672 hi = ht->ht_array;
3673 else
3674 ++hi;
3675 while (HASHITEM_EMPTY(hi))
3676 ++hi;
3677 return cat_prefix_varname('t', hi->hi_key);
3678 }
3679#endif
3680
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 /* v: variables */
3682 if (vidx < VV_LEN)
3683 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 vim_free(varnamebuf);
3686 varnamebuf = NULL;
3687 varnamebuflen = 0;
3688 return NULL;
3689}
3690
3691#endif /* FEAT_CMDL_COMPL */
3692
3693/*
3694 * types for expressions.
3695 */
3696typedef enum
3697{
3698 TYPE_UNKNOWN = 0
3699 , TYPE_EQUAL /* == */
3700 , TYPE_NEQUAL /* != */
3701 , TYPE_GREATER /* > */
3702 , TYPE_GEQUAL /* >= */
3703 , TYPE_SMALLER /* < */
3704 , TYPE_SEQUAL /* <= */
3705 , TYPE_MATCH /* =~ */
3706 , TYPE_NOMATCH /* !~ */
3707} exptype_T;
3708
3709/*
3710 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3713 */
3714
3715/*
3716 * Handle zero level expression.
3717 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003719 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 * Return OK or FAIL.
3721 */
3722 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003723eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 char_u **nextcmd;
3727 int evaluate;
3728{
3729 int ret;
3730 char_u *p;
3731
3732 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (ret == FAIL || !ends_excmd(*p))
3735 {
3736 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 /*
3739 * Report the invalid expression unless the expression evaluation has
3740 * been cancelled due to an aborting error, an interrupt, or an
3741 * exception.
3742 */
3743 if (!aborting())
3744 EMSG2(_(e_invexpr2), arg);
3745 ret = FAIL;
3746 }
3747 if (nextcmd != NULL)
3748 *nextcmd = check_nextcmd(p);
3749
3750 return ret;
3751}
3752
3753/*
3754 * Handle top level expression:
3755 * expr1 ? expr0 : expr0
3756 *
3757 * "arg" must point to the first non-white of the expression.
3758 * "arg" is advanced to the next non-white after the recognized expression.
3759 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003760 * Note: "rettv.v_lock" is not set.
3761 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 * Return OK or FAIL.
3763 */
3764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 int evaluate;
3769{
3770 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * Get the first variable.
3775 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003776 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 return FAIL;
3778
3779 if ((*arg)[0] == '?')
3780 {
3781 result = FALSE;
3782 if (evaluate)
3783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784 int error = FALSE;
3785
3786 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003788 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 if (error)
3790 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792
3793 /*
3794 * Get the second variable.
3795 */
3796 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return FAIL;
3799
3800 /*
3801 * Check for the ":".
3802 */
3803 if ((*arg)[0] != ':')
3804 {
3805 EMSG(_("E109: Missing ':' after '?'"));
3806 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return FAIL;
3809 }
3810
3811 /*
3812 * Get the third variable.
3813 */
3814 *arg = skipwhite(*arg + 1);
3815 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3816 {
3817 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003818 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 return FAIL;
3820 }
3821 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824
3825 return OK;
3826}
3827
3828/*
3829 * Handle first level expression:
3830 * expr2 || expr2 || expr2 logical OR
3831 *
3832 * "arg" must point to the first non-white of the expression.
3833 * "arg" is advanced to the next non-white after the recognized expression.
3834 *
3835 * Return OK or FAIL.
3836 */
3837 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003838eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 int evaluate;
3842{
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 long result;
3845 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 /*
3849 * Get the first variable.
3850 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return FAIL;
3853
3854 /*
3855 * Repeat until there is no following "||".
3856 */
3857 first = TRUE;
3858 result = FALSE;
3859 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3860 {
3861 if (evaluate && first)
3862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 if (error)
3867 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 first = FALSE;
3869 }
3870
3871 /*
3872 * Get the second variable.
3873 */
3874 *arg = skipwhite(*arg + 2);
3875 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3876 return FAIL;
3877
3878 /*
3879 * Compute the result.
3880 */
3881 if (evaluate && !result)
3882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 if (error)
3887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889 if (evaluate)
3890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 rettv->v_type = VAR_NUMBER;
3892 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 }
3895
3896 return OK;
3897}
3898
3899/*
3900 * Handle second level expression:
3901 * expr3 && expr3 && expr3 logical AND
3902 *
3903 * "arg" must point to the first non-white of the expression.
3904 * "arg" is advanced to the next non-white after the recognized expression.
3905 *
3906 * Return OK or FAIL.
3907 */
3908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 int evaluate;
3913{
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 long result;
3916 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003917 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919 /*
3920 * Get the first variable.
3921 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 return FAIL;
3924
3925 /*
3926 * Repeat until there is no following "&&".
3927 */
3928 first = TRUE;
3929 result = TRUE;
3930 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3931 {
3932 if (evaluate && first)
3933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003934 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003936 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003937 if (error)
3938 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 first = FALSE;
3940 }
3941
3942 /*
3943 * Get the second variable.
3944 */
3945 *arg = skipwhite(*arg + 2);
3946 if (eval4(arg, &var2, evaluate && result) == FAIL)
3947 return FAIL;
3948
3949 /*
3950 * Compute the result.
3951 */
3952 if (evaluate && result)
3953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003954 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003957 if (error)
3958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 if (evaluate)
3961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003962 rettv->v_type = VAR_NUMBER;
3963 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965 }
3966
3967 return OK;
3968}
3969
3970/*
3971 * Handle third level expression:
3972 * var1 == var2
3973 * var1 =~ var2
3974 * var1 != var2
3975 * var1 !~ var2
3976 * var1 > var2
3977 * var1 >= var2
3978 * var1 < var2
3979 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003980 * var1 is var2
3981 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 *
3983 * "arg" must point to the first non-white of the expression.
3984 * "arg" is advanced to the next non-white after the recognized expression.
3985 *
3986 * Return OK or FAIL.
3987 */
3988 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 int evaluate;
3993{
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 char_u *p;
3996 int i;
3997 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003998 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 int len = 2;
4000 long n1, n2;
4001 char_u *s1, *s2;
4002 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4003 regmatch_T regmatch;
4004 int ic;
4005 char_u *save_cpo;
4006
4007 /*
4008 * Get the first variable.
4009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 return FAIL;
4012
4013 p = *arg;
4014 switch (p[0])
4015 {
4016 case '=': if (p[1] == '=')
4017 type = TYPE_EQUAL;
4018 else if (p[1] == '~')
4019 type = TYPE_MATCH;
4020 break;
4021 case '!': if (p[1] == '=')
4022 type = TYPE_NEQUAL;
4023 else if (p[1] == '~')
4024 type = TYPE_NOMATCH;
4025 break;
4026 case '>': if (p[1] != '=')
4027 {
4028 type = TYPE_GREATER;
4029 len = 1;
4030 }
4031 else
4032 type = TYPE_GEQUAL;
4033 break;
4034 case '<': if (p[1] != '=')
4035 {
4036 type = TYPE_SMALLER;
4037 len = 1;
4038 }
4039 else
4040 type = TYPE_SEQUAL;
4041 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 case 'i': if (p[1] == 's')
4043 {
4044 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4045 len = 5;
4046 if (!vim_isIDc(p[len]))
4047 {
4048 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4049 type_is = TRUE;
4050 }
4051 }
4052 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054
4055 /*
4056 * If there is a comparitive operator, use it.
4057 */
4058 if (type != TYPE_UNKNOWN)
4059 {
4060 /* extra question mark appended: ignore case */
4061 if (p[len] == '?')
4062 {
4063 ic = TRUE;
4064 ++len;
4065 }
4066 /* extra '#' appended: match case */
4067 else if (p[len] == '#')
4068 {
4069 ic = FALSE;
4070 ++len;
4071 }
4072 /* nothing appened: use 'ignorecase' */
4073 else
4074 ic = p_ic;
4075
4076 /*
4077 * Get the second variable.
4078 */
4079 *arg = skipwhite(p + len);
4080 if (eval5(arg, &var2, evaluate) == FAIL)
4081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 return FAIL;
4084 }
4085
4086 if (evaluate)
4087 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004088 if (type_is && rettv->v_type != var2.v_type)
4089 {
4090 /* For "is" a different type always means FALSE, for "notis"
4091 * it means TRUE. */
4092 n1 = (type == TYPE_NEQUAL);
4093 }
4094 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4095 {
4096 if (type_is)
4097 {
4098 n1 = (rettv->v_type == var2.v_type
4099 && rettv->vval.v_list == var2.vval.v_list);
4100 if (type == TYPE_NEQUAL)
4101 n1 = !n1;
4102 }
4103 else if (rettv->v_type != var2.v_type
4104 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4105 {
4106 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004107 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004108 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004109 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004110 clear_tv(rettv);
4111 clear_tv(&var2);
4112 return FAIL;
4113 }
4114 else
4115 {
4116 /* Compare two Lists for being equal or unequal. */
4117 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4118 if (type == TYPE_NEQUAL)
4119 n1 = !n1;
4120 }
4121 }
4122
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004123 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4124 {
4125 if (type_is)
4126 {
4127 n1 = (rettv->v_type == var2.v_type
4128 && rettv->vval.v_dict == var2.vval.v_dict);
4129 if (type == TYPE_NEQUAL)
4130 n1 = !n1;
4131 }
4132 else if (rettv->v_type != var2.v_type
4133 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4134 {
4135 if (rettv->v_type != var2.v_type)
4136 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4137 else
4138 EMSG(_("E736: Invalid operation for Dictionary"));
4139 clear_tv(rettv);
4140 clear_tv(&var2);
4141 return FAIL;
4142 }
4143 else
4144 {
4145 /* Compare two Dictionaries for being equal or unequal. */
4146 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4147 if (type == TYPE_NEQUAL)
4148 n1 = !n1;
4149 }
4150 }
4151
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004152 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4153 {
4154 if (rettv->v_type != var2.v_type
4155 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4156 {
4157 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004158 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004159 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004160 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004161 clear_tv(rettv);
4162 clear_tv(&var2);
4163 return FAIL;
4164 }
4165 else
4166 {
4167 /* Compare two Funcrefs for being equal or unequal. */
4168 if (rettv->vval.v_string == NULL
4169 || var2.vval.v_string == NULL)
4170 n1 = FALSE;
4171 else
4172 n1 = STRCMP(rettv->vval.v_string,
4173 var2.vval.v_string) == 0;
4174 if (type == TYPE_NEQUAL)
4175 n1 = !n1;
4176 }
4177 }
4178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 /*
4180 * If one of the two variables is a number, compare as a number.
4181 * When using "=~" or "!~", always compare as string.
4182 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004183 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 n1 = get_tv_number(rettv);
4187 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 switch (type)
4189 {
4190 case TYPE_EQUAL: n1 = (n1 == n2); break;
4191 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4192 case TYPE_GREATER: n1 = (n1 > n2); break;
4193 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4194 case TYPE_SMALLER: n1 = (n1 < n2); break;
4195 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4196 case TYPE_UNKNOWN:
4197 case TYPE_MATCH:
4198 case TYPE_NOMATCH: break; /* avoid gcc warning */
4199 }
4200 }
4201 else
4202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 s1 = get_tv_string_buf(rettv, buf1);
4204 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4206 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4207 else
4208 i = 0;
4209 n1 = FALSE;
4210 switch (type)
4211 {
4212 case TYPE_EQUAL: n1 = (i == 0); break;
4213 case TYPE_NEQUAL: n1 = (i != 0); break;
4214 case TYPE_GREATER: n1 = (i > 0); break;
4215 case TYPE_GEQUAL: n1 = (i >= 0); break;
4216 case TYPE_SMALLER: n1 = (i < 0); break;
4217 case TYPE_SEQUAL: n1 = (i <= 0); break;
4218
4219 case TYPE_MATCH:
4220 case TYPE_NOMATCH:
4221 /* avoid 'l' flag in 'cpoptions' */
4222 save_cpo = p_cpo;
4223 p_cpo = (char_u *)"";
4224 regmatch.regprog = vim_regcomp(s2,
4225 RE_MAGIC + RE_STRING);
4226 regmatch.rm_ic = ic;
4227 if (regmatch.regprog != NULL)
4228 {
4229 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4230 vim_free(regmatch.regprog);
4231 if (type == TYPE_NOMATCH)
4232 n1 = !n1;
4233 }
4234 p_cpo = save_cpo;
4235 break;
4236
4237 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4238 }
4239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
4241 clear_tv(&var2);
4242 rettv->v_type = VAR_NUMBER;
4243 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245 }
4246
4247 return OK;
4248}
4249
4250/*
4251 * Handle fourth level expression:
4252 * + number addition
4253 * - number subtraction
4254 * . string concatenation
4255 *
4256 * "arg" must point to the first non-white of the expression.
4257 * "arg" is advanced to the next non-white after the recognized expression.
4258 *
4259 * Return OK or FAIL.
4260 */
4261 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 int evaluate;
4266{
Bram Moolenaar33570922005-01-25 22:26:29 +00004267 typval_T var2;
4268 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 int op;
4270 long n1, n2;
4271 char_u *s1, *s2;
4272 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4273 char_u *p;
4274
4275 /*
4276 * Get the first variable.
4277 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return FAIL;
4280
4281 /*
4282 * Repeat computing, until no '+', '-' or '.' is following.
4283 */
4284 for (;;)
4285 {
4286 op = **arg;
4287 if (op != '+' && op != '-' && op != '.')
4288 break;
4289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (op != '+' || rettv->v_type != VAR_LIST)
4291 {
4292 /* For "list + ...", an illegal use of the first operand as
4293 * a number cannot be determined before evaluating the 2nd
4294 * operand: if this is also a list, all is ok.
4295 * For "something . ...", "something - ..." or "non-list + ...",
4296 * we know that the first operand needs to be a string or number
4297 * without evaluating the 2nd operand. So check before to avoid
4298 * side effects after an error. */
4299 if (evaluate && get_tv_string_chk(rettv) == NULL)
4300 {
4301 clear_tv(rettv);
4302 return FAIL;
4303 }
4304 }
4305
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 /*
4307 * Get the second variable.
4308 */
4309 *arg = skipwhite(*arg + 1);
4310 if (eval6(arg, &var2, evaluate) == FAIL)
4311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314 }
4315
4316 if (evaluate)
4317 {
4318 /*
4319 * Compute the result.
4320 */
4321 if (op == '.')
4322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4324 s2 = get_tv_string_buf_chk(&var2, buf2);
4325 if (s2 == NULL) /* type error ? */
4326 {
4327 clear_tv(rettv);
4328 clear_tv(&var2);
4329 return FAIL;
4330 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004331 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 clear_tv(rettv);
4333 rettv->v_type = VAR_STRING;
4334 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004336 else if (op == '+' && rettv->v_type == VAR_LIST
4337 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004338 {
4339 /* concatenate Lists */
4340 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4341 &var3) == FAIL)
4342 {
4343 clear_tv(rettv);
4344 clear_tv(&var2);
4345 return FAIL;
4346 }
4347 clear_tv(rettv);
4348 *rettv = var3;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 else
4351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004352 int error = FALSE;
4353
4354 n1 = get_tv_number_chk(rettv, &error);
4355 if (error)
4356 {
4357 /* This can only happen for "list + non-list".
4358 * For "non-list + ..." or "something - ...", we returned
4359 * before evaluating the 2nd operand. */
4360 clear_tv(rettv);
4361 return FAIL;
4362 }
4363 n2 = get_tv_number_chk(&var2, &error);
4364 if (error)
4365 {
4366 clear_tv(rettv);
4367 clear_tv(&var2);
4368 return FAIL;
4369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (op == '+')
4372 n1 = n1 + n2;
4373 else
4374 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 rettv->v_type = VAR_NUMBER;
4376 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 }
4381 return OK;
4382}
4383
4384/*
4385 * Handle fifth level expression:
4386 * * number multiplication
4387 * / number division
4388 * % number modulo
4389 *
4390 * "arg" must point to the first non-white of the expression.
4391 * "arg" is advanced to the next non-white after the recognized expression.
4392 *
4393 * Return OK or FAIL.
4394 */
4395 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int evaluate;
4400{
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int op;
4403 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 /*
4407 * Get the first variable.
4408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 return FAIL;
4411
4412 /*
4413 * Repeat computing, until no '*', '/' or '%' is following.
4414 */
4415 for (;;)
4416 {
4417 op = **arg;
4418 if (op != '*' && op != '/' && op != '%')
4419 break;
4420
4421 if (evaluate)
4422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 if (error)
4426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 else
4429 n1 = 0;
4430
4431 /*
4432 * Get the second variable.
4433 */
4434 *arg = skipwhite(*arg + 1);
4435 if (eval7(arg, &var2, evaluate) == FAIL)
4436 return FAIL;
4437
4438 if (evaluate)
4439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004440 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 if (error)
4443 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 /*
4446 * Compute the result.
4447 */
4448 if (op == '*')
4449 n1 = n1 * n2;
4450 else if (op == '/')
4451 {
4452 if (n2 == 0) /* give an error message? */
4453 n1 = 0x7fffffffL;
4454 else
4455 n1 = n1 / n2;
4456 }
4457 else
4458 {
4459 if (n2 == 0) /* give an error message? */
4460 n1 = 0;
4461 else
4462 n1 = n1 % n2;
4463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 rettv->v_type = VAR_NUMBER;
4465 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467 }
4468
4469 return OK;
4470}
4471
4472/*
4473 * Handle sixth level expression:
4474 * number number constant
4475 * "string" string contstant
4476 * 'string' literal string contstant
4477 * &option-name option value
4478 * @r register contents
4479 * identifier variable value
4480 * function() function call
4481 * $VAR environment variable
4482 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004483 * [expr, expr] List
4484 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 *
4486 * Also handle:
4487 * ! in front logical NOT
4488 * - in front unary minus
4489 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 * trailing [] subscript in String or List
4491 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 *
4493 * "arg" must point to the first non-white of the expression.
4494 * "arg" is advanced to the next non-white after the recognized expression.
4495 *
4496 * Return OK or FAIL.
4497 */
4498 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 int evaluate;
4503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 long n;
4505 int len;
4506 char_u *s;
4507 int val;
4508 char_u *start_leader, *end_leader;
4509 int ret = OK;
4510 char_u *alias;
4511
4512 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004513 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004516 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518 /*
4519 * Skip '!' and '-' characters. They are handled later.
4520 */
4521 start_leader = *arg;
4522 while (**arg == '!' || **arg == '-' || **arg == '+')
4523 *arg = skipwhite(*arg + 1);
4524 end_leader = *arg;
4525
4526 switch (**arg)
4527 {
4528 /*
4529 * Number constant.
4530 */
4531 case '0':
4532 case '1':
4533 case '2':
4534 case '3':
4535 case '4':
4536 case '5':
4537 case '6':
4538 case '7':
4539 case '8':
4540 case '9':
4541 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4542 *arg += len;
4543 if (evaluate)
4544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004545 rettv->v_type = VAR_NUMBER;
4546 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 break;
4549
4550 /*
4551 * String constant: "string".
4552 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004553 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 break;
4555
4556 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004559 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 break;
4561
4562 /*
4563 * List: [expr, expr]
4564 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567
4568 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004569 * Dictionary: {key: val, key: val}
4570 */
4571 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4572 break;
4573
4574 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004575 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004577 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 break;
4579
4580 /*
4581 * Environment variable: $VAR.
4582 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 break;
4585
4586 /*
4587 * Register contents: @r.
4588 */
4589 case '@': ++*arg;
4590 if (evaluate)
4591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004593 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 if (**arg != NUL)
4596 ++*arg;
4597 break;
4598
4599 /*
4600 * nested expression: (expression).
4601 */
4602 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (**arg == ')')
4605 ++*arg;
4606 else if (ret == OK)
4607 {
4608 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 ret = FAIL;
4611 }
4612 break;
4613
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 break;
4616 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617
4618 if (ret == NOTDONE)
4619 {
4620 /*
4621 * Must be a variable or function name.
4622 * Can also be a curly-braces kind of name: {expr}.
4623 */
4624 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004625 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 if (alias != NULL)
4627 s = alias;
4628
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004629 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 ret = FAIL;
4631 else
4632 {
4633 if (**arg == '(') /* recursive! */
4634 {
4635 /* If "s" is the name of a variable of type VAR_FUNC
4636 * use its contents. */
4637 s = deref_func_name(s, &len);
4638
4639 /* Invoke the function. */
4640 ret = get_func_tv(s, len, rettv, arg,
4641 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004642 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 /* Stop the expression evaluation when immediately
4644 * aborting on error, or when an interrupt occurred or
4645 * an exception was thrown but not caught. */
4646 if (aborting())
4647 {
4648 if (ret == OK)
4649 clear_tv(rettv);
4650 ret = FAIL;
4651 }
4652 }
4653 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004654 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004655 else
4656 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 }
4658
4659 if (alias != NULL)
4660 vim_free(alias);
4661 }
4662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 *arg = skipwhite(*arg);
4664
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004665 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4666 * expr(expr). */
4667 if (ret == OK)
4668 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 /*
4671 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4672 */
4673 if (ret == OK && evaluate && end_leader > start_leader)
4674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 int error = FALSE;
4676
4677 val = get_tv_number_chk(rettv, &error);
4678 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004680 clear_tv(rettv);
4681 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 else
4684 {
4685 while (end_leader > start_leader)
4686 {
4687 --end_leader;
4688 if (*end_leader == '!')
4689 val = !val;
4690 else if (*end_leader == '-')
4691 val = -val;
4692 }
4693 clear_tv(rettv);
4694 rettv->v_type = VAR_NUMBER;
4695 rettv->vval.v_number = val;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698
4699 return ret;
4700}
4701
4702/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004703 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4704 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4706 */
4707 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004708eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004710 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004712 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713{
4714 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004715 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 long len = -1;
4718 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004720 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004722 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724 if (verbose)
4725 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 return FAIL;
4727 }
4728
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 /*
4732 * dict.name
4733 */
4734 key = *arg + 1;
4735 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4736 ;
4737 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 }
4741 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 /*
4744 * something[idx]
4745 *
4746 * Get the (first) variable from inside the [].
4747 */
4748 *arg = skipwhite(*arg + 1);
4749 if (**arg == ':')
4750 empty1 = TRUE;
4751 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4752 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4754 {
4755 /* not a number or string */
4756 clear_tv(&var1);
4757 return FAIL;
4758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759
4760 /*
4761 * Get the second variable from inside the [:].
4762 */
4763 if (**arg == ':')
4764 {
4765 range = TRUE;
4766 *arg = skipwhite(*arg + 1);
4767 if (**arg == ']')
4768 empty2 = TRUE;
4769 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 if (!empty1)
4772 clear_tv(&var1);
4773 return FAIL;
4774 }
4775 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4776 {
4777 /* not a number or string */
4778 if (!empty1)
4779 clear_tv(&var1);
4780 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 return FAIL;
4782 }
4783 }
4784
4785 /* Check for the ']'. */
4786 if (**arg != ']')
4787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004788 if (verbose)
4789 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 clear_tv(&var1);
4791 if (range)
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 }
4797
4798 if (evaluate)
4799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 n1 = 0;
4801 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 n1 = get_tv_number(&var1);
4804 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806 if (range)
4807 {
4808 if (empty2)
4809 n2 = -1;
4810 else
4811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 n2 = get_tv_number(&var2);
4813 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 }
4815 }
4816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 {
4819 case VAR_NUMBER:
4820 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 len = (long)STRLEN(s);
4823 if (range)
4824 {
4825 /* The resulting variable is a substring. If the indexes
4826 * are out of range the result is empty. */
4827 if (n1 < 0)
4828 {
4829 n1 = len + n1;
4830 if (n1 < 0)
4831 n1 = 0;
4832 }
4833 if (n2 < 0)
4834 n2 = len + n2;
4835 else if (n2 >= len)
4836 n2 = len;
4837 if (n1 >= len || n2 < 0 || n1 > n2)
4838 s = NULL;
4839 else
4840 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4841 }
4842 else
4843 {
4844 /* The resulting variable is a string of a single
4845 * character. If the index is too big or negative the
4846 * result is empty. */
4847 if (n1 >= len || n1 < 0)
4848 s = NULL;
4849 else
4850 s = vim_strnsave(s + n1, 1);
4851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(rettv);
4853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 break;
4856
4857 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 if (n1 < 0)
4860 n1 = len + n1;
4861 if (!empty1 && (n1 < 0 || n1 >= len))
4862 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004863 if (verbose)
4864 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 return FAIL;
4866 }
4867 if (range)
4868 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004869 list_T *l;
4870 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004871
4872 if (n2 < 0)
4873 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004874 else if (n2 >= len)
4875 n2 = len - 1;
4876 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004878 if (verbose)
4879 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 return FAIL;
4881 }
4882 l = list_alloc();
4883 if (l == NULL)
4884 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886 n1 <= n2; ++n1)
4887 {
4888 if (list_append_tv(l, &item->li_tv) == FAIL)
4889 {
4890 list_free(l);
4891 return FAIL;
4892 }
4893 item = item->li_next;
4894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 clear_tv(rettv);
4896 rettv->v_type = VAR_LIST;
4897 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004898 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 }
4900 else
4901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004903 &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 Moolenaar071d4272004-06-13 20:20:40 +00007008 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7009 {"filereadable", 1, 1, f_filereadable},
7010 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007011 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007012 {"finddir", 1, 3, f_finddir},
7013 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {"fnamemodify", 2, 2, f_fnamemodify},
7015 {"foldclosed", 1, 1, f_foldclosed},
7016 {"foldclosedend", 1, 1, f_foldclosedend},
7017 {"foldlevel", 1, 1, f_foldlevel},
7018 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007019 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007021 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007023 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007024 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {"getbufvar", 2, 2, f_getbufvar},
7026 {"getchar", 0, 1, f_getchar},
7027 {"getcharmod", 0, 0, f_getcharmod},
7028 {"getcmdline", 0, 0, f_getcmdline},
7029 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007030 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007032 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007033 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {"getfsize", 1, 1, f_getfsize},
7035 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007036 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007037 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007038 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007039 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007040 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007041 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007043 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"getwinposx", 0, 0, f_getwinposx},
7045 {"getwinposy", 0, 0, f_getwinposy},
7046 {"getwinvar", 2, 2, f_getwinvar},
7047 {"glob", 1, 1, f_glob},
7048 {"globpath", 2, 2, f_globpath},
7049 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007050 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007051 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7053 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7054 {"histadd", 2, 2, f_histadd},
7055 {"histdel", 1, 2, f_histdel},
7056 {"histget", 1, 2, f_histget},
7057 {"histnr", 1, 1, f_histnr},
7058 {"hlID", 1, 1, f_hlID},
7059 {"hlexists", 1, 1, f_hlexists},
7060 {"hostname", 0, 0, f_hostname},
7061 {"iconv", 3, 3, f_iconv},
7062 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007063 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007064 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007066 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {"inputrestore", 0, 0, f_inputrestore},
7068 {"inputsave", 0, 0, f_inputsave},
7069 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007070 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007072 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007074 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007077 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"libcall", 3, 3, f_libcall},
7079 {"libcallnr", 3, 3, f_libcallnr},
7080 {"line", 1, 1, f_line},
7081 {"line2byte", 1, 1, f_line2byte},
7082 {"lispindent", 1, 1, f_lispindent},
7083 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007084 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007085 {"maparg", 1, 3, f_maparg},
7086 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007087 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007088 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007089 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007090 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007091 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007092 {"max", 1, 1, f_max},
7093 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007094#ifdef vim_mkdir
7095 {"mkdir", 1, 3, f_mkdir},
7096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {"mode", 0, 0, f_mode},
7098 {"nextnonblank", 1, 1, f_nextnonblank},
7099 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007100 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007102 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007103 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007105 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007106 {"reltime", 0, 2, f_reltime},
7107 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {"remote_expr", 2, 3, f_remote_expr},
7109 {"remote_foreground", 1, 1, f_remote_foreground},
7110 {"remote_peek", 1, 2, f_remote_peek},
7111 {"remote_read", 1, 1, f_remote_read},
7112 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007113 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007115 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007117 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007118 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007119 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007120 {"searchpair", 3, 6, f_searchpair},
7121 {"searchpairpos", 3, 6, f_searchpairpos},
7122 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {"server2client", 2, 2, f_server2client},
7124 {"serverlist", 0, 0, f_serverlist},
7125 {"setbufvar", 3, 3, f_setbufvar},
7126 {"setcmdpos", 1, 1, f_setcmdpos},
7127 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007128 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007129 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007130 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007132 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 {"setwinvar", 3, 3, f_setwinvar},
7134 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007135 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007136 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007137 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007138 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007139 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007140 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141#ifdef HAVE_STRFTIME
7142 {"strftime", 1, 2, f_strftime},
7143#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007145 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {"strlen", 1, 1, f_strlen},
7147 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007148 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {"strtrans", 1, 1, f_strtrans},
7150 {"submatch", 1, 1, f_submatch},
7151 {"substitute", 4, 4, f_substitute},
7152 {"synID", 3, 3, f_synID},
7153 {"synIDattr", 2, 3, f_synIDattr},
7154 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007155 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007157 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007159 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007160 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007162 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {"tolower", 1, 1, f_tolower},
7164 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007165 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"virtcol", 1, 1, f_virtcol},
7169 {"visualmode", 0, 1, f_visualmode},
7170 {"winbufnr", 1, 1, f_winbufnr},
7171 {"wincol", 0, 0, f_wincol},
7172 {"winheight", 1, 1, f_winheight},
7173 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007174 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007176 {"winrestview", 1, 1, f_winrestview},
7177 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007179 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180};
7181
7182#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7183
7184/*
7185 * Function given to ExpandGeneric() to obtain the list of internal
7186 * or user defined function names.
7187 */
7188 char_u *
7189get_function_name(xp, idx)
7190 expand_T *xp;
7191 int idx;
7192{
7193 static int intidx = -1;
7194 char_u *name;
7195
7196 if (idx == 0)
7197 intidx = -1;
7198 if (intidx < 0)
7199 {
7200 name = get_user_func_name(xp, idx);
7201 if (name != NULL)
7202 return name;
7203 }
7204 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7205 {
7206 STRCPY(IObuff, functions[intidx].f_name);
7207 STRCAT(IObuff, "(");
7208 if (functions[intidx].f_max_argc == 0)
7209 STRCAT(IObuff, ")");
7210 return IObuff;
7211 }
7212
7213 return NULL;
7214}
7215
7216/*
7217 * Function given to ExpandGeneric() to obtain the list of internal or
7218 * user defined variable or function names.
7219 */
7220/*ARGSUSED*/
7221 char_u *
7222get_expr_name(xp, idx)
7223 expand_T *xp;
7224 int idx;
7225{
7226 static int intidx = -1;
7227 char_u *name;
7228
7229 if (idx == 0)
7230 intidx = -1;
7231 if (intidx < 0)
7232 {
7233 name = get_function_name(xp, idx);
7234 if (name != NULL)
7235 return name;
7236 }
7237 return get_user_var_name(xp, ++intidx);
7238}
7239
7240#endif /* FEAT_CMDL_COMPL */
7241
7242/*
7243 * Find internal function in table above.
7244 * Return index, or -1 if not found
7245 */
7246 static int
7247find_internal_func(name)
7248 char_u *name; /* name of the function */
7249{
7250 int first = 0;
7251 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7252 int cmp;
7253 int x;
7254
7255 /*
7256 * Find the function name in the table. Binary search.
7257 */
7258 while (first <= last)
7259 {
7260 x = first + ((unsigned)(last - first) >> 1);
7261 cmp = STRCMP(name, functions[x].f_name);
7262 if (cmp < 0)
7263 last = x - 1;
7264 else if (cmp > 0)
7265 first = x + 1;
7266 else
7267 return x;
7268 }
7269 return -1;
7270}
7271
7272/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007273 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7274 * name it contains, otherwise return "name".
7275 */
7276 static char_u *
7277deref_func_name(name, lenp)
7278 char_u *name;
7279 int *lenp;
7280{
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007282 int cc;
7283
7284 cc = name[*lenp];
7285 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007286 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007287 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007289 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 {
7292 *lenp = 0;
7293 return (char_u *)""; /* just in case */
7294 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007295 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 }
7298
7299 return name;
7300}
7301
7302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 * Allocate a variable for the result of a function.
7304 * Return OK or FAIL.
7305 */
7306 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7308 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 char_u *name; /* name of the function */
7310 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 char_u **arg; /* argument, pointing to the '(' */
7313 linenr_T firstline; /* first line of range */
7314 linenr_T lastline; /* last line of range */
7315 int *doesrange; /* return: function handled range */
7316 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
7319 char_u *argp;
7320 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007321 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 int argcount = 0; /* number of arguments found */
7323
7324 /*
7325 * Get the arguments.
7326 */
7327 argp = *arg;
7328 while (argcount < MAX_FUNC_ARGS)
7329 {
7330 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7331 if (*argp == ')' || *argp == ',' || *argp == NUL)
7332 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7334 {
7335 ret = FAIL;
7336 break;
7337 }
7338 ++argcount;
7339 if (*argp != ',')
7340 break;
7341 }
7342 if (*argp == ')')
7343 ++argp;
7344 else
7345 ret = FAIL;
7346
7347 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007348 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 {
7352 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007353 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007355 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007359 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
7361 *arg = skipwhite(argp);
7362 return ret;
7363}
7364
7365
7366/*
7367 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007368 * Return OK when the function can't be called, FAIL otherwise.
7369 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 */
7371 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007372call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 char_u *name; /* name of the function */
7375 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007378 typval_T *argvars; /* vars for arguments, must have "argcount"
7379 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 linenr_T firstline; /* first line of range */
7381 linenr_T lastline; /* last line of range */
7382 int *doesrange; /* return: function handled range */
7383 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387#define ERROR_UNKNOWN 0
7388#define ERROR_TOOMANY 1
7389#define ERROR_TOOFEW 2
7390#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391#define ERROR_DICT 4
7392#define ERROR_NONE 5
7393#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int error = ERROR_NONE;
7395 int i;
7396 int llen;
7397 ufunc_T *fp;
7398 int cc;
7399#define FLEN_FIXED 40
7400 char_u fname_buf[FLEN_FIXED + 1];
7401 char_u *fname;
7402
7403 /*
7404 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7405 * Change <SNR>123_name() to K_SNR 123_name().
7406 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7407 */
7408 cc = name[len];
7409 name[len] = NUL;
7410 llen = eval_fname_script(name);
7411 if (llen > 0)
7412 {
7413 fname_buf[0] = K_SPECIAL;
7414 fname_buf[1] = KS_EXTRA;
7415 fname_buf[2] = (int)KE_SNR;
7416 i = 3;
7417 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7418 {
7419 if (current_SID <= 0)
7420 error = ERROR_SCRIPT;
7421 else
7422 {
7423 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7424 i = (int)STRLEN(fname_buf);
7425 }
7426 }
7427 if (i + STRLEN(name + llen) < FLEN_FIXED)
7428 {
7429 STRCPY(fname_buf + i, name + llen);
7430 fname = fname_buf;
7431 }
7432 else
7433 {
7434 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7435 if (fname == NULL)
7436 error = ERROR_OTHER;
7437 else
7438 {
7439 mch_memmove(fname, fname_buf, (size_t)i);
7440 STRCPY(fname + i, name + llen);
7441 }
7442 }
7443 }
7444 else
7445 fname = name;
7446
7447 *doesrange = FALSE;
7448
7449
7450 /* execute the function if no errors detected and executing */
7451 if (evaluate && error == ERROR_NONE)
7452 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 error = ERROR_UNKNOWN;
7455
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007456 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 {
7458 /*
7459 * User defined function.
7460 */
7461 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007464 /* Trigger FuncUndefined event, may load the function. */
7465 if (fp == NULL
7466 && apply_autocmds(EVENT_FUNCUNDEFINED,
7467 fname, fname, TRUE, NULL)
7468 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007470 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 fp = find_func(fname);
7472 }
7473#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007474 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007475 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007476 {
7477 /* loaded a package, search for the function again */
7478 fp = find_func(fname);
7479 }
7480
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 if (fp != NULL)
7482 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007483 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007485 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007487 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007489 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007490 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
7492 {
7493 /*
7494 * Call the user function.
7495 * Save and restore search patterns, script variables and
7496 * redo buffer.
7497 */
7498 save_search_patterns();
7499 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007500 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007501 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007502 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007503 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7504 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7505 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007506 /* Function was unreferenced while being used, free it
7507 * now. */
7508 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 restoreRedobuff();
7510 restore_search_patterns();
7511 error = ERROR_NONE;
7512 }
7513 }
7514 }
7515 else
7516 {
7517 /*
7518 * Find the function name in the table, call its implementation.
7519 */
7520 i = find_internal_func(fname);
7521 if (i >= 0)
7522 {
7523 if (argcount < functions[i].f_min_argc)
7524 error = ERROR_TOOFEW;
7525 else if (argcount > functions[i].f_max_argc)
7526 error = ERROR_TOOMANY;
7527 else
7528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007529 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007530 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 error = ERROR_NONE;
7532 }
7533 }
7534 }
7535 /*
7536 * The function call (or "FuncUndefined" autocommand sequence) might
7537 * have been aborted by an error, an interrupt, or an explicitly thrown
7538 * exception that has not been caught so far. This situation can be
7539 * tested for by calling aborting(). For an error in an internal
7540 * function or for the "E132" error in call_user_func(), however, the
7541 * throw point at which the "force_abort" flag (temporarily reset by
7542 * emsg()) is normally updated has not been reached yet. We need to
7543 * update that flag first to make aborting() reliable.
7544 */
7545 update_force_abort();
7546 }
7547 if (error == ERROR_NONE)
7548 ret = OK;
7549
7550 /*
7551 * Report an error unless the argument evaluation or function call has been
7552 * cancelled due to an aborting error, an interrupt, or an exception.
7553 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 if (!aborting())
7555 {
7556 switch (error)
7557 {
7558 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007559 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 break;
7561 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007562 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 break;
7564 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007565 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566 name);
7567 break;
7568 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007569 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 name);
7571 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007573 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 name);
7575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 }
7577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
7579 name[len] = cc;
7580 if (fname != name && fname != fname_buf)
7581 vim_free(fname);
7582
7583 return ret;
7584}
7585
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007586/*
7587 * Give an error message with a function name. Handle <SNR> things.
7588 */
7589 static void
7590emsg_funcname(msg, name)
7591 char *msg;
7592 char_u *name;
7593{
7594 char_u *p;
7595
7596 if (*name == K_SPECIAL)
7597 p = concat_str((char_u *)"<SNR>", name + 3);
7598 else
7599 p = name;
7600 EMSG2(_(msg), p);
7601 if (p != name)
7602 vim_free(p);
7603}
7604
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605/*********************************************
7606 * Implementation of the built-in functions
7607 */
7608
7609/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007610 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 */
7612 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007613f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 typval_T *argvars;
7615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007619 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007620 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007622 if ((l = argvars[0].vval.v_list) != NULL
7623 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7624 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 }
7627 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007628 EMSG(_(e_listreq));
7629}
7630
7631/*
7632 * "append(lnum, string/list)" function
7633 */
7634 static void
7635f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 typval_T *argvars;
7637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007638{
7639 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007640 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007641 list_T *l = NULL;
7642 listitem_T *li = NULL;
7643 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007644 long added = 0;
7645
Bram Moolenaar0d660222005-01-07 21:51:51 +00007646 lnum = get_tv_lnum(argvars);
7647 if (lnum >= 0
7648 && lnum <= curbuf->b_ml.ml_line_count
7649 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007651 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007652 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007653 l = argvars[1].vval.v_list;
7654 if (l == NULL)
7655 return;
7656 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007657 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007658 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007659 for (;;)
7660 {
7661 if (l == NULL)
7662 tv = &argvars[1]; /* append a string */
7663 else if (li == NULL)
7664 break; /* end of list */
7665 else
7666 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007667 line = get_tv_string_chk(tv);
7668 if (line == NULL) /* type error */
7669 {
7670 rettv->vval.v_number = 1; /* Failed */
7671 break;
7672 }
7673 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007674 ++added;
7675 if (l == NULL)
7676 break;
7677 li = li->li_next;
7678 }
7679
7680 appended_lines_mark(lnum, added);
7681 if (curwin->w_cursor.lnum > lnum)
7682 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684 else
7685 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686}
7687
7688/*
7689 * "argc()" function
7690 */
7691/* ARGSUSED */
7692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 typval_T *argvars;
7695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698}
7699
7700/*
7701 * "argidx()" function
7702 */
7703/* ARGSUSED */
7704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007705f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 typval_T *argvars;
7707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007709 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710}
7711
7712/*
7713 * "argv(nr)" function
7714 */
7715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 typval_T *argvars;
7718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
7720 int idx;
7721
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007722 if (argvars[0].v_type != VAR_UNKNOWN)
7723 {
7724 idx = get_tv_number_chk(&argvars[0], NULL);
7725 if (idx >= 0 && idx < ARGCOUNT)
7726 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7727 else
7728 rettv->vval.v_string = NULL;
7729 rettv->v_type = VAR_STRING;
7730 }
7731 else if (rettv_list_alloc(rettv) == OK)
7732 for (idx = 0; idx < ARGCOUNT; ++idx)
7733 list_append_string(rettv->vval.v_list,
7734 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735}
7736
7737/*
7738 * "browse(save, title, initdir, default)" function
7739 */
7740/* ARGSUSED */
7741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 typval_T *argvars;
7744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
7746#ifdef FEAT_BROWSE
7747 int save;
7748 char_u *title;
7749 char_u *initdir;
7750 char_u *defname;
7751 char_u buf[NUMBUFLEN];
7752 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007753 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755 save = get_tv_number_chk(&argvars[0], &error);
7756 title = get_tv_string_chk(&argvars[1]);
7757 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7758 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007760 if (error || title == NULL || initdir == NULL || defname == NULL)
7761 rettv->vval.v_string = NULL;
7762 else
7763 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007764 do_browse(save ? BROWSE_SAVE : 0,
7765 title, defname, NULL, initdir, NULL, curbuf);
7766#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007770}
7771
7772/*
7773 * "browsedir(title, initdir)" function
7774 */
7775/* ARGSUSED */
7776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007777f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *argvars;
7779 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007780{
7781#ifdef FEAT_BROWSE
7782 char_u *title;
7783 char_u *initdir;
7784 char_u buf[NUMBUFLEN];
7785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007786 title = get_tv_string_chk(&argvars[0]);
7787 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007789 if (title == NULL || initdir == NULL)
7790 rettv->vval.v_string = NULL;
7791 else
7792 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007793 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
Bram Moolenaar33570922005-01-25 22:26:29 +00007800static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802/*
7803 * Find a buffer by number or exact name.
7804 */
7805 static buf_T *
7806find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007807 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
7809 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007811 if (avar->v_type == VAR_NUMBER)
7812 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007813 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007815 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007816 if (buf == NULL)
7817 {
7818 /* No full path name match, try a match with a URL or a "nofile"
7819 * buffer, these don't use the full path. */
7820 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7821 if (buf->b_fname != NULL
7822 && (path_with_url(buf->b_fname)
7823#ifdef FEAT_QUICKFIX
7824 || bt_nofile(buf)
7825#endif
7826 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007828 break;
7829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
7831 return buf;
7832}
7833
7834/*
7835 * "bufexists(expr)" function
7836 */
7837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007838f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007839 typval_T *argvars;
7840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843}
7844
7845/*
7846 * "buflisted(expr)" function
7847 */
7848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 typval_T *argvars;
7851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 buf_T *buf;
7854
7855 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007856 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857}
7858
7859/*
7860 * "bufloaded(expr)" function
7861 */
7862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007863f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 typval_T *argvars;
7865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
7867 buf_T *buf;
7868
7869 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871}
7872
Bram Moolenaar33570922005-01-25 22:26:29 +00007873static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007874
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875/*
7876 * Get buffer by number or pattern.
7877 */
7878 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007880 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 int save_magic;
7884 char_u *save_cpo;
7885 buf_T *buf;
7886
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007887 if (tv->v_type == VAR_NUMBER)
7888 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007889 if (tv->v_type != VAR_STRING)
7890 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 if (name == NULL || *name == NUL)
7892 return curbuf;
7893 if (name[0] == '$' && name[1] == NUL)
7894 return lastbuf;
7895
7896 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7897 save_magic = p_magic;
7898 p_magic = TRUE;
7899 save_cpo = p_cpo;
7900 p_cpo = (char_u *)"";
7901
7902 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7903 TRUE, FALSE));
7904
7905 p_magic = save_magic;
7906 p_cpo = save_cpo;
7907
7908 /* If not found, try expanding the name, like done for bufexists(). */
7909 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911
7912 return buf;
7913}
7914
7915/*
7916 * "bufname(expr)" function
7917 */
7918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007919f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 typval_T *argvars;
7921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 buf_T *buf;
7924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007925 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 buf = get_buf_tv(&argvars[0]);
7928 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 --emsg_off;
7934}
7935
7936/*
7937 * "bufnr(expr)" function
7938 */
7939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 typval_T *argvars;
7942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007945 int error = FALSE;
7946 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007948 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007950 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007951 --emsg_off;
7952
7953 /* If the buffer isn't found and the second argument is not zero create a
7954 * new buffer. */
7955 if (buf == NULL
7956 && argvars[1].v_type != VAR_UNKNOWN
7957 && get_tv_number_chk(&argvars[1], &error) != 0
7958 && !error
7959 && (name = get_tv_string_chk(&argvars[0])) != NULL
7960 && !error)
7961 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7962
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967}
7968
7969/*
7970 * "bufwinnr(nr)" function
7971 */
7972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 typval_T *argvars;
7975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977#ifdef FEAT_WINDOWS
7978 win_T *wp;
7979 int winnr = 0;
7980#endif
7981 buf_T *buf;
7982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007983 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986#ifdef FEAT_WINDOWS
7987 for (wp = firstwin; wp; wp = wp->w_next)
7988 {
7989 ++winnr;
7990 if (wp->w_buffer == buf)
7991 break;
7992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996#endif
7997 --emsg_off;
7998}
7999
8000/*
8001 * "byte2line(byte)" function
8002 */
8003/*ARGSUSED*/
8004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008005f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 typval_T *argvars;
8007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#else
8012 long boff = 0;
8013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008014 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008016 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 (linenr_T)0, &boff);
8020#endif
8021}
8022
8023/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008024 * "byteidx()" function
8025 */
8026/*ARGSUSED*/
8027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008028f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008029 typval_T *argvars;
8030 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008031{
8032#ifdef FEAT_MBYTE
8033 char_u *t;
8034#endif
8035 char_u *str;
8036 long idx;
8037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008038 str = get_tv_string_chk(&argvars[0]);
8039 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008041 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008042 return;
8043
8044#ifdef FEAT_MBYTE
8045 t = str;
8046 for ( ; idx > 0; idx--)
8047 {
8048 if (*t == NUL) /* EOL reached */
8049 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008050 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008051 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008052 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008053#else
8054 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008056#endif
8057}
8058
8059/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008060 * "call(func, arglist)" function
8061 */
8062 static void
8063f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 typval_T *argvars;
8065 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008066{
8067 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008068 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008069 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008070 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008071 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008073
8074 rettv->vval.v_number = 0;
8075 if (argvars[1].v_type != VAR_LIST)
8076 {
8077 EMSG(_(e_listreq));
8078 return;
8079 }
8080 if (argvars[1].vval.v_list == NULL)
8081 return;
8082
8083 if (argvars[0].v_type == VAR_FUNC)
8084 func = argvars[0].vval.v_string;
8085 else
8086 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008087 if (*func == NUL)
8088 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008089
Bram Moolenaare9a41262005-01-15 22:18:47 +00008090 if (argvars[2].v_type != VAR_UNKNOWN)
8091 {
8092 if (argvars[2].v_type != VAR_DICT)
8093 {
8094 EMSG(_(e_dictreq));
8095 return;
8096 }
8097 selfdict = argvars[2].vval.v_dict;
8098 }
8099
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008100 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8101 item = item->li_next)
8102 {
8103 if (argc == MAX_FUNC_ARGS)
8104 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008105 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008106 break;
8107 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008108 /* Make a copy of each argument. This is needed to be able to set
8109 * v_lock to VAR_FIXED in the copy without changing the original list.
8110 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008111 copy_tv(&item->li_tv, &argv[argc++]);
8112 }
8113
8114 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008115 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008116 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8117 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008118
8119 /* Free the arguments. */
8120 while (argc > 0)
8121 clear_tv(&argv[--argc]);
8122}
8123
8124/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008125 * "changenr()" function
8126 */
8127/*ARGSUSED*/
8128 static void
8129f_changenr(argvars, rettv)
8130 typval_T *argvars;
8131 typval_T *rettv;
8132{
8133 rettv->vval.v_number = curbuf->b_u_seq_cur;
8134}
8135
8136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 * "char2nr(string)" function
8138 */
8139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008140f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 typval_T *argvars;
8142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
8144#ifdef FEAT_MBYTE
8145 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008146 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 else
8148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150}
8151
8152/*
8153 * "cindent(lnum)" function
8154 */
8155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008157 typval_T *argvars;
8158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160#ifdef FEAT_CINDENT
8161 pos_T pos;
8162 linenr_T lnum;
8163
8164 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008165 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8167 {
8168 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 curwin->w_cursor = pos;
8171 }
8172 else
8173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175}
8176
8177/*
8178 * "col(string)" function
8179 */
8180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008181f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 typval_T *argvars;
8183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184{
8185 colnr_T col = 0;
8186 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008187 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008189 fp = var2fpos(&argvars[0], FALSE, &fnum);
8190 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
8192 if (fp->col == MAXCOL)
8193 {
8194 /* '> can be MAXCOL, get the length of the line then */
8195 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008196 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 else
8198 col = MAXCOL;
8199 }
8200 else
8201 {
8202 col = fp->col + 1;
8203#ifdef FEAT_VIRTUALEDIT
8204 /* col(".") when the cursor is on the NUL at the end of the line
8205 * because of "coladd" can be seen as an extra column. */
8206 if (virtual_active() && fp == &curwin->w_cursor)
8207 {
8208 char_u *p = ml_get_cursor();
8209
8210 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8211 curwin->w_virtcol - curwin->w_cursor.coladd))
8212 {
8213# ifdef FEAT_MBYTE
8214 int l;
8215
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008216 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 col += l;
8218# else
8219 if (*p != NUL && p[1] == NUL)
8220 ++col;
8221# endif
8222 }
8223 }
8224#endif
8225 }
8226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008227 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228}
8229
Bram Moolenaar572cb562005-08-05 21:35:02 +00008230#if defined(FEAT_INS_EXPAND)
8231/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008232 * "complete()" function
8233 */
8234/*ARGSUSED*/
8235 static void
8236f_complete(argvars, rettv)
8237 typval_T *argvars;
8238 typval_T *rettv;
8239{
8240 int startcol;
8241
8242 if ((State & INSERT) == 0)
8243 {
8244 EMSG(_("E785: complete() can only be used in Insert mode"));
8245 return;
8246 }
8247 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8248 {
8249 EMSG(_(e_invarg));
8250 return;
8251 }
8252
8253 startcol = get_tv_number_chk(&argvars[0], NULL);
8254 if (startcol <= 0)
8255 return;
8256
8257 set_completion(startcol - 1, argvars[1].vval.v_list);
8258}
8259
8260/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008261 * "complete_add()" function
8262 */
8263/*ARGSUSED*/
8264 static void
8265f_complete_add(argvars, rettv)
8266 typval_T *argvars;
8267 typval_T *rettv;
8268{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008269 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008270}
8271
8272/*
8273 * "complete_check()" function
8274 */
8275/*ARGSUSED*/
8276 static void
8277f_complete_check(argvars, rettv)
8278 typval_T *argvars;
8279 typval_T *rettv;
8280{
8281 int saved = RedrawingDisabled;
8282
8283 RedrawingDisabled = 0;
8284 ins_compl_check_keys(0);
8285 rettv->vval.v_number = compl_interrupted;
8286 RedrawingDisabled = saved;
8287}
8288#endif
8289
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290/*
8291 * "confirm(message, buttons[, default [, type]])" function
8292 */
8293/*ARGSUSED*/
8294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8300 char_u *message;
8301 char_u *buttons = NULL;
8302 char_u buf[NUMBUFLEN];
8303 char_u buf2[NUMBUFLEN];
8304 int def = 1;
8305 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008306 char_u *typestr;
8307 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008309 message = get_tv_string_chk(&argvars[0]);
8310 if (message == NULL)
8311 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008312 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008314 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8315 if (buttons == NULL)
8316 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008317 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008319 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8323 if (typestr == NULL)
8324 error = TRUE;
8325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008327 switch (TOUPPER_ASC(*typestr))
8328 {
8329 case 'E': type = VIM_ERROR; break;
8330 case 'Q': type = VIM_QUESTION; break;
8331 case 'I': type = VIM_INFO; break;
8332 case 'W': type = VIM_WARNING; break;
8333 case 'G': type = VIM_GENERIC; break;
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336 }
8337 }
8338 }
8339
8340 if (buttons == NULL || *buttons == NUL)
8341 buttons = (char_u *)_("&Ok");
8342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008343 if (error)
8344 rettv->vval.v_number = 0;
8345 else
8346 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 def, NULL);
8348#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350#endif
8351}
8352
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008353/*
8354 * "copy()" function
8355 */
8356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 typval_T *argvars;
8359 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008360{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008361 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363
8364/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008365 * "count()" function
8366 */
8367 static void
8368f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 typval_T *argvars;
8370 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008371{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008372 long n = 0;
8373 int ic = FALSE;
8374
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008376 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 listitem_T *li;
8378 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008379 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008380
Bram Moolenaare9a41262005-01-15 22:18:47 +00008381 if ((l = argvars[0].vval.v_list) != NULL)
8382 {
8383 li = l->lv_first;
8384 if (argvars[2].v_type != VAR_UNKNOWN)
8385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008386 int error = FALSE;
8387
8388 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008389 if (argvars[3].v_type != VAR_UNKNOWN)
8390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008391 idx = get_tv_number_chk(&argvars[3], &error);
8392 if (!error)
8393 {
8394 li = list_find(l, idx);
8395 if (li == NULL)
8396 EMSGN(_(e_listidx), idx);
8397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008399 if (error)
8400 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008401 }
8402
8403 for ( ; li != NULL; li = li->li_next)
8404 if (tv_equal(&li->li_tv, &argvars[1], ic))
8405 ++n;
8406 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008408 else if (argvars[0].v_type == VAR_DICT)
8409 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 int todo;
8411 dict_T *d;
8412 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008413
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008414 if ((d = argvars[0].vval.v_dict) != NULL)
8415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008416 int error = FALSE;
8417
Bram Moolenaare9a41262005-01-15 22:18:47 +00008418 if (argvars[2].v_type != VAR_UNKNOWN)
8419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008420 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008421 if (argvars[3].v_type != VAR_UNKNOWN)
8422 EMSG(_(e_invarg));
8423 }
8424
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008425 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008426 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008427 {
8428 if (!HASHITEM_EMPTY(hi))
8429 {
8430 --todo;
8431 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8432 ++n;
8433 }
8434 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 }
8436 }
8437 else
8438 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008439 rettv->vval.v_number = n;
8440}
8441
8442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8444 *
8445 * Checks the existence of a cscope connection.
8446 */
8447/*ARGSUSED*/
8448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008450 typval_T *argvars;
8451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453#ifdef FEAT_CSCOPE
8454 int num = 0;
8455 char_u *dbpath = NULL;
8456 char_u *prepend = NULL;
8457 char_u buf[NUMBUFLEN];
8458
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008459 if (argvars[0].v_type != VAR_UNKNOWN
8460 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 num = (int)get_tv_number(&argvars[0]);
8463 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008464 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008465 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 }
8467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008470 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#endif
8472}
8473
8474/*
8475 * "cursor(lnum, col)" function
8476 *
8477 * Moves the cursor to the specified line and column
8478 */
8479/*ARGSUSED*/
8480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008486#ifdef FEAT_VIRTUALEDIT
8487 long coladd = 0;
8488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
Bram Moolenaara5525202006-03-02 22:52:09 +00008490 if (argvars[1].v_type == VAR_UNKNOWN)
8491 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008492 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008493
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008494 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008495 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008496 line = pos.lnum;
8497 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008498#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008499 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008500#endif
8501 }
8502 else
8503 {
8504 line = get_tv_lnum(argvars);
8505 col = get_tv_number_chk(&argvars[1], NULL);
8506#ifdef FEAT_VIRTUALEDIT
8507 if (argvars[2].v_type != VAR_UNKNOWN)
8508 coladd = get_tv_number_chk(&argvars[2], NULL);
8509#endif
8510 }
8511 if (line < 0 || col < 0
8512#ifdef FEAT_VIRTUALEDIT
8513 || coladd < 0
8514#endif
8515 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008516 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (line > 0)
8518 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (col > 0)
8520 curwin->w_cursor.col = col - 1;
8521#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008522 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523#endif
8524
8525 /* Make sure the cursor is in a valid position. */
8526 check_cursor();
8527#ifdef FEAT_MBYTE
8528 /* Correct cursor for multi-byte character. */
8529 if (has_mbyte)
8530 mb_adjust_cursor();
8531#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008532
8533 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534}
8535
8536/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 */
8539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008541 typval_T *argvars;
8542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008544 int noref = 0;
8545
8546 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008547 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008548 if (noref < 0 || noref > 1)
8549 EMSG(_(e_invarg));
8550 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008551 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552}
8553
8554/*
8555 * "delete()" function
8556 */
8557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008558f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008559 typval_T *argvars;
8560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566}
8567
8568/*
8569 * "did_filetype()" function
8570 */
8571/*ARGSUSED*/
8572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008574 typval_T *argvars;
8575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008578 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#endif
8582}
8583
8584/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008585 * "diff_filler()" function
8586 */
8587/*ARGSUSED*/
8588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 typval_T *argvars;
8591 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008592{
8593#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008595#endif
8596}
8597
8598/*
8599 * "diff_hlID()" function
8600 */
8601/*ARGSUSED*/
8602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008604 typval_T *argvars;
8605 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008606{
8607#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008608 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008609 static linenr_T prev_lnum = 0;
8610 static int changedtick = 0;
8611 static int fnum = 0;
8612 static int change_start = 0;
8613 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008614 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008615 int filler_lines;
8616 int col;
8617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008618 if (lnum < 0) /* ignore type error in {lnum} arg */
8619 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008620 if (lnum != prev_lnum
8621 || changedtick != curbuf->b_changedtick
8622 || fnum != curbuf->b_fnum)
8623 {
8624 /* New line, buffer, change: need to get the values. */
8625 filler_lines = diff_check(curwin, lnum);
8626 if (filler_lines < 0)
8627 {
8628 if (filler_lines == -1)
8629 {
8630 change_start = MAXCOL;
8631 change_end = -1;
8632 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8633 hlID = HLF_ADD; /* added line */
8634 else
8635 hlID = HLF_CHD; /* changed line */
8636 }
8637 else
8638 hlID = HLF_ADD; /* added line */
8639 }
8640 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008641 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008642 prev_lnum = lnum;
8643 changedtick = curbuf->b_changedtick;
8644 fnum = curbuf->b_fnum;
8645 }
8646
8647 if (hlID == HLF_CHD || hlID == HLF_TXD)
8648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008650 if (col >= change_start && col <= change_end)
8651 hlID = HLF_TXD; /* changed text */
8652 else
8653 hlID = HLF_CHD; /* changed line */
8654 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008655 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008656#endif
8657}
8658
8659/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008660 * "empty({expr})" function
8661 */
8662 static void
8663f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 typval_T *argvars;
8665 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008666{
8667 int n;
8668
8669 switch (argvars[0].v_type)
8670 {
8671 case VAR_STRING:
8672 case VAR_FUNC:
8673 n = argvars[0].vval.v_string == NULL
8674 || *argvars[0].vval.v_string == NUL;
8675 break;
8676 case VAR_NUMBER:
8677 n = argvars[0].vval.v_number == 0;
8678 break;
8679 case VAR_LIST:
8680 n = argvars[0].vval.v_list == NULL
8681 || argvars[0].vval.v_list->lv_first == NULL;
8682 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008683 case VAR_DICT:
8684 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008686 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008687 default:
8688 EMSG2(_(e_intern2), "f_empty()");
8689 n = 0;
8690 }
8691
8692 rettv->vval.v_number = n;
8693}
8694
8695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 * "escape({string}, {chars})" function
8697 */
8698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008700 typval_T *argvars;
8701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 char_u buf[NUMBUFLEN];
8704
Bram Moolenaar758711c2005-02-02 23:11:38 +00008705 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8706 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708}
8709
8710/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008711 * "eval()" function
8712 */
8713/*ARGSUSED*/
8714 static void
8715f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008716 typval_T *argvars;
8717 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008718{
8719 char_u *s;
8720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008721 s = get_tv_string_chk(&argvars[0]);
8722 if (s != NULL)
8723 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008724
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008725 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8726 {
8727 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008728 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008729 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008730 else if (*s != NUL)
8731 EMSG(_(e_trailing));
8732}
8733
8734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 * "eventhandler()" function
8736 */
8737/*ARGSUSED*/
8738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *argvars;
8741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744}
8745
8746/*
8747 * "executable()" function
8748 */
8749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008750f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008751 typval_T *argvars;
8752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
8758 * "exists()" function
8759 */
8760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 char_u *p;
8766 char_u *name;
8767 int n = FALSE;
8768 int len = 0;
8769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 if (*p == '$') /* environment variable */
8772 {
8773 /* first try "normal" environment variables (fast) */
8774 if (mch_getenv(p + 1) != NULL)
8775 n = TRUE;
8776 else
8777 {
8778 /* try expanding things like $VIM and ${HOME} */
8779 p = expand_env_save(p);
8780 if (p != NULL && *p != '$')
8781 n = TRUE;
8782 vim_free(p);
8783 }
8784 }
8785 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008786 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 else if (*p == '*') /* internal or user defined function */
8788 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008789 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 }
8791 else if (*p == ':')
8792 {
8793 n = cmd_exists(p + 1);
8794 }
8795 else if (*p == '#')
8796 {
8797#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008798 if (p[1] == '#')
8799 n = autocmd_supported(p + 2);
8800 else
8801 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802#endif
8803 }
8804 else /* internal variable */
8805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008806 char_u *tofree;
8807 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008809 /* get_name_len() takes care of expanding curly braces */
8810 name = p;
8811 len = get_name_len(&p, &tofree, TRUE, FALSE);
8812 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008814 if (tofree != NULL)
8815 name = tofree;
8816 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8817 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008819 /* handle d.key, l[idx], f(expr) */
8820 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8821 if (n)
8822 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 }
8824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008826 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 }
8828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830}
8831
8832/*
8833 * "expand()" function
8834 */
8835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008837 typval_T *argvars;
8838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839{
8840 char_u *s;
8841 int len;
8842 char_u *errormsg;
8843 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8844 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 rettv->v_type = VAR_STRING;
8848 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (*s == '%' || *s == '#' || *s == '<')
8850 {
8851 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 --emsg_off;
8854 }
8855 else
8856 {
8857 /* When the optional second argument is non-zero, don't remove matches
8858 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008859 if (argvars[1].v_type != VAR_UNKNOWN
8860 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008862 if (!error)
8863 {
8864 ExpandInit(&xpc);
8865 xpc.xp_context = EXPAND_FILES;
8866 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008867 }
8868 else
8869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 }
8871}
8872
8873/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008874 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008876 */
8877 static void
8878f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *argvars;
8880 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008881{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008882 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008883 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008884 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008885 list_T *l1, *l2;
8886 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008887 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008888 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008889
Bram Moolenaare9a41262005-01-15 22:18:47 +00008890 l1 = argvars[0].vval.v_list;
8891 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008892 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8893 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008894 {
8895 if (argvars[2].v_type != VAR_UNKNOWN)
8896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897 before = get_tv_number_chk(&argvars[2], &error);
8898 if (error)
8899 return; /* type error; errmsg already given */
8900
Bram Moolenaar758711c2005-02-02 23:11:38 +00008901 if (before == l1->lv_len)
8902 item = NULL;
8903 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008904 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008905 item = list_find(l1, before);
8906 if (item == NULL)
8907 {
8908 EMSGN(_(e_listidx), before);
8909 return;
8910 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008911 }
8912 }
8913 else
8914 item = NULL;
8915 list_extend(l1, l2, item);
8916
Bram Moolenaare9a41262005-01-15 22:18:47 +00008917 copy_tv(&argvars[0], rettv);
8918 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008919 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008920 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8921 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 dict_T *d1, *d2;
8923 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008924 char_u *action;
8925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008926 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008927 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008928
8929 d1 = argvars[0].vval.v_dict;
8930 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008931 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8932 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008933 {
8934 /* Check the third argument. */
8935 if (argvars[2].v_type != VAR_UNKNOWN)
8936 {
8937 static char *(av[]) = {"keep", "force", "error"};
8938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 action = get_tv_string_chk(&argvars[2]);
8940 if (action == NULL)
8941 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942 for (i = 0; i < 3; ++i)
8943 if (STRCMP(action, av[i]) == 0)
8944 break;
8945 if (i == 3)
8946 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008947 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948 return;
8949 }
8950 }
8951 else
8952 action = (char_u *)"force";
8953
8954 /* Go over all entries in the second dict and add them to the
8955 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008958 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008959 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008961 --todo;
8962 di1 = dict_find(d1, hi2->hi_key, -1);
8963 if (di1 == NULL)
8964 {
8965 di1 = dictitem_copy(HI2DI(hi2));
8966 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8967 dictitem_free(di1);
8968 }
8969 else if (*action == 'e')
8970 {
8971 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8972 break;
8973 }
8974 else if (*action == 'f')
8975 {
8976 clear_tv(&di1->di_tv);
8977 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8978 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008979 }
8980 }
8981
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 copy_tv(&argvars[0], rettv);
8983 }
8984 }
8985 else
8986 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008987}
8988
8989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 * "filereadable()" function
8991 */
8992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 typval_T *argvars;
8995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997 FILE *fd;
8998 char_u *p;
8999 int n;
9000
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9003 {
9004 n = TRUE;
9005 fclose(fd);
9006 }
9007 else
9008 n = FALSE;
9009
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011}
9012
9013/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009014 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 * rights to write into.
9016 */
9017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T *argvars;
9020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009022 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024
Bram Moolenaar33570922005-01-25 22:26:29 +00009025static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009026
9027 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009031 int dir;
9032{
9033#ifdef FEAT_SEARCHPATH
9034 char_u *fname;
9035 char_u *fresult = NULL;
9036 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9037 char_u *p;
9038 char_u pathbuf[NUMBUFLEN];
9039 int count = 1;
9040 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009041 int error = FALSE;
9042#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009043
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009044 rettv->vval.v_string = NULL;
9045 rettv->v_type = VAR_STRING;
9046
9047#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009049
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009050 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9053 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009054 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 else
9056 {
9057 if (*p != NUL)
9058 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009061 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009063 }
9064
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009065 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9066 error = TRUE;
9067
9068 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009070 do
9071 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009072 if (rettv->v_type == VAR_STRING)
9073 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 fresult = find_file_in_path_option(first ? fname : NULL,
9075 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009076 0, first, path, dir, NULL,
9077 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009079
9080 if (fresult != NULL && rettv->v_type == VAR_LIST)
9081 list_append_string(rettv->vval.v_list, fresult, -1);
9082
9083 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009084 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009085
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009086 if (rettv->v_type == VAR_STRING)
9087 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009088#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009089}
9090
Bram Moolenaar33570922005-01-25 22:26:29 +00009091static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9092static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009093
9094/*
9095 * Implementation of map() and filter().
9096 */
9097 static void
9098filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009101 int map;
9102{
9103 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009104 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 listitem_T *li, *nli;
9106 list_T *l = NULL;
9107 dictitem_T *di;
9108 hashtab_T *ht;
9109 hashitem_T *hi;
9110 dict_T *d = NULL;
9111 typval_T save_val;
9112 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009113 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009114 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009115 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009116 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009117
9118 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009119 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009120 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009121 if ((l = argvars[0].vval.v_list) == NULL
9122 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009123 return;
9124 }
9125 else if (argvars[0].v_type == VAR_DICT)
9126 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009127 if ((d = argvars[0].vval.v_dict) == NULL
9128 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009129 return;
9130 }
9131 else
9132 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009133 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009134 return;
9135 }
9136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 expr = get_tv_string_buf_chk(&argvars[1], buf);
9138 /* On type errors, the preceding call has already displayed an error
9139 * message. Avoid a misleading error message for an empty string that
9140 * was not passed as argument. */
9141 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143 prepare_vimvar(VV_VAL, &save_val);
9144 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009145
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009146 /* We reset "did_emsg" to be able to detect whether an error
9147 * occurred during evaluation of the expression. */
9148 save_did_emsg = did_emsg;
9149 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009153 prepare_vimvar(VV_KEY, &save_key);
9154 vimvars[VV_KEY].vv_type = VAR_STRING;
9155
9156 ht = &d->dv_hashtab;
9157 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009158 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009159 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009161 if (!HASHITEM_EMPTY(hi))
9162 {
9163 --todo;
9164 di = HI2DI(hi);
9165 if (tv_check_lock(di->di_tv.v_lock, msg))
9166 break;
9167 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009168 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009169 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 break;
9171 if (!map && rem)
9172 dictitem_remove(d, di);
9173 clear_tv(&vimvars[VV_KEY].vv_tv);
9174 }
9175 }
9176 hash_unlock(ht);
9177
9178 restore_vimvar(VV_KEY, &save_key);
9179 }
9180 else
9181 {
9182 for (li = l->lv_first; li != NULL; li = nli)
9183 {
9184 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009185 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009186 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009187 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009188 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009189 break;
9190 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009192 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009193 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009196
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009197 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009199
9200 copy_tv(&argvars[0], rettv);
9201}
9202
9203 static int
9204filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009205 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009206 char_u *expr;
9207 int map;
9208 int *remp;
9209{
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009211 char_u *s;
9212
Bram Moolenaar33570922005-01-25 22:26:29 +00009213 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009214 s = expr;
9215 if (eval1(&s, &rettv, TRUE) == FAIL)
9216 return FAIL;
9217 if (*s != NUL) /* check for trailing chars after expr */
9218 {
9219 EMSG2(_(e_invexpr2), s);
9220 return FAIL;
9221 }
9222 if (map)
9223 {
9224 /* map(): replace the list item value */
9225 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009226 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009227 *tv = rettv;
9228 }
9229 else
9230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 int error = FALSE;
9232
Bram Moolenaare9a41262005-01-15 22:18:47 +00009233 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009235 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 /* On type error, nothing has been removed; return FAIL to stop the
9237 * loop. The error message was given by get_tv_number_chk(). */
9238 if (error)
9239 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009240 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009242 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009243}
9244
9245/*
9246 * "filter()" function
9247 */
9248 static void
9249f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009250 typval_T *argvars;
9251 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009252{
9253 filter_map(argvars, rettv, FALSE);
9254}
9255
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009256/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009257 * "finddir({fname}[, {path}[, {count}]])" function
9258 */
9259 static void
9260f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 typval_T *argvars;
9262 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009263{
9264 findfilendir(argvars, rettv, TRUE);
9265}
9266
9267/*
9268 * "findfile({fname}[, {path}[, {count}]])" function
9269 */
9270 static void
9271f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *argvars;
9273 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009274{
9275 findfilendir(argvars, rettv, FALSE);
9276}
9277
9278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 * "fnamemodify({fname}, {mods})" function
9280 */
9281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 typval_T *argvars;
9284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 char_u *fname;
9287 char_u *mods;
9288 int usedlen = 0;
9289 int len;
9290 char_u *fbuf = NULL;
9291 char_u buf[NUMBUFLEN];
9292
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 fname = get_tv_string_chk(&argvars[0]);
9294 mods = get_tv_string_buf_chk(&argvars[1], buf);
9295 if (fname == NULL || mods == NULL)
9296 fname = NULL;
9297 else
9298 {
9299 len = (int)STRLEN(fname);
9300 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009307 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 vim_free(fbuf);
9309}
9310
Bram Moolenaar33570922005-01-25 22:26:29 +00009311static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312
9313/*
9314 * "foldclosed()" function
9315 */
9316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009318 typval_T *argvars;
9319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 int end;
9321{
9322#ifdef FEAT_FOLDING
9323 linenr_T lnum;
9324 linenr_T first, last;
9325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9328 {
9329 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9330 {
9331 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 return;
9336 }
9337 }
9338#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340}
9341
9342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009343 * "foldclosed()" function
9344 */
9345 static void
9346f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009347 typval_T *argvars;
9348 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009349{
9350 foldclosed_both(argvars, rettv, FALSE);
9351}
9352
9353/*
9354 * "foldclosedend()" function
9355 */
9356 static void
9357f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009358 typval_T *argvars;
9359 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009360{
9361 foldclosed_both(argvars, rettv, TRUE);
9362}
9363
9364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 * "foldlevel()" function
9366 */
9367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009369 typval_T *argvars;
9370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372#ifdef FEAT_FOLDING
9373 linenr_T lnum;
9374
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 else
9379#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381}
9382
9383/*
9384 * "foldtext()" function
9385 */
9386/*ARGSUSED*/
9387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009389 typval_T *argvars;
9390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391{
9392#ifdef FEAT_FOLDING
9393 linenr_T lnum;
9394 char_u *s;
9395 char_u *r;
9396 int len;
9397 char *txt;
9398#endif
9399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->v_type = VAR_STRING;
9401 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9404 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9405 <= curbuf->b_ml.ml_line_count
9406 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
9408 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009409 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9410 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 {
9412 if (!linewhite(lnum))
9413 break;
9414 ++lnum;
9415 }
9416
9417 /* Find interesting text in this line. */
9418 s = skipwhite(ml_get(lnum));
9419 /* skip C comment-start */
9420 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009423 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009424 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009425 {
9426 s = skipwhite(ml_get(lnum + 1));
9427 if (*s == '*')
9428 s = skipwhite(s + 1);
9429 }
9430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 txt = _("+-%s%3ld lines: ");
9432 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009433 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 + 20 /* for %3ld */
9435 + STRLEN(s))); /* concatenated */
9436 if (r != NULL)
9437 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009438 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9439 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9440 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 len = (int)STRLEN(r);
9442 STRCAT(r, s);
9443 /* remove 'foldmarker' and 'commentstring' */
9444 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 }
9447 }
9448#endif
9449}
9450
9451/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009452 * "foldtextresult(lnum)" function
9453 */
9454/*ARGSUSED*/
9455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 typval_T *argvars;
9458 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009459{
9460#ifdef FEAT_FOLDING
9461 linenr_T lnum;
9462 char_u *text;
9463 char_u buf[51];
9464 foldinfo_T foldinfo;
9465 int fold_count;
9466#endif
9467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 rettv->v_type = VAR_STRING;
9469 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009470#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009472 /* treat illegal types and illegal string values for {lnum} the same */
9473 if (lnum < 0)
9474 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009475 fold_count = foldedCount(curwin, lnum, &foldinfo);
9476 if (fold_count > 0)
9477 {
9478 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9479 &foldinfo, buf);
9480 if (text == buf)
9481 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009483 }
9484#endif
9485}
9486
9487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 * "foreground()" function
9489 */
9490/*ARGSUSED*/
9491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009493 typval_T *argvars;
9494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497#ifdef FEAT_GUI
9498 if (gui.in_use)
9499 gui_mch_set_foreground();
9500#else
9501# ifdef WIN32
9502 win32_set_foreground();
9503# endif
9504#endif
9505}
9506
9507/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009508 * "function()" function
9509 */
9510/*ARGSUSED*/
9511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 typval_T *argvars;
9514 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515{
9516 char_u *s;
9517
Bram Moolenaara7043832005-01-21 11:56:39 +00009518 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009520 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 EMSG2(_(e_invarg2), s);
9522 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009523 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 else
9525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 rettv->vval.v_string = vim_strsave(s);
9527 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009528 }
9529}
9530
9531/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009532 * "garbagecollect()" function
9533 */
9534/*ARGSUSED*/
9535 static void
9536f_garbagecollect(argvars, rettv)
9537 typval_T *argvars;
9538 typval_T *rettv;
9539{
9540 garbage_collect();
9541}
9542
9543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009544 * "get()" function
9545 */
9546 static void
9547f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009548 typval_T *argvars;
9549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009550{
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 listitem_T *li;
9552 list_T *l;
9553 dictitem_T *di;
9554 dict_T *d;
9555 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009556
Bram Moolenaare9a41262005-01-15 22:18:47 +00009557 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009558 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009559 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 int error = FALSE;
9562
9563 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9564 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009565 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009566 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009567 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009568 else if (argvars[0].v_type == VAR_DICT)
9569 {
9570 if ((d = argvars[0].vval.v_dict) != NULL)
9571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009572 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009573 if (di != NULL)
9574 tv = &di->di_tv;
9575 }
9576 }
9577 else
9578 EMSG2(_(e_listdictarg), "get()");
9579
9580 if (tv == NULL)
9581 {
9582 if (argvars[2].v_type == VAR_UNKNOWN)
9583 rettv->vval.v_number = 0;
9584 else
9585 copy_tv(&argvars[2], rettv);
9586 }
9587 else
9588 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009589}
9590
Bram Moolenaar342337a2005-07-21 21:11:17 +00009591static 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 +00009592
9593/*
9594 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009595 * Return a range (from start to end) of lines in rettv from the specified
9596 * buffer.
9597 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009598 */
9599 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009600get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009601 buf_T *buf;
9602 linenr_T start;
9603 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009604 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009605 typval_T *rettv;
9606{
9607 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009608
Bram Moolenaar342337a2005-07-21 21:11:17 +00009609 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009610 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009611 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009612 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009613 }
9614 else
9615 rettv->vval.v_number = 0;
9616
9617 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9618 return;
9619
9620 if (!retlist)
9621 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009622 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9623 p = ml_get_buf(buf, start, FALSE);
9624 else
9625 p = (char_u *)"";
9626
9627 rettv->v_type = VAR_STRING;
9628 rettv->vval.v_string = vim_strsave(p);
9629 }
9630 else
9631 {
9632 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009633 return;
9634
9635 if (start < 1)
9636 start = 1;
9637 if (end > buf->b_ml.ml_line_count)
9638 end = buf->b_ml.ml_line_count;
9639 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009640 if (list_append_string(rettv->vval.v_list,
9641 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009642 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009643 }
9644}
9645
9646/*
9647 * "getbufline()" function
9648 */
9649 static void
9650f_getbufline(argvars, rettv)
9651 typval_T *argvars;
9652 typval_T *rettv;
9653{
9654 linenr_T lnum;
9655 linenr_T end;
9656 buf_T *buf;
9657
9658 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9659 ++emsg_off;
9660 buf = get_buf_tv(&argvars[0]);
9661 --emsg_off;
9662
Bram Moolenaar661b1822005-07-28 22:36:45 +00009663 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009664 if (argvars[2].v_type == VAR_UNKNOWN)
9665 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009666 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009667 end = get_tv_lnum_buf(&argvars[2], buf);
9668
Bram Moolenaar342337a2005-07-21 21:11:17 +00009669 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009670}
9671
Bram Moolenaar0d660222005-01-07 21:51:51 +00009672/*
9673 * "getbufvar()" function
9674 */
9675 static void
9676f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009679{
9680 buf_T *buf;
9681 buf_T *save_curbuf;
9682 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009683 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9686 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009687 ++emsg_off;
9688 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009689
9690 rettv->v_type = VAR_STRING;
9691 rettv->vval.v_string = NULL;
9692
9693 if (buf != NULL && varname != NULL)
9694 {
9695 if (*varname == '&') /* buffer-local-option */
9696 {
9697 /* set curbuf to be our buf, temporarily */
9698 save_curbuf = curbuf;
9699 curbuf = buf;
9700
9701 get_option_tv(&varname, rettv, TRUE);
9702
9703 /* restore previous notion of curbuf */
9704 curbuf = save_curbuf;
9705 }
9706 else
9707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708 if (*varname == NUL)
9709 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9710 * scope prefix before the NUL byte is required by
9711 * find_var_in_ht(). */
9712 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009713 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009714 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009715 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009716 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009717 }
9718 }
9719
9720 --emsg_off;
9721}
9722
9723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 * "getchar()" function
9725 */
9726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 typval_T *argvars;
9729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009732 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733
9734 ++no_mapping;
9735 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009736 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 /* getchar(): blocking wait. */
9738 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 /* getchar(1): only check if char avail */
9741 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 else if (error || vpeekc() == NUL)
9743 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 n = 0;
9745 else
9746 /* getchar(0) and char avail: return char */
9747 n = safe_vgetc();
9748 --no_mapping;
9749 --allow_keys;
9750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (IS_SPECIAL(n) || mod_mask != 0)
9753 {
9754 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9755 int i = 0;
9756
9757 /* Turn a special key into three bytes, plus modifier. */
9758 if (mod_mask != 0)
9759 {
9760 temp[i++] = K_SPECIAL;
9761 temp[i++] = KS_MODIFIER;
9762 temp[i++] = mod_mask;
9763 }
9764 if (IS_SPECIAL(n))
9765 {
9766 temp[i++] = K_SPECIAL;
9767 temp[i++] = K_SECOND(n);
9768 temp[i++] = K_THIRD(n);
9769 }
9770#ifdef FEAT_MBYTE
9771 else if (has_mbyte)
9772 i += (*mb_char2bytes)(n, temp + i);
9773#endif
9774 else
9775 temp[i++] = n;
9776 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 rettv->v_type = VAR_STRING;
9778 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 }
9780}
9781
9782/*
9783 * "getcharmod()" function
9784 */
9785/*ARGSUSED*/
9786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009788 typval_T *argvars;
9789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792}
9793
9794/*
9795 * "getcmdline()" function
9796 */
9797/*ARGSUSED*/
9798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009800 typval_T *argvars;
9801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803 rettv->v_type = VAR_STRING;
9804 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
9807/*
9808 * "getcmdpos()" function
9809 */
9810/*ARGSUSED*/
9811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009813 typval_T *argvars;
9814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817}
9818
9819/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009820 * "getcmdtype()" function
9821 */
9822/*ARGSUSED*/
9823 static void
9824f_getcmdtype(argvars, rettv)
9825 typval_T *argvars;
9826 typval_T *rettv;
9827{
9828 rettv->v_type = VAR_STRING;
9829 rettv->vval.v_string = alloc(2);
9830 if (rettv->vval.v_string != NULL)
9831 {
9832 rettv->vval.v_string[0] = get_cmdline_type();
9833 rettv->vval.v_string[1] = NUL;
9834 }
9835}
9836
9837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 * "getcwd()" function
9839 */
9840/*ARGSUSED*/
9841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009843 typval_T *argvars;
9844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 char_u cwd[MAXPATHL];
9847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 else
9852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009855 if (rettv->vval.v_string != NULL)
9856 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857#endif
9858 }
9859}
9860
9861/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009862 * "getfontname()" function
9863 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009864/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009867 typval_T *argvars;
9868 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->v_type = VAR_STRING;
9871 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009872#ifdef FEAT_GUI
9873 if (gui.in_use)
9874 {
9875 GuiFont font;
9876 char_u *name = NULL;
9877
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009878 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009879 {
9880 /* Get the "Normal" font. Either the name saved by
9881 * hl_set_font_name() or from the font ID. */
9882 font = gui.norm_font;
9883 name = hl_get_font_name();
9884 }
9885 else
9886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009888 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9889 return;
9890 font = gui_mch_get_font(name, FALSE);
9891 if (font == NOFONT)
9892 return; /* Invalid font name, return empty string. */
9893 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009895 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009896 gui_mch_free_font(font);
9897 }
9898#endif
9899}
9900
9901/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009902 * "getfperm({fname})" function
9903 */
9904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009906 typval_T *argvars;
9907 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009908{
9909 char_u *fname;
9910 struct stat st;
9911 char_u *perm = NULL;
9912 char_u flags[] = "rwx";
9913 int i;
9914
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009915 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009918 if (mch_stat((char *)fname, &st) >= 0)
9919 {
9920 perm = vim_strsave((char_u *)"---------");
9921 if (perm != NULL)
9922 {
9923 for (i = 0; i < 9; i++)
9924 {
9925 if (st.st_mode & (1 << (8 - i)))
9926 perm[i] = flags[i % 3];
9927 }
9928 }
9929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009931}
9932
9933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 * "getfsize({fname})" function
9935 */
9936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 typval_T *argvars;
9939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 char_u *fname;
9942 struct stat st;
9943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947
9948 if (mch_stat((char *)fname, &st) >= 0)
9949 {
9950 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 }
9955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957}
9958
9959/*
9960 * "getftime({fname})" function
9961 */
9962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009964 typval_T *argvars;
9965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
9967 char_u *fname;
9968 struct stat st;
9969
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009970 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
9972 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976}
9977
9978/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009979 * "getftype({fname})" function
9980 */
9981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009983 typval_T *argvars;
9984 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009985{
9986 char_u *fname;
9987 struct stat st;
9988 char_u *type = NULL;
9989 char *t;
9990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009992
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009994 if (mch_lstat((char *)fname, &st) >= 0)
9995 {
9996#ifdef S_ISREG
9997 if (S_ISREG(st.st_mode))
9998 t = "file";
9999 else if (S_ISDIR(st.st_mode))
10000 t = "dir";
10001# ifdef S_ISLNK
10002 else if (S_ISLNK(st.st_mode))
10003 t = "link";
10004# endif
10005# ifdef S_ISBLK
10006 else if (S_ISBLK(st.st_mode))
10007 t = "bdev";
10008# endif
10009# ifdef S_ISCHR
10010 else if (S_ISCHR(st.st_mode))
10011 t = "cdev";
10012# endif
10013# ifdef S_ISFIFO
10014 else if (S_ISFIFO(st.st_mode))
10015 t = "fifo";
10016# endif
10017# ifdef S_ISSOCK
10018 else if (S_ISSOCK(st.st_mode))
10019 t = "fifo";
10020# endif
10021 else
10022 t = "other";
10023#else
10024# ifdef S_IFMT
10025 switch (st.st_mode & S_IFMT)
10026 {
10027 case S_IFREG: t = "file"; break;
10028 case S_IFDIR: t = "dir"; break;
10029# ifdef S_IFLNK
10030 case S_IFLNK: t = "link"; break;
10031# endif
10032# ifdef S_IFBLK
10033 case S_IFBLK: t = "bdev"; break;
10034# endif
10035# ifdef S_IFCHR
10036 case S_IFCHR: t = "cdev"; break;
10037# endif
10038# ifdef S_IFIFO
10039 case S_IFIFO: t = "fifo"; break;
10040# endif
10041# ifdef S_IFSOCK
10042 case S_IFSOCK: t = "socket"; break;
10043# endif
10044 default: t = "other";
10045 }
10046# else
10047 if (mch_isdir(fname))
10048 t = "dir";
10049 else
10050 t = "file";
10051# endif
10052#endif
10053 type = vim_strsave((char_u *)t);
10054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010056}
10057
10058/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010059 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010060 */
10061 static void
10062f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010063 typval_T *argvars;
10064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010065{
10066 linenr_T lnum;
10067 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010068 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010069
10070 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010071 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010072 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010073 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010074 retlist = FALSE;
10075 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010076 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010077 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010078 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010079 retlist = TRUE;
10080 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010081
Bram Moolenaar342337a2005-07-21 21:11:17 +000010082 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010083}
10084
10085/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010086 * "getpos(string)" function
10087 */
10088 static void
10089f_getpos(argvars, rettv)
10090 typval_T *argvars;
10091 typval_T *rettv;
10092{
10093 pos_T *fp;
10094 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010095 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010096
10097 if (rettv_list_alloc(rettv) == OK)
10098 {
10099 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010100 fp = var2fpos(&argvars[0], TRUE, &fnum);
10101 if (fnum != -1)
10102 list_append_number(l, (varnumber_T)fnum);
10103 else
10104 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010105 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10106 : (varnumber_T)0);
10107 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10108 : (varnumber_T)0);
10109 list_append_number(l,
10110#ifdef FEAT_VIRTUALEDIT
10111 (fp != NULL) ? (varnumber_T)fp->coladd :
10112#endif
10113 (varnumber_T)0);
10114 }
10115 else
10116 rettv->vval.v_number = FALSE;
10117}
10118
10119/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010120 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010121 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010122/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010123 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010124f_getqflist(argvars, rettv)
10125 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010126 typval_T *rettv;
10127{
10128#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010129 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010130#endif
10131
10132 rettv->vval.v_number = FALSE;
10133#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010134 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010135 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010136 wp = NULL;
10137 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10138 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010139 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010140 if (wp == NULL)
10141 return;
10142 }
10143
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010144 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010145 }
10146#endif
10147}
10148
10149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 * "getreg()" function
10151 */
10152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 typval_T *argvars;
10155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
10157 char_u *strregname;
10158 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010159 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010162 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010164 strregname = get_tv_string_chk(&argvars[0]);
10165 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010166 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010170 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 regname = (strregname == NULL ? '"' : *strregname);
10172 if (regname == 0)
10173 regname = '"';
10174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 rettv->vval.v_string = error ? NULL :
10177 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178}
10179
10180/*
10181 * "getregtype()" function
10182 */
10183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010185 typval_T *argvars;
10186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187{
10188 char_u *strregname;
10189 int regname;
10190 char_u buf[NUMBUFLEN + 2];
10191 long reglen = 0;
10192
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010193 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 {
10195 strregname = get_tv_string_chk(&argvars[0]);
10196 if (strregname == NULL) /* type error; errmsg already given */
10197 {
10198 rettv->v_type = VAR_STRING;
10199 rettv->vval.v_string = NULL;
10200 return;
10201 }
10202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 else
10204 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206
10207 regname = (strregname == NULL ? '"' : *strregname);
10208 if (regname == 0)
10209 regname = '"';
10210
10211 buf[0] = NUL;
10212 buf[1] = NUL;
10213 switch (get_reg_type(regname, &reglen))
10214 {
10215 case MLINE: buf[0] = 'V'; break;
10216 case MCHAR: buf[0] = 'v'; break;
10217#ifdef FEAT_VISUAL
10218 case MBLOCK:
10219 buf[0] = Ctrl_V;
10220 sprintf((char *)buf + 1, "%ld", reglen + 1);
10221 break;
10222#endif
10223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224 rettv->v_type = VAR_STRING;
10225 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226}
10227
10228/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010229 * "gettabwinvar()" function
10230 */
10231 static void
10232f_gettabwinvar(argvars, rettv)
10233 typval_T *argvars;
10234 typval_T *rettv;
10235{
10236 getwinvar(argvars, rettv, 1);
10237}
10238
10239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 * "getwinposx()" function
10241 */
10242/*ARGSUSED*/
10243 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010245 typval_T *argvars;
10246 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249#ifdef FEAT_GUI
10250 if (gui.in_use)
10251 {
10252 int x, y;
10253
10254 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 }
10257#endif
10258}
10259
10260/*
10261 * "getwinposy()" function
10262 */
10263/*ARGSUSED*/
10264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010265f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010266 typval_T *argvars;
10267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270#ifdef FEAT_GUI
10271 if (gui.in_use)
10272 {
10273 int x, y;
10274
10275 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010276 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 }
10278#endif
10279}
10280
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010281/*
10282 * Find window specifed by "vp" in tabpage "tp".
10283 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010284 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010285find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010286 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010287 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010288{
10289#ifdef FEAT_WINDOWS
10290 win_T *wp;
10291#endif
10292 int nr;
10293
10294 nr = get_tv_number_chk(vp, NULL);
10295
10296#ifdef FEAT_WINDOWS
10297 if (nr < 0)
10298 return NULL;
10299 if (nr == 0)
10300 return curwin;
10301
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010302 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10303 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010304 if (--nr <= 0)
10305 break;
10306 return wp;
10307#else
10308 if (nr == 0 || nr == 1)
10309 return curwin;
10310 return NULL;
10311#endif
10312}
10313
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314/*
10315 * "getwinvar()" function
10316 */
10317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010319 typval_T *argvars;
10320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010322 getwinvar(argvars, rettv, 0);
10323}
10324
10325/*
10326 * getwinvar() and gettabwinvar()
10327 */
10328 static void
10329getwinvar(argvars, rettv, off)
10330 typval_T *argvars;
10331 typval_T *rettv;
10332 int off; /* 1 for gettabwinvar() */
10333{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 win_T *win, *oldcurwin;
10335 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010337 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010339#ifdef FEAT_WINDOWS
10340 if (off == 1)
10341 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10342 else
10343 tp = curtab;
10344#endif
10345 win = find_win_by_nr(&argvars[off], tp);
10346 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010347 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010349 rettv->v_type = VAR_STRING;
10350 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351
10352 if (win != NULL && varname != NULL)
10353 {
10354 if (*varname == '&') /* window-local-option */
10355 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010356 /* Set curwin to be our win, temporarily. Also set curbuf, so
10357 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 oldcurwin = curwin;
10359 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010360 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010362 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363
10364 /* restore previous notion of curwin */
10365 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010366 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 }
10368 else
10369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010370 if (*varname == NUL)
10371 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10372 * scope prefix before the NUL byte is required by
10373 * find_var_in_ht(). */
10374 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010376 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010378 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 }
10380 }
10381
10382 --emsg_off;
10383}
10384
10385/*
10386 * "glob()" function
10387 */
10388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010389f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010390 typval_T *argvars;
10391 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 expand_T xpc;
10394
10395 ExpandInit(&xpc);
10396 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->v_type = VAR_STRING;
10398 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400}
10401
10402/*
10403 * "globpath()" function
10404 */
10405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *argvars;
10408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
10410 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010414 if (file == NULL)
10415 rettv->vval.v_string = NULL;
10416 else
10417 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418}
10419
10420/*
10421 * "has()" function
10422 */
10423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010424f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010425 typval_T *argvars;
10426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
10428 int i;
10429 char_u *name;
10430 int n = FALSE;
10431 static char *(has_list[]) =
10432 {
10433#ifdef AMIGA
10434 "amiga",
10435# ifdef FEAT_ARP
10436 "arp",
10437# endif
10438#endif
10439#ifdef __BEOS__
10440 "beos",
10441#endif
10442#ifdef MSDOS
10443# ifdef DJGPP
10444 "dos32",
10445# else
10446 "dos16",
10447# endif
10448#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010449#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 "mac",
10451#endif
10452#if defined(MACOS_X_UNIX)
10453 "macunix",
10454#endif
10455#ifdef OS2
10456 "os2",
10457#endif
10458#ifdef __QNX__
10459 "qnx",
10460#endif
10461#ifdef RISCOS
10462 "riscos",
10463#endif
10464#ifdef UNIX
10465 "unix",
10466#endif
10467#ifdef VMS
10468 "vms",
10469#endif
10470#ifdef WIN16
10471 "win16",
10472#endif
10473#ifdef WIN32
10474 "win32",
10475#endif
10476#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10477 "win32unix",
10478#endif
10479#ifdef WIN64
10480 "win64",
10481#endif
10482#ifdef EBCDIC
10483 "ebcdic",
10484#endif
10485#ifndef CASE_INSENSITIVE_FILENAME
10486 "fname_case",
10487#endif
10488#ifdef FEAT_ARABIC
10489 "arabic",
10490#endif
10491#ifdef FEAT_AUTOCMD
10492 "autocmd",
10493#endif
10494#ifdef FEAT_BEVAL
10495 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010496# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10497 "balloon_multiline",
10498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499#endif
10500#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10501 "builtin_terms",
10502# ifdef ALL_BUILTIN_TCAPS
10503 "all_builtin_terms",
10504# endif
10505#endif
10506#ifdef FEAT_BYTEOFF
10507 "byte_offset",
10508#endif
10509#ifdef FEAT_CINDENT
10510 "cindent",
10511#endif
10512#ifdef FEAT_CLIENTSERVER
10513 "clientserver",
10514#endif
10515#ifdef FEAT_CLIPBOARD
10516 "clipboard",
10517#endif
10518#ifdef FEAT_CMDL_COMPL
10519 "cmdline_compl",
10520#endif
10521#ifdef FEAT_CMDHIST
10522 "cmdline_hist",
10523#endif
10524#ifdef FEAT_COMMENTS
10525 "comments",
10526#endif
10527#ifdef FEAT_CRYPT
10528 "cryptv",
10529#endif
10530#ifdef FEAT_CSCOPE
10531 "cscope",
10532#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010533#ifdef CURSOR_SHAPE
10534 "cursorshape",
10535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536#ifdef DEBUG
10537 "debug",
10538#endif
10539#ifdef FEAT_CON_DIALOG
10540 "dialog_con",
10541#endif
10542#ifdef FEAT_GUI_DIALOG
10543 "dialog_gui",
10544#endif
10545#ifdef FEAT_DIFF
10546 "diff",
10547#endif
10548#ifdef FEAT_DIGRAPHS
10549 "digraphs",
10550#endif
10551#ifdef FEAT_DND
10552 "dnd",
10553#endif
10554#ifdef FEAT_EMACS_TAGS
10555 "emacs_tags",
10556#endif
10557 "eval", /* always present, of course! */
10558#ifdef FEAT_EX_EXTRA
10559 "ex_extra",
10560#endif
10561#ifdef FEAT_SEARCH_EXTRA
10562 "extra_search",
10563#endif
10564#ifdef FEAT_FKMAP
10565 "farsi",
10566#endif
10567#ifdef FEAT_SEARCHPATH
10568 "file_in_path",
10569#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010570#if defined(UNIX) && !defined(USE_SYSTEM)
10571 "filterpipe",
10572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573#ifdef FEAT_FIND_ID
10574 "find_in_path",
10575#endif
10576#ifdef FEAT_FOLDING
10577 "folding",
10578#endif
10579#ifdef FEAT_FOOTER
10580 "footer",
10581#endif
10582#if !defined(USE_SYSTEM) && defined(UNIX)
10583 "fork",
10584#endif
10585#ifdef FEAT_GETTEXT
10586 "gettext",
10587#endif
10588#ifdef FEAT_GUI
10589 "gui",
10590#endif
10591#ifdef FEAT_GUI_ATHENA
10592# ifdef FEAT_GUI_NEXTAW
10593 "gui_neXtaw",
10594# else
10595 "gui_athena",
10596# endif
10597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598#ifdef FEAT_GUI_GTK
10599 "gui_gtk",
10600# ifdef HAVE_GTK2
10601 "gui_gtk2",
10602# endif
10603#endif
10604#ifdef FEAT_GUI_MAC
10605 "gui_mac",
10606#endif
10607#ifdef FEAT_GUI_MOTIF
10608 "gui_motif",
10609#endif
10610#ifdef FEAT_GUI_PHOTON
10611 "gui_photon",
10612#endif
10613#ifdef FEAT_GUI_W16
10614 "gui_win16",
10615#endif
10616#ifdef FEAT_GUI_W32
10617 "gui_win32",
10618#endif
10619#ifdef FEAT_HANGULIN
10620 "hangul_input",
10621#endif
10622#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10623 "iconv",
10624#endif
10625#ifdef FEAT_INS_EXPAND
10626 "insert_expand",
10627#endif
10628#ifdef FEAT_JUMPLIST
10629 "jumplist",
10630#endif
10631#ifdef FEAT_KEYMAP
10632 "keymap",
10633#endif
10634#ifdef FEAT_LANGMAP
10635 "langmap",
10636#endif
10637#ifdef FEAT_LIBCALL
10638 "libcall",
10639#endif
10640#ifdef FEAT_LINEBREAK
10641 "linebreak",
10642#endif
10643#ifdef FEAT_LISP
10644 "lispindent",
10645#endif
10646#ifdef FEAT_LISTCMDS
10647 "listcmds",
10648#endif
10649#ifdef FEAT_LOCALMAP
10650 "localmap",
10651#endif
10652#ifdef FEAT_MENU
10653 "menu",
10654#endif
10655#ifdef FEAT_SESSION
10656 "mksession",
10657#endif
10658#ifdef FEAT_MODIFY_FNAME
10659 "modify_fname",
10660#endif
10661#ifdef FEAT_MOUSE
10662 "mouse",
10663#endif
10664#ifdef FEAT_MOUSESHAPE
10665 "mouseshape",
10666#endif
10667#if defined(UNIX) || defined(VMS)
10668# ifdef FEAT_MOUSE_DEC
10669 "mouse_dec",
10670# endif
10671# ifdef FEAT_MOUSE_GPM
10672 "mouse_gpm",
10673# endif
10674# ifdef FEAT_MOUSE_JSB
10675 "mouse_jsbterm",
10676# endif
10677# ifdef FEAT_MOUSE_NET
10678 "mouse_netterm",
10679# endif
10680# ifdef FEAT_MOUSE_PTERM
10681 "mouse_pterm",
10682# endif
10683# ifdef FEAT_MOUSE_XTERM
10684 "mouse_xterm",
10685# endif
10686#endif
10687#ifdef FEAT_MBYTE
10688 "multi_byte",
10689#endif
10690#ifdef FEAT_MBYTE_IME
10691 "multi_byte_ime",
10692#endif
10693#ifdef FEAT_MULTI_LANG
10694 "multi_lang",
10695#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010696#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010697#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010698 "mzscheme",
10699#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701#ifdef FEAT_OLE
10702 "ole",
10703#endif
10704#ifdef FEAT_OSFILETYPE
10705 "osfiletype",
10706#endif
10707#ifdef FEAT_PATH_EXTRA
10708 "path_extra",
10709#endif
10710#ifdef FEAT_PERL
10711#ifndef DYNAMIC_PERL
10712 "perl",
10713#endif
10714#endif
10715#ifdef FEAT_PYTHON
10716#ifndef DYNAMIC_PYTHON
10717 "python",
10718#endif
10719#endif
10720#ifdef FEAT_POSTSCRIPT
10721 "postscript",
10722#endif
10723#ifdef FEAT_PRINTER
10724 "printer",
10725#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010726#ifdef FEAT_PROFILE
10727 "profile",
10728#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010729#ifdef FEAT_RELTIME
10730 "reltime",
10731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732#ifdef FEAT_QUICKFIX
10733 "quickfix",
10734#endif
10735#ifdef FEAT_RIGHTLEFT
10736 "rightleft",
10737#endif
10738#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10739 "ruby",
10740#endif
10741#ifdef FEAT_SCROLLBIND
10742 "scrollbind",
10743#endif
10744#ifdef FEAT_CMDL_INFO
10745 "showcmd",
10746 "cmdline_info",
10747#endif
10748#ifdef FEAT_SIGNS
10749 "signs",
10750#endif
10751#ifdef FEAT_SMARTINDENT
10752 "smartindent",
10753#endif
10754#ifdef FEAT_SNIFF
10755 "sniff",
10756#endif
10757#ifdef FEAT_STL_OPT
10758 "statusline",
10759#endif
10760#ifdef FEAT_SUN_WORKSHOP
10761 "sun_workshop",
10762#endif
10763#ifdef FEAT_NETBEANS_INTG
10764 "netbeans_intg",
10765#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010766#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010767 "spell",
10768#endif
10769#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 "syntax",
10771#endif
10772#if defined(USE_SYSTEM) || !defined(UNIX)
10773 "system",
10774#endif
10775#ifdef FEAT_TAG_BINS
10776 "tag_binary",
10777#endif
10778#ifdef FEAT_TAG_OLDSTATIC
10779 "tag_old_static",
10780#endif
10781#ifdef FEAT_TAG_ANYWHITE
10782 "tag_any_white",
10783#endif
10784#ifdef FEAT_TCL
10785# ifndef DYNAMIC_TCL
10786 "tcl",
10787# endif
10788#endif
10789#ifdef TERMINFO
10790 "terminfo",
10791#endif
10792#ifdef FEAT_TERMRESPONSE
10793 "termresponse",
10794#endif
10795#ifdef FEAT_TEXTOBJ
10796 "textobjects",
10797#endif
10798#ifdef HAVE_TGETENT
10799 "tgetent",
10800#endif
10801#ifdef FEAT_TITLE
10802 "title",
10803#endif
10804#ifdef FEAT_TOOLBAR
10805 "toolbar",
10806#endif
10807#ifdef FEAT_USR_CMDS
10808 "user-commands", /* was accidentally included in 5.4 */
10809 "user_commands",
10810#endif
10811#ifdef FEAT_VIMINFO
10812 "viminfo",
10813#endif
10814#ifdef FEAT_VERTSPLIT
10815 "vertsplit",
10816#endif
10817#ifdef FEAT_VIRTUALEDIT
10818 "virtualedit",
10819#endif
10820#ifdef FEAT_VISUAL
10821 "visual",
10822#endif
10823#ifdef FEAT_VISUALEXTRA
10824 "visualextra",
10825#endif
10826#ifdef FEAT_VREPLACE
10827 "vreplace",
10828#endif
10829#ifdef FEAT_WILDIGN
10830 "wildignore",
10831#endif
10832#ifdef FEAT_WILDMENU
10833 "wildmenu",
10834#endif
10835#ifdef FEAT_WINDOWS
10836 "windows",
10837#endif
10838#ifdef FEAT_WAK
10839 "winaltkeys",
10840#endif
10841#ifdef FEAT_WRITEBACKUP
10842 "writebackup",
10843#endif
10844#ifdef FEAT_XIM
10845 "xim",
10846#endif
10847#ifdef FEAT_XFONTSET
10848 "xfontset",
10849#endif
10850#ifdef USE_XSMP
10851 "xsmp",
10852#endif
10853#ifdef USE_XSMP_INTERACT
10854 "xsmp_interact",
10855#endif
10856#ifdef FEAT_XCLIPBOARD
10857 "xterm_clipboard",
10858#endif
10859#ifdef FEAT_XTERM_SAVE
10860 "xterm_save",
10861#endif
10862#if defined(UNIX) && defined(FEAT_X11)
10863 "X11",
10864#endif
10865 NULL
10866 };
10867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 for (i = 0; has_list[i] != NULL; ++i)
10870 if (STRICMP(name, has_list[i]) == 0)
10871 {
10872 n = TRUE;
10873 break;
10874 }
10875
10876 if (n == FALSE)
10877 {
10878 if (STRNICMP(name, "patch", 5) == 0)
10879 n = has_patch(atoi((char *)name + 5));
10880 else if (STRICMP(name, "vim_starting") == 0)
10881 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010882#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10883 else if (STRICMP(name, "balloon_multiline") == 0)
10884 n = multiline_balloon_available();
10885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886#ifdef DYNAMIC_TCL
10887 else if (STRICMP(name, "tcl") == 0)
10888 n = tcl_enabled(FALSE);
10889#endif
10890#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10891 else if (STRICMP(name, "iconv") == 0)
10892 n = iconv_enabled(FALSE);
10893#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010894#ifdef DYNAMIC_MZSCHEME
10895 else if (STRICMP(name, "mzscheme") == 0)
10896 n = mzscheme_enabled(FALSE);
10897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898#ifdef DYNAMIC_RUBY
10899 else if (STRICMP(name, "ruby") == 0)
10900 n = ruby_enabled(FALSE);
10901#endif
10902#ifdef DYNAMIC_PYTHON
10903 else if (STRICMP(name, "python") == 0)
10904 n = python_enabled(FALSE);
10905#endif
10906#ifdef DYNAMIC_PERL
10907 else if (STRICMP(name, "perl") == 0)
10908 n = perl_enabled(FALSE);
10909#endif
10910#ifdef FEAT_GUI
10911 else if (STRICMP(name, "gui_running") == 0)
10912 n = (gui.in_use || gui.starting);
10913# ifdef FEAT_GUI_W32
10914 else if (STRICMP(name, "gui_win32s") == 0)
10915 n = gui_is_win32s();
10916# endif
10917# ifdef FEAT_BROWSE
10918 else if (STRICMP(name, "browse") == 0)
10919 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10920# endif
10921#endif
10922#ifdef FEAT_SYN_HL
10923 else if (STRICMP(name, "syntax_items") == 0)
10924 n = syntax_present(curbuf);
10925#endif
10926#if defined(WIN3264)
10927 else if (STRICMP(name, "win95") == 0)
10928 n = mch_windows95();
10929#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010930#ifdef FEAT_NETBEANS_INTG
10931 else if (STRICMP(name, "netbeans_enabled") == 0)
10932 n = usingNetbeans;
10933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 }
10935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937}
10938
10939/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940 * "has_key()" function
10941 */
10942 static void
10943f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T *argvars;
10945 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946{
10947 rettv->vval.v_number = 0;
10948 if (argvars[0].v_type != VAR_DICT)
10949 {
10950 EMSG(_(e_dictreq));
10951 return;
10952 }
10953 if (argvars[0].vval.v_dict == NULL)
10954 return;
10955
10956 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010957 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958}
10959
10960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 * "hasmapto()" function
10962 */
10963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *argvars;
10966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
10968 char_u *name;
10969 char_u *mode;
10970 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010971 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010974 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 mode = (char_u *)"nvo";
10976 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010979 if (argvars[2].v_type != VAR_UNKNOWN)
10980 abbr = get_tv_number(&argvars[2]);
10981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982
Bram Moolenaar2c932302006-03-18 21:42:09 +000010983 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987}
10988
10989/*
10990 * "histadd()" function
10991 */
10992/*ARGSUSED*/
10993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010995 typval_T *argvars;
10996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997{
10998#ifdef FEAT_CMDHIST
10999 int histype;
11000 char_u *str;
11001 char_u buf[NUMBUFLEN];
11002#endif
11003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 if (check_restricted() || check_secure())
11006 return;
11007#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11009 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 if (histype >= 0)
11011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 if (*str != NUL)
11014 {
11015 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 return;
11018 }
11019 }
11020#endif
11021}
11022
11023/*
11024 * "histdel()" function
11025 */
11026/*ARGSUSED*/
11027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011029 typval_T *argvars;
11030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031{
11032#ifdef FEAT_CMDHIST
11033 int n;
11034 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011035 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011037 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11038 if (str == NULL)
11039 n = 0;
11040 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 else
11048 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011049 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 get_tv_string_buf(&argvars[1], buf));
11051 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054#endif
11055}
11056
11057/*
11058 * "histget()" function
11059 */
11060/*ARGSUSED*/
11061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 typval_T *argvars;
11064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065{
11066#ifdef FEAT_CMDHIST
11067 int type;
11068 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11072 if (str == NULL)
11073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 {
11076 type = get_histtype(str);
11077 if (argvars[1].v_type == VAR_UNKNOWN)
11078 idx = get_history_idx(type);
11079 else
11080 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11081 /* -1 on type error */
11082 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011087 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088}
11089
11090/*
11091 * "histnr()" function
11092 */
11093/*ARGSUSED*/
11094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011096 typval_T *argvars;
11097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 int i;
11100
11101#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102 char_u *history = get_tv_string_chk(&argvars[0]);
11103
11104 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 if (i >= HIST_CMD && i < HIST_COUNT)
11106 i = get_history_idx(i);
11107 else
11108#endif
11109 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111}
11112
11113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 * "highlightID(name)" function
11115 */
11116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011118 typval_T *argvars;
11119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122}
11123
11124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011125 * "highlight_exists()" function
11126 */
11127 static void
11128f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131{
11132 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11133}
11134
11135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 * "hostname()" function
11137 */
11138/*ARGSUSED*/
11139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011141 typval_T *argvars;
11142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
11144 char_u hostname[256];
11145
11146 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->v_type = VAR_STRING;
11148 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149}
11150
11151/*
11152 * iconv() function
11153 */
11154/*ARGSUSED*/
11155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156f_iconv(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{
11160#ifdef FEAT_MBYTE
11161 char_u buf1[NUMBUFLEN];
11162 char_u buf2[NUMBUFLEN];
11163 char_u *from, *to, *str;
11164 vimconv_T vimconv;
11165#endif
11166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->v_type = VAR_STRING;
11168 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169
11170#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 str = get_tv_string(&argvars[0]);
11172 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11173 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 vimconv.vc_type = CONV_NONE;
11175 convert_setup(&vimconv, from, to);
11176
11177 /* If the encodings are equal, no conversion needed. */
11178 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011181 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182
11183 convert_setup(&vimconv, NULL, NULL);
11184 vim_free(from);
11185 vim_free(to);
11186#endif
11187}
11188
11189/*
11190 * "indent()" function
11191 */
11192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 typval_T *argvars;
11195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196{
11197 linenr_T lnum;
11198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011199 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204}
11205
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011206/*
11207 * "index()" function
11208 */
11209 static void
11210f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *argvars;
11212 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011213{
Bram Moolenaar33570922005-01-25 22:26:29 +000011214 list_T *l;
11215 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011216 long idx = 0;
11217 int ic = FALSE;
11218
11219 rettv->vval.v_number = -1;
11220 if (argvars[0].v_type != VAR_LIST)
11221 {
11222 EMSG(_(e_listreq));
11223 return;
11224 }
11225 l = argvars[0].vval.v_list;
11226 if (l != NULL)
11227 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011228 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011229 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231 int error = FALSE;
11232
Bram Moolenaar758711c2005-02-02 23:11:38 +000011233 /* Start at specified item. Use the cached index that list_find()
11234 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011236 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011237 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238 ic = get_tv_number_chk(&argvars[3], &error);
11239 if (error)
11240 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011241 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011242
Bram Moolenaar758711c2005-02-02 23:11:38 +000011243 for ( ; item != NULL; item = item->li_next, ++idx)
11244 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011245 {
11246 rettv->vval.v_number = idx;
11247 break;
11248 }
11249 }
11250}
11251
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252static int inputsecret_flag = 0;
11253
11254/*
11255 * "input()" function
11256 * Also handles inputsecret() when inputsecret is set.
11257 */
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011263 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 char_u *p = NULL;
11265 int c;
11266 char_u buf[NUMBUFLEN];
11267 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011268 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011269 int xp_type = EXPAND_NOTHING;
11270 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273
11274#ifdef NO_CONSOLE_INPUT
11275 /* While starting up, there is no place to enter text. */
11276 if (no_console_input())
11277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 return;
11280 }
11281#endif
11282
11283 cmd_silent = FALSE; /* Want to see the prompt. */
11284 if (prompt != NULL)
11285 {
11286 /* Only the part of the message after the last NL is considered as
11287 * prompt for the command line */
11288 p = vim_strrchr(prompt, '\n');
11289 if (p == NULL)
11290 p = prompt;
11291 else
11292 {
11293 ++p;
11294 c = *p;
11295 *p = NUL;
11296 msg_start();
11297 msg_clr_eos();
11298 msg_puts_attr(prompt, echo_attr);
11299 msg_didout = FALSE;
11300 msg_starthere();
11301 *p = c;
11302 }
11303 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 if (argvars[1].v_type != VAR_UNKNOWN)
11306 {
11307 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11308 if (defstr != NULL)
11309 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310
Bram Moolenaar4463f292005-09-25 22:20:24 +000011311 if (argvars[2].v_type != VAR_UNKNOWN)
11312 {
11313 char_u *xp_name;
11314 int xp_namelen;
11315 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011316
Bram Moolenaar4463f292005-09-25 22:20:24 +000011317 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011318
Bram Moolenaar4463f292005-09-25 22:20:24 +000011319 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11320 if (xp_name == NULL)
11321 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011322
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011323 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011324
Bram Moolenaar4463f292005-09-25 22:20:24 +000011325 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11326 &xp_arg) == FAIL)
11327 return;
11328 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011329 }
11330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 if (defstr != NULL)
11332 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011333 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11334 xp_type, xp_arg);
11335
11336 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 /* since the user typed this, no need to wait for return */
11339 need_wait_return = FALSE;
11340 msg_didout = FALSE;
11341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 cmd_silent = cmd_silent_save;
11343}
11344
11345/*
11346 * "inputdialog()" function
11347 */
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011350 typval_T *argvars;
11351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352{
11353#if defined(FEAT_GUI_TEXTDIALOG)
11354 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11355 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11356 {
11357 char_u *message;
11358 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 message = get_tv_string_chk(&argvars[0]);
11362 if (argvars[1].v_type != VAR_UNKNOWN
11363 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011364 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 else
11366 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 if (message != NULL && defstr != NULL
11368 && do_dialog(VIM_QUESTION, NULL, message,
11369 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 else
11372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 if (message != NULL && defstr != NULL
11374 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011375 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->vval.v_string = vim_strsave(
11377 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383 else
11384#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386}
11387
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011388/*
11389 * "inputlist()" function
11390 */
11391 static void
11392f_inputlist(argvars, rettv)
11393 typval_T *argvars;
11394 typval_T *rettv;
11395{
11396 listitem_T *li;
11397 int selected;
11398 int mouse_used;
11399
11400 rettv->vval.v_number = 0;
11401#ifdef NO_CONSOLE_INPUT
11402 /* While starting up, there is no place to enter text. */
11403 if (no_console_input())
11404 return;
11405#endif
11406 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11407 {
11408 EMSG2(_(e_listarg), "inputlist()");
11409 return;
11410 }
11411
11412 msg_start();
11413 lines_left = Rows; /* avoid more prompt */
11414 msg_scroll = TRUE;
11415 msg_clr_eos();
11416
11417 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11418 {
11419 msg_puts(get_tv_string(&li->li_tv));
11420 msg_putchar('\n');
11421 }
11422
11423 /* Ask for choice. */
11424 selected = prompt_for_number(&mouse_used);
11425 if (mouse_used)
11426 selected -= lines_left;
11427
11428 rettv->vval.v_number = selected;
11429}
11430
11431
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11433
11434/*
11435 * "inputrestore()" function
11436 */
11437/*ARGSUSED*/
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *argvars;
11441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
11443 if (ga_userinput.ga_len > 0)
11444 {
11445 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11447 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 }
11450 else if (p_verbose > 1)
11451 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011452 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 }
11455}
11456
11457/*
11458 * "inputsave()" function
11459 */
11460/*ARGSUSED*/
11461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011463 typval_T *argvars;
11464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465{
11466 /* Add an entry to the stack of typehead storage. */
11467 if (ga_grow(&ga_userinput, 1) == OK)
11468 {
11469 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11470 + ga_userinput.ga_len);
11471 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
11474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476}
11477
11478/*
11479 * "inputsecret()" function
11480 */
11481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011483 typval_T *argvars;
11484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485{
11486 ++cmdline_star;
11487 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 --cmdline_star;
11490 --inputsecret_flag;
11491}
11492
11493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011494 * "insert()" function
11495 */
11496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011498 typval_T *argvars;
11499 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011500{
11501 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 listitem_T *item;
11503 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011505
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011506 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011507 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011508 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011509 else if ((l = argvars[0].vval.v_list) != NULL
11510 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011511 {
11512 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011513 before = get_tv_number_chk(&argvars[2], &error);
11514 if (error)
11515 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011516
Bram Moolenaar758711c2005-02-02 23:11:38 +000011517 if (before == l->lv_len)
11518 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011519 else
11520 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011521 item = list_find(l, before);
11522 if (item == NULL)
11523 {
11524 EMSGN(_(e_listidx), before);
11525 l = NULL;
11526 }
11527 }
11528 if (l != NULL)
11529 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011530 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011531 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011532 }
11533 }
11534}
11535
11536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 * "isdirectory()" function
11538 */
11539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011541 typval_T *argvars;
11542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545}
11546
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011547/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011548 * "islocked()" function
11549 */
11550 static void
11551f_islocked(argvars, rettv)
11552 typval_T *argvars;
11553 typval_T *rettv;
11554{
11555 lval_T lv;
11556 char_u *end;
11557 dictitem_T *di;
11558
11559 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011560 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11561 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011562 if (end != NULL && lv.ll_name != NULL)
11563 {
11564 if (*end != NUL)
11565 EMSG(_(e_trailing));
11566 else
11567 {
11568 if (lv.ll_tv == NULL)
11569 {
11570 if (check_changedtick(lv.ll_name))
11571 rettv->vval.v_number = 1; /* always locked */
11572 else
11573 {
11574 di = find_var(lv.ll_name, NULL);
11575 if (di != NULL)
11576 {
11577 /* Consider a variable locked when:
11578 * 1. the variable itself is locked
11579 * 2. the value of the variable is locked.
11580 * 3. the List or Dict value is locked.
11581 */
11582 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11583 || tv_islocked(&di->di_tv));
11584 }
11585 }
11586 }
11587 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011588 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011589 else if (lv.ll_newkey != NULL)
11590 EMSG2(_(e_dictkey), lv.ll_newkey);
11591 else if (lv.ll_list != NULL)
11592 /* List item. */
11593 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11594 else
11595 /* Dictionary item. */
11596 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11597 }
11598 }
11599
11600 clear_lval(&lv);
11601}
11602
Bram Moolenaar33570922005-01-25 22:26:29 +000011603static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011604
11605/*
11606 * Turn a dict into a list:
11607 * "what" == 0: list of keys
11608 * "what" == 1: list of values
11609 * "what" == 2: list of items
11610 */
11611 static void
11612dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011613 typval_T *argvars;
11614 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011615 int what;
11616{
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 list_T *l2;
11618 dictitem_T *di;
11619 hashitem_T *hi;
11620 listitem_T *li;
11621 listitem_T *li2;
11622 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011623 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011624
11625 rettv->vval.v_number = 0;
11626 if (argvars[0].v_type != VAR_DICT)
11627 {
11628 EMSG(_(e_dictreq));
11629 return;
11630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011631 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011632 return;
11633
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011634 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011635 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011636
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011637 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011640 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011641 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011642 --todo;
11643 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011644
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011645 li = listitem_alloc();
11646 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011647 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011648 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011649
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011650 if (what == 0)
11651 {
11652 /* keys() */
11653 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011654 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011655 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11656 }
11657 else if (what == 1)
11658 {
11659 /* values() */
11660 copy_tv(&di->di_tv, &li->li_tv);
11661 }
11662 else
11663 {
11664 /* items() */
11665 l2 = list_alloc();
11666 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011667 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011668 li->li_tv.vval.v_list = l2;
11669 if (l2 == NULL)
11670 break;
11671 ++l2->lv_refcount;
11672
11673 li2 = listitem_alloc();
11674 if (li2 == NULL)
11675 break;
11676 list_append(l2, li2);
11677 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011678 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011679 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11680
11681 li2 = listitem_alloc();
11682 if (li2 == NULL)
11683 break;
11684 list_append(l2, li2);
11685 copy_tv(&di->di_tv, &li2->li_tv);
11686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011687 }
11688 }
11689}
11690
11691/*
11692 * "items(dict)" function
11693 */
11694 static void
11695f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011696 typval_T *argvars;
11697 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011698{
11699 dict_list(argvars, rettv, 2);
11700}
11701
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011703 * "join()" function
11704 */
11705 static void
11706f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011707 typval_T *argvars;
11708 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011709{
11710 garray_T ga;
11711 char_u *sep;
11712
11713 rettv->vval.v_number = 0;
11714 if (argvars[0].v_type != VAR_LIST)
11715 {
11716 EMSG(_(e_listreq));
11717 return;
11718 }
11719 if (argvars[0].vval.v_list == NULL)
11720 return;
11721 if (argvars[1].v_type == VAR_UNKNOWN)
11722 sep = (char_u *)" ";
11723 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011725
11726 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727
11728 if (sep != NULL)
11729 {
11730 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011731 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011732 ga_append(&ga, NUL);
11733 rettv->vval.v_string = (char_u *)ga.ga_data;
11734 }
11735 else
11736 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011737}
11738
11739/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011740 * "keys()" function
11741 */
11742 static void
11743f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 typval_T *argvars;
11745 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011746{
11747 dict_list(argvars, rettv, 0);
11748}
11749
11750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 * "last_buffer_nr()" function.
11752 */
11753/*ARGSUSED*/
11754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011756 typval_T *argvars;
11757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758{
11759 int n = 0;
11760 buf_T *buf;
11761
11762 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11763 if (n < buf->b_fnum)
11764 n = buf->b_fnum;
11765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011766 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011767}
11768
11769/*
11770 * "len()" function
11771 */
11772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011774 typval_T *argvars;
11775 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011776{
11777 switch (argvars[0].v_type)
11778 {
11779 case VAR_STRING:
11780 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_number = (varnumber_T)STRLEN(
11782 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011783 break;
11784 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011786 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011787 case VAR_DICT:
11788 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11789 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011790 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011791 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011792 break;
11793 }
11794}
11795
Bram Moolenaar33570922005-01-25 22:26:29 +000011796static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011797
11798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011800 typval_T *argvars;
11801 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011802 int type;
11803{
11804#ifdef FEAT_LIBCALL
11805 char_u *string_in;
11806 char_u **string_result;
11807 int nr_result;
11808#endif
11809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011815
11816 if (check_restricted() || check_secure())
11817 return;
11818
11819#ifdef FEAT_LIBCALL
11820 /* The first two args must be strings, otherwise its meaningless */
11821 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11822 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011823 string_in = NULL;
11824 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011825 string_in = argvars[2].vval.v_string;
11826 if (type == VAR_NUMBER)
11827 string_result = NULL;
11828 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011830 if (mch_libcall(argvars[0].vval.v_string,
11831 argvars[1].vval.v_string,
11832 string_in,
11833 argvars[2].vval.v_number,
11834 string_result,
11835 &nr_result) == OK
11836 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011837 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011838 }
11839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840}
11841
11842/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011843 * "libcall()" function
11844 */
11845 static void
11846f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *argvars;
11848 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011849{
11850 libcall_common(argvars, rettv, VAR_STRING);
11851}
11852
11853/*
11854 * "libcallnr()" function
11855 */
11856 static void
11857f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011858 typval_T *argvars;
11859 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011860{
11861 libcall_common(argvars, rettv, VAR_NUMBER);
11862}
11863
11864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 * "line(string)" function
11866 */
11867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011869 typval_T *argvars;
11870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871{
11872 linenr_T lnum = 0;
11873 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011874 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011876 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 if (fp != NULL)
11878 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880}
11881
11882/*
11883 * "line2byte(lnum)" function
11884 */
11885/*ARGSUSED*/
11886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011888 typval_T *argvars;
11889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890{
11891#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011892 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893#else
11894 linenr_T lnum;
11895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11901 if (rettv->vval.v_number >= 0)
11902 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903#endif
11904}
11905
11906/*
11907 * "lispindent(lnum)" function
11908 */
11909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011911 typval_T *argvars;
11912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913{
11914#ifdef FEAT_LISP
11915 pos_T pos;
11916 linenr_T lnum;
11917
11918 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11921 {
11922 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011923 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 curwin->w_cursor = pos;
11925 }
11926 else
11927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011928 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929}
11930
11931/*
11932 * "localtime()" function
11933 */
11934/*ARGSUSED*/
11935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011936f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011937 typval_T *argvars;
11938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941}
11942
Bram Moolenaar33570922005-01-25 22:26:29 +000011943static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944
11945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 typval_T *argvars;
11948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 int exact;
11950{
11951 char_u *keys;
11952 char_u *which;
11953 char_u buf[NUMBUFLEN];
11954 char_u *keys_buf = NULL;
11955 char_u *rhs;
11956 int mode;
11957 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011958 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959
11960 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->v_type = VAR_STRING;
11962 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 if (*keys == NUL)
11966 return;
11967
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011971 if (argvars[2].v_type != VAR_UNKNOWN)
11972 abbr = get_tv_number(&argvars[2]);
11973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 else
11975 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 if (which == NULL)
11977 return;
11978
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 mode = get_map_mode(&which, 0);
11980
11981 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011982 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 vim_free(keys_buf);
11984 if (rhs != NULL)
11985 {
11986 ga_init(&ga);
11987 ga.ga_itemsize = 1;
11988 ga.ga_growsize = 40;
11989
11990 while (*rhs != NUL)
11991 ga_concat(&ga, str2special(&rhs, FALSE));
11992
11993 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 }
11996}
11997
11998/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011999 * "map()" function
12000 */
12001 static void
12002f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012003 typval_T *argvars;
12004 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012005{
12006 filter_map(argvars, rettv, TRUE);
12007}
12008
12009/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 */
12012 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012013f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012014 typval_T *argvars;
12015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012017 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018}
12019
12020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012021 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 */
12023 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012025 typval_T *argvars;
12026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012028 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029}
12030
Bram Moolenaar33570922005-01-25 22:26:29 +000012031static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
12033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012035 typval_T *argvars;
12036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 int type;
12038{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012039 char_u *str = NULL;
12040 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 char_u *pat;
12042 regmatch_T regmatch;
12043 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012044 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 char_u *save_cpo;
12046 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012047 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012048 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012049 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012050 list_T *l = NULL;
12051 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012052 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012053 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054
12055 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12056 save_cpo = p_cpo;
12057 p_cpo = (char_u *)"";
12058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012059 rettv->vval.v_number = -1;
12060 if (type == 3)
12061 {
12062 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012063 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012064 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012065 }
12066 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->v_type = VAR_STRING;
12069 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012072 if (argvars[0].v_type == VAR_LIST)
12073 {
12074 if ((l = argvars[0].vval.v_list) == NULL)
12075 goto theend;
12076 li = l->lv_first;
12077 }
12078 else
12079 expr = str = get_tv_string(&argvars[0]);
12080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12082 if (pat == NULL)
12083 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012084
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012085 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012087 int error = FALSE;
12088
12089 start = get_tv_number_chk(&argvars[2], &error);
12090 if (error)
12091 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012092 if (l != NULL)
12093 {
12094 li = list_find(l, start);
12095 if (li == NULL)
12096 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012097 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012098 }
12099 else
12100 {
12101 if (start < 0)
12102 start = 0;
12103 if (start > (long)STRLEN(str))
12104 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012105 /* When "count" argument is there ignore matches before "start",
12106 * otherwise skip part of the string. Differs when pattern is "^"
12107 * or "\<". */
12108 if (argvars[3].v_type != VAR_UNKNOWN)
12109 startcol = start;
12110 else
12111 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012112 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012113
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012114 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012115 nth = get_tv_number_chk(&argvars[3], &error);
12116 if (error)
12117 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 }
12119
12120 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12121 if (regmatch.regprog != NULL)
12122 {
12123 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012124
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012125 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012126 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012127 if (l != NULL)
12128 {
12129 if (li == NULL)
12130 {
12131 match = FALSE;
12132 break;
12133 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012134 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012135 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012136 if (str == NULL)
12137 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012138 }
12139
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012140 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012141
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012142 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012143 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012144 if (l == NULL && !match)
12145 break;
12146
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012147 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012148 if (l != NULL)
12149 {
12150 li = li->li_next;
12151 ++idx;
12152 }
12153 else
12154 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012155#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012156 startcol = (colnr_T)(regmatch.startp[0]
12157 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012158#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012159 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012160#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012161 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012162 }
12163
12164 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012166 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012167 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012168 int i;
12169
12170 /* return list with matched string and submatches */
12171 for (i = 0; i < NSUBEXP; ++i)
12172 {
12173 if (regmatch.endp[i] == NULL)
12174 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012175 if (list_append_string(rettv->vval.v_list,
12176 regmatch.startp[i],
12177 (int)(regmatch.endp[i] - regmatch.startp[i]))
12178 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012179 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012180 }
12181 }
12182 else if (type == 2)
12183 {
12184 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012185 if (l != NULL)
12186 copy_tv(&li->li_tv, rettv);
12187 else
12188 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012190 }
12191 else if (l != NULL)
12192 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 else
12194 {
12195 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 (varnumber_T)(regmatch.startp[0] - str);
12198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012201 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 }
12203 }
12204 vim_free(regmatch.regprog);
12205 }
12206
12207theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012208 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 p_cpo = save_cpo;
12210}
12211
12212/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213 * "match()" function
12214 */
12215 static void
12216f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012217 typval_T *argvars;
12218 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012219{
12220 find_some_match(argvars, rettv, 1);
12221}
12222
12223/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012224 * "matcharg()" function
12225 */
12226 static void
12227f_matcharg(argvars, rettv)
12228 typval_T *argvars;
12229 typval_T *rettv;
12230{
12231 if (rettv_list_alloc(rettv) == OK)
12232 {
12233#ifdef FEAT_SEARCH_EXTRA
12234 int mi = get_tv_number(&argvars[0]);
12235
12236 if (mi >= 1 && mi <= 3)
12237 {
12238 list_append_string(rettv->vval.v_list,
12239 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12240 list_append_string(rettv->vval.v_list,
12241 curwin->w_match_pat[mi - 1], -1);
12242 }
12243#endif
12244 }
12245}
12246
12247/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248 * "matchend()" function
12249 */
12250 static void
12251f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012252 typval_T *argvars;
12253 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012254{
12255 find_some_match(argvars, rettv, 0);
12256}
12257
12258/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012259 * "matchlist()" function
12260 */
12261 static void
12262f_matchlist(argvars, rettv)
12263 typval_T *argvars;
12264 typval_T *rettv;
12265{
12266 find_some_match(argvars, rettv, 3);
12267}
12268
12269/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012270 * "matchstr()" function
12271 */
12272 static void
12273f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012274 typval_T *argvars;
12275 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012276{
12277 find_some_match(argvars, rettv, 2);
12278}
12279
Bram Moolenaar33570922005-01-25 22:26:29 +000012280static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012281
12282 static void
12283max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012284 typval_T *argvars;
12285 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012286 int domax;
12287{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012288 long n = 0;
12289 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012291
12292 if (argvars[0].v_type == VAR_LIST)
12293 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012294 list_T *l;
12295 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012296
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012297 l = argvars[0].vval.v_list;
12298 if (l != NULL)
12299 {
12300 li = l->lv_first;
12301 if (li != NULL)
12302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012303 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012304 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012305 {
12306 li = li->li_next;
12307 if (li == NULL)
12308 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012309 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012310 if (domax ? i > n : i < n)
12311 n = i;
12312 }
12313 }
12314 }
12315 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012316 else if (argvars[0].v_type == VAR_DICT)
12317 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012318 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012319 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012320 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012321 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012322
12323 d = argvars[0].vval.v_dict;
12324 if (d != NULL)
12325 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012326 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012327 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012329 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012331 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012332 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012333 if (first)
12334 {
12335 n = i;
12336 first = FALSE;
12337 }
12338 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012339 n = i;
12340 }
12341 }
12342 }
12343 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012344 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012345 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012346 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012347}
12348
12349/*
12350 * "max()" function
12351 */
12352 static void
12353f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012354 typval_T *argvars;
12355 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012356{
12357 max_min(argvars, rettv, TRUE);
12358}
12359
12360/*
12361 * "min()" function
12362 */
12363 static void
12364f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012365 typval_T *argvars;
12366 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012367{
12368 max_min(argvars, rettv, FALSE);
12369}
12370
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012371static int mkdir_recurse __ARGS((char_u *dir, int prot));
12372
12373/*
12374 * Create the directory in which "dir" is located, and higher levels when
12375 * needed.
12376 */
12377 static int
12378mkdir_recurse(dir, prot)
12379 char_u *dir;
12380 int prot;
12381{
12382 char_u *p;
12383 char_u *updir;
12384 int r = FAIL;
12385
12386 /* Get end of directory name in "dir".
12387 * We're done when it's "/" or "c:/". */
12388 p = gettail_sep(dir);
12389 if (p <= get_past_head(dir))
12390 return OK;
12391
12392 /* If the directory exists we're done. Otherwise: create it.*/
12393 updir = vim_strnsave(dir, (int)(p - dir));
12394 if (updir == NULL)
12395 return FAIL;
12396 if (mch_isdir(updir))
12397 r = OK;
12398 else if (mkdir_recurse(updir, prot) == OK)
12399 r = vim_mkdir_emsg(updir, prot);
12400 vim_free(updir);
12401 return r;
12402}
12403
12404#ifdef vim_mkdir
12405/*
12406 * "mkdir()" function
12407 */
12408 static void
12409f_mkdir(argvars, rettv)
12410 typval_T *argvars;
12411 typval_T *rettv;
12412{
12413 char_u *dir;
12414 char_u buf[NUMBUFLEN];
12415 int prot = 0755;
12416
12417 rettv->vval.v_number = FAIL;
12418 if (check_restricted() || check_secure())
12419 return;
12420
12421 dir = get_tv_string_buf(&argvars[0], buf);
12422 if (argvars[1].v_type != VAR_UNKNOWN)
12423 {
12424 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012425 prot = get_tv_number_chk(&argvars[2], NULL);
12426 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012427 mkdir_recurse(dir, prot);
12428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012429 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012430}
12431#endif
12432
Bram Moolenaar0d660222005-01-07 21:51:51 +000012433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 * "mode()" function
12435 */
12436/*ARGSUSED*/
12437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012438f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012439 typval_T *argvars;
12440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441{
12442 char_u buf[2];
12443
12444#ifdef FEAT_VISUAL
12445 if (VIsual_active)
12446 {
12447 if (VIsual_select)
12448 buf[0] = VIsual_mode + 's' - 'v';
12449 else
12450 buf[0] = VIsual_mode;
12451 }
12452 else
12453#endif
12454 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12455 buf[0] = 'r';
12456 else if (State & INSERT)
12457 {
12458 if (State & REPLACE_FLAG)
12459 buf[0] = 'R';
12460 else
12461 buf[0] = 'i';
12462 }
12463 else if (State & CMDLINE)
12464 buf[0] = 'c';
12465 else
12466 buf[0] = 'n';
12467
12468 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469 rettv->vval.v_string = vim_strsave(buf);
12470 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471}
12472
12473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012474 * "nextnonblank()" function
12475 */
12476 static void
12477f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012478 typval_T *argvars;
12479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012480{
12481 linenr_T lnum;
12482
12483 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012485 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486 {
12487 lnum = 0;
12488 break;
12489 }
12490 if (*skipwhite(ml_get(lnum)) != NUL)
12491 break;
12492 }
12493 rettv->vval.v_number = lnum;
12494}
12495
12496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497 * "nr2char()" function
12498 */
12499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012501 typval_T *argvars;
12502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
12504 char_u buf[NUMBUFLEN];
12505
12506#ifdef FEAT_MBYTE
12507 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012508 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 else
12510#endif
12511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 buf[1] = NUL;
12514 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 rettv->v_type = VAR_STRING;
12516 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012517}
12518
12519/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012520 * "pathshorten()" function
12521 */
12522 static void
12523f_pathshorten(argvars, rettv)
12524 typval_T *argvars;
12525 typval_T *rettv;
12526{
12527 char_u *p;
12528
12529 rettv->v_type = VAR_STRING;
12530 p = get_tv_string_chk(&argvars[0]);
12531 if (p == NULL)
12532 rettv->vval.v_string = NULL;
12533 else
12534 {
12535 p = vim_strsave(p);
12536 rettv->vval.v_string = p;
12537 if (p != NULL)
12538 shorten_dir(p);
12539 }
12540}
12541
12542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012543 * "prevnonblank()" function
12544 */
12545 static void
12546f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012547 typval_T *argvars;
12548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012549{
12550 linenr_T lnum;
12551
12552 lnum = get_tv_lnum(argvars);
12553 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12554 lnum = 0;
12555 else
12556 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12557 --lnum;
12558 rettv->vval.v_number = lnum;
12559}
12560
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012561#ifdef HAVE_STDARG_H
12562/* This dummy va_list is here because:
12563 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12564 * - locally in the function results in a "used before set" warning
12565 * - using va_start() to initialize it gives "function with fixed args" error */
12566static va_list ap;
12567#endif
12568
Bram Moolenaar8c711452005-01-14 21:53:12 +000012569/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012570 * "printf()" function
12571 */
12572 static void
12573f_printf(argvars, rettv)
12574 typval_T *argvars;
12575 typval_T *rettv;
12576{
12577 rettv->v_type = VAR_STRING;
12578 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012579#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012580 {
12581 char_u buf[NUMBUFLEN];
12582 int len;
12583 char_u *s;
12584 int saved_did_emsg = did_emsg;
12585 char *fmt;
12586
12587 /* Get the required length, allocate the buffer and do it for real. */
12588 did_emsg = FALSE;
12589 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012590 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012591 if (!did_emsg)
12592 {
12593 s = alloc(len + 1);
12594 if (s != NULL)
12595 {
12596 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012597 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012598 }
12599 }
12600 did_emsg |= saved_did_emsg;
12601 }
12602#endif
12603}
12604
12605/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012606 * "pumvisible()" function
12607 */
12608/*ARGSUSED*/
12609 static void
12610f_pumvisible(argvars, rettv)
12611 typval_T *argvars;
12612 typval_T *rettv;
12613{
12614 rettv->vval.v_number = 0;
12615#ifdef FEAT_INS_EXPAND
12616 if (pum_visible())
12617 rettv->vval.v_number = 1;
12618#endif
12619}
12620
12621/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012622 * "range()" function
12623 */
12624 static void
12625f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012626 typval_T *argvars;
12627 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012628{
12629 long start;
12630 long end;
12631 long stride = 1;
12632 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012636 if (argvars[1].v_type == VAR_UNKNOWN)
12637 {
12638 end = start - 1;
12639 start = 0;
12640 }
12641 else
12642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012643 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012644 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012646 }
12647
12648 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012649 if (error)
12650 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012651 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012652 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012653 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012654 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012655 else
12656 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012657 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012658 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012659 if (list_append_number(rettv->vval.v_list,
12660 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012661 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012662 }
12663}
12664
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012665/*
12666 * "readfile()" function
12667 */
12668 static void
12669f_readfile(argvars, rettv)
12670 typval_T *argvars;
12671 typval_T *rettv;
12672{
12673 int binary = FALSE;
12674 char_u *fname;
12675 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012676 listitem_T *li;
12677#define FREAD_SIZE 200 /* optimized for text lines */
12678 char_u buf[FREAD_SIZE];
12679 int readlen; /* size of last fread() */
12680 int buflen; /* nr of valid chars in buf[] */
12681 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12682 int tolist; /* first byte in buf[] still to be put in list */
12683 int chop; /* how many CR to chop off */
12684 char_u *prev = NULL; /* previously read bytes, if any */
12685 int prevlen = 0; /* length of "prev" if not NULL */
12686 char_u *s;
12687 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012688 long maxline = MAXLNUM;
12689 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012690
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012691 if (argvars[1].v_type != VAR_UNKNOWN)
12692 {
12693 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12694 binary = TRUE;
12695 if (argvars[2].v_type != VAR_UNKNOWN)
12696 maxline = get_tv_number(&argvars[2]);
12697 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012698
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012699 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012700 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012701
12702 /* Always open the file in binary mode, library functions have a mind of
12703 * their own about CR-LF conversion. */
12704 fname = get_tv_string(&argvars[0]);
12705 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12706 {
12707 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12708 return;
12709 }
12710
12711 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012712 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012713 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012714 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012715 buflen = filtd + readlen;
12716 tolist = 0;
12717 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12718 {
12719 if (buf[filtd] == '\n' || readlen <= 0)
12720 {
12721 /* Only when in binary mode add an empty list item when the
12722 * last line ends in a '\n'. */
12723 if (!binary && readlen == 0 && filtd == 0)
12724 break;
12725
12726 /* Found end-of-line or end-of-file: add a text line to the
12727 * list. */
12728 chop = 0;
12729 if (!binary)
12730 while (filtd - chop - 1 >= tolist
12731 && buf[filtd - chop - 1] == '\r')
12732 ++chop;
12733 len = filtd - tolist - chop;
12734 if (prev == NULL)
12735 s = vim_strnsave(buf + tolist, len);
12736 else
12737 {
12738 s = alloc((unsigned)(prevlen + len + 1));
12739 if (s != NULL)
12740 {
12741 mch_memmove(s, prev, prevlen);
12742 vim_free(prev);
12743 prev = NULL;
12744 mch_memmove(s + prevlen, buf + tolist, len);
12745 s[prevlen + len] = NUL;
12746 }
12747 }
12748 tolist = filtd + 1;
12749
12750 li = listitem_alloc();
12751 if (li == NULL)
12752 {
12753 vim_free(s);
12754 break;
12755 }
12756 li->li_tv.v_type = VAR_STRING;
12757 li->li_tv.v_lock = 0;
12758 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012759 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012760
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012761 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012762 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012763 if (readlen <= 0)
12764 break;
12765 }
12766 else if (buf[filtd] == NUL)
12767 buf[filtd] = '\n';
12768 }
12769 if (readlen <= 0)
12770 break;
12771
12772 if (tolist == 0)
12773 {
12774 /* "buf" is full, need to move text to an allocated buffer */
12775 if (prev == NULL)
12776 {
12777 prev = vim_strnsave(buf, buflen);
12778 prevlen = buflen;
12779 }
12780 else
12781 {
12782 s = alloc((unsigned)(prevlen + buflen));
12783 if (s != NULL)
12784 {
12785 mch_memmove(s, prev, prevlen);
12786 mch_memmove(s + prevlen, buf, buflen);
12787 vim_free(prev);
12788 prev = s;
12789 prevlen += buflen;
12790 }
12791 }
12792 filtd = 0;
12793 }
12794 else
12795 {
12796 mch_memmove(buf, buf + tolist, buflen - tolist);
12797 filtd -= tolist;
12798 }
12799 }
12800
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012801 /*
12802 * For a negative line count use only the lines at the end of the file,
12803 * free the rest.
12804 */
12805 if (maxline < 0)
12806 while (cnt > -maxline)
12807 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012808 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012809 --cnt;
12810 }
12811
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012812 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012813 fclose(fd);
12814}
12815
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012816#if defined(FEAT_RELTIME)
12817static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12818
12819/*
12820 * Convert a List to proftime_T.
12821 * Return FAIL when there is something wrong.
12822 */
12823 static int
12824list2proftime(arg, tm)
12825 typval_T *arg;
12826 proftime_T *tm;
12827{
12828 long n1, n2;
12829 int error = FALSE;
12830
12831 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12832 || arg->vval.v_list->lv_len != 2)
12833 return FAIL;
12834 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12835 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12836# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012837 tm->HighPart = n1;
12838 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012839# else
12840 tm->tv_sec = n1;
12841 tm->tv_usec = n2;
12842# endif
12843 return error ? FAIL : OK;
12844}
12845#endif /* FEAT_RELTIME */
12846
12847/*
12848 * "reltime()" function
12849 */
12850 static void
12851f_reltime(argvars, rettv)
12852 typval_T *argvars;
12853 typval_T *rettv;
12854{
12855#ifdef FEAT_RELTIME
12856 proftime_T res;
12857 proftime_T start;
12858
12859 if (argvars[0].v_type == VAR_UNKNOWN)
12860 {
12861 /* No arguments: get current time. */
12862 profile_start(&res);
12863 }
12864 else if (argvars[1].v_type == VAR_UNKNOWN)
12865 {
12866 if (list2proftime(&argvars[0], &res) == FAIL)
12867 return;
12868 profile_end(&res);
12869 }
12870 else
12871 {
12872 /* Two arguments: compute the difference. */
12873 if (list2proftime(&argvars[0], &start) == FAIL
12874 || list2proftime(&argvars[1], &res) == FAIL)
12875 return;
12876 profile_sub(&res, &start);
12877 }
12878
12879 if (rettv_list_alloc(rettv) == OK)
12880 {
12881 long n1, n2;
12882
12883# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012884 n1 = res.HighPart;
12885 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012886# else
12887 n1 = res.tv_sec;
12888 n2 = res.tv_usec;
12889# endif
12890 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12891 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12892 }
12893#endif
12894}
12895
12896/*
12897 * "reltimestr()" function
12898 */
12899 static void
12900f_reltimestr(argvars, rettv)
12901 typval_T *argvars;
12902 typval_T *rettv;
12903{
12904#ifdef FEAT_RELTIME
12905 proftime_T tm;
12906#endif
12907
12908 rettv->v_type = VAR_STRING;
12909 rettv->vval.v_string = NULL;
12910#ifdef FEAT_RELTIME
12911 if (list2proftime(&argvars[0], &tm) == OK)
12912 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12913#endif
12914}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012915
Bram Moolenaar0d660222005-01-07 21:51:51 +000012916#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12917static void make_connection __ARGS((void));
12918static int check_connection __ARGS((void));
12919
12920 static void
12921make_connection()
12922{
12923 if (X_DISPLAY == NULL
12924# ifdef FEAT_GUI
12925 && !gui.in_use
12926# endif
12927 )
12928 {
12929 x_force_connect = TRUE;
12930 setup_term_clip();
12931 x_force_connect = FALSE;
12932 }
12933}
12934
12935 static int
12936check_connection()
12937{
12938 make_connection();
12939 if (X_DISPLAY == NULL)
12940 {
12941 EMSG(_("E240: No connection to Vim server"));
12942 return FAIL;
12943 }
12944 return OK;
12945}
12946#endif
12947
12948#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012949static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012950
12951 static void
12952remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012953 typval_T *argvars;
12954 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012955 int expr;
12956{
12957 char_u *server_name;
12958 char_u *keys;
12959 char_u *r = NULL;
12960 char_u buf[NUMBUFLEN];
12961# ifdef WIN32
12962 HWND w;
12963# else
12964 Window w;
12965# endif
12966
12967 if (check_restricted() || check_secure())
12968 return;
12969
12970# ifdef FEAT_X11
12971 if (check_connection() == FAIL)
12972 return;
12973# endif
12974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012975 server_name = get_tv_string_chk(&argvars[0]);
12976 if (server_name == NULL)
12977 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012978 keys = get_tv_string_buf(&argvars[1], buf);
12979# ifdef WIN32
12980 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12981# else
12982 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12983 < 0)
12984# endif
12985 {
12986 if (r != NULL)
12987 EMSG(r); /* sending worked but evaluation failed */
12988 else
12989 EMSG2(_("E241: Unable to send to %s"), server_name);
12990 return;
12991 }
12992
12993 rettv->vval.v_string = r;
12994
12995 if (argvars[2].v_type != VAR_UNKNOWN)
12996 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012997 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012998 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012999 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013000
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013001 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013002 v.di_tv.v_type = VAR_STRING;
13003 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013004 idvar = get_tv_string_chk(&argvars[2]);
13005 if (idvar != NULL)
13006 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013007 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008 }
13009}
13010#endif
13011
13012/*
13013 * "remote_expr()" function
13014 */
13015/*ARGSUSED*/
13016 static void
13017f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013018 typval_T *argvars;
13019 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013020{
13021 rettv->v_type = VAR_STRING;
13022 rettv->vval.v_string = NULL;
13023#ifdef FEAT_CLIENTSERVER
13024 remote_common(argvars, rettv, TRUE);
13025#endif
13026}
13027
13028/*
13029 * "remote_foreground()" function
13030 */
13031/*ARGSUSED*/
13032 static void
13033f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 typval_T *argvars;
13035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013036{
13037 rettv->vval.v_number = 0;
13038#ifdef FEAT_CLIENTSERVER
13039# ifdef WIN32
13040 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013041 {
13042 char_u *server_name = get_tv_string_chk(&argvars[0]);
13043
13044 if (server_name != NULL)
13045 serverForeground(server_name);
13046 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013047# else
13048 /* Send a foreground() expression to the server. */
13049 argvars[1].v_type = VAR_STRING;
13050 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13051 argvars[2].v_type = VAR_UNKNOWN;
13052 remote_common(argvars, rettv, TRUE);
13053 vim_free(argvars[1].vval.v_string);
13054# endif
13055#endif
13056}
13057
13058/*ARGSUSED*/
13059 static void
13060f_remote_peek(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#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013066 char_u *s = NULL;
13067# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013068 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013069# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013071
13072 if (check_restricted() || check_secure())
13073 {
13074 rettv->vval.v_number = -1;
13075 return;
13076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013077 serverid = get_tv_string_chk(&argvars[0]);
13078 if (serverid == NULL)
13079 {
13080 rettv->vval.v_number = -1;
13081 return; /* type error; errmsg already given */
13082 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013083# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013084 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013085 if (n == 0)
13086 rettv->vval.v_number = -1;
13087 else
13088 {
13089 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13090 rettv->vval.v_number = (s != NULL);
13091 }
13092# else
13093 rettv->vval.v_number = 0;
13094 if (check_connection() == FAIL)
13095 return;
13096
13097 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013098 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013099# endif
13100
13101 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 char_u *retvar;
13104
Bram Moolenaar33570922005-01-25 22:26:29 +000013105 v.di_tv.v_type = VAR_STRING;
13106 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013107 retvar = get_tv_string_chk(&argvars[1]);
13108 if (retvar != NULL)
13109 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013111 }
13112#else
13113 rettv->vval.v_number = -1;
13114#endif
13115}
13116
13117/*ARGSUSED*/
13118 static void
13119f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013120 typval_T *argvars;
13121 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013122{
13123 char_u *r = NULL;
13124
13125#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013126 char_u *serverid = get_tv_string_chk(&argvars[0]);
13127
13128 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013129 {
13130# ifdef WIN32
13131 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013132 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013133
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013134 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135 if (n != 0)
13136 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13137 if (r == NULL)
13138# else
13139 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013140 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013141# endif
13142 EMSG(_("E277: Unable to read a server reply"));
13143 }
13144#endif
13145 rettv->v_type = VAR_STRING;
13146 rettv->vval.v_string = r;
13147}
13148
13149/*
13150 * "remote_send()" function
13151 */
13152/*ARGSUSED*/
13153 static void
13154f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013155 typval_T *argvars;
13156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013157{
13158 rettv->v_type = VAR_STRING;
13159 rettv->vval.v_string = NULL;
13160#ifdef FEAT_CLIENTSERVER
13161 remote_common(argvars, rettv, FALSE);
13162#endif
13163}
13164
13165/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013166 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 */
13168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013170 typval_T *argvars;
13171 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013172{
Bram Moolenaar33570922005-01-25 22:26:29 +000013173 list_T *l;
13174 listitem_T *item, *item2;
13175 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013176 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013177 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013178 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013179 dict_T *d;
13180 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013181
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013182 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013183 if (argvars[0].v_type == VAR_DICT)
13184 {
13185 if (argvars[2].v_type != VAR_UNKNOWN)
13186 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013187 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013188 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013190 key = get_tv_string_chk(&argvars[1]);
13191 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 di = dict_find(d, key, -1);
13194 if (di == NULL)
13195 EMSG2(_(e_dictkey), key);
13196 else
13197 {
13198 *rettv = di->di_tv;
13199 init_tv(&di->di_tv);
13200 dictitem_remove(d, di);
13201 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013202 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013203 }
13204 }
13205 else if (argvars[0].v_type != VAR_LIST)
13206 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013207 else if ((l = argvars[0].vval.v_list) != NULL
13208 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013210 int error = FALSE;
13211
13212 idx = get_tv_number_chk(&argvars[1], &error);
13213 if (error)
13214 ; /* type error: do nothing, errmsg already given */
13215 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013216 EMSGN(_(e_listidx), idx);
13217 else
13218 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013219 if (argvars[2].v_type == VAR_UNKNOWN)
13220 {
13221 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013222 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013223 *rettv = item->li_tv;
13224 vim_free(item);
13225 }
13226 else
13227 {
13228 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229 end = get_tv_number_chk(&argvars[2], &error);
13230 if (error)
13231 ; /* type error: do nothing */
13232 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013233 EMSGN(_(e_listidx), end);
13234 else
13235 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013236 int cnt = 0;
13237
13238 for (li = item; li != NULL; li = li->li_next)
13239 {
13240 ++cnt;
13241 if (li == item2)
13242 break;
13243 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013244 if (li == NULL) /* didn't find "item2" after "item" */
13245 EMSG(_(e_invrange));
13246 else
13247 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013248 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013249 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013250 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013251 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013252 l->lv_first = item;
13253 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013254 item->li_prev = NULL;
13255 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013256 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013257 }
13258 }
13259 }
13260 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013261 }
13262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263}
13264
13265/*
13266 * "rename({from}, {to})" function
13267 */
13268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013269f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013270 typval_T *argvars;
13271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272{
13273 char_u buf[NUMBUFLEN];
13274
13275 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13279 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280}
13281
13282/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013283 * "repeat()" function
13284 */
13285/*ARGSUSED*/
13286 static void
13287f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 typval_T *argvars;
13289 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013290{
13291 char_u *p;
13292 int n;
13293 int slen;
13294 int len;
13295 char_u *r;
13296 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013297
13298 n = get_tv_number(&argvars[1]);
13299 if (argvars[0].v_type == VAR_LIST)
13300 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013301 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013302 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013303 if (list_extend(rettv->vval.v_list,
13304 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013305 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013306 }
13307 else
13308 {
13309 p = get_tv_string(&argvars[0]);
13310 rettv->v_type = VAR_STRING;
13311 rettv->vval.v_string = NULL;
13312
13313 slen = (int)STRLEN(p);
13314 len = slen * n;
13315 if (len <= 0)
13316 return;
13317
13318 r = alloc(len + 1);
13319 if (r != NULL)
13320 {
13321 for (i = 0; i < n; i++)
13322 mch_memmove(r + i * slen, p, (size_t)slen);
13323 r[len] = NUL;
13324 }
13325
13326 rettv->vval.v_string = r;
13327 }
13328}
13329
13330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 * "resolve()" function
13332 */
13333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013335 typval_T *argvars;
13336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337{
13338 char_u *p;
13339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341#ifdef FEAT_SHORTCUT
13342 {
13343 char_u *v = NULL;
13344
13345 v = mch_resolve_shortcut(p);
13346 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013349 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 }
13351#else
13352# ifdef HAVE_READLINK
13353 {
13354 char_u buf[MAXPATHL + 1];
13355 char_u *cpy;
13356 int len;
13357 char_u *remain = NULL;
13358 char_u *q;
13359 int is_relative_to_current = FALSE;
13360 int has_trailing_pathsep = FALSE;
13361 int limit = 100;
13362
13363 p = vim_strsave(p);
13364
13365 if (p[0] == '.' && (vim_ispathsep(p[1])
13366 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13367 is_relative_to_current = TRUE;
13368
13369 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013370 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 has_trailing_pathsep = TRUE;
13372
13373 q = getnextcomp(p);
13374 if (*q != NUL)
13375 {
13376 /* Separate the first path component in "p", and keep the
13377 * remainder (beginning with the path separator). */
13378 remain = vim_strsave(q - 1);
13379 q[-1] = NUL;
13380 }
13381
13382 for (;;)
13383 {
13384 for (;;)
13385 {
13386 len = readlink((char *)p, (char *)buf, MAXPATHL);
13387 if (len <= 0)
13388 break;
13389 buf[len] = NUL;
13390
13391 if (limit-- == 0)
13392 {
13393 vim_free(p);
13394 vim_free(remain);
13395 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 goto fail;
13398 }
13399
13400 /* Ensure that the result will have a trailing path separator
13401 * if the argument has one. */
13402 if (remain == NULL && has_trailing_pathsep)
13403 add_pathsep(buf);
13404
13405 /* Separate the first path component in the link value and
13406 * concatenate the remainders. */
13407 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13408 if (*q != NUL)
13409 {
13410 if (remain == NULL)
13411 remain = vim_strsave(q - 1);
13412 else
13413 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013414 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 if (cpy != NULL)
13416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 vim_free(remain);
13418 remain = cpy;
13419 }
13420 }
13421 q[-1] = NUL;
13422 }
13423
13424 q = gettail(p);
13425 if (q > p && *q == NUL)
13426 {
13427 /* Ignore trailing path separator. */
13428 q[-1] = NUL;
13429 q = gettail(p);
13430 }
13431 if (q > p && !mch_isFullName(buf))
13432 {
13433 /* symlink is relative to directory of argument */
13434 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13435 if (cpy != NULL)
13436 {
13437 STRCPY(cpy, p);
13438 STRCPY(gettail(cpy), buf);
13439 vim_free(p);
13440 p = cpy;
13441 }
13442 }
13443 else
13444 {
13445 vim_free(p);
13446 p = vim_strsave(buf);
13447 }
13448 }
13449
13450 if (remain == NULL)
13451 break;
13452
13453 /* Append the first path component of "remain" to "p". */
13454 q = getnextcomp(remain + 1);
13455 len = q - remain - (*q != NUL);
13456 cpy = vim_strnsave(p, STRLEN(p) + len);
13457 if (cpy != NULL)
13458 {
13459 STRNCAT(cpy, remain, len);
13460 vim_free(p);
13461 p = cpy;
13462 }
13463 /* Shorten "remain". */
13464 if (*q != NUL)
13465 STRCPY(remain, q - 1);
13466 else
13467 {
13468 vim_free(remain);
13469 remain = NULL;
13470 }
13471 }
13472
13473 /* If the result is a relative path name, make it explicitly relative to
13474 * the current directory if and only if the argument had this form. */
13475 if (!vim_ispathsep(*p))
13476 {
13477 if (is_relative_to_current
13478 && *p != NUL
13479 && !(p[0] == '.'
13480 && (p[1] == NUL
13481 || vim_ispathsep(p[1])
13482 || (p[1] == '.'
13483 && (p[2] == NUL
13484 || vim_ispathsep(p[2]))))))
13485 {
13486 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013487 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 if (cpy != NULL)
13489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 vim_free(p);
13491 p = cpy;
13492 }
13493 }
13494 else if (!is_relative_to_current)
13495 {
13496 /* Strip leading "./". */
13497 q = p;
13498 while (q[0] == '.' && vim_ispathsep(q[1]))
13499 q += 2;
13500 if (q > p)
13501 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13502 }
13503 }
13504
13505 /* Ensure that the result will have no trailing path separator
13506 * if the argument had none. But keep "/" or "//". */
13507 if (!has_trailing_pathsep)
13508 {
13509 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013510 if (after_pathsep(p, q))
13511 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 }
13513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 }
13516# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518# endif
13519#endif
13520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522
13523#ifdef HAVE_READLINK
13524fail:
13525#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527}
13528
13529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013530 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 */
13532 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013533f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536{
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 list_T *l;
13538 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539
Bram Moolenaar0d660222005-01-07 21:51:51 +000013540 rettv->vval.v_number = 0;
13541 if (argvars[0].v_type != VAR_LIST)
13542 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013543 else if ((l = argvars[0].vval.v_list) != NULL
13544 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545 {
13546 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013547 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013548 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013549 while (li != NULL)
13550 {
13551 ni = li->li_prev;
13552 list_append(l, li);
13553 li = ni;
13554 }
13555 rettv->vval.v_list = l;
13556 rettv->v_type = VAR_LIST;
13557 ++l->lv_refcount;
13558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559}
13560
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013561#define SP_NOMOVE 0x01 /* don't move cursor */
13562#define SP_REPEAT 0x02 /* repeat to find outer pair */
13563#define SP_RETCOUNT 0x04 /* return matchcount */
13564#define SP_SETPCMARK 0x08 /* set previous context mark */
13565#define SP_START 0x10 /* accept match at start position */
13566#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13567#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013568
Bram Moolenaar33570922005-01-25 22:26:29 +000013569static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013570
13571/*
13572 * Get flags for a search function.
13573 * Possibly sets "p_ws".
13574 * Returns BACKWARD, FORWARD or zero (for an error).
13575 */
13576 static int
13577get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013578 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579 int *flagsp;
13580{
13581 int dir = FORWARD;
13582 char_u *flags;
13583 char_u nbuf[NUMBUFLEN];
13584 int mask;
13585
13586 if (varp->v_type != VAR_UNKNOWN)
13587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 flags = get_tv_string_buf_chk(varp, nbuf);
13589 if (flags == NULL)
13590 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013591 while (*flags != NUL)
13592 {
13593 switch (*flags)
13594 {
13595 case 'b': dir = BACKWARD; break;
13596 case 'w': p_ws = TRUE; break;
13597 case 'W': p_ws = FALSE; break;
13598 default: mask = 0;
13599 if (flagsp != NULL)
13600 switch (*flags)
13601 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013602 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013603 case 'e': mask = SP_END; break;
13604 case 'm': mask = SP_RETCOUNT; break;
13605 case 'n': mask = SP_NOMOVE; break;
13606 case 'p': mask = SP_SUBPAT; break;
13607 case 'r': mask = SP_REPEAT; break;
13608 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013609 }
13610 if (mask == 0)
13611 {
13612 EMSG2(_(e_invarg2), flags);
13613 dir = 0;
13614 }
13615 else
13616 *flagsp |= mask;
13617 }
13618 if (dir == 0)
13619 break;
13620 ++flags;
13621 }
13622 }
13623 return dir;
13624}
13625
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013627 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013629 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013630search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013631 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013632 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013633 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013635 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 char_u *pat;
13637 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013638 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 int save_p_ws = p_ws;
13640 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013641 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013642 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013643 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013644 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013647 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013648 if (dir == 0)
13649 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013650 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013651 if (flags & SP_START)
13652 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013653 if (flags & SP_END)
13654 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013655
13656 /* Optional extra argument: line number to stop searching. */
13657 if (argvars[1].v_type != VAR_UNKNOWN
13658 && argvars[2].v_type != VAR_UNKNOWN)
13659 {
13660 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13661 if (lnum_stop < 0)
13662 goto theend;
13663 }
13664
Bram Moolenaar231334e2005-07-25 20:46:57 +000013665 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013666 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013667 * Check to make sure only those flags are set.
13668 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13669 * flags cannot be set. Check for that condition also.
13670 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013671 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013672 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013675 goto theend;
13676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013678 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013679 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13680 options, RE_SEARCH, (linenr_T)lnum_stop);
13681 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013683 if (flags & SP_SUBPAT)
13684 retval = subpatnum;
13685 else
13686 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013687 if (flags & SP_SETPCMARK)
13688 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013690 if (match_pos != NULL)
13691 {
13692 /* Store the match cursor position */
13693 match_pos->lnum = pos.lnum;
13694 match_pos->col = pos.col + 1;
13695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 /* "/$" will put the cursor after the end of the line, may need to
13697 * correct that here */
13698 check_cursor();
13699 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013700
13701 /* If 'n' flag is used: restore cursor position. */
13702 if (flags & SP_NOMOVE)
13703 curwin->w_cursor = save_cursor;
13704theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013706
13707 return retval;
13708}
13709
13710/*
13711 * "search()" function
13712 */
13713 static void
13714f_search(argvars, rettv)
13715 typval_T *argvars;
13716 typval_T *rettv;
13717{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013718 int flags = 0;
13719
13720 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721}
13722
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013724 * "searchdecl()" function
13725 */
13726 static void
13727f_searchdecl(argvars, rettv)
13728 typval_T *argvars;
13729 typval_T *rettv;
13730{
13731 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013732 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013733 int error = FALSE;
13734 char_u *name;
13735
13736 rettv->vval.v_number = 1; /* default: FAIL */
13737
13738 name = get_tv_string_chk(&argvars[0]);
13739 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013740 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013741 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013742 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13743 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13744 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013745 if (!error && name != NULL)
13746 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013747 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013748}
13749
13750/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013751 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013753 static int
13754searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013755 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013756 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757{
13758 char_u *spat, *mpat, *epat;
13759 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 int dir;
13762 int flags = 0;
13763 char_u nbuf1[NUMBUFLEN];
13764 char_u nbuf2[NUMBUFLEN];
13765 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013766 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013767 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 spat = get_tv_string_chk(&argvars[0]);
13771 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13772 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13773 if (spat == NULL || mpat == NULL || epat == NULL)
13774 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 /* Handle the optional fourth argument: flags */
13777 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013778 if (dir == 0)
13779 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013780
13781 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013782 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13783 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013784 if ((flags & (SP_END | SP_SUBPAT)) != 0
13785 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013786 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013787 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013788 goto theend;
13789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013791 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013792 if (argvars[3].v_type == VAR_UNKNOWN
13793 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 skip = (char_u *)"";
13795 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013798 if (argvars[5].v_type != VAR_UNKNOWN)
13799 {
13800 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13801 if (lnum_stop < 0)
13802 goto theend;
13803 }
13804 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013805 if (skip == NULL)
13806 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013808 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13809 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013810
13811theend:
13812 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013813
13814 return retval;
13815}
13816
13817/*
13818 * "searchpair()" function
13819 */
13820 static void
13821f_searchpair(argvars, rettv)
13822 typval_T *argvars;
13823 typval_T *rettv;
13824{
13825 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13826}
13827
13828/*
13829 * "searchpairpos()" function
13830 */
13831 static void
13832f_searchpairpos(argvars, rettv)
13833 typval_T *argvars;
13834 typval_T *rettv;
13835{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013836 pos_T match_pos;
13837 int lnum = 0;
13838 int col = 0;
13839
13840 rettv->vval.v_number = 0;
13841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013842 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013843 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013844
13845 if (searchpair_cmn(argvars, &match_pos) > 0)
13846 {
13847 lnum = match_pos.lnum;
13848 col = match_pos.col;
13849 }
13850
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013851 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13852 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013853}
13854
13855/*
13856 * Search for a start/middle/end thing.
13857 * Used by searchpair(), see its documentation for the details.
13858 * Returns 0 or -1 for no match,
13859 */
13860 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013861do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013862 char_u *spat; /* start pattern */
13863 char_u *mpat; /* middle pattern */
13864 char_u *epat; /* end pattern */
13865 int dir; /* BACKWARD or FORWARD */
13866 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013867 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013868 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013869 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013870{
13871 char_u *save_cpo;
13872 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13873 long retval = 0;
13874 pos_T pos;
13875 pos_T firstpos;
13876 pos_T foundpos;
13877 pos_T save_cursor;
13878 pos_T save_pos;
13879 int n;
13880 int r;
13881 int nest = 1;
13882 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013883 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013884
13885 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13886 save_cpo = p_cpo;
13887 p_cpo = (char_u *)"";
13888
13889 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13890 * start/middle/end (pat3, for the top pair). */
13891 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13892 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13893 if (pat2 == NULL || pat3 == NULL)
13894 goto theend;
13895 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13896 if (*mpat == NUL)
13897 STRCPY(pat3, pat2);
13898 else
13899 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13900 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013901 if (flags & SP_START)
13902 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013903
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 save_cursor = curwin->w_cursor;
13905 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013906 clearpos(&firstpos);
13907 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 pat = pat3;
13909 for (;;)
13910 {
13911 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013912 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13914 /* didn't find it or found the first match again: FAIL */
13915 break;
13916
13917 if (firstpos.lnum == 0)
13918 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013919 if (equalpos(pos, foundpos))
13920 {
13921 /* Found the same position again. Can happen with a pattern that
13922 * has "\zs" at the end and searching backwards. Advance one
13923 * character and try again. */
13924 if (dir == BACKWARD)
13925 decl(&pos);
13926 else
13927 incl(&pos);
13928 }
13929 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930
13931 /* If the skip pattern matches, ignore this match. */
13932 if (*skip != NUL)
13933 {
13934 save_pos = curwin->w_cursor;
13935 curwin->w_cursor = pos;
13936 r = eval_to_bool(skip, &err, NULL, FALSE);
13937 curwin->w_cursor = save_pos;
13938 if (err)
13939 {
13940 /* Evaluating {skip} caused an error, break here. */
13941 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013942 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 break;
13944 }
13945 if (r)
13946 continue;
13947 }
13948
13949 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13950 {
13951 /* Found end when searching backwards or start when searching
13952 * forward: nested pair. */
13953 ++nest;
13954 pat = pat2; /* nested, don't search for middle */
13955 }
13956 else
13957 {
13958 /* Found end when searching forward or start when searching
13959 * backward: end of (nested) pair; or found middle in outer pair. */
13960 if (--nest == 1)
13961 pat = pat3; /* outer level, search for middle */
13962 }
13963
13964 if (nest == 0)
13965 {
13966 /* Found the match: return matchcount or line number. */
13967 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013968 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013970 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013971 if (flags & SP_SETPCMARK)
13972 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 curwin->w_cursor = pos;
13974 if (!(flags & SP_REPEAT))
13975 break;
13976 nest = 1; /* search for next unmatched */
13977 }
13978 }
13979
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013980 if (match_pos != NULL)
13981 {
13982 /* Store the match cursor position */
13983 match_pos->lnum = curwin->w_cursor.lnum;
13984 match_pos->col = curwin->w_cursor.col + 1;
13985 }
13986
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013988 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 curwin->w_cursor = save_cursor;
13990
13991theend:
13992 vim_free(pat2);
13993 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013995
13996 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997}
13998
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013999/*
14000 * "searchpos()" function
14001 */
14002 static void
14003f_searchpos(argvars, rettv)
14004 typval_T *argvars;
14005 typval_T *rettv;
14006{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014007 pos_T match_pos;
14008 int lnum = 0;
14009 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014010 int n;
14011 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014012
14013 rettv->vval.v_number = 0;
14014
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014015 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014016 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014017
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014018 n = search_cmn(argvars, &match_pos, &flags);
14019 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014020 {
14021 lnum = match_pos.lnum;
14022 col = match_pos.col;
14023 }
14024
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014025 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14026 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014027 if (flags & SP_SUBPAT)
14028 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014029}
14030
14031
Bram Moolenaar0d660222005-01-07 21:51:51 +000014032/*ARGSUSED*/
14033 static void
14034f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 typval_T *argvars;
14036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014038#ifdef FEAT_CLIENTSERVER
14039 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 char_u *server = get_tv_string_chk(&argvars[0]);
14041 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042
Bram Moolenaar0d660222005-01-07 21:51:51 +000014043 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 if (server == NULL || reply == NULL)
14045 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014046 if (check_restricted() || check_secure())
14047 return;
14048# ifdef FEAT_X11
14049 if (check_connection() == FAIL)
14050 return;
14051# endif
14052
14053 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014055 EMSG(_("E258: Unable to send to client"));
14056 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058 rettv->vval.v_number = 0;
14059#else
14060 rettv->vval.v_number = -1;
14061#endif
14062}
14063
14064/*ARGSUSED*/
14065 static void
14066f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014067 typval_T *argvars;
14068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014069{
14070 char_u *r = NULL;
14071
14072#ifdef FEAT_CLIENTSERVER
14073# ifdef WIN32
14074 r = serverGetVimNames();
14075# else
14076 make_connection();
14077 if (X_DISPLAY != NULL)
14078 r = serverGetVimNames(X_DISPLAY);
14079# endif
14080#endif
14081 rettv->v_type = VAR_STRING;
14082 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083}
14084
14085/*
14086 * "setbufvar()" function
14087 */
14088/*ARGSUSED*/
14089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 typval_T *argvars;
14092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093{
14094 buf_T *buf;
14095#ifdef FEAT_AUTOCMD
14096 aco_save_T aco;
14097#else
14098 buf_T *save_curbuf;
14099#endif
14100 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014101 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 char_u nbuf[NUMBUFLEN];
14103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014104 rettv->vval.v_number = 0;
14105
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 if (check_restricted() || check_secure())
14107 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14109 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014110 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 varp = &argvars[2];
14112
14113 if (buf != NULL && varname != NULL && varp != NULL)
14114 {
14115 /* set curbuf to be our buf, temporarily */
14116#ifdef FEAT_AUTOCMD
14117 aucmd_prepbuf(&aco, buf);
14118#else
14119 save_curbuf = curbuf;
14120 curbuf = buf;
14121#endif
14122
14123 if (*varname == '&')
14124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 long numval;
14126 char_u *strval;
14127 int error = FALSE;
14128
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 numval = get_tv_number_chk(varp, &error);
14131 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 if (!error && strval != NULL)
14133 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 }
14135 else
14136 {
14137 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14138 if (bufvarname != NULL)
14139 {
14140 STRCPY(bufvarname, "b:");
14141 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014142 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 vim_free(bufvarname);
14144 }
14145 }
14146
14147 /* reset notion of buffer */
14148#ifdef FEAT_AUTOCMD
14149 aucmd_restbuf(&aco);
14150#else
14151 curbuf = save_curbuf;
14152#endif
14153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154}
14155
14156/*
14157 * "setcmdpos()" function
14158 */
14159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014161 typval_T *argvars;
14162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 int pos = (int)get_tv_number(&argvars[0]) - 1;
14165
14166 if (pos >= 0)
14167 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168}
14169
14170/*
14171 * "setline()" function
14172 */
14173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 typval_T *argvars;
14176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177{
14178 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014179 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014180 list_T *l = NULL;
14181 listitem_T *li = NULL;
14182 long added = 0;
14183 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014185 lnum = get_tv_lnum(&argvars[0]);
14186 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014188 l = argvars[1].vval.v_list;
14189 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014191 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014193
14194 rettv->vval.v_number = 0; /* OK */
14195 for (;;)
14196 {
14197 if (l != NULL)
14198 {
14199 /* list argument, get next string */
14200 if (li == NULL)
14201 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014203 li = li->li_next;
14204 }
14205
14206 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014208 break;
14209 if (lnum <= curbuf->b_ml.ml_line_count)
14210 {
14211 /* existing line, replace it */
14212 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14213 {
14214 changed_bytes(lnum, 0);
14215 check_cursor_col();
14216 rettv->vval.v_number = 0; /* OK */
14217 }
14218 }
14219 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14220 {
14221 /* lnum is one past the last line, append the line */
14222 ++added;
14223 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14224 rettv->vval.v_number = 0; /* OK */
14225 }
14226
14227 if (l == NULL) /* only one string argument */
14228 break;
14229 ++lnum;
14230 }
14231
14232 if (added > 0)
14233 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234}
14235
14236/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014237 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014238 */
14239/*ARGSUSED*/
14240 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014241set_qf_ll_list(wp, list_arg, action_arg, rettv)
14242 win_T *wp;
14243 typval_T *list_arg;
14244 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014245 typval_T *rettv;
14246{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014247#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014248 char_u *act;
14249 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014250#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014251
Bram Moolenaar2641f772005-03-25 21:58:17 +000014252 rettv->vval.v_number = -1;
14253
14254#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014255 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014256 EMSG(_(e_listreq));
14257 else
14258 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014259 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014260
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014261 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014262 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014263 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264 if (act == NULL)
14265 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014266 if (*act == 'a' || *act == 'r')
14267 action = *act;
14268 }
14269
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014270 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014271 rettv->vval.v_number = 0;
14272 }
14273#endif
14274}
14275
14276/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014277 * "setloclist()" function
14278 */
14279/*ARGSUSED*/
14280 static void
14281f_setloclist(argvars, rettv)
14282 typval_T *argvars;
14283 typval_T *rettv;
14284{
14285 win_T *win;
14286
14287 rettv->vval.v_number = -1;
14288
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014289 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014290 if (win != NULL)
14291 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14292}
14293
14294/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014295 * "setpos()" function
14296 */
14297/*ARGSUSED*/
14298 static void
14299f_setpos(argvars, rettv)
14300 typval_T *argvars;
14301 typval_T *rettv;
14302{
14303 pos_T pos;
14304 int fnum;
14305 char_u *name;
14306
14307 name = get_tv_string_chk(argvars);
14308 if (name != NULL)
14309 {
14310 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14311 {
14312 --pos.col;
14313 if (name[0] == '.') /* cursor */
14314 {
14315 if (fnum == curbuf->b_fnum)
14316 {
14317 curwin->w_cursor = pos;
14318 check_cursor();
14319 }
14320 else
14321 EMSG(_(e_invarg));
14322 }
14323 else if (name[0] == '\'') /* mark */
14324 (void)setmark_pos(name[1], &pos, fnum);
14325 else
14326 EMSG(_(e_invarg));
14327 }
14328 }
14329}
14330
14331/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014332 * "setqflist()" function
14333 */
14334/*ARGSUSED*/
14335 static void
14336f_setqflist(argvars, rettv)
14337 typval_T *argvars;
14338 typval_T *rettv;
14339{
14340 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14341}
14342
14343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 * "setreg()" function
14345 */
14346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014347f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014348 typval_T *argvars;
14349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350{
14351 int regname;
14352 char_u *strregname;
14353 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 int append;
14356 char_u yank_type;
14357 long block_len;
14358
14359 block_len = -1;
14360 yank_type = MAUTO;
14361 append = FALSE;
14362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 if (strregname == NULL)
14367 return; /* type error; errmsg already given */
14368 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 if (regname == 0 || regname == '@')
14370 regname = '"';
14371 else if (regname == '=')
14372 return;
14373
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014374 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 stropt = get_tv_string_chk(&argvars[2]);
14377 if (stropt == NULL)
14378 return; /* type error */
14379 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 switch (*stropt)
14381 {
14382 case 'a': case 'A': /* append */
14383 append = TRUE;
14384 break;
14385 case 'v': case 'c': /* character-wise selection */
14386 yank_type = MCHAR;
14387 break;
14388 case 'V': case 'l': /* line-wise selection */
14389 yank_type = MLINE;
14390 break;
14391#ifdef FEAT_VISUAL
14392 case 'b': case Ctrl_V: /* block-wise selection */
14393 yank_type = MBLOCK;
14394 if (VIM_ISDIGIT(stropt[1]))
14395 {
14396 ++stropt;
14397 block_len = getdigits(&stropt) - 1;
14398 --stropt;
14399 }
14400 break;
14401#endif
14402 }
14403 }
14404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 strval = get_tv_string_chk(&argvars[1]);
14406 if (strval != NULL)
14407 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410}
14411
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014412/*
14413 * "settabwinvar()" function
14414 */
14415 static void
14416f_settabwinvar(argvars, rettv)
14417 typval_T *argvars;
14418 typval_T *rettv;
14419{
14420 setwinvar(argvars, rettv, 1);
14421}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422
14423/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014424 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014428 typval_T *argvars;
14429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014431 setwinvar(argvars, rettv, 0);
14432}
14433
14434/*
14435 * "setwinvar()" and "settabwinvar()" functions
14436 */
14437 static void
14438setwinvar(argvars, rettv, off)
14439 typval_T *argvars;
14440 typval_T *rettv;
14441 int off;
14442{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 win_T *win;
14444#ifdef FEAT_WINDOWS
14445 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014446 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447#endif
14448 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014451 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 rettv->vval.v_number = 0;
14454
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 if (check_restricted() || check_secure())
14456 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014457
14458#ifdef FEAT_WINDOWS
14459 if (off == 1)
14460 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14461 else
14462 tp = curtab;
14463#endif
14464 win = find_win_by_nr(&argvars[off], tp);
14465 varname = get_tv_string_chk(&argvars[off + 1]);
14466 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467
14468 if (win != NULL && varname != NULL && varp != NULL)
14469 {
14470#ifdef FEAT_WINDOWS
14471 /* set curwin to be our win, temporarily */
14472 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014473 save_curtab = curtab;
14474 goto_tabpage_tp(tp);
14475 if (!win_valid(win))
14476 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 curwin = win;
14478 curbuf = curwin->w_buffer;
14479#endif
14480
14481 if (*varname == '&')
14482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014483 long numval;
14484 char_u *strval;
14485 int error = FALSE;
14486
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014488 numval = get_tv_number_chk(varp, &error);
14489 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 if (!error && strval != NULL)
14491 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 }
14493 else
14494 {
14495 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14496 if (winvarname != NULL)
14497 {
14498 STRCPY(winvarname, "w:");
14499 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014500 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 vim_free(winvarname);
14502 }
14503 }
14504
14505#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014506 /* Restore current tabpage and window, if still valid (autocomands can
14507 * make them invalid). */
14508 if (valid_tabpage(save_curtab))
14509 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 if (win_valid(save_curwin))
14511 {
14512 curwin = save_curwin;
14513 curbuf = curwin->w_buffer;
14514 }
14515#endif
14516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517}
14518
14519/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014520 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 */
14522 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014523f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *argvars;
14525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014527 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528
Bram Moolenaar0d660222005-01-07 21:51:51 +000014529 p = get_tv_string(&argvars[0]);
14530 rettv->vval.v_string = vim_strsave(p);
14531 simplify_filename(rettv->vval.v_string); /* simplify in place */
14532 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533}
14534
Bram Moolenaar0d660222005-01-07 21:51:51 +000014535static int
14536#ifdef __BORLANDC__
14537 _RTLENTRYF
14538#endif
14539 item_compare __ARGS((const void *s1, const void *s2));
14540static int
14541#ifdef __BORLANDC__
14542 _RTLENTRYF
14543#endif
14544 item_compare2 __ARGS((const void *s1, const void *s2));
14545
14546static int item_compare_ic;
14547static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549#define ITEM_COMPARE_FAIL 999
14550
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014552 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554 static int
14555#ifdef __BORLANDC__
14556_RTLENTRYF
14557#endif
14558item_compare(s1, s2)
14559 const void *s1;
14560 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014562 char_u *p1, *p2;
14563 char_u *tofree1, *tofree2;
14564 int res;
14565 char_u numbuf1[NUMBUFLEN];
14566 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014568 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14569 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 if (item_compare_ic)
14571 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573 res = STRCMP(p1, p2);
14574 vim_free(tofree1);
14575 vim_free(tofree2);
14576 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577}
14578
14579 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580#ifdef __BORLANDC__
14581_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583item_compare2(s1, s2)
14584 const void *s1;
14585 const void *s2;
14586{
14587 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014589 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014590 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 /* shortcut after failure in previous call; compare all items equal */
14593 if (item_compare_func_err)
14594 return 0;
14595
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014596 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14597 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014598 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14599 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600
14601 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014602 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014603 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604 clear_tv(&argv[0]);
14605 clear_tv(&argv[1]);
14606
14607 if (res == FAIL)
14608 res = ITEM_COMPARE_FAIL;
14609 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014610 /* return value has wrong type */
14611 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14612 if (item_compare_func_err)
14613 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614 clear_tv(&rettv);
14615 return res;
14616}
14617
14618/*
14619 * "sort({list})" function
14620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014623 typval_T *argvars;
14624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625{
Bram Moolenaar33570922005-01-25 22:26:29 +000014626 list_T *l;
14627 listitem_T *li;
14628 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629 long len;
14630 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632 rettv->vval.v_number = 0;
14633 if (argvars[0].v_type != VAR_LIST)
14634 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 else
14636 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014638 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639 return;
14640 rettv->vval.v_list = l;
14641 rettv->v_type = VAR_LIST;
14642 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 len = list_len(l);
14645 if (len <= 1)
14646 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 item_compare_ic = FALSE;
14649 item_compare_func = NULL;
14650 if (argvars[1].v_type != VAR_UNKNOWN)
14651 {
14652 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654 else
14655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 int error = FALSE;
14657
14658 i = get_tv_number_chk(&argvars[1], &error);
14659 if (error)
14660 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014661 if (i == 1)
14662 item_compare_ic = TRUE;
14663 else
14664 item_compare_func = get_tv_string(&argvars[1]);
14665 }
14666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670 if (ptrs == NULL)
14671 return;
14672 i = 0;
14673 for (li = l->lv_first; li != NULL; li = li->li_next)
14674 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014676 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677 /* test the compare function */
14678 if (item_compare_func != NULL
14679 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14680 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014681 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683 {
14684 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014685 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014686 item_compare_func == NULL ? item_compare : item_compare2);
14687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 if (!item_compare_func_err)
14689 {
14690 /* Clear the List and append the items in the sorted order. */
14691 l->lv_first = l->lv_last = NULL;
14692 l->lv_len = 0;
14693 for (i = 0; i < len; ++i)
14694 list_append(l, ptrs[i]);
14695 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 }
14697
14698 vim_free(ptrs);
14699 }
14700}
14701
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014702/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014703 * "soundfold({word})" function
14704 */
14705 static void
14706f_soundfold(argvars, rettv)
14707 typval_T *argvars;
14708 typval_T *rettv;
14709{
14710 char_u *s;
14711
14712 rettv->v_type = VAR_STRING;
14713 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014714#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014715 rettv->vval.v_string = eval_soundfold(s);
14716#else
14717 rettv->vval.v_string = vim_strsave(s);
14718#endif
14719}
14720
14721/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014722 * "spellbadword()" function
14723 */
14724/* ARGSUSED */
14725 static void
14726f_spellbadword(argvars, rettv)
14727 typval_T *argvars;
14728 typval_T *rettv;
14729{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014730 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014731 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014732 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014733
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014734 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014735 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014736
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014737#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014738 if (argvars[0].v_type == VAR_UNKNOWN)
14739 {
14740 /* Find the start and length of the badly spelled word. */
14741 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14742 if (len != 0)
14743 word = ml_get_cursor();
14744 }
14745 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14746 {
14747 char_u *str = get_tv_string_chk(&argvars[0]);
14748 int capcol = -1;
14749
14750 if (str != NULL)
14751 {
14752 /* Check the argument for spelling. */
14753 while (*str != NUL)
14754 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014755 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014756 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014757 {
14758 word = str;
14759 break;
14760 }
14761 str += len;
14762 }
14763 }
14764 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014765#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014766
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014767 list_append_string(rettv->vval.v_list, word, len);
14768 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014769 attr == HLF_SPB ? "bad" :
14770 attr == HLF_SPR ? "rare" :
14771 attr == HLF_SPL ? "local" :
14772 attr == HLF_SPC ? "caps" :
14773 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014774}
14775
14776/*
14777 * "spellsuggest()" function
14778 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014779/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014780 static void
14781f_spellsuggest(argvars, rettv)
14782 typval_T *argvars;
14783 typval_T *rettv;
14784{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014785#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014786 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014787 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014788 int maxcount;
14789 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014790 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014791 listitem_T *li;
14792 int need_capital = FALSE;
14793#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014794
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014795 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014796 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014797
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014798#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014799 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14800 {
14801 str = get_tv_string(&argvars[0]);
14802 if (argvars[1].v_type != VAR_UNKNOWN)
14803 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014804 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014805 if (maxcount <= 0)
14806 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014807 if (argvars[2].v_type != VAR_UNKNOWN)
14808 {
14809 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14810 if (typeerr)
14811 return;
14812 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014813 }
14814 else
14815 maxcount = 25;
14816
Bram Moolenaar4770d092006-01-12 23:22:24 +000014817 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014818
14819 for (i = 0; i < ga.ga_len; ++i)
14820 {
14821 str = ((char_u **)ga.ga_data)[i];
14822
14823 li = listitem_alloc();
14824 if (li == NULL)
14825 vim_free(str);
14826 else
14827 {
14828 li->li_tv.v_type = VAR_STRING;
14829 li->li_tv.v_lock = 0;
14830 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014831 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014832 }
14833 }
14834 ga_clear(&ga);
14835 }
14836#endif
14837}
14838
Bram Moolenaar0d660222005-01-07 21:51:51 +000014839 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014840f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014841 typval_T *argvars;
14842 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014843{
14844 char_u *str;
14845 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014846 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847 regmatch_T regmatch;
14848 char_u patbuf[NUMBUFLEN];
14849 char_u *save_cpo;
14850 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014851 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014852 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014853 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014854
14855 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14856 save_cpo = p_cpo;
14857 p_cpo = (char_u *)"";
14858
14859 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014860 if (argvars[1].v_type != VAR_UNKNOWN)
14861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014862 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14863 if (pat == NULL)
14864 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014865 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014866 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014867 }
14868 if (pat == NULL || *pat == NUL)
14869 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014870
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014871 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014872 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014873 if (typeerr)
14874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875
Bram Moolenaar0d660222005-01-07 21:51:51 +000014876 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14877 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014879 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014880 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014881 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014882 if (*str == NUL)
14883 match = FALSE; /* empty item at the end */
14884 else
14885 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014886 if (match)
14887 end = regmatch.startp[0];
14888 else
14889 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014890 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14891 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014892 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014893 if (list_append_string(rettv->vval.v_list, str,
14894 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014895 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014896 }
14897 if (!match)
14898 break;
14899 /* Advance to just after the match. */
14900 if (regmatch.endp[0] > str)
14901 col = 0;
14902 else
14903 {
14904 /* Don't get stuck at the same match. */
14905#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014906 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014907#else
14908 col = 1;
14909#endif
14910 }
14911 str = regmatch.endp[0];
14912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913
Bram Moolenaar0d660222005-01-07 21:51:51 +000014914 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916
Bram Moolenaar0d660222005-01-07 21:51:51 +000014917 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918}
14919
Bram Moolenaar2c932302006-03-18 21:42:09 +000014920/*
14921 * "str2nr()" function
14922 */
14923 static void
14924f_str2nr(argvars, rettv)
14925 typval_T *argvars;
14926 typval_T *rettv;
14927{
14928 int base = 10;
14929 char_u *p;
14930 long n;
14931
14932 if (argvars[1].v_type != VAR_UNKNOWN)
14933 {
14934 base = get_tv_number(&argvars[1]);
14935 if (base != 8 && base != 10 && base != 16)
14936 {
14937 EMSG(_(e_invarg));
14938 return;
14939 }
14940 }
14941
14942 p = skipwhite(get_tv_string(&argvars[0]));
14943 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14944 rettv->vval.v_number = n;
14945}
14946
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947#ifdef HAVE_STRFTIME
14948/*
14949 * "strftime({format}[, {time}])" function
14950 */
14951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014952f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 typval_T *argvars;
14954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955{
14956 char_u result_buf[256];
14957 struct tm *curtime;
14958 time_t seconds;
14959 char_u *p;
14960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014964 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965 seconds = time(NULL);
14966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 curtime = localtime(&seconds);
14969 /* MSVC returns NULL for an invalid value of seconds. */
14970 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 else
14973 {
14974# ifdef FEAT_MBYTE
14975 vimconv_T conv;
14976 char_u *enc;
14977
14978 conv.vc_type = CONV_NONE;
14979 enc = enc_locale();
14980 convert_setup(&conv, p_enc, enc);
14981 if (conv.vc_type != CONV_NONE)
14982 p = string_convert(&conv, p, NULL);
14983# endif
14984 if (p != NULL)
14985 (void)strftime((char *)result_buf, sizeof(result_buf),
14986 (char *)p, curtime);
14987 else
14988 result_buf[0] = NUL;
14989
14990# ifdef FEAT_MBYTE
14991 if (conv.vc_type != CONV_NONE)
14992 vim_free(p);
14993 convert_setup(&conv, enc, p_enc);
14994 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014995 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996 else
14997# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999
15000# ifdef FEAT_MBYTE
15001 /* Release conversion descriptors */
15002 convert_setup(&conv, NULL, NULL);
15003 vim_free(enc);
15004# endif
15005 }
15006}
15007#endif
15008
15009/*
15010 * "stridx()" function
15011 */
15012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015013f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015014 typval_T *argvars;
15015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016{
15017 char_u buf[NUMBUFLEN];
15018 char_u *needle;
15019 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015020 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015022 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015024 needle = get_tv_string_chk(&argvars[1]);
15025 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015026 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015027 if (needle == NULL || haystack == NULL)
15028 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 if (argvars[2].v_type != VAR_UNKNOWN)
15031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015032 int error = FALSE;
15033
15034 start_idx = get_tv_number_chk(&argvars[2], &error);
15035 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015036 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015037 if (start_idx >= 0)
15038 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015039 }
15040
15041 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15042 if (pos != NULL)
15043 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044}
15045
15046/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015047 * "string()" function
15048 */
15049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 typval_T *argvars;
15052 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015053{
15054 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015055 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015058 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015059 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015060 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061}
15062
15063/*
15064 * "strlen()" function
15065 */
15066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015067f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015068 typval_T *argvars;
15069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015071 rettv->vval.v_number = (varnumber_T)(STRLEN(
15072 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073}
15074
15075/*
15076 * "strpart()" function
15077 */
15078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015080 typval_T *argvars;
15081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082{
15083 char_u *p;
15084 int n;
15085 int len;
15086 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015087 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015089 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 slen = (int)STRLEN(p);
15091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015092 n = get_tv_number_chk(&argvars[1], &error);
15093 if (error)
15094 len = 0;
15095 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015096 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 else
15098 len = slen - n; /* default len: all bytes that are available. */
15099
15100 /*
15101 * Only return the overlap between the specified part and the actual
15102 * string.
15103 */
15104 if (n < 0)
15105 {
15106 len += n;
15107 n = 0;
15108 }
15109 else if (n > slen)
15110 n = slen;
15111 if (len < 0)
15112 len = 0;
15113 else if (n + len > slen)
15114 len = slen - n;
15115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015116 rettv->v_type = VAR_STRING;
15117 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118}
15119
15120/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121 * "strridx()" function
15122 */
15123 static void
15124f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015125 typval_T *argvars;
15126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127{
15128 char_u buf[NUMBUFLEN];
15129 char_u *needle;
15130 char_u *haystack;
15131 char_u *rest;
15132 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015133 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135 needle = get_tv_string_chk(&argvars[1]);
15136 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015137
15138 rettv->vval.v_number = -1;
15139 if (needle == NULL || haystack == NULL)
15140 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015141
15142 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015143 if (argvars[2].v_type != VAR_UNKNOWN)
15144 {
15145 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015146 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015147 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015149 }
15150 else
15151 end_idx = haystack_len;
15152
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015154 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015155 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015156 lastmatch = haystack + end_idx;
15157 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015159 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 for (rest = haystack; *rest != '\0'; ++rest)
15161 {
15162 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015163 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164 break;
15165 lastmatch = rest;
15166 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015167 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168
15169 if (lastmatch == NULL)
15170 rettv->vval.v_number = -1;
15171 else
15172 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15173}
15174
15175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 * "strtrans()" function
15177 */
15178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015179f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015180 typval_T *argvars;
15181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015183 rettv->v_type = VAR_STRING;
15184 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185}
15186
15187/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015188 * "submatch()" function
15189 */
15190 static void
15191f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *argvars;
15193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194{
15195 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015196 rettv->vval.v_string =
15197 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198}
15199
15200/*
15201 * "substitute()" function
15202 */
15203 static void
15204f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 typval_T *argvars;
15206 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207{
15208 char_u patbuf[NUMBUFLEN];
15209 char_u subbuf[NUMBUFLEN];
15210 char_u flagsbuf[NUMBUFLEN];
15211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 char_u *str = get_tv_string_chk(&argvars[0]);
15213 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15214 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15215 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15216
Bram Moolenaar0d660222005-01-07 21:51:51 +000015217 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015218 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15219 rettv->vval.v_string = NULL;
15220 else
15221 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222}
15223
15224/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015225 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 */
15227/*ARGSUSED*/
15228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015229f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015230 typval_T *argvars;
15231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232{
15233 int id = 0;
15234#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015235 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 long col;
15237 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015238 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015240 lnum = get_tv_lnum(argvars); /* -1 on type error */
15241 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15242 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015245 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015246 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247#endif
15248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015249 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250}
15251
15252/*
15253 * "synIDattr(id, what [, mode])" function
15254 */
15255/*ARGSUSED*/
15256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 typval_T *argvars;
15259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260{
15261 char_u *p = NULL;
15262#ifdef FEAT_SYN_HL
15263 int id;
15264 char_u *what;
15265 char_u *mode;
15266 char_u modebuf[NUMBUFLEN];
15267 int modec;
15268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 id = get_tv_number(&argvars[0]);
15270 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015271 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015273 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 modec = TOLOWER_ASC(mode[0]);
15275 if (modec != 't' && modec != 'c'
15276#ifdef FEAT_GUI
15277 && modec != 'g'
15278#endif
15279 )
15280 modec = 0; /* replace invalid with current */
15281 }
15282 else
15283 {
15284#ifdef FEAT_GUI
15285 if (gui.in_use)
15286 modec = 'g';
15287 else
15288#endif
15289 if (t_colors > 1)
15290 modec = 'c';
15291 else
15292 modec = 't';
15293 }
15294
15295
15296 switch (TOLOWER_ASC(what[0]))
15297 {
15298 case 'b':
15299 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15300 p = highlight_color(id, what, modec);
15301 else /* bold */
15302 p = highlight_has_attr(id, HL_BOLD, modec);
15303 break;
15304
15305 case 'f': /* fg[#] */
15306 p = highlight_color(id, what, modec);
15307 break;
15308
15309 case 'i':
15310 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15311 p = highlight_has_attr(id, HL_INVERSE, modec);
15312 else /* italic */
15313 p = highlight_has_attr(id, HL_ITALIC, modec);
15314 break;
15315
15316 case 'n': /* name */
15317 p = get_highlight_name(NULL, id - 1);
15318 break;
15319
15320 case 'r': /* reverse */
15321 p = highlight_has_attr(id, HL_INVERSE, modec);
15322 break;
15323
15324 case 's': /* standout */
15325 p = highlight_has_attr(id, HL_STANDOUT, modec);
15326 break;
15327
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015328 case 'u':
15329 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15330 /* underline */
15331 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15332 else
15333 /* undercurl */
15334 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 break;
15336 }
15337
15338 if (p != NULL)
15339 p = vim_strsave(p);
15340#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 rettv->v_type = VAR_STRING;
15342 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343}
15344
15345/*
15346 * "synIDtrans(id)" function
15347 */
15348/*ARGSUSED*/
15349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015350f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015351 typval_T *argvars;
15352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353{
15354 int id;
15355
15356#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358
15359 if (id > 0)
15360 id = syn_get_final_id(id);
15361 else
15362#endif
15363 id = 0;
15364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366}
15367
15368/*
15369 * "system()" function
15370 */
15371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015373 typval_T *argvars;
15374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015376 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015378 char_u *infile = NULL;
15379 char_u buf[NUMBUFLEN];
15380 int err = FALSE;
15381 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015383 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015384 {
15385 /*
15386 * Write the string to a temp file, to be used for input of the shell
15387 * command.
15388 */
15389 if ((infile = vim_tempname('i')) == NULL)
15390 {
15391 EMSG(_(e_notmp));
15392 return;
15393 }
15394
15395 fd = mch_fopen((char *)infile, WRITEBIN);
15396 if (fd == NULL)
15397 {
15398 EMSG2(_(e_notopen), infile);
15399 goto done;
15400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015401 p = get_tv_string_buf_chk(&argvars[1], buf);
15402 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015403 {
15404 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015405 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015406 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015407 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15408 err = TRUE;
15409 if (fclose(fd) != 0)
15410 err = TRUE;
15411 if (err)
15412 {
15413 EMSG(_("E677: Error writing temp file"));
15414 goto done;
15415 }
15416 }
15417
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015418 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15419 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015420
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421#ifdef USE_CR
15422 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015423 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 {
15425 char_u *s;
15426
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015427 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 {
15429 if (*s == CAR)
15430 *s = NL;
15431 }
15432 }
15433#else
15434# ifdef USE_CRNL
15435 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015436 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 {
15438 char_u *s, *d;
15439
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015440 d = res;
15441 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 {
15443 if (s[0] == CAR && s[1] == NL)
15444 ++s;
15445 *d++ = *s;
15446 }
15447 *d = NUL;
15448 }
15449# endif
15450#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015451
15452done:
15453 if (infile != NULL)
15454 {
15455 mch_remove(infile);
15456 vim_free(infile);
15457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 rettv->v_type = VAR_STRING;
15459 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460}
15461
15462/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015463 * "tabpagebuflist()" function
15464 */
15465/* ARGSUSED */
15466 static void
15467f_tabpagebuflist(argvars, rettv)
15468 typval_T *argvars;
15469 typval_T *rettv;
15470{
15471#ifndef FEAT_WINDOWS
15472 rettv->vval.v_number = 0;
15473#else
15474 tabpage_T *tp;
15475 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015476
15477 if (argvars[0].v_type == VAR_UNKNOWN)
15478 wp = firstwin;
15479 else
15480 {
15481 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15482 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015483 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015484 }
15485 if (wp == NULL)
15486 rettv->vval.v_number = 0;
15487 else
15488 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015489 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015490 rettv->vval.v_number = 0;
15491 else
15492 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015493 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015494 if (list_append_number(rettv->vval.v_list,
15495 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015496 break;
15497 }
15498 }
15499#endif
15500}
15501
15502
15503/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015504 * "tabpagenr()" function
15505 */
15506/* ARGSUSED */
15507 static void
15508f_tabpagenr(argvars, rettv)
15509 typval_T *argvars;
15510 typval_T *rettv;
15511{
15512 int nr = 1;
15513#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015514 char_u *arg;
15515
15516 if (argvars[0].v_type != VAR_UNKNOWN)
15517 {
15518 arg = get_tv_string_chk(&argvars[0]);
15519 nr = 0;
15520 if (arg != NULL)
15521 {
15522 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015523 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015524 else
15525 EMSG2(_(e_invexpr2), arg);
15526 }
15527 }
15528 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015529 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015530#endif
15531 rettv->vval.v_number = nr;
15532}
15533
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015534
15535#ifdef FEAT_WINDOWS
15536static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15537
15538/*
15539 * Common code for tabpagewinnr() and winnr().
15540 */
15541 static int
15542get_winnr(tp, argvar)
15543 tabpage_T *tp;
15544 typval_T *argvar;
15545{
15546 win_T *twin;
15547 int nr = 1;
15548 win_T *wp;
15549 char_u *arg;
15550
15551 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15552 if (argvar->v_type != VAR_UNKNOWN)
15553 {
15554 arg = get_tv_string_chk(argvar);
15555 if (arg == NULL)
15556 nr = 0; /* type error; errmsg already given */
15557 else if (STRCMP(arg, "$") == 0)
15558 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15559 else if (STRCMP(arg, "#") == 0)
15560 {
15561 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15562 if (twin == NULL)
15563 nr = 0;
15564 }
15565 else
15566 {
15567 EMSG2(_(e_invexpr2), arg);
15568 nr = 0;
15569 }
15570 }
15571
15572 if (nr > 0)
15573 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15574 wp != twin; wp = wp->w_next)
15575 ++nr;
15576 return nr;
15577}
15578#endif
15579
15580/*
15581 * "tabpagewinnr()" function
15582 */
15583/* ARGSUSED */
15584 static void
15585f_tabpagewinnr(argvars, rettv)
15586 typval_T *argvars;
15587 typval_T *rettv;
15588{
15589 int nr = 1;
15590#ifdef FEAT_WINDOWS
15591 tabpage_T *tp;
15592
15593 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15594 if (tp == NULL)
15595 nr = 0;
15596 else
15597 nr = get_winnr(tp, &argvars[1]);
15598#endif
15599 rettv->vval.v_number = nr;
15600}
15601
15602
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015603/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015604 * "tagfiles()" function
15605 */
15606/*ARGSUSED*/
15607 static void
15608f_tagfiles(argvars, rettv)
15609 typval_T *argvars;
15610 typval_T *rettv;
15611{
15612 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015613 tagname_T tn;
15614 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015616 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015617 {
15618 rettv->vval.v_number = 0;
15619 return;
15620 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015621
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015622 for (first = TRUE; ; first = FALSE)
15623 if (get_tagfname(&tn, first, fname) == FAIL
15624 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015625 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015626 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015627}
15628
15629/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015630 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015631 */
15632 static void
15633f_taglist(argvars, rettv)
15634 typval_T *argvars;
15635 typval_T *rettv;
15636{
15637 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015638
15639 tag_pattern = get_tv_string(&argvars[0]);
15640
15641 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015642 if (*tag_pattern == NUL)
15643 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015644
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015645 if (rettv_list_alloc(rettv) == OK)
15646 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015647}
15648
15649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 * "tempname()" function
15651 */
15652/*ARGSUSED*/
15653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015655 typval_T *argvars;
15656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657{
15658 static int x = 'A';
15659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660 rettv->v_type = VAR_STRING;
15661 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662
15663 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15664 * names. Skip 'I' and 'O', they are used for shell redirection. */
15665 do
15666 {
15667 if (x == 'Z')
15668 x = '0';
15669 else if (x == '9')
15670 x = 'A';
15671 else
15672 {
15673#ifdef EBCDIC
15674 if (x == 'I')
15675 x = 'J';
15676 else if (x == 'R')
15677 x = 'S';
15678 else
15679#endif
15680 ++x;
15681 }
15682 } while (x == 'I' || x == 'O');
15683}
15684
15685/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015686 * "test(list)" function: Just checking the walls...
15687 */
15688/*ARGSUSED*/
15689 static void
15690f_test(argvars, rettv)
15691 typval_T *argvars;
15692 typval_T *rettv;
15693{
15694 /* Used for unit testing. Change the code below to your liking. */
15695#if 0
15696 listitem_T *li;
15697 list_T *l;
15698 char_u *bad, *good;
15699
15700 if (argvars[0].v_type != VAR_LIST)
15701 return;
15702 l = argvars[0].vval.v_list;
15703 if (l == NULL)
15704 return;
15705 li = l->lv_first;
15706 if (li == NULL)
15707 return;
15708 bad = get_tv_string(&li->li_tv);
15709 li = li->li_next;
15710 if (li == NULL)
15711 return;
15712 good = get_tv_string(&li->li_tv);
15713 rettv->vval.v_number = test_edit_score(bad, good);
15714#endif
15715}
15716
15717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 * "tolower(string)" function
15719 */
15720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015722 typval_T *argvars;
15723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724{
15725 char_u *p;
15726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 p = vim_strsave(get_tv_string(&argvars[0]));
15728 rettv->v_type = VAR_STRING;
15729 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730
15731 if (p != NULL)
15732 while (*p != NUL)
15733 {
15734#ifdef FEAT_MBYTE
15735 int l;
15736
15737 if (enc_utf8)
15738 {
15739 int c, lc;
15740
15741 c = utf_ptr2char(p);
15742 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015743 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 /* TODO: reallocate string when byte count changes. */
15745 if (utf_char2len(lc) == l)
15746 utf_char2bytes(lc, p);
15747 p += l;
15748 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015749 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 p += l; /* skip multi-byte character */
15751 else
15752#endif
15753 {
15754 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15755 ++p;
15756 }
15757 }
15758}
15759
15760/*
15761 * "toupper(string)" function
15762 */
15763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015764f_toupper(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015769 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770}
15771
15772/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015773 * "tr(string, fromstr, tostr)" function
15774 */
15775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015776f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 typval_T *argvars;
15778 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015779{
15780 char_u *instr;
15781 char_u *fromstr;
15782 char_u *tostr;
15783 char_u *p;
15784#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015785 int inlen;
15786 int fromlen;
15787 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015788 int idx;
15789 char_u *cpstr;
15790 int cplen;
15791 int first = TRUE;
15792#endif
15793 char_u buf[NUMBUFLEN];
15794 char_u buf2[NUMBUFLEN];
15795 garray_T ga;
15796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015797 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015798 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15799 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015800
15801 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 rettv->v_type = VAR_STRING;
15803 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015804 if (fromstr == NULL || tostr == NULL)
15805 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015806 ga_init2(&ga, (int)sizeof(char), 80);
15807
15808#ifdef FEAT_MBYTE
15809 if (!has_mbyte)
15810#endif
15811 /* not multi-byte: fromstr and tostr must be the same length */
15812 if (STRLEN(fromstr) != STRLEN(tostr))
15813 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015814#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015815error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015816#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015817 EMSG2(_(e_invarg2), fromstr);
15818 ga_clear(&ga);
15819 return;
15820 }
15821
15822 /* fromstr and tostr have to contain the same number of chars */
15823 while (*instr != NUL)
15824 {
15825#ifdef FEAT_MBYTE
15826 if (has_mbyte)
15827 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015828 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015829 cpstr = instr;
15830 cplen = inlen;
15831 idx = 0;
15832 for (p = fromstr; *p != NUL; p += fromlen)
15833 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015834 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015835 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15836 {
15837 for (p = tostr; *p != NUL; p += tolen)
15838 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015839 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015840 if (idx-- == 0)
15841 {
15842 cplen = tolen;
15843 cpstr = p;
15844 break;
15845 }
15846 }
15847 if (*p == NUL) /* tostr is shorter than fromstr */
15848 goto error;
15849 break;
15850 }
15851 ++idx;
15852 }
15853
15854 if (first && cpstr == instr)
15855 {
15856 /* Check that fromstr and tostr have the same number of
15857 * (multi-byte) characters. Done only once when a character
15858 * of instr doesn't appear in fromstr. */
15859 first = FALSE;
15860 for (p = tostr; *p != NUL; p += tolen)
15861 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015862 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015863 --idx;
15864 }
15865 if (idx != 0)
15866 goto error;
15867 }
15868
15869 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015870 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015871 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015872
15873 instr += inlen;
15874 }
15875 else
15876#endif
15877 {
15878 /* When not using multi-byte chars we can do it faster. */
15879 p = vim_strchr(fromstr, *instr);
15880 if (p != NULL)
15881 ga_append(&ga, tostr[p - fromstr]);
15882 else
15883 ga_append(&ga, *instr);
15884 ++instr;
15885 }
15886 }
15887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015888 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015889}
15890
15891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 * "type(expr)" function
15893 */
15894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015895f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 typval_T *argvars;
15897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015899 int n;
15900
15901 switch (argvars[0].v_type)
15902 {
15903 case VAR_NUMBER: n = 0; break;
15904 case VAR_STRING: n = 1; break;
15905 case VAR_FUNC: n = 2; break;
15906 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015907 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015908 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15909 }
15910 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911}
15912
15913/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015914 * "values(dict)" function
15915 */
15916 static void
15917f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015918 typval_T *argvars;
15919 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015920{
15921 dict_list(argvars, rettv, 1);
15922}
15923
15924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 * "virtcol(string)" function
15926 */
15927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015928f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015929 typval_T *argvars;
15930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931{
15932 colnr_T vcol = 0;
15933 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015934 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015936 fp = var2fpos(&argvars[0], FALSE, &fnum);
15937 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15938 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939 {
15940 getvvcol(curwin, fp, NULL, NULL, &vcol);
15941 ++vcol;
15942 }
15943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015944 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945}
15946
15947/*
15948 * "visualmode()" function
15949 */
15950/*ARGSUSED*/
15951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015952f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015953 typval_T *argvars;
15954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955{
15956#ifdef FEAT_VISUAL
15957 char_u str[2];
15958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015959 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 str[0] = curbuf->b_visual_mode_eval;
15961 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015962 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963
15964 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015965 if ((argvars[0].v_type == VAR_NUMBER
15966 && argvars[0].vval.v_number != 0)
15967 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015968 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969 curbuf->b_visual_mode_eval = NUL;
15970#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015971 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972#endif
15973}
15974
15975/*
15976 * "winbufnr(nr)" function
15977 */
15978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015979f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015980 typval_T *argvars;
15981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982{
15983 win_T *wp;
15984
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015985 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015987 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015989 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990}
15991
15992/*
15993 * "wincol()" function
15994 */
15995/*ARGSUSED*/
15996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015997f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015998 typval_T *argvars;
15999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000{
16001 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016002 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003}
16004
16005/*
16006 * "winheight(nr)" function
16007 */
16008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016010 typval_T *argvars;
16011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012{
16013 win_T *wp;
16014
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016015 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016017 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016019 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020}
16021
16022/*
16023 * "winline()" function
16024 */
16025/*ARGSUSED*/
16026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016027f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016028 typval_T *argvars;
16029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030{
16031 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016032 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033}
16034
16035/*
16036 * "winnr()" function
16037 */
16038/* ARGSUSED */
16039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016040f_winnr(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 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016045
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016047 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016049 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050}
16051
16052/*
16053 * "winrestcmd()" function
16054 */
16055/* ARGSUSED */
16056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016057f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016058 typval_T *argvars;
16059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060{
16061#ifdef FEAT_WINDOWS
16062 win_T *wp;
16063 int winnr = 1;
16064 garray_T ga;
16065 char_u buf[50];
16066
16067 ga_init2(&ga, (int)sizeof(char), 70);
16068 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16069 {
16070 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16071 ga_concat(&ga, buf);
16072# ifdef FEAT_VERTSPLIT
16073 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16074 ga_concat(&ga, buf);
16075# endif
16076 ++winnr;
16077 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016078 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016080 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016082 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085}
16086
16087/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016088 * "winrestview()" function
16089 */
16090/* ARGSUSED */
16091 static void
16092f_winrestview(argvars, rettv)
16093 typval_T *argvars;
16094 typval_T *rettv;
16095{
16096 dict_T *dict;
16097
16098 if (argvars[0].v_type != VAR_DICT
16099 || (dict = argvars[0].vval.v_dict) == NULL)
16100 EMSG(_(e_invarg));
16101 else
16102 {
16103 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16104 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16105#ifdef FEAT_VIRTUALEDIT
16106 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16107#endif
16108 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016109 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016110
16111 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16112#ifdef FEAT_DIFF
16113 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16114#endif
16115 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16116 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16117
16118 check_cursor();
16119 changed_cline_bef_curs();
16120 invalidate_botline();
16121 redraw_later(VALID);
16122
16123 if (curwin->w_topline == 0)
16124 curwin->w_topline = 1;
16125 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16126 curwin->w_topline = curbuf->b_ml.ml_line_count;
16127#ifdef FEAT_DIFF
16128 check_topfill(curwin, TRUE);
16129#endif
16130 }
16131}
16132
16133/*
16134 * "winsaveview()" function
16135 */
16136/* ARGSUSED */
16137 static void
16138f_winsaveview(argvars, rettv)
16139 typval_T *argvars;
16140 typval_T *rettv;
16141{
16142 dict_T *dict;
16143
16144 dict = dict_alloc();
16145 if (dict == NULL)
16146 return;
16147 rettv->v_type = VAR_DICT;
16148 rettv->vval.v_dict = dict;
16149 ++dict->dv_refcount;
16150
16151 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16152 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16153#ifdef FEAT_VIRTUALEDIT
16154 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16155#endif
16156 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16157
16158 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16159#ifdef FEAT_DIFF
16160 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16161#endif
16162 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16163 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16164}
16165
16166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167 * "winwidth(nr)" function
16168 */
16169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 typval_T *argvars;
16172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173{
16174 win_T *wp;
16175
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016176 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 else
16180#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016181 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016183 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184#endif
16185}
16186
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016188 * "writefile()" function
16189 */
16190 static void
16191f_writefile(argvars, rettv)
16192 typval_T *argvars;
16193 typval_T *rettv;
16194{
16195 int binary = FALSE;
16196 char_u *fname;
16197 FILE *fd;
16198 listitem_T *li;
16199 char_u *s;
16200 int ret = 0;
16201 int c;
16202
16203 if (argvars[0].v_type != VAR_LIST)
16204 {
16205 EMSG2(_(e_listarg), "writefile()");
16206 return;
16207 }
16208 if (argvars[0].vval.v_list == NULL)
16209 return;
16210
16211 if (argvars[2].v_type != VAR_UNKNOWN
16212 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16213 binary = TRUE;
16214
16215 /* Always open the file in binary mode, library functions have a mind of
16216 * their own about CR-LF conversion. */
16217 fname = get_tv_string(&argvars[1]);
16218 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16219 {
16220 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16221 ret = -1;
16222 }
16223 else
16224 {
16225 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16226 li = li->li_next)
16227 {
16228 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16229 {
16230 if (*s == '\n')
16231 c = putc(NUL, fd);
16232 else
16233 c = putc(*s, fd);
16234 if (c == EOF)
16235 {
16236 ret = -1;
16237 break;
16238 }
16239 }
16240 if (!binary || li->li_next != NULL)
16241 if (putc('\n', fd) == EOF)
16242 {
16243 ret = -1;
16244 break;
16245 }
16246 if (ret < 0)
16247 {
16248 EMSG(_(e_write));
16249 break;
16250 }
16251 }
16252 fclose(fd);
16253 }
16254
16255 rettv->vval.v_number = ret;
16256}
16257
16258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016260 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 */
16262 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016263var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016264 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016266 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016268 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016270 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271
Bram Moolenaara5525202006-03-02 22:52:09 +000016272 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016273 if (varp->v_type == VAR_LIST)
16274 {
16275 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016276 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016277 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016278
16279 l = varp->vval.v_list;
16280 if (l == NULL)
16281 return NULL;
16282
16283 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016284 pos.lnum = list_find_nr(l, 0L, &error);
16285 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016286 return NULL; /* invalid line number */
16287
16288 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016289 pos.col = list_find_nr(l, 1L, &error);
16290 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016291 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016292 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016293 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016294 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016295 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016296 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016297
Bram Moolenaara5525202006-03-02 22:52:09 +000016298#ifdef FEAT_VIRTUALEDIT
16299 /* Get the virtual offset. Defaults to zero. */
16300 pos.coladd = list_find_nr(l, 2L, &error);
16301 if (error)
16302 pos.coladd = 0;
16303#endif
16304
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016305 return &pos;
16306 }
16307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016308 name = get_tv_string_chk(varp);
16309 if (name == NULL)
16310 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 if (name[0] == '.') /* cursor */
16312 return &curwin->w_cursor;
16313 if (name[0] == '\'') /* mark */
16314 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016315 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16317 return NULL;
16318 return pp;
16319 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016320
16321#ifdef FEAT_VIRTUALEDIT
16322 pos.coladd = 0;
16323#endif
16324
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016325 if (name[0] == 'w' && lnum)
16326 {
16327 pos.col = 0;
16328 if (name[1] == '0') /* "w0": first visible line */
16329 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016330 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016331 pos.lnum = curwin->w_topline;
16332 return &pos;
16333 }
16334 else if (name[1] == '$') /* "w$": last visible line */
16335 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016336 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016337 pos.lnum = curwin->w_botline - 1;
16338 return &pos;
16339 }
16340 }
16341 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 {
16343 if (lnum)
16344 {
16345 pos.lnum = curbuf->b_ml.ml_line_count;
16346 pos.col = 0;
16347 }
16348 else
16349 {
16350 pos.lnum = curwin->w_cursor.lnum;
16351 pos.col = (colnr_T)STRLEN(ml_get_curline());
16352 }
16353 return &pos;
16354 }
16355 return NULL;
16356}
16357
16358/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016359 * Convert list in "arg" into a position and optional file number.
16360 * When "fnump" is NULL there is no file number, only 3 items.
16361 * Note that the column is passed on as-is, the caller may want to decrement
16362 * it to use 1 for the first column.
16363 * Return FAIL when conversion is not possible, doesn't check the position for
16364 * validity.
16365 */
16366 static int
16367list2fpos(arg, posp, fnump)
16368 typval_T *arg;
16369 pos_T *posp;
16370 int *fnump;
16371{
16372 list_T *l = arg->vval.v_list;
16373 long i = 0;
16374 long n;
16375
16376 /* List must be: [fnum, lnum, col, coladd] */
16377 if (arg->v_type != VAR_LIST || l == NULL
16378 || l->lv_len != (fnump == NULL ? 3 : 4))
16379 return FAIL;
16380
16381 if (fnump != NULL)
16382 {
16383 n = list_find_nr(l, i++, NULL); /* fnum */
16384 if (n < 0)
16385 return FAIL;
16386 if (n == 0)
16387 n = curbuf->b_fnum; /* current buffer */
16388 *fnump = n;
16389 }
16390
16391 n = list_find_nr(l, i++, NULL); /* lnum */
16392 if (n < 0)
16393 return FAIL;
16394 posp->lnum = n;
16395
16396 n = list_find_nr(l, i++, NULL); /* col */
16397 if (n < 0)
16398 return FAIL;
16399 posp->col = n;
16400
16401#ifdef FEAT_VIRTUALEDIT
16402 n = list_find_nr(l, i, NULL);
16403 if (n < 0)
16404 return FAIL;
16405 posp->coladd = n;
16406#endif
16407
16408 return OK;
16409}
16410
16411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 * Get the length of an environment variable name.
16413 * Advance "arg" to the first character after the name.
16414 * Return 0 for error.
16415 */
16416 static int
16417get_env_len(arg)
16418 char_u **arg;
16419{
16420 char_u *p;
16421 int len;
16422
16423 for (p = *arg; vim_isIDc(*p); ++p)
16424 ;
16425 if (p == *arg) /* no name found */
16426 return 0;
16427
16428 len = (int)(p - *arg);
16429 *arg = p;
16430 return len;
16431}
16432
16433/*
16434 * Get the length of the name of a function or internal variable.
16435 * "arg" is advanced to the first non-white character after the name.
16436 * Return 0 if something is wrong.
16437 */
16438 static int
16439get_id_len(arg)
16440 char_u **arg;
16441{
16442 char_u *p;
16443 int len;
16444
16445 /* Find the end of the name. */
16446 for (p = *arg; eval_isnamec(*p); ++p)
16447 ;
16448 if (p == *arg) /* no name found */
16449 return 0;
16450
16451 len = (int)(p - *arg);
16452 *arg = skipwhite(p);
16453
16454 return len;
16455}
16456
16457/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016458 * Get the length of the name of a variable or function.
16459 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016461 * Return -1 if curly braces expansion failed.
16462 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 * If the name contains 'magic' {}'s, expand them and return the
16464 * expanded name in an allocated string via 'alias' - caller must free.
16465 */
16466 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016467get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 char_u **arg;
16469 char_u **alias;
16470 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016471 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472{
16473 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474 char_u *p;
16475 char_u *expr_start;
16476 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477
16478 *alias = NULL; /* default to no alias */
16479
16480 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16481 && (*arg)[2] == (int)KE_SNR)
16482 {
16483 /* hard coded <SNR>, already translated */
16484 *arg += 3;
16485 return get_id_len(arg) + 3;
16486 }
16487 len = eval_fname_script(*arg);
16488 if (len > 0)
16489 {
16490 /* literal "<SID>", "s:" or "<SNR>" */
16491 *arg += len;
16492 }
16493
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016495 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016497 p = find_name_end(*arg, &expr_start, &expr_end,
16498 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 if (expr_start != NULL)
16500 {
16501 char_u *temp_string;
16502
16503 if (!evaluate)
16504 {
16505 len += (int)(p - *arg);
16506 *arg = skipwhite(p);
16507 return len;
16508 }
16509
16510 /*
16511 * Include any <SID> etc in the expanded string:
16512 * Thus the -len here.
16513 */
16514 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16515 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016516 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 *alias = temp_string;
16518 *arg = skipwhite(p);
16519 return (int)STRLEN(temp_string);
16520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521
16522 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016523 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 EMSG2(_(e_invexpr2), *arg);
16525
16526 return len;
16527}
16528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016529/*
16530 * Find the end of a variable or function name, taking care of magic braces.
16531 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16532 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016533 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016534 * Return a pointer to just after the name. Equal to "arg" if there is no
16535 * valid name.
16536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016538find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 char_u *arg;
16540 char_u **expr_start;
16541 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016542 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544 int mb_nest = 0;
16545 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 char_u *p;
16547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016548 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016550 *expr_start = NULL;
16551 *expr_end = NULL;
16552 }
16553
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016554 /* Quick check for valid starting character. */
16555 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16556 return arg;
16557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016558 for (p = arg; *p != NUL
16559 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016560 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016561 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016562 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016563 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016564 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016565 if (*p == '\'')
16566 {
16567 /* skip over 'string' to avoid counting [ and ] inside it. */
16568 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16569 ;
16570 if (*p == NUL)
16571 break;
16572 }
16573 else if (*p == '"')
16574 {
16575 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16576 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16577 if (*p == '\\' && p[1] != NUL)
16578 ++p;
16579 if (*p == NUL)
16580 break;
16581 }
16582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016583 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016585 if (*p == '[')
16586 ++br_nest;
16587 else if (*p == ']')
16588 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016591 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016593 if (*p == '{')
16594 {
16595 mb_nest++;
16596 if (expr_start != NULL && *expr_start == NULL)
16597 *expr_start = p;
16598 }
16599 else if (*p == '}')
16600 {
16601 mb_nest--;
16602 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16603 *expr_end = p;
16604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 }
16607
16608 return p;
16609}
16610
16611/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016612 * Expands out the 'magic' {}'s in a variable/function name.
16613 * Note that this can call itself recursively, to deal with
16614 * constructs like foo{bar}{baz}{bam}
16615 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16616 * "in_start" ^
16617 * "expr_start" ^
16618 * "expr_end" ^
16619 * "in_end" ^
16620 *
16621 * Returns a new allocated string, which the caller must free.
16622 * Returns NULL for failure.
16623 */
16624 static char_u *
16625make_expanded_name(in_start, expr_start, expr_end, in_end)
16626 char_u *in_start;
16627 char_u *expr_start;
16628 char_u *expr_end;
16629 char_u *in_end;
16630{
16631 char_u c1;
16632 char_u *retval = NULL;
16633 char_u *temp_result;
16634 char_u *nextcmd = NULL;
16635
16636 if (expr_end == NULL || in_end == NULL)
16637 return NULL;
16638 *expr_start = NUL;
16639 *expr_end = NUL;
16640 c1 = *in_end;
16641 *in_end = NUL;
16642
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016643 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016644 if (temp_result != NULL && nextcmd == NULL)
16645 {
16646 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16647 + (in_end - expr_end) + 1));
16648 if (retval != NULL)
16649 {
16650 STRCPY(retval, in_start);
16651 STRCAT(retval, temp_result);
16652 STRCAT(retval, expr_end + 1);
16653 }
16654 }
16655 vim_free(temp_result);
16656
16657 *in_end = c1; /* put char back for error messages */
16658 *expr_start = '{';
16659 *expr_end = '}';
16660
16661 if (retval != NULL)
16662 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016663 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016664 if (expr_start != NULL)
16665 {
16666 /* Further expansion! */
16667 temp_result = make_expanded_name(retval, expr_start,
16668 expr_end, temp_result);
16669 vim_free(retval);
16670 retval = temp_result;
16671 }
16672 }
16673
16674 return retval;
16675}
16676
16677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016679 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 */
16681 static int
16682eval_isnamec(c)
16683 int c;
16684{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016685 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16686}
16687
16688/*
16689 * Return TRUE if character "c" can be used as the first character in a
16690 * variable or function name (excluding '{' and '}').
16691 */
16692 static int
16693eval_isnamec1(c)
16694 int c;
16695{
16696 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697}
16698
16699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 * Set number v: variable to "val".
16701 */
16702 void
16703set_vim_var_nr(idx, val)
16704 int idx;
16705 long val;
16706{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016707 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708}
16709
16710/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016711 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 */
16713 long
16714get_vim_var_nr(idx)
16715 int idx;
16716{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016717 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718}
16719
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016720#if defined(FEAT_AUTOCMD) || defined(PROTO)
16721/*
16722 * Get string v: variable value. Uses a static buffer, can only be used once.
16723 */
16724 char_u *
16725get_vim_var_str(idx)
16726 int idx;
16727{
16728 return get_tv_string(&vimvars[idx].vv_tv);
16729}
16730#endif
16731
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732/*
16733 * Set v:count, v:count1 and v:prevcount.
16734 */
16735 void
16736set_vcount(count, count1)
16737 long count;
16738 long count1;
16739{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016740 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16741 vimvars[VV_COUNT].vv_nr = count;
16742 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743}
16744
16745/*
16746 * Set string v: variable to a copy of "val".
16747 */
16748 void
16749set_vim_var_string(idx, val, len)
16750 int idx;
16751 char_u *val;
16752 int len; /* length of "val" to use or -1 (whole string) */
16753{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016754 /* Need to do this (at least) once, since we can't initialize a union.
16755 * Will always be invoked when "v:progname" is set. */
16756 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16757
Bram Moolenaare9a41262005-01-15 22:18:47 +000016758 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016760 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016762 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016764 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765}
16766
16767/*
16768 * Set v:register if needed.
16769 */
16770 void
16771set_reg_var(c)
16772 int c;
16773{
16774 char_u regname;
16775
16776 if (c == 0 || c == ' ')
16777 regname = '"';
16778 else
16779 regname = c;
16780 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016781 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016782 set_vim_var_string(VV_REG, &regname, 1);
16783}
16784
16785/*
16786 * Get or set v:exception. If "oldval" == NULL, return the current value.
16787 * Otherwise, restore the value to "oldval" and return NULL.
16788 * Must always be called in pairs to save and restore v:exception! Does not
16789 * take care of memory allocations.
16790 */
16791 char_u *
16792v_exception(oldval)
16793 char_u *oldval;
16794{
16795 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016796 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797
Bram Moolenaare9a41262005-01-15 22:18:47 +000016798 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 return NULL;
16800}
16801
16802/*
16803 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16804 * Otherwise, restore the value to "oldval" and return NULL.
16805 * Must always be called in pairs to save and restore v:throwpoint! Does not
16806 * take care of memory allocations.
16807 */
16808 char_u *
16809v_throwpoint(oldval)
16810 char_u *oldval;
16811{
16812 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016813 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814
Bram Moolenaare9a41262005-01-15 22:18:47 +000016815 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 return NULL;
16817}
16818
16819#if defined(FEAT_AUTOCMD) || defined(PROTO)
16820/*
16821 * Set v:cmdarg.
16822 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16823 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16824 * Must always be called in pairs!
16825 */
16826 char_u *
16827set_cmdarg(eap, oldarg)
16828 exarg_T *eap;
16829 char_u *oldarg;
16830{
16831 char_u *oldval;
16832 char_u *newval;
16833 unsigned len;
16834
Bram Moolenaare9a41262005-01-15 22:18:47 +000016835 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016836 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016838 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016839 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016840 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 }
16842
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016843 if (eap->force_bin == FORCE_BIN)
16844 len = 6;
16845 else if (eap->force_bin == FORCE_NOBIN)
16846 len = 8;
16847 else
16848 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016849
16850 if (eap->read_edit)
16851 len += 7;
16852
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016853 if (eap->force_ff != 0)
16854 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16855# ifdef FEAT_MBYTE
16856 if (eap->force_enc != 0)
16857 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016858 if (eap->bad_char != 0)
16859 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016860# endif
16861
16862 newval = alloc(len + 1);
16863 if (newval == NULL)
16864 return NULL;
16865
16866 if (eap->force_bin == FORCE_BIN)
16867 sprintf((char *)newval, " ++bin");
16868 else if (eap->force_bin == FORCE_NOBIN)
16869 sprintf((char *)newval, " ++nobin");
16870 else
16871 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016872
16873 if (eap->read_edit)
16874 STRCAT(newval, " ++edit");
16875
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016876 if (eap->force_ff != 0)
16877 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16878 eap->cmd + eap->force_ff);
16879# ifdef FEAT_MBYTE
16880 if (eap->force_enc != 0)
16881 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16882 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016883 if (eap->bad_char != 0)
16884 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16885 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016886# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016887 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016888 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889}
16890#endif
16891
16892/*
16893 * Get the value of internal variable "name".
16894 * Return OK or FAIL.
16895 */
16896 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016897get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 char_u *name;
16899 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016901 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902{
16903 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 typval_T *tv = NULL;
16905 typval_T atv;
16906 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908
16909 /* truncate the name, so that we can use strcmp() */
16910 cc = name[len];
16911 name[len] = NUL;
16912
16913 /*
16914 * Check for "b:changedtick".
16915 */
16916 if (STRCMP(name, "b:changedtick") == 0)
16917 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016918 atv.v_type = VAR_NUMBER;
16919 atv.vval.v_number = curbuf->b_changedtick;
16920 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 }
16922
16923 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 * Check for user-defined variables.
16925 */
16926 else
16927 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016928 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016930 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 }
16932
Bram Moolenaare9a41262005-01-15 22:18:47 +000016933 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016935 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016936 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 ret = FAIL;
16938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016939 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016940 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941
16942 name[len] = cc;
16943
16944 return ret;
16945}
16946
16947/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016948 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16949 * Also handle function call with Funcref variable: func(expr)
16950 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16951 */
16952 static int
16953handle_subscript(arg, rettv, evaluate, verbose)
16954 char_u **arg;
16955 typval_T *rettv;
16956 int evaluate; /* do more than finding the end */
16957 int verbose; /* give error messages */
16958{
16959 int ret = OK;
16960 dict_T *selfdict = NULL;
16961 char_u *s;
16962 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016963 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016964
16965 while (ret == OK
16966 && (**arg == '['
16967 || (**arg == '.' && rettv->v_type == VAR_DICT)
16968 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16969 && !vim_iswhite(*(*arg - 1)))
16970 {
16971 if (**arg == '(')
16972 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016973 /* need to copy the funcref so that we can clear rettv */
16974 functv = *rettv;
16975 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016976
16977 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016978 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016979 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016980 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16981 &len, evaluate, selfdict);
16982
16983 /* Clear the funcref afterwards, so that deleting it while
16984 * evaluating the arguments is possible (see test55). */
16985 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016986
16987 /* Stop the expression evaluation when immediately aborting on
16988 * error, or when an interrupt occurred or an exception was thrown
16989 * but not caught. */
16990 if (aborting())
16991 {
16992 if (ret == OK)
16993 clear_tv(rettv);
16994 ret = FAIL;
16995 }
16996 dict_unref(selfdict);
16997 selfdict = NULL;
16998 }
16999 else /* **arg == '[' || **arg == '.' */
17000 {
17001 dict_unref(selfdict);
17002 if (rettv->v_type == VAR_DICT)
17003 {
17004 selfdict = rettv->vval.v_dict;
17005 if (selfdict != NULL)
17006 ++selfdict->dv_refcount;
17007 }
17008 else
17009 selfdict = NULL;
17010 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17011 {
17012 clear_tv(rettv);
17013 ret = FAIL;
17014 }
17015 }
17016 }
17017 dict_unref(selfdict);
17018 return ret;
17019}
17020
17021/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017022 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17023 * value).
17024 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017027{
Bram Moolenaar33570922005-01-25 22:26:29 +000017028 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017029}
17030
17031/*
17032 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 * The string "s" must have been allocated, it is consumed.
17034 * Return NULL for out of memory, the variable otherwise.
17035 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017036 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017037alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038 char_u *s;
17039{
Bram Moolenaar33570922005-01-25 22:26:29 +000017040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017042 rettv = alloc_tv();
17043 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045 rettv->v_type = VAR_STRING;
17046 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 }
17048 else
17049 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051}
17052
17053/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017054 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017056 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017058 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059{
17060 if (varp != NULL)
17061 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017062 switch (varp->v_type)
17063 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017064 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017065 func_unref(varp->vval.v_string);
17066 /*FALLTHROUGH*/
17067 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017068 vim_free(varp->vval.v_string);
17069 break;
17070 case VAR_LIST:
17071 list_unref(varp->vval.v_list);
17072 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017073 case VAR_DICT:
17074 dict_unref(varp->vval.v_dict);
17075 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017076 case VAR_NUMBER:
17077 case VAR_UNKNOWN:
17078 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017079 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017080 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017081 break;
17082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 vim_free(varp);
17084 }
17085}
17086
17087/*
17088 * Free the memory for a variable value and set the value to NULL or 0.
17089 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017090 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017091clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017092 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093{
17094 if (varp != NULL)
17095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017096 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017098 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017099 func_unref(varp->vval.v_string);
17100 /*FALLTHROUGH*/
17101 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017102 vim_free(varp->vval.v_string);
17103 varp->vval.v_string = NULL;
17104 break;
17105 case VAR_LIST:
17106 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017107 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017108 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017109 case VAR_DICT:
17110 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017111 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017112 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017114 varp->vval.v_number = 0;
17115 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116 case VAR_UNKNOWN:
17117 break;
17118 default:
17119 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017121 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 }
17123}
17124
17125/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017126 * Set the value of a variable to NULL without freeing items.
17127 */
17128 static void
17129init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017130 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131{
17132 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017134}
17135
17136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137 * Get the number value of a variable.
17138 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017139 * For incompatible types, return 0.
17140 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17141 * caller of incompatible types: it sets *denote to TRUE if "denote"
17142 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 */
17144 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017145get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017146 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017148 int error = FALSE;
17149
17150 return get_tv_number_chk(varp, &error); /* return 0L on error */
17151}
17152
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017153 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017154get_tv_number_chk(varp, denote)
17155 typval_T *varp;
17156 int *denote;
17157{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017158 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017160 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017162 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017163 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017164 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017165 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017166 break;
17167 case VAR_STRING:
17168 if (varp->vval.v_string != NULL)
17169 vim_str2nr(varp->vval.v_string, NULL, NULL,
17170 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017171 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017172 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017173 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017174 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017175 case VAR_DICT:
17176 EMSG(_("E728: Using a Dictionary as a number"));
17177 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017178 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017179 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 if (denote == NULL) /* useful for values that must be unsigned */
17183 n = -1;
17184 else
17185 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017186 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187}
17188
17189/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017190 * Get the lnum from the first argument.
17191 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017192 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 */
17194 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017196 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197{
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 linenr_T lnum;
17200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017201 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 if (lnum == 0) /* no valid number, try using line() */
17203 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204 rettv.v_type = VAR_NUMBER;
17205 f_line(argvars, &rettv);
17206 lnum = rettv.vval.v_number;
17207 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 }
17209 return lnum;
17210}
17211
17212/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017213 * Get the lnum from the first argument.
17214 * Also accepts "$", then "buf" is used.
17215 * Returns 0 on error.
17216 */
17217 static linenr_T
17218get_tv_lnum_buf(argvars, buf)
17219 typval_T *argvars;
17220 buf_T *buf;
17221{
17222 if (argvars[0].v_type == VAR_STRING
17223 && argvars[0].vval.v_string != NULL
17224 && argvars[0].vval.v_string[0] == '$'
17225 && buf != NULL)
17226 return buf->b_ml.ml_line_count;
17227 return get_tv_number_chk(&argvars[0], NULL);
17228}
17229
17230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 * Get the string value of a variable.
17232 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017233 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17234 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 * If the String variable has never been set, return an empty string.
17236 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017237 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17238 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 */
17240 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017242 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243{
17244 static char_u mybuf[NUMBUFLEN];
17245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017246 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247}
17248
17249 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017250get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017251 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017252 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017254 char_u *res = get_tv_string_buf_chk(varp, buf);
17255
17256 return res != NULL ? res : (char_u *)"";
17257}
17258
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017259 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260get_tv_string_chk(varp)
17261 typval_T *varp;
17262{
17263 static char_u mybuf[NUMBUFLEN];
17264
17265 return get_tv_string_buf_chk(varp, mybuf);
17266}
17267
17268 static char_u *
17269get_tv_string_buf_chk(varp, buf)
17270 typval_T *varp;
17271 char_u *buf;
17272{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017275 case VAR_NUMBER:
17276 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17277 return buf;
17278 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017279 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017280 break;
17281 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017282 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017283 break;
17284 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017285 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017286 break;
17287 case VAR_STRING:
17288 if (varp->vval.v_string != NULL)
17289 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017290 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017292 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017295 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296}
17297
17298/*
17299 * Find variable "name" in the list of variables.
17300 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017301 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017302 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017303 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017306find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017311 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312
Bram Moolenaara7043832005-01-21 11:56:39 +000017313 ht = find_var_ht(name, &varname);
17314 if (htp != NULL)
17315 *htp = ht;
17316 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017318 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319}
17320
17321/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017323 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017325 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017326find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017327 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017328 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017329 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017330{
Bram Moolenaar33570922005-01-25 22:26:29 +000017331 hashitem_T *hi;
17332
17333 if (*varname == NUL)
17334 {
17335 /* Must be something like "s:", otherwise "ht" would be NULL. */
17336 switch (varname[-2])
17337 {
17338 case 's': return &SCRIPT_SV(current_SID).sv_var;
17339 case 'g': return &globvars_var;
17340 case 'v': return &vimvars_var;
17341 case 'b': return &curbuf->b_bufvar;
17342 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017343#ifdef FEAT_WINDOWS
17344 case 't': return &curtab->tp_winvar;
17345#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017346 case 'l': return current_funccal == NULL
17347 ? NULL : &current_funccal->l_vars_var;
17348 case 'a': return current_funccal == NULL
17349 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017350 }
17351 return NULL;
17352 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017353
17354 hi = hash_find(ht, varname);
17355 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017356 {
17357 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017358 * worked find the variable again. Don't auto-load a script if it was
17359 * loaded already, otherwise it would be loaded every time when
17360 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017361 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017362 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017363 hi = hash_find(ht, varname);
17364 if (HASHITEM_EMPTY(hi))
17365 return NULL;
17366 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017368}
17369
17370/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017372 * Set "varname" to the start of name without ':'.
17373 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017374 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017375find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 char_u *name;
17377 char_u **varname;
17378{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017379 hashitem_T *hi;
17380
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 if (name[1] != ':')
17382 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017383 /* The name must not start with a colon or #. */
17384 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 return NULL;
17386 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017387
17388 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017389 hi = hash_find(&compat_hashtab, name);
17390 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017391 return &compat_hashtab;
17392
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 return &globvarht; /* global variable */
17395 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 }
17397 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017398 if (*name == 'g') /* global variable */
17399 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017400 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17401 */
17402 if (vim_strchr(name + 2, ':') != NULL
17403 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017404 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017406 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017408 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017409#ifdef FEAT_WINDOWS
17410 if (*name == 't') /* tab page variable */
17411 return &curtab->tp_vars.dv_hashtab;
17412#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017413 if (*name == 'v') /* v: variable */
17414 return &vimvarht;
17415 if (*name == 'a' && current_funccal != NULL) /* function argument */
17416 return &current_funccal->l_avars.dv_hashtab;
17417 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17418 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 if (*name == 's' /* script variable */
17420 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17421 return &SCRIPT_VARS(current_SID);
17422 return NULL;
17423}
17424
17425/*
17426 * Get the string value of a (global/local) variable.
17427 * Returns NULL when it doesn't exist.
17428 */
17429 char_u *
17430get_var_value(name)
17431 char_u *name;
17432{
Bram Moolenaar33570922005-01-25 22:26:29 +000017433 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434
Bram Moolenaara7043832005-01-21 11:56:39 +000017435 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 if (v == NULL)
17437 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017438 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439}
17440
17441/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017442 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 * sourcing this script and when executing functions defined in the script.
17444 */
17445 void
17446new_script_vars(id)
17447 scid_T id;
17448{
Bram Moolenaara7043832005-01-21 11:56:39 +000017449 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017450 hashtab_T *ht;
17451 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017452
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17454 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017455 /* Re-allocating ga_data means that an ht_array pointing to
17456 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017457 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017458 for (i = 1; i <= ga_scripts.ga_len; ++i)
17459 {
17460 ht = &SCRIPT_VARS(i);
17461 if (ht->ht_mask == HT_INIT_SIZE - 1)
17462 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017463 sv = &SCRIPT_SV(i);
17464 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017465 }
17466
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 while (ga_scripts.ga_len < id)
17468 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017469 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17470 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 }
17473 }
17474}
17475
17476/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017477 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17478 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 */
17480 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017481init_var_dict(dict, dict_var)
17482 dict_T *dict;
17483 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484{
Bram Moolenaar33570922005-01-25 22:26:29 +000017485 hash_init(&dict->dv_hashtab);
17486 dict->dv_refcount = 99999;
17487 dict_var->di_tv.vval.v_dict = dict;
17488 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017489 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017490 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17491 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492}
17493
17494/*
17495 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 * Frees all allocated variables and the value they contain.
17497 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 */
17499 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017500vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 hashtab_T *ht;
17502{
17503 vars_clear_ext(ht, TRUE);
17504}
17505
17506/*
17507 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17508 */
17509 static void
17510vars_clear_ext(ht, free_val)
17511 hashtab_T *ht;
17512 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513{
Bram Moolenaara7043832005-01-21 11:56:39 +000017514 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 hashitem_T *hi;
17516 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517
Bram Moolenaar33570922005-01-25 22:26:29 +000017518 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017519 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017520 for (hi = ht->ht_array; todo > 0; ++hi)
17521 {
17522 if (!HASHITEM_EMPTY(hi))
17523 {
17524 --todo;
17525
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017527 * ht_array might change then. hash_clear() takes care of it
17528 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017529 v = HI2DI(hi);
17530 if (free_val)
17531 clear_tv(&v->di_tv);
17532 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17533 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017534 }
17535 }
17536 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017537 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538}
17539
Bram Moolenaara7043832005-01-21 11:56:39 +000017540/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017541 * Delete a variable from hashtab "ht" at item "hi".
17542 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017545delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 hashtab_T *ht;
17547 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548{
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017550
17551 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017552 clear_tv(&di->di_tv);
17553 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554}
17555
17556/*
17557 * List the value of one internal variable.
17558 */
17559 static void
17560list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 char_u *prefix;
17563{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017564 char_u *tofree;
17565 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017566 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017567
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017568 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017569 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017570 s == NULL ? (char_u *)"" : s);
17571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572}
17573
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574 static void
17575list_one_var_a(prefix, name, type, string)
17576 char_u *prefix;
17577 char_u *name;
17578 int type;
17579 char_u *string;
17580{
17581 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17582 if (name != NULL) /* "a:" vars don't have a name stored */
17583 msg_puts(name);
17584 msg_putchar(' ');
17585 msg_advance(22);
17586 if (type == VAR_NUMBER)
17587 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017588 else if (type == VAR_FUNC)
17589 msg_putchar('*');
17590 else if (type == VAR_LIST)
17591 {
17592 msg_putchar('[');
17593 if (*string == '[')
17594 ++string;
17595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017596 else if (type == VAR_DICT)
17597 {
17598 msg_putchar('{');
17599 if (*string == '{')
17600 ++string;
17601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 else
17603 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017604
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017606
17607 if (type == VAR_FUNC)
17608 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609}
17610
17611/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017612 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 * If the variable already exists, the value is updated.
17614 * Otherwise the variable is created.
17615 */
17616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017619 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017620 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621{
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017625 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017628 {
17629 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17630 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17631 ? name[2] : name[0]))
17632 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017633 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017634 return;
17635 }
17636 if (function_exists(name))
17637 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017638 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017639 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017640 return;
17641 }
17642 }
17643
Bram Moolenaara7043832005-01-21 11:56:39 +000017644 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017645 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017646 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017647 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017648 return;
17649 }
17650
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017651 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017652 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017654 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017655 if (var_check_ro(v->di_flags, name)
17656 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017657 return;
17658 if (v->di_tv.v_type != tv->v_type
17659 && !((v->di_tv.v_type == VAR_STRING
17660 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 && (tv->v_type == VAR_STRING
17662 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017663 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017664 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017665 return;
17666 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017667
17668 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017669 * Handle setting internal v: variables separately: we don't change
17670 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017671 */
17672 if (ht == &vimvarht)
17673 {
17674 if (v->di_tv.v_type == VAR_STRING)
17675 {
17676 vim_free(v->di_tv.vval.v_string);
17677 if (copy || tv->v_type != VAR_STRING)
17678 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17679 else
17680 {
17681 /* Take over the string to avoid an extra alloc/free. */
17682 v->di_tv.vval.v_string = tv->vval.v_string;
17683 tv->vval.v_string = NULL;
17684 }
17685 }
17686 else if (v->di_tv.v_type != VAR_NUMBER)
17687 EMSG2(_(e_intern2), "set_var()");
17688 else
17689 v->di_tv.vval.v_number = get_tv_number(tv);
17690 return;
17691 }
17692
17693 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 }
17695 else /* add a new variable */
17696 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017697 /* Make sure the variable name is valid. */
17698 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017699 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17700 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017701 {
17702 EMSG2(_(e_illvar), varname);
17703 return;
17704 }
17705
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017706 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17707 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017708 if (v == NULL)
17709 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017713 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 return;
17715 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017716 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017720 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017721 else
17722 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017723 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017724 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017725 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727}
17728
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017729/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017730 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17731 * Also give an error message.
17732 */
17733 static int
17734var_check_ro(flags, name)
17735 int flags;
17736 char_u *name;
17737{
17738 if (flags & DI_FLAGS_RO)
17739 {
17740 EMSG2(_(e_readonlyvar), name);
17741 return TRUE;
17742 }
17743 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17744 {
17745 EMSG2(_(e_readonlysbx), name);
17746 return TRUE;
17747 }
17748 return FALSE;
17749}
17750
17751/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017752 * Return TRUE if typeval "tv" is set to be locked (immutable).
17753 * Also give an error message, using "name".
17754 */
17755 static int
17756tv_check_lock(lock, name)
17757 int lock;
17758 char_u *name;
17759{
17760 if (lock & VAR_LOCKED)
17761 {
17762 EMSG2(_("E741: Value is locked: %s"),
17763 name == NULL ? (char_u *)_("Unknown") : name);
17764 return TRUE;
17765 }
17766 if (lock & VAR_FIXED)
17767 {
17768 EMSG2(_("E742: Cannot change value of %s"),
17769 name == NULL ? (char_u *)_("Unknown") : name);
17770 return TRUE;
17771 }
17772 return FALSE;
17773}
17774
17775/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017776 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017777 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017778 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017781copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017782 typval_T *from;
17783 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017785 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017786 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017787 switch (from->v_type)
17788 {
17789 case VAR_NUMBER:
17790 to->vval.v_number = from->vval.v_number;
17791 break;
17792 case VAR_STRING:
17793 case VAR_FUNC:
17794 if (from->vval.v_string == NULL)
17795 to->vval.v_string = NULL;
17796 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017797 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017798 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017799 if (from->v_type == VAR_FUNC)
17800 func_ref(to->vval.v_string);
17801 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017802 break;
17803 case VAR_LIST:
17804 if (from->vval.v_list == NULL)
17805 to->vval.v_list = NULL;
17806 else
17807 {
17808 to->vval.v_list = from->vval.v_list;
17809 ++to->vval.v_list->lv_refcount;
17810 }
17811 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017812 case VAR_DICT:
17813 if (from->vval.v_dict == NULL)
17814 to->vval.v_dict = NULL;
17815 else
17816 {
17817 to->vval.v_dict = from->vval.v_dict;
17818 ++to->vval.v_dict->dv_refcount;
17819 }
17820 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017821 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017822 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017823 break;
17824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825}
17826
17827/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017828 * Make a copy of an item.
17829 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017830 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17831 * reference to an already copied list/dict can be used.
17832 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017833 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017834 static int
17835item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017836 typval_T *from;
17837 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017838 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017839 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017840{
17841 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017842 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017843
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017845 {
17846 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017847 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017848 }
17849 ++recurse;
17850
17851 switch (from->v_type)
17852 {
17853 case VAR_NUMBER:
17854 case VAR_STRING:
17855 case VAR_FUNC:
17856 copy_tv(from, to);
17857 break;
17858 case VAR_LIST:
17859 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017860 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017861 if (from->vval.v_list == NULL)
17862 to->vval.v_list = NULL;
17863 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17864 {
17865 /* use the copy made earlier */
17866 to->vval.v_list = from->vval.v_list->lv_copylist;
17867 ++to->vval.v_list->lv_refcount;
17868 }
17869 else
17870 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17871 if (to->vval.v_list == NULL)
17872 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017873 break;
17874 case VAR_DICT:
17875 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017876 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017877 if (from->vval.v_dict == NULL)
17878 to->vval.v_dict = NULL;
17879 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17880 {
17881 /* use the copy made earlier */
17882 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17883 ++to->vval.v_dict->dv_refcount;
17884 }
17885 else
17886 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17887 if (to->vval.v_dict == NULL)
17888 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017889 break;
17890 default:
17891 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017892 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017893 }
17894 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017895 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017896}
17897
17898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 * ":echo expr1 ..." print each argument separated with a space, add a
17900 * newline at the end.
17901 * ":echon expr1 ..." print each argument plain.
17902 */
17903 void
17904ex_echo(eap)
17905 exarg_T *eap;
17906{
17907 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017908 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017909 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 char_u *p;
17911 int needclr = TRUE;
17912 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017913 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914
17915 if (eap->skip)
17916 ++emsg_skip;
17917 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17918 {
17919 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017920 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 {
17922 /*
17923 * Report the invalid expression unless the expression evaluation
17924 * has been cancelled due to an aborting error, an interrupt, or an
17925 * exception.
17926 */
17927 if (!aborting())
17928 EMSG2(_(e_invexpr2), p);
17929 break;
17930 }
17931 if (!eap->skip)
17932 {
17933 if (atstart)
17934 {
17935 atstart = FALSE;
17936 /* Call msg_start() after eval1(), evaluating the expression
17937 * may cause a message to appear. */
17938 if (eap->cmdidx == CMD_echo)
17939 msg_start();
17940 }
17941 else if (eap->cmdidx == CMD_echo)
17942 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017943 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017944 if (p != NULL)
17945 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017947 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017949 if (*p != TAB && needclr)
17950 {
17951 /* remove any text still there from the command */
17952 msg_clr_eos();
17953 needclr = FALSE;
17954 }
17955 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 }
17957 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017958 {
17959#ifdef FEAT_MBYTE
17960 if (has_mbyte)
17961 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017962 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017963
17964 (void)msg_outtrans_len_attr(p, i, echo_attr);
17965 p += i - 1;
17966 }
17967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017969 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017972 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017974 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 arg = skipwhite(arg);
17976 }
17977 eap->nextcmd = check_nextcmd(arg);
17978
17979 if (eap->skip)
17980 --emsg_skip;
17981 else
17982 {
17983 /* remove text that may still be there from the command */
17984 if (needclr)
17985 msg_clr_eos();
17986 if (eap->cmdidx == CMD_echo)
17987 msg_end();
17988 }
17989}
17990
17991/*
17992 * ":echohl {name}".
17993 */
17994 void
17995ex_echohl(eap)
17996 exarg_T *eap;
17997{
17998 int id;
17999
18000 id = syn_name2id(eap->arg);
18001 if (id == 0)
18002 echo_attr = 0;
18003 else
18004 echo_attr = syn_id2attr(id);
18005}
18006
18007/*
18008 * ":execute expr1 ..." execute the result of an expression.
18009 * ":echomsg expr1 ..." Print a message
18010 * ":echoerr expr1 ..." Print an error
18011 * Each gets spaces around each argument and a newline at the end for
18012 * echo commands
18013 */
18014 void
18015ex_execute(eap)
18016 exarg_T *eap;
18017{
18018 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018019 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 int ret = OK;
18021 char_u *p;
18022 garray_T ga;
18023 int len;
18024 int save_did_emsg;
18025
18026 ga_init2(&ga, 1, 80);
18027
18028 if (eap->skip)
18029 ++emsg_skip;
18030 while (*arg != NUL && *arg != '|' && *arg != '\n')
18031 {
18032 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018033 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 {
18035 /*
18036 * Report the invalid expression unless the expression evaluation
18037 * has been cancelled due to an aborting error, an interrupt, or an
18038 * exception.
18039 */
18040 if (!aborting())
18041 EMSG2(_(e_invexpr2), p);
18042 ret = FAIL;
18043 break;
18044 }
18045
18046 if (!eap->skip)
18047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018048 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 len = (int)STRLEN(p);
18050 if (ga_grow(&ga, len + 2) == FAIL)
18051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018052 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 ret = FAIL;
18054 break;
18055 }
18056 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 ga.ga_len += len;
18060 }
18061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018062 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 arg = skipwhite(arg);
18064 }
18065
18066 if (ret != FAIL && ga.ga_data != NULL)
18067 {
18068 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018071 out_flush();
18072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 else if (eap->cmdidx == CMD_echoerr)
18074 {
18075 /* We don't want to abort following commands, restore did_emsg. */
18076 save_did_emsg = did_emsg;
18077 EMSG((char_u *)ga.ga_data);
18078 if (!force_abort)
18079 did_emsg = save_did_emsg;
18080 }
18081 else if (eap->cmdidx == CMD_execute)
18082 do_cmdline((char_u *)ga.ga_data,
18083 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18084 }
18085
18086 ga_clear(&ga);
18087
18088 if (eap->skip)
18089 --emsg_skip;
18090
18091 eap->nextcmd = check_nextcmd(arg);
18092}
18093
18094/*
18095 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18096 * "arg" points to the "&" or '+' when called, to "option" when returning.
18097 * Returns NULL when no option name found. Otherwise pointer to the char
18098 * after the option name.
18099 */
18100 static char_u *
18101find_option_end(arg, opt_flags)
18102 char_u **arg;
18103 int *opt_flags;
18104{
18105 char_u *p = *arg;
18106
18107 ++p;
18108 if (*p == 'g' && p[1] == ':')
18109 {
18110 *opt_flags = OPT_GLOBAL;
18111 p += 2;
18112 }
18113 else if (*p == 'l' && p[1] == ':')
18114 {
18115 *opt_flags = OPT_LOCAL;
18116 p += 2;
18117 }
18118 else
18119 *opt_flags = 0;
18120
18121 if (!ASCII_ISALPHA(*p))
18122 return NULL;
18123 *arg = p;
18124
18125 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18126 p += 4; /* termcap option */
18127 else
18128 while (ASCII_ISALPHA(*p))
18129 ++p;
18130 return p;
18131}
18132
18133/*
18134 * ":function"
18135 */
18136 void
18137ex_function(eap)
18138 exarg_T *eap;
18139{
18140 char_u *theline;
18141 int j;
18142 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 char_u *name = NULL;
18145 char_u *p;
18146 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018147 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 garray_T newargs;
18149 garray_T newlines;
18150 int varargs = FALSE;
18151 int mustend = FALSE;
18152 int flags = 0;
18153 ufunc_T *fp;
18154 int indent;
18155 int nesting;
18156 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018157 dictitem_T *v;
18158 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018159 static int func_nr = 0; /* number for nameless function */
18160 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018161 hashtab_T *ht;
18162 int todo;
18163 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018164 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165
18166 /*
18167 * ":function" without argument: list functions.
18168 */
18169 if (ends_excmd(*eap->arg))
18170 {
18171 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018172 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018173 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018174 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018175 {
18176 if (!HASHITEM_EMPTY(hi))
18177 {
18178 --todo;
18179 fp = HI2UF(hi);
18180 if (!isdigit(*fp->uf_name))
18181 list_func_head(fp, FALSE);
18182 }
18183 }
18184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185 eap->nextcmd = check_nextcmd(eap->arg);
18186 return;
18187 }
18188
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018189 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018190 * ":function /pat": list functions matching pattern.
18191 */
18192 if (*eap->arg == '/')
18193 {
18194 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18195 if (!eap->skip)
18196 {
18197 regmatch_T regmatch;
18198
18199 c = *p;
18200 *p = NUL;
18201 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18202 *p = c;
18203 if (regmatch.regprog != NULL)
18204 {
18205 regmatch.rm_ic = p_ic;
18206
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018207 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018208 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18209 {
18210 if (!HASHITEM_EMPTY(hi))
18211 {
18212 --todo;
18213 fp = HI2UF(hi);
18214 if (!isdigit(*fp->uf_name)
18215 && vim_regexec(&regmatch, fp->uf_name, 0))
18216 list_func_head(fp, FALSE);
18217 }
18218 }
18219 }
18220 }
18221 if (*p == '/')
18222 ++p;
18223 eap->nextcmd = check_nextcmd(p);
18224 return;
18225 }
18226
18227 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018228 * Get the function name. There are these situations:
18229 * func normal function name
18230 * "name" == func, "fudi.fd_dict" == NULL
18231 * dict.func new dictionary entry
18232 * "name" == NULL, "fudi.fd_dict" set,
18233 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18234 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018235 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018236 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18237 * dict.func existing dict entry that's not a Funcref
18238 * "name" == NULL, "fudi.fd_dict" set,
18239 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018242 name = trans_function_name(&p, eap->skip, 0, &fudi);
18243 paren = (vim_strchr(p, '(') != NULL);
18244 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 {
18246 /*
18247 * Return on an invalid expression in braces, unless the expression
18248 * evaluation has been cancelled due to an aborting error, an
18249 * interrupt, or an exception.
18250 */
18251 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018253 if (!eap->skip && fudi.fd_newkey != NULL)
18254 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018255 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 else
18259 eap->skip = TRUE;
18260 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018261
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 /* An error in a function call during evaluation of an expression in magic
18263 * braces should not cause the function not to be defined. */
18264 saved_did_emsg = did_emsg;
18265 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266
18267 /*
18268 * ":function func" with only function name: list function.
18269 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018270 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 {
18272 if (!ends_excmd(*skipwhite(p)))
18273 {
18274 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018275 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 }
18277 eap->nextcmd = check_nextcmd(p);
18278 if (eap->nextcmd != NULL)
18279 *p = NUL;
18280 if (!eap->skip && !got_int)
18281 {
18282 fp = find_func(name);
18283 if (fp != NULL)
18284 {
18285 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018286 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018288 if (FUNCLINE(fp, j) == NULL)
18289 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 msg_putchar('\n');
18291 msg_outnum((long)(j + 1));
18292 if (j < 9)
18293 msg_putchar(' ');
18294 if (j < 99)
18295 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018296 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 out_flush(); /* show a line at a time */
18298 ui_breakcheck();
18299 }
18300 if (!got_int)
18301 {
18302 msg_putchar('\n');
18303 msg_puts((char_u *)" endfunction");
18304 }
18305 }
18306 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018307 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018309 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 }
18311
18312 /*
18313 * ":function name(arg1, arg2)" Define function.
18314 */
18315 p = skipwhite(p);
18316 if (*p != '(')
18317 {
18318 if (!eap->skip)
18319 {
18320 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018321 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 }
18323 /* attempt to continue by skipping some text */
18324 if (vim_strchr(p, '(') != NULL)
18325 p = vim_strchr(p, '(');
18326 }
18327 p = skipwhite(p + 1);
18328
18329 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18330 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18331
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018332 if (!eap->skip)
18333 {
18334 /* Check the name of the function. */
18335 if (name != NULL)
18336 arg = name;
18337 else
18338 arg = fudi.fd_newkey;
18339 if (arg != NULL)
18340 {
18341 if (*arg == K_SPECIAL)
18342 j = 3;
18343 else
18344 j = 0;
18345 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18346 : eval_isnamec(arg[j])))
18347 ++j;
18348 if (arg[j] != NUL)
18349 emsg_funcname(_(e_invarg2), arg);
18350 }
18351 }
18352
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 /*
18354 * Isolate the arguments: "arg1, arg2, ...)"
18355 */
18356 while (*p != ')')
18357 {
18358 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18359 {
18360 varargs = TRUE;
18361 p += 3;
18362 mustend = TRUE;
18363 }
18364 else
18365 {
18366 arg = p;
18367 while (ASCII_ISALNUM(*p) || *p == '_')
18368 ++p;
18369 if (arg == p || isdigit(*arg)
18370 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18371 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18372 {
18373 if (!eap->skip)
18374 EMSG2(_("E125: Illegal argument: %s"), arg);
18375 break;
18376 }
18377 if (ga_grow(&newargs, 1) == FAIL)
18378 goto erret;
18379 c = *p;
18380 *p = NUL;
18381 arg = vim_strsave(arg);
18382 if (arg == NULL)
18383 goto erret;
18384 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18385 *p = c;
18386 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 if (*p == ',')
18388 ++p;
18389 else
18390 mustend = TRUE;
18391 }
18392 p = skipwhite(p);
18393 if (mustend && *p != ')')
18394 {
18395 if (!eap->skip)
18396 EMSG2(_(e_invarg2), eap->arg);
18397 break;
18398 }
18399 }
18400 ++p; /* skip the ')' */
18401
Bram Moolenaare9a41262005-01-15 22:18:47 +000018402 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 for (;;)
18404 {
18405 p = skipwhite(p);
18406 if (STRNCMP(p, "range", 5) == 0)
18407 {
18408 flags |= FC_RANGE;
18409 p += 5;
18410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018411 else if (STRNCMP(p, "dict", 4) == 0)
18412 {
18413 flags |= FC_DICT;
18414 p += 4;
18415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 else if (STRNCMP(p, "abort", 5) == 0)
18417 {
18418 flags |= FC_ABORT;
18419 p += 5;
18420 }
18421 else
18422 break;
18423 }
18424
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018425 /* When there is a line break use what follows for the function body.
18426 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18427 if (*p == '\n')
18428 line_arg = p + 1;
18429 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 EMSG(_(e_trailing));
18431
18432 /*
18433 * Read the body of the function, until ":endfunction" is found.
18434 */
18435 if (KeyTyped)
18436 {
18437 /* Check if the function already exists, don't let the user type the
18438 * whole function before telling him it doesn't work! For a script we
18439 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018440 if (!eap->skip && !eap->forceit)
18441 {
18442 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18443 EMSG(_(e_funcdict));
18444 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018445 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018448 if (!eap->skip && did_emsg)
18449 goto erret;
18450
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 msg_putchar('\n'); /* don't overwrite the function name */
18452 cmdline_row = msg_row;
18453 }
18454
18455 indent = 2;
18456 nesting = 0;
18457 for (;;)
18458 {
18459 msg_scroll = TRUE;
18460 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018461 sourcing_lnum_off = sourcing_lnum;
18462
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018463 if (line_arg != NULL)
18464 {
18465 /* Use eap->arg, split up in parts by line breaks. */
18466 theline = line_arg;
18467 p = vim_strchr(theline, '\n');
18468 if (p == NULL)
18469 line_arg += STRLEN(line_arg);
18470 else
18471 {
18472 *p = NUL;
18473 line_arg = p + 1;
18474 }
18475 }
18476 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 theline = getcmdline(':', 0L, indent);
18478 else
18479 theline = eap->getline(':', eap->cookie, indent);
18480 if (KeyTyped)
18481 lines_left = Rows - 1;
18482 if (theline == NULL)
18483 {
18484 EMSG(_("E126: Missing :endfunction"));
18485 goto erret;
18486 }
18487
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018488 /* Detect line continuation: sourcing_lnum increased more than one. */
18489 if (sourcing_lnum > sourcing_lnum_off + 1)
18490 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18491 else
18492 sourcing_lnum_off = 0;
18493
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 if (skip_until != NULL)
18495 {
18496 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18497 * don't check for ":endfunc". */
18498 if (STRCMP(theline, skip_until) == 0)
18499 {
18500 vim_free(skip_until);
18501 skip_until = NULL;
18502 }
18503 }
18504 else
18505 {
18506 /* skip ':' and blanks*/
18507 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18508 ;
18509
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018510 /* Check for "endfunction". */
18511 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018513 if (line_arg == NULL)
18514 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 break;
18516 }
18517
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018518 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 * at "end". */
18520 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18521 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018522 else if (STRNCMP(p, "if", 2) == 0
18523 || STRNCMP(p, "wh", 2) == 0
18524 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 || STRNCMP(p, "try", 3) == 0)
18526 indent += 2;
18527
18528 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018529 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018531 if (*p == '!')
18532 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 p += eval_fname_script(p);
18534 if (ASCII_ISALPHA(*p))
18535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018536 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 if (*skipwhite(p) == '(')
18538 {
18539 ++nesting;
18540 indent += 2;
18541 }
18542 }
18543 }
18544
18545 /* Check for ":append" or ":insert". */
18546 p = skip_range(p, NULL);
18547 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18548 || (p[0] == 'i'
18549 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18550 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18551 skip_until = vim_strsave((char_u *)".");
18552
18553 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18554 arg = skipwhite(skiptowhite(p));
18555 if (arg[0] == '<' && arg[1] =='<'
18556 && ((p[0] == 'p' && p[1] == 'y'
18557 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18558 || (p[0] == 'p' && p[1] == 'e'
18559 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18560 || (p[0] == 't' && p[1] == 'c'
18561 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18562 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18563 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018564 || (p[0] == 'm' && p[1] == 'z'
18565 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 ))
18567 {
18568 /* ":python <<" continues until a dot, like ":append" */
18569 p = skipwhite(arg + 2);
18570 if (*p == NUL)
18571 skip_until = vim_strsave((char_u *)".");
18572 else
18573 skip_until = vim_strsave(p);
18574 }
18575 }
18576
18577 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018578 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018579 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018580 if (line_arg == NULL)
18581 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018583 }
18584
18585 /* Copy the line to newly allocated memory. get_one_sourceline()
18586 * allocates 250 bytes per line, this saves 80% on average. The cost
18587 * is an extra alloc/free. */
18588 p = vim_strsave(theline);
18589 if (p != NULL)
18590 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018591 if (line_arg == NULL)
18592 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018593 theline = p;
18594 }
18595
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018596 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18597
18598 /* Add NULL lines for continuation lines, so that the line count is
18599 * equal to the index in the growarray. */
18600 while (sourcing_lnum_off-- > 0)
18601 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018602
18603 /* Check for end of eap->arg. */
18604 if (line_arg != NULL && *line_arg == NUL)
18605 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 }
18607
18608 /* Don't define the function when skipping commands or when an error was
18609 * detected. */
18610 if (eap->skip || did_emsg)
18611 goto erret;
18612
18613 /*
18614 * If there are no errors, add the function
18615 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018616 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018617 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018618 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018620 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018621 emsg_funcname("E707: Function name conflicts with variable: %s",
18622 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018623 goto erret;
18624 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018625
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018626 fp = find_func(name);
18627 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018629 if (!eap->forceit)
18630 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018631 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018632 goto erret;
18633 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018634 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018635 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018636 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018637 name);
18638 goto erret;
18639 }
18640 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018641 ga_clear_strings(&(fp->uf_args));
18642 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018643 vim_free(name);
18644 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 }
18647 else
18648 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018649 char numbuf[20];
18650
18651 fp = NULL;
18652 if (fudi.fd_newkey == NULL && !eap->forceit)
18653 {
18654 EMSG(_(e_funcdict));
18655 goto erret;
18656 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018657 if (fudi.fd_di == NULL)
18658 {
18659 /* Can't add a function to a locked dictionary */
18660 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18661 goto erret;
18662 }
18663 /* Can't change an existing function if it is locked */
18664 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18665 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018666
18667 /* Give the function a sequential number. Can only be used with a
18668 * Funcref! */
18669 vim_free(name);
18670 sprintf(numbuf, "%d", ++func_nr);
18671 name = vim_strsave((char_u *)numbuf);
18672 if (name == NULL)
18673 goto erret;
18674 }
18675
18676 if (fp == NULL)
18677 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018678 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018679 {
18680 int slen, plen;
18681 char_u *scriptname;
18682
18683 /* Check that the autoload name matches the script name. */
18684 j = FAIL;
18685 if (sourcing_name != NULL)
18686 {
18687 scriptname = autoload_name(name);
18688 if (scriptname != NULL)
18689 {
18690 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018691 plen = (int)STRLEN(p);
18692 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018693 if (slen > plen && fnamecmp(p,
18694 sourcing_name + slen - plen) == 0)
18695 j = OK;
18696 vim_free(scriptname);
18697 }
18698 }
18699 if (j == FAIL)
18700 {
18701 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18702 goto erret;
18703 }
18704 }
18705
18706 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707 if (fp == NULL)
18708 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018709
18710 if (fudi.fd_dict != NULL)
18711 {
18712 if (fudi.fd_di == NULL)
18713 {
18714 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018715 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018716 if (fudi.fd_di == NULL)
18717 {
18718 vim_free(fp);
18719 goto erret;
18720 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018721 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18722 {
18723 vim_free(fudi.fd_di);
18724 goto erret;
18725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018726 }
18727 else
18728 /* overwrite existing dict entry */
18729 clear_tv(&fudi.fd_di->di_tv);
18730 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018731 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018732 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018733 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018734
18735 /* behave like "dict" was used */
18736 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018737 }
18738
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018740 STRCPY(fp->uf_name, name);
18741 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018743 fp->uf_args = newargs;
18744 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018745#ifdef FEAT_PROFILE
18746 fp->uf_tml_count = NULL;
18747 fp->uf_tml_total = NULL;
18748 fp->uf_tml_self = NULL;
18749 fp->uf_profiling = FALSE;
18750 if (prof_def_func())
18751 func_do_profile(fp);
18752#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018753 fp->uf_varargs = varargs;
18754 fp->uf_flags = flags;
18755 fp->uf_calls = 0;
18756 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018757 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758
18759erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 ga_clear_strings(&newargs);
18761 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018762ret_free:
18763 vim_free(skip_until);
18764 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767}
18768
18769/*
18770 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018771 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018773 * flags:
18774 * TFN_INT: internal function name OK
18775 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 * Advances "pp" to just after the function name (if no error).
18777 */
18778 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018779trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 char_u **pp;
18781 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018782 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018783 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018785 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 char_u *start;
18787 char_u *end;
18788 int lead;
18789 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018792
18793 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018794 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018796
18797 /* Check for hard coded <SNR>: already translated function ID (from a user
18798 * command). */
18799 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18800 && (*pp)[2] == (int)KE_SNR)
18801 {
18802 *pp += 3;
18803 len = get_id_len(pp) + 3;
18804 return vim_strnsave(start, len);
18805 }
18806
18807 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18808 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018810 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018812
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018813 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18814 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018815 if (end == start)
18816 {
18817 if (!skip)
18818 EMSG(_("E129: Function name required"));
18819 goto theend;
18820 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018821 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018822 {
18823 /*
18824 * Report an invalid expression in braces, unless the expression
18825 * evaluation has been cancelled due to an aborting error, an
18826 * interrupt, or an exception.
18827 */
18828 if (!aborting())
18829 {
18830 if (end != NULL)
18831 EMSG2(_(e_invarg2), start);
18832 }
18833 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018834 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018835 goto theend;
18836 }
18837
18838 if (lv.ll_tv != NULL)
18839 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018840 if (fdp != NULL)
18841 {
18842 fdp->fd_dict = lv.ll_dict;
18843 fdp->fd_newkey = lv.ll_newkey;
18844 lv.ll_newkey = NULL;
18845 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018846 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018847 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18848 {
18849 name = vim_strsave(lv.ll_tv->vval.v_string);
18850 *pp = end;
18851 }
18852 else
18853 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018854 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18855 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018856 EMSG(_(e_funcref));
18857 else
18858 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018859 name = NULL;
18860 }
18861 goto theend;
18862 }
18863
18864 if (lv.ll_name == NULL)
18865 {
18866 /* Error found, but continue after the function name. */
18867 *pp = end;
18868 goto theend;
18869 }
18870
18871 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018872 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018873 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018874 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18875 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18876 {
18877 /* When there was "s:" already or the name expanded to get a
18878 * leading "s:" then remove it. */
18879 lv.ll_name += 2;
18880 len -= 2;
18881 lead = 2;
18882 }
18883 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018884 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018885 {
18886 if (lead == 2) /* skip over "s:" */
18887 lv.ll_name += 2;
18888 len = (int)(end - lv.ll_name);
18889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018890
18891 /*
18892 * Copy the function name to allocated memory.
18893 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18894 * Accept <SNR>123_name() outside a script.
18895 */
18896 if (skip)
18897 lead = 0; /* do nothing */
18898 else if (lead > 0)
18899 {
18900 lead = 3;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018901 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018902 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018903 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018904 if (current_SID <= 0)
18905 {
18906 EMSG(_(e_usingsid));
18907 goto theend;
18908 }
18909 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18910 lead += (int)STRLEN(sid_buf);
18911 }
18912 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018913 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018914 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018915 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018916 goto theend;
18917 }
18918 name = alloc((unsigned)(len + lead + 1));
18919 if (name != NULL)
18920 {
18921 if (lead > 0)
18922 {
18923 name[0] = K_SPECIAL;
18924 name[1] = KS_EXTRA;
18925 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018926 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018927 STRCPY(name + 3, sid_buf);
18928 }
18929 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18930 name[len + lead] = NUL;
18931 }
18932 *pp = end;
18933
18934theend:
18935 clear_lval(&lv);
18936 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937}
18938
18939/*
18940 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18941 * Return 2 if "p" starts with "s:".
18942 * Return 0 otherwise.
18943 */
18944 static int
18945eval_fname_script(p)
18946 char_u *p;
18947{
18948 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18949 || STRNICMP(p + 1, "SNR>", 4) == 0))
18950 return 5;
18951 if (p[0] == 's' && p[1] == ':')
18952 return 2;
18953 return 0;
18954}
18955
18956/*
18957 * Return TRUE if "p" starts with "<SID>" or "s:".
18958 * Only works if eval_fname_script() returned non-zero for "p"!
18959 */
18960 static int
18961eval_fname_sid(p)
18962 char_u *p;
18963{
18964 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18965}
18966
18967/*
18968 * List the head of the function: "name(arg1, arg2)".
18969 */
18970 static void
18971list_func_head(fp, indent)
18972 ufunc_T *fp;
18973 int indent;
18974{
18975 int j;
18976
18977 msg_start();
18978 if (indent)
18979 MSG_PUTS(" ");
18980 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018981 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 {
18983 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018984 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 }
18986 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018987 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018989 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 {
18991 if (j)
18992 MSG_PUTS(", ");
18993 msg_puts(FUNCARG(fp, j));
18994 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018995 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 {
18997 if (j)
18998 MSG_PUTS(", ");
18999 MSG_PUTS("...");
19000 }
19001 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019002 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019003 if (p_verbose > 0)
19004 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005}
19006
19007/*
19008 * Find a function by name, return pointer to it in ufuncs.
19009 * Return NULL for unknown function.
19010 */
19011 static ufunc_T *
19012find_func(name)
19013 char_u *name;
19014{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019015 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019017 hi = hash_find(&func_hashtab, name);
19018 if (!HASHITEM_EMPTY(hi))
19019 return HI2UF(hi);
19020 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021}
19022
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019023#if defined(EXITFREE) || defined(PROTO)
19024 void
19025free_all_functions()
19026{
19027 hashitem_T *hi;
19028
19029 /* Need to start all over every time, because func_free() may change the
19030 * hash table. */
19031 while (func_hashtab.ht_used > 0)
19032 for (hi = func_hashtab.ht_array; ; ++hi)
19033 if (!HASHITEM_EMPTY(hi))
19034 {
19035 func_free(HI2UF(hi));
19036 break;
19037 }
19038}
19039#endif
19040
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019041/*
19042 * Return TRUE if a function "name" exists.
19043 */
19044 static int
19045function_exists(name)
19046 char_u *name;
19047{
19048 char_u *p = name;
19049 int n = FALSE;
19050
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019051 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019052 if (p != NULL)
19053 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019054 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019055 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019056 else
19057 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019058 vim_free(p);
19059 }
19060 return n;
19061}
19062
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019063/*
19064 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019065 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019066 */
19067 static int
19068builtin_function(name)
19069 char_u *name;
19070{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019071 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19072 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019073}
19074
Bram Moolenaar05159a02005-02-26 23:04:13 +000019075#if defined(FEAT_PROFILE) || defined(PROTO)
19076/*
19077 * Start profiling function "fp".
19078 */
19079 static void
19080func_do_profile(fp)
19081 ufunc_T *fp;
19082{
19083 fp->uf_tm_count = 0;
19084 profile_zero(&fp->uf_tm_self);
19085 profile_zero(&fp->uf_tm_total);
19086 if (fp->uf_tml_count == NULL)
19087 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19088 (sizeof(int) * fp->uf_lines.ga_len));
19089 if (fp->uf_tml_total == NULL)
19090 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19091 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19092 if (fp->uf_tml_self == NULL)
19093 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19094 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19095 fp->uf_tml_idx = -1;
19096 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19097 || fp->uf_tml_self == NULL)
19098 return; /* out of memory */
19099
19100 fp->uf_profiling = TRUE;
19101}
19102
19103/*
19104 * Dump the profiling results for all functions in file "fd".
19105 */
19106 void
19107func_dump_profile(fd)
19108 FILE *fd;
19109{
19110 hashitem_T *hi;
19111 int todo;
19112 ufunc_T *fp;
19113 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019114 ufunc_T **sorttab;
19115 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019117 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019118 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19119
Bram Moolenaar05159a02005-02-26 23:04:13 +000019120 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19121 {
19122 if (!HASHITEM_EMPTY(hi))
19123 {
19124 --todo;
19125 fp = HI2UF(hi);
19126 if (fp->uf_profiling)
19127 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019128 if (sorttab != NULL)
19129 sorttab[st_len++] = fp;
19130
Bram Moolenaar05159a02005-02-26 23:04:13 +000019131 if (fp->uf_name[0] == K_SPECIAL)
19132 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19133 else
19134 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19135 if (fp->uf_tm_count == 1)
19136 fprintf(fd, "Called 1 time\n");
19137 else
19138 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19139 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19140 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19141 fprintf(fd, "\n");
19142 fprintf(fd, "count total (s) self (s)\n");
19143
19144 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19145 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019146 if (FUNCLINE(fp, i) == NULL)
19147 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019148 prof_func_line(fd, fp->uf_tml_count[i],
19149 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019150 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19151 }
19152 fprintf(fd, "\n");
19153 }
19154 }
19155 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019156
19157 if (sorttab != NULL && st_len > 0)
19158 {
19159 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19160 prof_total_cmp);
19161 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19162 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19163 prof_self_cmp);
19164 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19165 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019166}
Bram Moolenaar73830342005-02-28 22:48:19 +000019167
19168 static void
19169prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19170 FILE *fd;
19171 ufunc_T **sorttab;
19172 int st_len;
19173 char *title;
19174 int prefer_self; /* when equal print only self time */
19175{
19176 int i;
19177 ufunc_T *fp;
19178
19179 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19180 fprintf(fd, "count total (s) self (s) function\n");
19181 for (i = 0; i < 20 && i < st_len; ++i)
19182 {
19183 fp = sorttab[i];
19184 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19185 prefer_self);
19186 if (fp->uf_name[0] == K_SPECIAL)
19187 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19188 else
19189 fprintf(fd, " %s()\n", fp->uf_name);
19190 }
19191 fprintf(fd, "\n");
19192}
19193
19194/*
19195 * Print the count and times for one function or function line.
19196 */
19197 static void
19198prof_func_line(fd, count, total, self, prefer_self)
19199 FILE *fd;
19200 int count;
19201 proftime_T *total;
19202 proftime_T *self;
19203 int prefer_self; /* when equal print only self time */
19204{
19205 if (count > 0)
19206 {
19207 fprintf(fd, "%5d ", count);
19208 if (prefer_self && profile_equal(total, self))
19209 fprintf(fd, " ");
19210 else
19211 fprintf(fd, "%s ", profile_msg(total));
19212 if (!prefer_self && profile_equal(total, self))
19213 fprintf(fd, " ");
19214 else
19215 fprintf(fd, "%s ", profile_msg(self));
19216 }
19217 else
19218 fprintf(fd, " ");
19219}
19220
19221/*
19222 * Compare function for total time sorting.
19223 */
19224 static int
19225#ifdef __BORLANDC__
19226_RTLENTRYF
19227#endif
19228prof_total_cmp(s1, s2)
19229 const void *s1;
19230 const void *s2;
19231{
19232 ufunc_T *p1, *p2;
19233
19234 p1 = *(ufunc_T **)s1;
19235 p2 = *(ufunc_T **)s2;
19236 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19237}
19238
19239/*
19240 * Compare function for self time sorting.
19241 */
19242 static int
19243#ifdef __BORLANDC__
19244_RTLENTRYF
19245#endif
19246prof_self_cmp(s1, s2)
19247 const void *s1;
19248 const void *s2;
19249{
19250 ufunc_T *p1, *p2;
19251
19252 p1 = *(ufunc_T **)s1;
19253 p2 = *(ufunc_T **)s2;
19254 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19255}
19256
Bram Moolenaar05159a02005-02-26 23:04:13 +000019257#endif
19258
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019259/* The names of packages that once were loaded is remembered. */
19260static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19261
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019262/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019263 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019264 * Return TRUE if a package was loaded.
19265 */
19266 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019267script_autoload(name, reload)
19268 char_u *name;
19269 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019270{
19271 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019272 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019273 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019274 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019275
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019276 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019277 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019278 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019279 return FALSE;
19280
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019281 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019282
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019283 /* Find the name in the list of previously loaded package names. Skip
19284 * "autoload/", it's always the same. */
19285 for (i = 0; i < ga_loaded.ga_len; ++i)
19286 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19287 break;
19288 if (!reload && i < ga_loaded.ga_len)
19289 ret = FALSE; /* was loaded already */
19290 else
19291 {
19292 /* Remember the name if it wasn't loaded already. */
19293 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19294 {
19295 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19296 tofree = NULL;
19297 }
19298
19299 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019300 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019301 ret = TRUE;
19302 }
19303
19304 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019305 return ret;
19306}
19307
19308/*
19309 * Return the autoload script name for a function or variable name.
19310 * Returns NULL when out of memory.
19311 */
19312 static char_u *
19313autoload_name(name)
19314 char_u *name;
19315{
19316 char_u *p;
19317 char_u *scriptname;
19318
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019319 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019320 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19321 if (scriptname == NULL)
19322 return FALSE;
19323 STRCPY(scriptname, "autoload/");
19324 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019325 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019326 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019327 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019328 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019329 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019330}
19331
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19333
19334/*
19335 * Function given to ExpandGeneric() to obtain the list of user defined
19336 * function names.
19337 */
19338 char_u *
19339get_user_func_name(xp, idx)
19340 expand_T *xp;
19341 int idx;
19342{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019343 static long_u done;
19344 static hashitem_T *hi;
19345 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346
19347 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019349 done = 0;
19350 hi = func_hashtab.ht_array;
19351 }
19352 if (done < func_hashtab.ht_used)
19353 {
19354 if (done++ > 0)
19355 ++hi;
19356 while (HASHITEM_EMPTY(hi))
19357 ++hi;
19358 fp = HI2UF(hi);
19359
19360 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19361 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362
19363 cat_func_name(IObuff, fp);
19364 if (xp->xp_context != EXPAND_USER_FUNC)
19365 {
19366 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019367 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 STRCAT(IObuff, ")");
19369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 return IObuff;
19371 }
19372 return NULL;
19373}
19374
19375#endif /* FEAT_CMDL_COMPL */
19376
19377/*
19378 * Copy the function name of "fp" to buffer "buf".
19379 * "buf" must be able to hold the function name plus three bytes.
19380 * Takes care of script-local function names.
19381 */
19382 static void
19383cat_func_name(buf, fp)
19384 char_u *buf;
19385 ufunc_T *fp;
19386{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019387 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 {
19389 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019390 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 }
19392 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019393 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394}
19395
19396/*
19397 * ":delfunction {name}"
19398 */
19399 void
19400ex_delfunction(eap)
19401 exarg_T *eap;
19402{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019403 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 char_u *p;
19405 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019406 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407
19408 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019409 name = trans_function_name(&p, eap->skip, 0, &fudi);
19410 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019412 {
19413 if (fudi.fd_dict != NULL && !eap->skip)
19414 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 if (!ends_excmd(*skipwhite(p)))
19418 {
19419 vim_free(name);
19420 EMSG(_(e_trailing));
19421 return;
19422 }
19423 eap->nextcmd = check_nextcmd(p);
19424 if (eap->nextcmd != NULL)
19425 *p = NUL;
19426
19427 if (!eap->skip)
19428 fp = find_func(name);
19429 vim_free(name);
19430
19431 if (!eap->skip)
19432 {
19433 if (fp == NULL)
19434 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019435 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 return;
19437 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019438 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 {
19440 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19441 return;
19442 }
19443
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019444 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019446 /* Delete the dict item that refers to the function, it will
19447 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019448 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019450 else
19451 func_free(fp);
19452 }
19453}
19454
19455/*
19456 * Free a function and remove it from the list of functions.
19457 */
19458 static void
19459func_free(fp)
19460 ufunc_T *fp;
19461{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019462 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019463
19464 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019465 ga_clear_strings(&(fp->uf_args));
19466 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019467#ifdef FEAT_PROFILE
19468 vim_free(fp->uf_tml_count);
19469 vim_free(fp->uf_tml_total);
19470 vim_free(fp->uf_tml_self);
19471#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019472
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019473 /* remove the function from the function hashtable */
19474 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19475 if (HASHITEM_EMPTY(hi))
19476 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019477 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019478 hash_remove(&func_hashtab, hi);
19479
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019480 vim_free(fp);
19481}
19482
19483/*
19484 * Unreference a Function: decrement the reference count and free it when it
19485 * becomes zero. Only for numbered functions.
19486 */
19487 static void
19488func_unref(name)
19489 char_u *name;
19490{
19491 ufunc_T *fp;
19492
19493 if (name != NULL && isdigit(*name))
19494 {
19495 fp = find_func(name);
19496 if (fp == NULL)
19497 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019498 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019499 {
19500 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019501 * when "uf_calls" becomes zero. */
19502 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019503 func_free(fp);
19504 }
19505 }
19506}
19507
19508/*
19509 * Count a reference to a Function.
19510 */
19511 static void
19512func_ref(name)
19513 char_u *name;
19514{
19515 ufunc_T *fp;
19516
19517 if (name != NULL && isdigit(*name))
19518 {
19519 fp = find_func(name);
19520 if (fp == NULL)
19521 EMSG2(_(e_intern2), "func_ref()");
19522 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019523 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 }
19525}
19526
19527/*
19528 * Call a user function.
19529 */
19530 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019531call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 ufunc_T *fp; /* pointer to function */
19533 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 typval_T *argvars; /* arguments */
19535 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 linenr_T firstline; /* first line of range */
19537 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019538 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539{
Bram Moolenaar33570922005-01-25 22:26:29 +000019540 char_u *save_sourcing_name;
19541 linenr_T save_sourcing_lnum;
19542 scid_T save_current_SID;
19543 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019544 int save_did_emsg;
19545 static int depth = 0;
19546 dictitem_T *v;
19547 int fixvar_idx = 0; /* index in fixvar[] */
19548 int i;
19549 int ai;
19550 char_u numbuf[NUMBUFLEN];
19551 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019552#ifdef FEAT_PROFILE
19553 proftime_T wait_start;
19554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555
19556 /* If depth of calling is getting too high, don't execute the function */
19557 if (depth >= p_mfd)
19558 {
19559 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560 rettv->v_type = VAR_NUMBER;
19561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 return;
19563 }
19564 ++depth;
19565
19566 line_breakcheck(); /* check for CTRL-C hit */
19567
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019568 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019569 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019571 fc.rettv = rettv;
19572 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 fc.linenr = 0;
19574 fc.returned = FALSE;
19575 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019577 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578 fc.dbg_tick = debug_tick;
19579
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 /*
19581 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19582 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19583 * each argument variable and saves a lot of time.
19584 */
19585 /*
19586 * Init l: variables.
19587 */
19588 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019589 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019590 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019591 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19592 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019594 name = v->di_key;
19595 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19597 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19598 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019599 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019600 v->di_tv.vval.v_dict = selfdict;
19601 ++selfdict->dv_refcount;
19602 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019603
Bram Moolenaar33570922005-01-25 22:26:29 +000019604 /*
19605 * Init a: variables.
19606 * Set a:0 to "argcount".
19607 * Set a:000 to a list with room for the "..." arguments.
19608 */
19609 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19610 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019611 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 v = &fc.fixvar[fixvar_idx++].var;
19613 STRCPY(v->di_key, "000");
19614 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19615 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19616 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019617 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 v->di_tv.vval.v_list = &fc.l_varlist;
19619 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19620 fc.l_varlist.lv_refcount = 99999;
19621
19622 /*
19623 * Set a:firstline to "firstline" and a:lastline to "lastline".
19624 * Set a:name to named arguments.
19625 * Set a:N to the "..." arguments.
19626 */
19627 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19628 (varnumber_T)firstline);
19629 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19630 (varnumber_T)lastline);
19631 for (i = 0; i < argcount; ++i)
19632 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019633 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 if (ai < 0)
19635 /* named argument a:name */
19636 name = FUNCARG(fp, i);
19637 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019638 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 /* "..." argument a:1, a:2, etc. */
19640 sprintf((char *)numbuf, "%d", ai + 1);
19641 name = numbuf;
19642 }
19643 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19644 {
19645 v = &fc.fixvar[fixvar_idx++].var;
19646 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19647 }
19648 else
19649 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019650 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19651 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 if (v == NULL)
19653 break;
19654 v->di_flags = DI_FLAGS_RO;
19655 }
19656 STRCPY(v->di_key, name);
19657 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19658
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019659 /* Note: the values are copied directly to avoid alloc/free.
19660 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019661 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019662 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019663
19664 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19665 {
19666 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19667 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019668 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019669 }
19670 }
19671
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 /* Don't redraw while executing the function. */
19673 ++RedrawingDisabled;
19674 save_sourcing_name = sourcing_name;
19675 save_sourcing_lnum = sourcing_lnum;
19676 sourcing_lnum = 1;
19677 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019678 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 if (sourcing_name != NULL)
19680 {
19681 if (save_sourcing_name != NULL
19682 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19683 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19684 else
19685 STRCPY(sourcing_name, "function ");
19686 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19687
19688 if (p_verbose >= 12)
19689 {
19690 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019691 verbose_enter_scroll();
19692
Bram Moolenaar555b2802005-05-19 21:08:39 +000019693 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694 if (p_verbose >= 14)
19695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019697 char_u numbuf[NUMBUFLEN];
19698 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699
19700 msg_puts((char_u *)"(");
19701 for (i = 0; i < argcount; ++i)
19702 {
19703 if (i > 0)
19704 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019705 if (argvars[i].v_type == VAR_NUMBER)
19706 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 else
19708 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019709 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019710 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019712 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 }
19714 }
19715 msg_puts((char_u *)")");
19716 }
19717 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019718
19719 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 --no_wait_return;
19721 }
19722 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019723#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019724 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019725 {
19726 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19727 func_do_profile(fp);
19728 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019729 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019730 {
19731 ++fp->uf_tm_count;
19732 profile_start(&fp->uf_tm_start);
19733 profile_zero(&fp->uf_tm_children);
19734 }
19735 script_prof_save(&wait_start);
19736 }
19737#endif
19738
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019740 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 save_did_emsg = did_emsg;
19742 did_emsg = FALSE;
19743
19744 /* call do_cmdline() to execute the lines */
19745 do_cmdline(NULL, get_func_line, (void *)&fc,
19746 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19747
19748 --RedrawingDisabled;
19749
19750 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019751 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019753 clear_tv(rettv);
19754 rettv->v_type = VAR_NUMBER;
19755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 }
19757
Bram Moolenaar05159a02005-02-26 23:04:13 +000019758#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019759 if (do_profiling == PROF_YES && (fp->uf_profiling
19760 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019761 {
19762 profile_end(&fp->uf_tm_start);
19763 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19764 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019765 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019766 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019767 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019768 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19769 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019770 }
19771 }
19772#endif
19773
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 /* when being verbose, mention the return value */
19775 if (p_verbose >= 12)
19776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019778 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019781 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019783 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19784 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019785 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019787 char_u buf[MSG_BUF_LEN];
19788 char_u numbuf[NUMBUFLEN];
19789 char_u *tofree;
19790
Bram Moolenaar555b2802005-05-19 21:08:39 +000019791 /* The value may be very long. Skip the middle part, so that we
19792 * have some idea how it starts and ends. smsg() would always
19793 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019794 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019795 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019796 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019797 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 }
19799 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019800
19801 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 --no_wait_return;
19803 }
19804
19805 vim_free(sourcing_name);
19806 sourcing_name = save_sourcing_name;
19807 sourcing_lnum = save_sourcing_lnum;
19808 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019809#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019810 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019811 script_prof_restore(&wait_start);
19812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813
19814 if (p_verbose >= 12 && sourcing_name != NULL)
19815 {
19816 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019817 verbose_enter_scroll();
19818
Bram Moolenaar555b2802005-05-19 21:08:39 +000019819 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019821
19822 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 --no_wait_return;
19824 }
19825
19826 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019827 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 /* The a: variables typevals were not alloced, only free the allocated
19830 * variables. */
19831 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19832
19833 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 --depth;
19835}
19836
19837/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019838 * Add a number variable "name" to dict "dp" with value "nr".
19839 */
19840 static void
19841add_nr_var(dp, v, name, nr)
19842 dict_T *dp;
19843 dictitem_T *v;
19844 char *name;
19845 varnumber_T nr;
19846{
19847 STRCPY(v->di_key, name);
19848 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19849 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19850 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019851 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019852 v->di_tv.vval.v_number = nr;
19853}
19854
19855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 * ":return [expr]"
19857 */
19858 void
19859ex_return(eap)
19860 exarg_T *eap;
19861{
19862 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864 int returning = FALSE;
19865
19866 if (current_funccal == NULL)
19867 {
19868 EMSG(_("E133: :return not inside a function"));
19869 return;
19870 }
19871
19872 if (eap->skip)
19873 ++emsg_skip;
19874
19875 eap->nextcmd = NULL;
19876 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019877 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 {
19879 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019880 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 }
19884 /* It's safer to return also on error. */
19885 else if (!eap->skip)
19886 {
19887 /*
19888 * Return unless the expression evaluation has been cancelled due to an
19889 * aborting error, an interrupt, or an exception.
19890 */
19891 if (!aborting())
19892 returning = do_return(eap, FALSE, TRUE, NULL);
19893 }
19894
19895 /* When skipping or the return gets pending, advance to the next command
19896 * in this line (!returning). Otherwise, ignore the rest of the line.
19897 * Following lines will be ignored by get_func_line(). */
19898 if (returning)
19899 eap->nextcmd = NULL;
19900 else if (eap->nextcmd == NULL) /* no argument */
19901 eap->nextcmd = check_nextcmd(arg);
19902
19903 if (eap->skip)
19904 --emsg_skip;
19905}
19906
19907/*
19908 * Return from a function. Possibly makes the return pending. Also called
19909 * for a pending return at the ":endtry" or after returning from an extra
19910 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019912 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 * FALSE when the return gets pending.
19914 */
19915 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019916do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 exarg_T *eap;
19918 int reanimate;
19919 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921{
19922 int idx;
19923 struct condstack *cstack = eap->cstack;
19924
19925 if (reanimate)
19926 /* Undo the return. */
19927 current_funccal->returned = FALSE;
19928
19929 /*
19930 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19931 * not in its finally clause (which then is to be executed next) is found.
19932 * In this case, make the ":return" pending for execution at the ":endtry".
19933 * Otherwise, return normally.
19934 */
19935 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19936 if (idx >= 0)
19937 {
19938 cstack->cs_pending[idx] = CSTP_RETURN;
19939
19940 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 /* A pending return again gets pending. "rettv" points to an
19942 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019944 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 else
19946 {
19947 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019948 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019952 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 {
19954 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019955 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 else
19958 EMSG(_(e_outofmem));
19959 }
19960 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962
19963 if (reanimate)
19964 {
19965 /* The pending return value could be overwritten by a ":return"
19966 * without argument in a finally clause; reset the default
19967 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968 current_funccal->rettv->v_type = VAR_NUMBER;
19969 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 }
19971 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 }
19974 else
19975 {
19976 current_funccal->returned = TRUE;
19977
19978 /* If the return is carried out now, store the return value. For
19979 * a return immediately after reanimation, the value is already
19980 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019981 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019983 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019984 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 }
19988 }
19989
19990 return idx < 0;
19991}
19992
19993/*
19994 * Free the variable with a pending return value.
19995 */
19996 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997discard_pending_return(rettv)
19998 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999{
Bram Moolenaar33570922005-01-25 22:26:29 +000020000 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001}
20002
20003/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 * is an allocated string. Used by report_pending() for verbose messages.
20006 */
20007 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008get_return_cmd(rettv)
20009 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020011 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020012 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020013 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020015 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020016 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020017 if (s == NULL)
20018 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019
20020 STRCPY(IObuff, ":return ");
20021 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20022 if (STRLEN(s) + 8 >= IOSIZE)
20023 STRCPY(IObuff + IOSIZE - 4, "...");
20024 vim_free(tofree);
20025 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026}
20027
20028/*
20029 * Get next function line.
20030 * Called by do_cmdline() to get the next line.
20031 * Returns allocated string, or NULL for end of function.
20032 */
20033/* ARGSUSED */
20034 char_u *
20035get_func_line(c, cookie, indent)
20036 int c; /* not used */
20037 void *cookie;
20038 int indent; /* not used */
20039{
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020041 ufunc_T *fp = fcp->func;
20042 char_u *retval;
20043 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044
20045 /* If breakpoints have been added/deleted need to check for it. */
20046 if (fcp->dbg_tick != debug_tick)
20047 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020048 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 sourcing_lnum);
20050 fcp->dbg_tick = debug_tick;
20051 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020052#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020053 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020054 func_line_end(cookie);
20055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056
Bram Moolenaar05159a02005-02-26 23:04:13 +000020057 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020058 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20059 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 retval = NULL;
20061 else
20062 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020063 /* Skip NULL lines (continuation lines). */
20064 while (fcp->linenr < gap->ga_len
20065 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20066 ++fcp->linenr;
20067 if (fcp->linenr >= gap->ga_len)
20068 retval = NULL;
20069 else
20070 {
20071 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20072 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020073#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020074 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020075 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020076#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 }
20079
20080 /* Did we encounter a breakpoint? */
20081 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20082 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020083 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020085 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 sourcing_lnum);
20087 fcp->dbg_tick = debug_tick;
20088 }
20089
20090 return retval;
20091}
20092
Bram Moolenaar05159a02005-02-26 23:04:13 +000020093#if defined(FEAT_PROFILE) || defined(PROTO)
20094/*
20095 * Called when starting to read a function line.
20096 * "sourcing_lnum" must be correct!
20097 * When skipping lines it may not actually be executed, but we won't find out
20098 * until later and we need to store the time now.
20099 */
20100 void
20101func_line_start(cookie)
20102 void *cookie;
20103{
20104 funccall_T *fcp = (funccall_T *)cookie;
20105 ufunc_T *fp = fcp->func;
20106
20107 if (fp->uf_profiling && sourcing_lnum >= 1
20108 && sourcing_lnum <= fp->uf_lines.ga_len)
20109 {
20110 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020111 /* Skip continuation lines. */
20112 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20113 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020114 fp->uf_tml_execed = FALSE;
20115 profile_start(&fp->uf_tml_start);
20116 profile_zero(&fp->uf_tml_children);
20117 profile_get_wait(&fp->uf_tml_wait);
20118 }
20119}
20120
20121/*
20122 * Called when actually executing a function line.
20123 */
20124 void
20125func_line_exec(cookie)
20126 void *cookie;
20127{
20128 funccall_T *fcp = (funccall_T *)cookie;
20129 ufunc_T *fp = fcp->func;
20130
20131 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20132 fp->uf_tml_execed = TRUE;
20133}
20134
20135/*
20136 * Called when done with a function line.
20137 */
20138 void
20139func_line_end(cookie)
20140 void *cookie;
20141{
20142 funccall_T *fcp = (funccall_T *)cookie;
20143 ufunc_T *fp = fcp->func;
20144
20145 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20146 {
20147 if (fp->uf_tml_execed)
20148 {
20149 ++fp->uf_tml_count[fp->uf_tml_idx];
20150 profile_end(&fp->uf_tml_start);
20151 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020152 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020153 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20154 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020155 }
20156 fp->uf_tml_idx = -1;
20157 }
20158}
20159#endif
20160
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161/*
20162 * Return TRUE if the currently active function should be ended, because a
20163 * return was encountered or an error occured. Used inside a ":while".
20164 */
20165 int
20166func_has_ended(cookie)
20167 void *cookie;
20168{
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170
20171 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20172 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020173 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 || fcp->returned);
20175}
20176
20177/*
20178 * return TRUE if cookie indicates a function which "abort"s on errors.
20179 */
20180 int
20181func_has_abort(cookie)
20182 void *cookie;
20183{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020184 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185}
20186
20187#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20188typedef enum
20189{
20190 VAR_FLAVOUR_DEFAULT,
20191 VAR_FLAVOUR_SESSION,
20192 VAR_FLAVOUR_VIMINFO
20193} var_flavour_T;
20194
20195static var_flavour_T var_flavour __ARGS((char_u *varname));
20196
20197 static var_flavour_T
20198var_flavour(varname)
20199 char_u *varname;
20200{
20201 char_u *p = varname;
20202
20203 if (ASCII_ISUPPER(*p))
20204 {
20205 while (*(++p))
20206 if (ASCII_ISLOWER(*p))
20207 return VAR_FLAVOUR_SESSION;
20208 return VAR_FLAVOUR_VIMINFO;
20209 }
20210 else
20211 return VAR_FLAVOUR_DEFAULT;
20212}
20213#endif
20214
20215#if defined(FEAT_VIMINFO) || defined(PROTO)
20216/*
20217 * Restore global vars that start with a capital from the viminfo file
20218 */
20219 int
20220read_viminfo_varlist(virp, writing)
20221 vir_T *virp;
20222 int writing;
20223{
20224 char_u *tab;
20225 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227
20228 if (!writing && (find_viminfo_parameter('!') != NULL))
20229 {
20230 tab = vim_strchr(virp->vir_line + 1, '\t');
20231 if (tab != NULL)
20232 {
20233 *tab++ = '\0'; /* isolate the variable name */
20234 if (*tab == 'S') /* string var */
20235 is_string = TRUE;
20236
20237 tab = vim_strchr(tab, '\t');
20238 if (tab != NULL)
20239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 if (is_string)
20241 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020242 tv.v_type = VAR_STRING;
20243 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 }
20246 else
20247 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020248 tv.v_type = VAR_NUMBER;
20249 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020251 set_var(virp->vir_line + 1, &tv, FALSE);
20252 if (is_string)
20253 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 }
20255 }
20256 }
20257
20258 return viminfo_readline(virp);
20259}
20260
20261/*
20262 * Write global vars that start with a capital to the viminfo file
20263 */
20264 void
20265write_viminfo_varlist(fp)
20266 FILE *fp;
20267{
Bram Moolenaar33570922005-01-25 22:26:29 +000020268 hashitem_T *hi;
20269 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020270 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020271 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020272 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020273 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020274 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275
20276 if (find_viminfo_parameter('!') == NULL)
20277 return;
20278
20279 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020280
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020281 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020282 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020284 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020286 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 this_var = HI2DI(hi);
20288 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020289 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020291 {
20292 case VAR_STRING: s = "STR"; break;
20293 case VAR_NUMBER: s = "NUM"; break;
20294 default: continue;
20295 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020297 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020298 if (p != NULL)
20299 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020300 vim_free(tofree);
20301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 }
20303 }
20304}
20305#endif
20306
20307#if defined(FEAT_SESSION) || defined(PROTO)
20308 int
20309store_session_globals(fd)
20310 FILE *fd;
20311{
Bram Moolenaar33570922005-01-25 22:26:29 +000020312 hashitem_T *hi;
20313 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020314 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 char_u *p, *t;
20316
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020317 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020318 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020320 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020322 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 this_var = HI2DI(hi);
20324 if ((this_var->di_tv.v_type == VAR_NUMBER
20325 || this_var->di_tv.v_type == VAR_STRING)
20326 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020327 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020328 /* Escape special characters with a backslash. Turn a LF and
20329 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020330 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020331 (char_u *)"\\\"\n\r");
20332 if (p == NULL) /* out of memory */
20333 break;
20334 for (t = p; *t != NUL; ++t)
20335 if (*t == '\n')
20336 *t = 'n';
20337 else if (*t == '\r')
20338 *t = 'r';
20339 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020340 this_var->di_key,
20341 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20342 : ' ',
20343 p,
20344 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20345 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020346 || put_eol(fd) == FAIL)
20347 {
20348 vim_free(p);
20349 return FAIL;
20350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 vim_free(p);
20352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 }
20354 }
20355 return OK;
20356}
20357#endif
20358
Bram Moolenaar661b1822005-07-28 22:36:45 +000020359/*
20360 * Display script name where an item was last set.
20361 * Should only be invoked when 'verbose' is non-zero.
20362 */
20363 void
20364last_set_msg(scriptID)
20365 scid_T scriptID;
20366{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020367 char_u *p;
20368
Bram Moolenaar661b1822005-07-28 22:36:45 +000020369 if (scriptID != 0)
20370 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020371 p = home_replace_save(NULL, get_scriptname(scriptID));
20372 if (p != NULL)
20373 {
20374 verbose_enter();
20375 MSG_PUTS(_("\n\tLast set from "));
20376 MSG_PUTS(p);
20377 vim_free(p);
20378 verbose_leave();
20379 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020380 }
20381}
20382
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383#endif /* FEAT_EVAL */
20384
20385#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20386
20387
20388#ifdef WIN3264
20389/*
20390 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20391 */
20392static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20393static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20394static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20395
20396/*
20397 * Get the short pathname of a file.
20398 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20399 */
20400 static int
20401get_short_pathname(fnamep, bufp, fnamelen)
20402 char_u **fnamep;
20403 char_u **bufp;
20404 int *fnamelen;
20405{
20406 int l,len;
20407 char_u *newbuf;
20408
20409 len = *fnamelen;
20410
20411 l = GetShortPathName(*fnamep, *fnamep, len);
20412 if (l > len - 1)
20413 {
20414 /* If that doesn't work (not enough space), then save the string
20415 * and try again with a new buffer big enough
20416 */
20417 newbuf = vim_strnsave(*fnamep, l);
20418 if (newbuf == NULL)
20419 return 0;
20420
20421 vim_free(*bufp);
20422 *fnamep = *bufp = newbuf;
20423
20424 l = GetShortPathName(*fnamep,*fnamep,l+1);
20425
20426 /* Really should always succeed, as the buffer is big enough */
20427 }
20428
20429 *fnamelen = l;
20430 return 1;
20431}
20432
20433/*
20434 * Create a short path name. Returns the length of the buffer it needs.
20435 * Doesn't copy over the end of the buffer passed in.
20436 */
20437 static int
20438shortpath_for_invalid_fname(fname, bufp, fnamelen)
20439 char_u **fname;
20440 char_u **bufp;
20441 int *fnamelen;
20442{
20443 char_u *s, *p, *pbuf2, *pbuf3;
20444 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020445 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446
20447 /* Make a copy */
20448 len2 = *fnamelen;
20449 pbuf2 = vim_strnsave(*fname, len2);
20450 pbuf3 = NULL;
20451
20452 s = pbuf2 + len2 - 1; /* Find the end */
20453 slen = 1;
20454 plen = len2;
20455
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020456 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 {
20458 --s;
20459 ++slen;
20460 --plen;
20461 }
20462
20463 do
20464 {
20465 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020466 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 {
20468 --s;
20469 ++slen;
20470 --plen;
20471 }
20472 if (s <= pbuf2)
20473 break;
20474
20475 /* Remeber the character that is about to be blatted */
20476 ch = *s;
20477 *s = 0; /* get_short_pathname requires a null-terminated string */
20478
20479 /* Try it in situ */
20480 p = pbuf2;
20481 if (!get_short_pathname(&p, &pbuf3, &plen))
20482 {
20483 vim_free(pbuf2);
20484 return -1;
20485 }
20486 *s = ch; /* Preserve the string */
20487 } while (plen == 0);
20488
20489 if (plen > 0)
20490 {
20491 /* Remeber the length of the new string. */
20492 *fnamelen = len = plen + slen;
20493 vim_free(*bufp);
20494 if (len > len2)
20495 {
20496 /* If there's not enough space in the currently allocated string,
20497 * then copy it to a buffer big enough.
20498 */
20499 *fname= *bufp = vim_strnsave(p, len);
20500 if (*fname == NULL)
20501 return -1;
20502 }
20503 else
20504 {
20505 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20506 *fname = *bufp = pbuf2;
20507 if (p != pbuf2)
20508 strncpy(*fname, p, plen);
20509 pbuf2 = NULL;
20510 }
20511 /* Concat the next bit */
20512 strncpy(*fname + plen, s, slen);
20513 (*fname)[len] = '\0';
20514 }
20515 vim_free(pbuf3);
20516 vim_free(pbuf2);
20517 return 0;
20518}
20519
20520/*
20521 * Get a pathname for a partial path.
20522 */
20523 static int
20524shortpath_for_partial(fnamep, bufp, fnamelen)
20525 char_u **fnamep;
20526 char_u **bufp;
20527 int *fnamelen;
20528{
20529 int sepcount, len, tflen;
20530 char_u *p;
20531 char_u *pbuf, *tfname;
20532 int hasTilde;
20533
20534 /* Count up the path seperators from the RHS.. so we know which part
20535 * of the path to return.
20536 */
20537 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020538 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 if (vim_ispathsep(*p))
20540 ++sepcount;
20541
20542 /* Need full path first (use expand_env() to remove a "~/") */
20543 hasTilde = (**fnamep == '~');
20544 if (hasTilde)
20545 pbuf = tfname = expand_env_save(*fnamep);
20546 else
20547 pbuf = tfname = FullName_save(*fnamep, FALSE);
20548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020549 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550
20551 if (!get_short_pathname(&tfname, &pbuf, &len))
20552 return -1;
20553
20554 if (len == 0)
20555 {
20556 /* Don't have a valid filename, so shorten the rest of the
20557 * path if we can. This CAN give us invalid 8.3 filenames, but
20558 * there's not a lot of point in guessing what it might be.
20559 */
20560 len = tflen;
20561 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20562 return -1;
20563 }
20564
20565 /* Count the paths backward to find the beginning of the desired string. */
20566 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020567 {
20568#ifdef FEAT_MBYTE
20569 if (has_mbyte)
20570 p -= mb_head_off(tfname, p);
20571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 if (vim_ispathsep(*p))
20573 {
20574 if (sepcount == 0 || (hasTilde && sepcount == 1))
20575 break;
20576 else
20577 sepcount --;
20578 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 if (hasTilde)
20581 {
20582 --p;
20583 if (p >= tfname)
20584 *p = '~';
20585 else
20586 return -1;
20587 }
20588 else
20589 ++p;
20590
20591 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20592 vim_free(*bufp);
20593 *fnamelen = (int)STRLEN(p);
20594 *bufp = pbuf;
20595 *fnamep = p;
20596
20597 return 0;
20598}
20599#endif /* WIN3264 */
20600
20601/*
20602 * Adjust a filename, according to a string of modifiers.
20603 * *fnamep must be NUL terminated when called. When returning, the length is
20604 * determined by *fnamelen.
20605 * Returns valid flags.
20606 * When there is an error, *fnamep is set to NULL.
20607 */
20608 int
20609modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20610 char_u *src; /* string with modifiers */
20611 int *usedlen; /* characters after src that are used */
20612 char_u **fnamep; /* file name so far */
20613 char_u **bufp; /* buffer for allocated file name or NULL */
20614 int *fnamelen; /* length of fnamep */
20615{
20616 int valid = 0;
20617 char_u *tail;
20618 char_u *s, *p, *pbuf;
20619 char_u dirname[MAXPATHL];
20620 int c;
20621 int has_fullname = 0;
20622#ifdef WIN3264
20623 int has_shortname = 0;
20624#endif
20625
20626repeat:
20627 /* ":p" - full path/file_name */
20628 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20629 {
20630 has_fullname = 1;
20631
20632 valid |= VALID_PATH;
20633 *usedlen += 2;
20634
20635 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20636 if ((*fnamep)[0] == '~'
20637#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20638 && ((*fnamep)[1] == '/'
20639# ifdef BACKSLASH_IN_FILENAME
20640 || (*fnamep)[1] == '\\'
20641# endif
20642 || (*fnamep)[1] == NUL)
20643
20644#endif
20645 )
20646 {
20647 *fnamep = expand_env_save(*fnamep);
20648 vim_free(*bufp); /* free any allocated file name */
20649 *bufp = *fnamep;
20650 if (*fnamep == NULL)
20651 return -1;
20652 }
20653
20654 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020655 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 {
20657 if (vim_ispathsep(*p)
20658 && p[1] == '.'
20659 && (p[2] == NUL
20660 || vim_ispathsep(p[2])
20661 || (p[2] == '.'
20662 && (p[3] == NUL || vim_ispathsep(p[3])))))
20663 break;
20664 }
20665
20666 /* FullName_save() is slow, don't use it when not needed. */
20667 if (*p != NUL || !vim_isAbsName(*fnamep))
20668 {
20669 *fnamep = FullName_save(*fnamep, *p != NUL);
20670 vim_free(*bufp); /* free any allocated file name */
20671 *bufp = *fnamep;
20672 if (*fnamep == NULL)
20673 return -1;
20674 }
20675
20676 /* Append a path separator to a directory. */
20677 if (mch_isdir(*fnamep))
20678 {
20679 /* Make room for one or two extra characters. */
20680 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20681 vim_free(*bufp); /* free any allocated file name */
20682 *bufp = *fnamep;
20683 if (*fnamep == NULL)
20684 return -1;
20685 add_pathsep(*fnamep);
20686 }
20687 }
20688
20689 /* ":." - path relative to the current directory */
20690 /* ":~" - path relative to the home directory */
20691 /* ":8" - shortname path - postponed till after */
20692 while (src[*usedlen] == ':'
20693 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20694 {
20695 *usedlen += 2;
20696 if (c == '8')
20697 {
20698#ifdef WIN3264
20699 has_shortname = 1; /* Postpone this. */
20700#endif
20701 continue;
20702 }
20703 pbuf = NULL;
20704 /* Need full path first (use expand_env() to remove a "~/") */
20705 if (!has_fullname)
20706 {
20707 if (c == '.' && **fnamep == '~')
20708 p = pbuf = expand_env_save(*fnamep);
20709 else
20710 p = pbuf = FullName_save(*fnamep, FALSE);
20711 }
20712 else
20713 p = *fnamep;
20714
20715 has_fullname = 0;
20716
20717 if (p != NULL)
20718 {
20719 if (c == '.')
20720 {
20721 mch_dirname(dirname, MAXPATHL);
20722 s = shorten_fname(p, dirname);
20723 if (s != NULL)
20724 {
20725 *fnamep = s;
20726 if (pbuf != NULL)
20727 {
20728 vim_free(*bufp); /* free any allocated file name */
20729 *bufp = pbuf;
20730 pbuf = NULL;
20731 }
20732 }
20733 }
20734 else
20735 {
20736 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20737 /* Only replace it when it starts with '~' */
20738 if (*dirname == '~')
20739 {
20740 s = vim_strsave(dirname);
20741 if (s != NULL)
20742 {
20743 *fnamep = s;
20744 vim_free(*bufp);
20745 *bufp = s;
20746 }
20747 }
20748 }
20749 vim_free(pbuf);
20750 }
20751 }
20752
20753 tail = gettail(*fnamep);
20754 *fnamelen = (int)STRLEN(*fnamep);
20755
20756 /* ":h" - head, remove "/file_name", can be repeated */
20757 /* Don't remove the first "/" or "c:\" */
20758 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20759 {
20760 valid |= VALID_HEAD;
20761 *usedlen += 2;
20762 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020763 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 --tail;
20765 *fnamelen = (int)(tail - *fnamep);
20766#ifdef VMS
20767 if (*fnamelen > 0)
20768 *fnamelen += 1; /* the path separator is part of the path */
20769#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020770 while (tail > s && !after_pathsep(s, tail))
20771 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 }
20773
20774 /* ":8" - shortname */
20775 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20776 {
20777 *usedlen += 2;
20778#ifdef WIN3264
20779 has_shortname = 1;
20780#endif
20781 }
20782
20783#ifdef WIN3264
20784 /* Check shortname after we have done 'heads' and before we do 'tails'
20785 */
20786 if (has_shortname)
20787 {
20788 pbuf = NULL;
20789 /* Copy the string if it is shortened by :h */
20790 if (*fnamelen < (int)STRLEN(*fnamep))
20791 {
20792 p = vim_strnsave(*fnamep, *fnamelen);
20793 if (p == 0)
20794 return -1;
20795 vim_free(*bufp);
20796 *bufp = *fnamep = p;
20797 }
20798
20799 /* Split into two implementations - makes it easier. First is where
20800 * there isn't a full name already, second is where there is.
20801 */
20802 if (!has_fullname && !vim_isAbsName(*fnamep))
20803 {
20804 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20805 return -1;
20806 }
20807 else
20808 {
20809 int l;
20810
20811 /* Simple case, already have the full-name
20812 * Nearly always shorter, so try first time. */
20813 l = *fnamelen;
20814 if (!get_short_pathname(fnamep, bufp, &l))
20815 return -1;
20816
20817 if (l == 0)
20818 {
20819 /* Couldn't find the filename.. search the paths.
20820 */
20821 l = *fnamelen;
20822 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20823 return -1;
20824 }
20825 *fnamelen = l;
20826 }
20827 }
20828#endif /* WIN3264 */
20829
20830 /* ":t" - tail, just the basename */
20831 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20832 {
20833 *usedlen += 2;
20834 *fnamelen -= (int)(tail - *fnamep);
20835 *fnamep = tail;
20836 }
20837
20838 /* ":e" - extension, can be repeated */
20839 /* ":r" - root, without extension, can be repeated */
20840 while (src[*usedlen] == ':'
20841 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20842 {
20843 /* find a '.' in the tail:
20844 * - for second :e: before the current fname
20845 * - otherwise: The last '.'
20846 */
20847 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20848 s = *fnamep - 2;
20849 else
20850 s = *fnamep + *fnamelen - 1;
20851 for ( ; s > tail; --s)
20852 if (s[0] == '.')
20853 break;
20854 if (src[*usedlen + 1] == 'e') /* :e */
20855 {
20856 if (s > tail)
20857 {
20858 *fnamelen += (int)(*fnamep - (s + 1));
20859 *fnamep = s + 1;
20860#ifdef VMS
20861 /* cut version from the extension */
20862 s = *fnamep + *fnamelen - 1;
20863 for ( ; s > *fnamep; --s)
20864 if (s[0] == ';')
20865 break;
20866 if (s > *fnamep)
20867 *fnamelen = s - *fnamep;
20868#endif
20869 }
20870 else if (*fnamep <= tail)
20871 *fnamelen = 0;
20872 }
20873 else /* :r */
20874 {
20875 if (s > tail) /* remove one extension */
20876 *fnamelen = (int)(s - *fnamep);
20877 }
20878 *usedlen += 2;
20879 }
20880
20881 /* ":s?pat?foo?" - substitute */
20882 /* ":gs?pat?foo?" - global substitute */
20883 if (src[*usedlen] == ':'
20884 && (src[*usedlen + 1] == 's'
20885 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20886 {
20887 char_u *str;
20888 char_u *pat;
20889 char_u *sub;
20890 int sep;
20891 char_u *flags;
20892 int didit = FALSE;
20893
20894 flags = (char_u *)"";
20895 s = src + *usedlen + 2;
20896 if (src[*usedlen + 1] == 'g')
20897 {
20898 flags = (char_u *)"g";
20899 ++s;
20900 }
20901
20902 sep = *s++;
20903 if (sep)
20904 {
20905 /* find end of pattern */
20906 p = vim_strchr(s, sep);
20907 if (p != NULL)
20908 {
20909 pat = vim_strnsave(s, (int)(p - s));
20910 if (pat != NULL)
20911 {
20912 s = p + 1;
20913 /* find end of substitution */
20914 p = vim_strchr(s, sep);
20915 if (p != NULL)
20916 {
20917 sub = vim_strnsave(s, (int)(p - s));
20918 str = vim_strnsave(*fnamep, *fnamelen);
20919 if (sub != NULL && str != NULL)
20920 {
20921 *usedlen = (int)(p + 1 - src);
20922 s = do_string_sub(str, pat, sub, flags);
20923 if (s != NULL)
20924 {
20925 *fnamep = s;
20926 *fnamelen = (int)STRLEN(s);
20927 vim_free(*bufp);
20928 *bufp = s;
20929 didit = TRUE;
20930 }
20931 }
20932 vim_free(sub);
20933 vim_free(str);
20934 }
20935 vim_free(pat);
20936 }
20937 }
20938 /* after using ":s", repeat all the modifiers */
20939 if (didit)
20940 goto repeat;
20941 }
20942 }
20943
20944 return valid;
20945}
20946
20947/*
20948 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20949 * "flags" can be "g" to do a global substitute.
20950 * Returns an allocated string, NULL for error.
20951 */
20952 char_u *
20953do_string_sub(str, pat, sub, flags)
20954 char_u *str;
20955 char_u *pat;
20956 char_u *sub;
20957 char_u *flags;
20958{
20959 int sublen;
20960 regmatch_T regmatch;
20961 int i;
20962 int do_all;
20963 char_u *tail;
20964 garray_T ga;
20965 char_u *ret;
20966 char_u *save_cpo;
20967
20968 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20969 save_cpo = p_cpo;
20970 p_cpo = (char_u *)"";
20971
20972 ga_init2(&ga, 1, 200);
20973
20974 do_all = (flags[0] == 'g');
20975
20976 regmatch.rm_ic = p_ic;
20977 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20978 if (regmatch.regprog != NULL)
20979 {
20980 tail = str;
20981 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20982 {
20983 /*
20984 * Get some space for a temporary buffer to do the substitution
20985 * into. It will contain:
20986 * - The text up to where the match is.
20987 * - The substituted text.
20988 * - The text after the match.
20989 */
20990 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20991 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20992 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20993 {
20994 ga_clear(&ga);
20995 break;
20996 }
20997
20998 /* copy the text up to where the match is */
20999 i = (int)(regmatch.startp[0] - tail);
21000 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21001 /* add the substituted text */
21002 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21003 + ga.ga_len + i, TRUE, TRUE, FALSE);
21004 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 /* avoid getting stuck on a match with an empty string */
21006 if (tail == regmatch.endp[0])
21007 {
21008 if (*tail == NUL)
21009 break;
21010 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21011 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 }
21013 else
21014 {
21015 tail = regmatch.endp[0];
21016 if (*tail == NUL)
21017 break;
21018 }
21019 if (!do_all)
21020 break;
21021 }
21022
21023 if (ga.ga_data != NULL)
21024 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21025
21026 vim_free(regmatch.regprog);
21027 }
21028
21029 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21030 ga_clear(&ga);
21031 p_cpo = save_cpo;
21032
21033 return ret;
21034}
21035
21036#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */