blob: 1f208b9180d6557ee553d9da61d728ad890667de [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 Moolenaar33570922005-01-25 22:26:29 +00001416 argvars = (typval_T *)alloc((unsigned)(argc * 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 Moolenaar33570922005-01-25 22:26:29 +00007321 typval_T argvars[MAX_FUNC_ARGS]; /* 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 Moolenaar33570922005-01-25 22:26:29 +00007378 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 linenr_T firstline; /* first line of range */
7380 linenr_T lastline; /* last line of range */
7381 int *doesrange; /* return: function handled range */
7382 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
7385 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386#define ERROR_UNKNOWN 0
7387#define ERROR_TOOMANY 1
7388#define ERROR_TOOFEW 2
7389#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390#define ERROR_DICT 4
7391#define ERROR_NONE 5
7392#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 int error = ERROR_NONE;
7394 int i;
7395 int llen;
7396 ufunc_T *fp;
7397 int cc;
7398#define FLEN_FIXED 40
7399 char_u fname_buf[FLEN_FIXED + 1];
7400 char_u *fname;
7401
7402 /*
7403 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7404 * Change <SNR>123_name() to K_SNR 123_name().
7405 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7406 */
7407 cc = name[len];
7408 name[len] = NUL;
7409 llen = eval_fname_script(name);
7410 if (llen > 0)
7411 {
7412 fname_buf[0] = K_SPECIAL;
7413 fname_buf[1] = KS_EXTRA;
7414 fname_buf[2] = (int)KE_SNR;
7415 i = 3;
7416 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7417 {
7418 if (current_SID <= 0)
7419 error = ERROR_SCRIPT;
7420 else
7421 {
7422 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7423 i = (int)STRLEN(fname_buf);
7424 }
7425 }
7426 if (i + STRLEN(name + llen) < FLEN_FIXED)
7427 {
7428 STRCPY(fname_buf + i, name + llen);
7429 fname = fname_buf;
7430 }
7431 else
7432 {
7433 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7434 if (fname == NULL)
7435 error = ERROR_OTHER;
7436 else
7437 {
7438 mch_memmove(fname, fname_buf, (size_t)i);
7439 STRCPY(fname + i, name + llen);
7440 }
7441 }
7442 }
7443 else
7444 fname = name;
7445
7446 *doesrange = FALSE;
7447
7448
7449 /* execute the function if no errors detected and executing */
7450 if (evaluate && error == ERROR_NONE)
7451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 error = ERROR_UNKNOWN;
7454
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007455 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
7457 /*
7458 * User defined function.
7459 */
7460 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007461
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007463 /* Trigger FuncUndefined event, may load the function. */
7464 if (fp == NULL
7465 && apply_autocmds(EVENT_FUNCUNDEFINED,
7466 fname, fname, TRUE, NULL)
7467 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007469 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 fp = find_func(fname);
7471 }
7472#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007473 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007474 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007475 {
7476 /* loaded a package, search for the function again */
7477 fp = find_func(fname);
7478 }
7479
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (fp != NULL)
7481 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007482 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007484 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007486 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007488 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 else
7491 {
7492 /*
7493 * Call the user function.
7494 * Save and restore search patterns, script variables and
7495 * redo buffer.
7496 */
7497 save_search_patterns();
7498 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007499 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007500 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007502 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7503 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7504 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007505 /* Function was unreferenced while being used, free it
7506 * now. */
7507 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 restoreRedobuff();
7509 restore_search_patterns();
7510 error = ERROR_NONE;
7511 }
7512 }
7513 }
7514 else
7515 {
7516 /*
7517 * Find the function name in the table, call its implementation.
7518 */
7519 i = find_internal_func(fname);
7520 if (i >= 0)
7521 {
7522 if (argcount < functions[i].f_min_argc)
7523 error = ERROR_TOOFEW;
7524 else if (argcount > functions[i].f_max_argc)
7525 error = ERROR_TOOMANY;
7526 else
7527 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007528 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007529 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 error = ERROR_NONE;
7531 }
7532 }
7533 }
7534 /*
7535 * The function call (or "FuncUndefined" autocommand sequence) might
7536 * have been aborted by an error, an interrupt, or an explicitly thrown
7537 * exception that has not been caught so far. This situation can be
7538 * tested for by calling aborting(). For an error in an internal
7539 * function or for the "E132" error in call_user_func(), however, the
7540 * throw point at which the "force_abort" flag (temporarily reset by
7541 * emsg()) is normally updated has not been reached yet. We need to
7542 * update that flag first to make aborting() reliable.
7543 */
7544 update_force_abort();
7545 }
7546 if (error == ERROR_NONE)
7547 ret = OK;
7548
7549 /*
7550 * Report an error unless the argument evaluation or function call has been
7551 * cancelled due to an aborting error, an interrupt, or an exception.
7552 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 if (!aborting())
7554 {
7555 switch (error)
7556 {
7557 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007558 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 break;
7560 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007561 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 break;
7563 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007564 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 name);
7566 break;
7567 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007568 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 name);
7570 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007572 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007573 name);
7574 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 }
7576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577
7578 name[len] = cc;
7579 if (fname != name && fname != fname_buf)
7580 vim_free(fname);
7581
7582 return ret;
7583}
7584
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007585/*
7586 * Give an error message with a function name. Handle <SNR> things.
7587 */
7588 static void
7589emsg_funcname(msg, name)
7590 char *msg;
7591 char_u *name;
7592{
7593 char_u *p;
7594
7595 if (*name == K_SPECIAL)
7596 p = concat_str((char_u *)"<SNR>", name + 3);
7597 else
7598 p = name;
7599 EMSG2(_(msg), p);
7600 if (p != name)
7601 vim_free(p);
7602}
7603
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604/*********************************************
7605 * Implementation of the built-in functions
7606 */
7607
7608/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007609 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 */
7611 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007612f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007613 typval_T *argvars;
7614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007619 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007621 if ((l = argvars[0].vval.v_list) != NULL
7622 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7623 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007624 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007625 }
7626 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007627 EMSG(_(e_listreq));
7628}
7629
7630/*
7631 * "append(lnum, string/list)" function
7632 */
7633 static void
7634f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007635 typval_T *argvars;
7636 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007637{
7638 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007639 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007640 list_T *l = NULL;
7641 listitem_T *li = NULL;
7642 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007643 long added = 0;
7644
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645 lnum = get_tv_lnum(argvars);
7646 if (lnum >= 0
7647 && lnum <= curbuf->b_ml.ml_line_count
7648 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007650 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007652 l = argvars[1].vval.v_list;
7653 if (l == NULL)
7654 return;
7655 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007656 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007658 for (;;)
7659 {
7660 if (l == NULL)
7661 tv = &argvars[1]; /* append a string */
7662 else if (li == NULL)
7663 break; /* end of list */
7664 else
7665 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007666 line = get_tv_string_chk(tv);
7667 if (line == NULL) /* type error */
7668 {
7669 rettv->vval.v_number = 1; /* Failed */
7670 break;
7671 }
7672 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007673 ++added;
7674 if (l == NULL)
7675 break;
7676 li = li->li_next;
7677 }
7678
7679 appended_lines_mark(lnum, added);
7680 if (curwin->w_cursor.lnum > lnum)
7681 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007683 else
7684 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686
7687/*
7688 * "argc()" function
7689 */
7690/* ARGSUSED */
7691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 typval_T *argvars;
7694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007696 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697}
7698
7699/*
7700 * "argidx()" function
7701 */
7702/* ARGSUSED */
7703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007704f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 typval_T *argvars;
7706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007708 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709}
7710
7711/*
7712 * "argv(nr)" function
7713 */
7714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 typval_T *argvars;
7717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
7719 int idx;
7720
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007721 if (argvars[0].v_type != VAR_UNKNOWN)
7722 {
7723 idx = get_tv_number_chk(&argvars[0], NULL);
7724 if (idx >= 0 && idx < ARGCOUNT)
7725 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7726 else
7727 rettv->vval.v_string = NULL;
7728 rettv->v_type = VAR_STRING;
7729 }
7730 else if (rettv_list_alloc(rettv) == OK)
7731 for (idx = 0; idx < ARGCOUNT; ++idx)
7732 list_append_string(rettv->vval.v_list,
7733 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735
7736/*
7737 * "browse(save, title, initdir, default)" function
7738 */
7739/* ARGSUSED */
7740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 typval_T *argvars;
7743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745#ifdef FEAT_BROWSE
7746 int save;
7747 char_u *title;
7748 char_u *initdir;
7749 char_u *defname;
7750 char_u buf[NUMBUFLEN];
7751 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007752 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007754 save = get_tv_number_chk(&argvars[0], &error);
7755 title = get_tv_string_chk(&argvars[1]);
7756 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7757 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007759 if (error || title == NULL || initdir == NULL || defname == NULL)
7760 rettv->vval.v_string = NULL;
7761 else
7762 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007763 do_browse(save ? BROWSE_SAVE : 0,
7764 title, defname, NULL, initdir, NULL, curbuf);
7765#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007766 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007767#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007769}
7770
7771/*
7772 * "browsedir(title, initdir)" function
7773 */
7774/* ARGSUSED */
7775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 typval_T *argvars;
7778 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007779{
7780#ifdef FEAT_BROWSE
7781 char_u *title;
7782 char_u *initdir;
7783 char_u buf[NUMBUFLEN];
7784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007785 title = get_tv_string_chk(&argvars[0]);
7786 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007788 if (title == NULL || initdir == NULL)
7789 rettv->vval.v_string = NULL;
7790 else
7791 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007792 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007796 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797}
7798
Bram Moolenaar33570922005-01-25 22:26:29 +00007799static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007800
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801/*
7802 * Find a buffer by number or exact name.
7803 */
7804 static buf_T *
7805find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 if (avar->v_type == VAR_NUMBER)
7811 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007812 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007815 if (buf == NULL)
7816 {
7817 /* No full path name match, try a match with a URL or a "nofile"
7818 * buffer, these don't use the full path. */
7819 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7820 if (buf->b_fname != NULL
7821 && (path_with_url(buf->b_fname)
7822#ifdef FEAT_QUICKFIX
7823 || bt_nofile(buf)
7824#endif
7825 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007826 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007827 break;
7828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830 return buf;
7831}
7832
7833/*
7834 * "bufexists(expr)" function
7835 */
7836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007838 typval_T *argvars;
7839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842}
7843
7844/*
7845 * "buflisted(expr)" function
7846 */
7847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007849 typval_T *argvars;
7850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852 buf_T *buf;
7853
7854 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856}
7857
7858/*
7859 * "bufloaded(expr)" function
7860 */
7861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 typval_T *argvars;
7864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 buf_T *buf;
7867
7868 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870}
7871
Bram Moolenaar33570922005-01-25 22:26:29 +00007872static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007873
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874/*
7875 * Get buffer by number or pattern.
7876 */
7877 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007879 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 int save_magic;
7883 char_u *save_cpo;
7884 buf_T *buf;
7885
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007886 if (tv->v_type == VAR_NUMBER)
7887 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007888 if (tv->v_type != VAR_STRING)
7889 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 if (name == NULL || *name == NUL)
7891 return curbuf;
7892 if (name[0] == '$' && name[1] == NUL)
7893 return lastbuf;
7894
7895 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7896 save_magic = p_magic;
7897 p_magic = TRUE;
7898 save_cpo = p_cpo;
7899 p_cpo = (char_u *)"";
7900
7901 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7902 TRUE, FALSE));
7903
7904 p_magic = save_magic;
7905 p_cpo = save_cpo;
7906
7907 /* If not found, try expanding the name, like done for bufexists(). */
7908 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007909 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
7911 return buf;
7912}
7913
7914/*
7915 * "bufname(expr)" function
7916 */
7917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 typval_T *argvars;
7920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
7922 buf_T *buf;
7923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007924 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926 buf = get_buf_tv(&argvars[0]);
7927 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007931 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 --emsg_off;
7933}
7934
7935/*
7936 * "bufnr(expr)" function
7937 */
7938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007940 typval_T *argvars;
7941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
7943 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007944 int error = FALSE;
7945 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007947 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007950 --emsg_off;
7951
7952 /* If the buffer isn't found and the second argument is not zero create a
7953 * new buffer. */
7954 if (buf == NULL
7955 && argvars[1].v_type != VAR_UNKNOWN
7956 && get_tv_number_chk(&argvars[1], &error) != 0
7957 && !error
7958 && (name = get_tv_string_chk(&argvars[0])) != NULL
7959 && !error)
7960 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7961
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966}
7967
7968/*
7969 * "bufwinnr(nr)" function
7970 */
7971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007972f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 typval_T *argvars;
7974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
7976#ifdef FEAT_WINDOWS
7977 win_T *wp;
7978 int winnr = 0;
7979#endif
7980 buf_T *buf;
7981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007982 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007984 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985#ifdef FEAT_WINDOWS
7986 for (wp = firstwin; wp; wp = wp->w_next)
7987 {
7988 ++winnr;
7989 if (wp->w_buffer == buf)
7990 break;
7991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007992 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995#endif
7996 --emsg_off;
7997}
7998
7999/*
8000 * "byte2line(byte)" function
8001 */
8002/*ARGSUSED*/
8003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 typval_T *argvars;
8006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010#else
8011 long boff = 0;
8012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 (linenr_T)0, &boff);
8019#endif
8020}
8021
8022/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008023 * "byteidx()" function
8024 */
8025/*ARGSUSED*/
8026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008028 typval_T *argvars;
8029 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008030{
8031#ifdef FEAT_MBYTE
8032 char_u *t;
8033#endif
8034 char_u *str;
8035 long idx;
8036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008037 str = get_tv_string_chk(&argvars[0]);
8038 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008039 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008040 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008041 return;
8042
8043#ifdef FEAT_MBYTE
8044 t = str;
8045 for ( ; idx > 0; idx--)
8046 {
8047 if (*t == NUL) /* EOL reached */
8048 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008049 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008050 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008051 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008052#else
8053 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008055#endif
8056}
8057
8058/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008059 * "call(func, arglist)" function
8060 */
8061 static void
8062f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008063 typval_T *argvars;
8064 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008065{
8066 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008068 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008069 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008070 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008072
8073 rettv->vval.v_number = 0;
8074 if (argvars[1].v_type != VAR_LIST)
8075 {
8076 EMSG(_(e_listreq));
8077 return;
8078 }
8079 if (argvars[1].vval.v_list == NULL)
8080 return;
8081
8082 if (argvars[0].v_type == VAR_FUNC)
8083 func = argvars[0].vval.v_string;
8084 else
8085 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008086 if (*func == NUL)
8087 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008088
Bram Moolenaare9a41262005-01-15 22:18:47 +00008089 if (argvars[2].v_type != VAR_UNKNOWN)
8090 {
8091 if (argvars[2].v_type != VAR_DICT)
8092 {
8093 EMSG(_(e_dictreq));
8094 return;
8095 }
8096 selfdict = argvars[2].vval.v_dict;
8097 }
8098
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008099 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8100 item = item->li_next)
8101 {
8102 if (argc == MAX_FUNC_ARGS)
8103 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008104 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008105 break;
8106 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008107 /* Make a copy of each argument. This is needed to be able to set
8108 * v_lock to VAR_FIXED in the copy without changing the original list.
8109 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008110 copy_tv(&item->li_tv, &argv[argc++]);
8111 }
8112
8113 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008114 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008115 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8116 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008117
8118 /* Free the arguments. */
8119 while (argc > 0)
8120 clear_tv(&argv[--argc]);
8121}
8122
8123/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008124 * "changenr()" function
8125 */
8126/*ARGSUSED*/
8127 static void
8128f_changenr(argvars, rettv)
8129 typval_T *argvars;
8130 typval_T *rettv;
8131{
8132 rettv->vval.v_number = curbuf->b_u_seq_cur;
8133}
8134
8135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 * "char2nr(string)" function
8137 */
8138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 typval_T *argvars;
8141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143#ifdef FEAT_MBYTE
8144 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008145 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 else
8147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008148 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149}
8150
8151/*
8152 * "cindent(lnum)" function
8153 */
8154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008156 typval_T *argvars;
8157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159#ifdef FEAT_CINDENT
8160 pos_T pos;
8161 linenr_T lnum;
8162
8163 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008164 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8166 {
8167 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008168 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 curwin->w_cursor = pos;
8170 }
8171 else
8172#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174}
8175
8176/*
8177 * "col(string)" function
8178 */
8179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008180f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008181 typval_T *argvars;
8182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183{
8184 colnr_T col = 0;
8185 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008186 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008188 fp = var2fpos(&argvars[0], FALSE, &fnum);
8189 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {
8191 if (fp->col == MAXCOL)
8192 {
8193 /* '> can be MAXCOL, get the length of the line then */
8194 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008195 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 else
8197 col = MAXCOL;
8198 }
8199 else
8200 {
8201 col = fp->col + 1;
8202#ifdef FEAT_VIRTUALEDIT
8203 /* col(".") when the cursor is on the NUL at the end of the line
8204 * because of "coladd" can be seen as an extra column. */
8205 if (virtual_active() && fp == &curwin->w_cursor)
8206 {
8207 char_u *p = ml_get_cursor();
8208
8209 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8210 curwin->w_virtcol - curwin->w_cursor.coladd))
8211 {
8212# ifdef FEAT_MBYTE
8213 int l;
8214
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008215 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 col += l;
8217# else
8218 if (*p != NUL && p[1] == NUL)
8219 ++col;
8220# endif
8221 }
8222 }
8223#endif
8224 }
8225 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008226 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227}
8228
Bram Moolenaar572cb562005-08-05 21:35:02 +00008229#if defined(FEAT_INS_EXPAND)
8230/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008231 * "complete()" function
8232 */
8233/*ARGSUSED*/
8234 static void
8235f_complete(argvars, rettv)
8236 typval_T *argvars;
8237 typval_T *rettv;
8238{
8239 int startcol;
8240
8241 if ((State & INSERT) == 0)
8242 {
8243 EMSG(_("E785: complete() can only be used in Insert mode"));
8244 return;
8245 }
8246 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8247 {
8248 EMSG(_(e_invarg));
8249 return;
8250 }
8251
8252 startcol = get_tv_number_chk(&argvars[0], NULL);
8253 if (startcol <= 0)
8254 return;
8255
8256 set_completion(startcol - 1, argvars[1].vval.v_list);
8257}
8258
8259/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008260 * "complete_add()" function
8261 */
8262/*ARGSUSED*/
8263 static void
8264f_complete_add(argvars, rettv)
8265 typval_T *argvars;
8266 typval_T *rettv;
8267{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008268 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008269}
8270
8271/*
8272 * "complete_check()" function
8273 */
8274/*ARGSUSED*/
8275 static void
8276f_complete_check(argvars, rettv)
8277 typval_T *argvars;
8278 typval_T *rettv;
8279{
8280 int saved = RedrawingDisabled;
8281
8282 RedrawingDisabled = 0;
8283 ins_compl_check_keys(0);
8284 rettv->vval.v_number = compl_interrupted;
8285 RedrawingDisabled = saved;
8286}
8287#endif
8288
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289/*
8290 * "confirm(message, buttons[, default [, type]])" function
8291 */
8292/*ARGSUSED*/
8293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 typval_T *argvars;
8296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8299 char_u *message;
8300 char_u *buttons = NULL;
8301 char_u buf[NUMBUFLEN];
8302 char_u buf2[NUMBUFLEN];
8303 int def = 1;
8304 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008305 char_u *typestr;
8306 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008308 message = get_tv_string_chk(&argvars[0]);
8309 if (message == NULL)
8310 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008313 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8314 if (buttons == NULL)
8315 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008318 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008321 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8322 if (typestr == NULL)
8323 error = TRUE;
8324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008326 switch (TOUPPER_ASC(*typestr))
8327 {
8328 case 'E': type = VIM_ERROR; break;
8329 case 'Q': type = VIM_QUESTION; break;
8330 case 'I': type = VIM_INFO; break;
8331 case 'W': type = VIM_WARNING; break;
8332 case 'G': type = VIM_GENERIC; break;
8333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 }
8335 }
8336 }
8337 }
8338
8339 if (buttons == NULL || *buttons == NUL)
8340 buttons = (char_u *)_("&Ok");
8341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008342 if (error)
8343 rettv->vval.v_number = 0;
8344 else
8345 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 def, NULL);
8347#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008348 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349#endif
8350}
8351
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352/*
8353 * "copy()" function
8354 */
8355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008356f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 typval_T *argvars;
8358 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008359{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008360 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
8363/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008364 * "count()" function
8365 */
8366 static void
8367f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 typval_T *argvars;
8369 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008370{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008371 long n = 0;
8372 int ic = FALSE;
8373
Bram Moolenaare9a41262005-01-15 22:18:47 +00008374 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008375 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008376 listitem_T *li;
8377 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008378 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008379
Bram Moolenaare9a41262005-01-15 22:18:47 +00008380 if ((l = argvars[0].vval.v_list) != NULL)
8381 {
8382 li = l->lv_first;
8383 if (argvars[2].v_type != VAR_UNKNOWN)
8384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 int error = FALSE;
8386
8387 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008388 if (argvars[3].v_type != VAR_UNKNOWN)
8389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008390 idx = get_tv_number_chk(&argvars[3], &error);
8391 if (!error)
8392 {
8393 li = list_find(l, idx);
8394 if (li == NULL)
8395 EMSGN(_(e_listidx), idx);
8396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398 if (error)
8399 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 }
8401
8402 for ( ; li != NULL; li = li->li_next)
8403 if (tv_equal(&li->li_tv, &argvars[1], ic))
8404 ++n;
8405 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008406 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008407 else if (argvars[0].v_type == VAR_DICT)
8408 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008409 int todo;
8410 dict_T *d;
8411 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008413 if ((d = argvars[0].vval.v_dict) != NULL)
8414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008415 int error = FALSE;
8416
Bram Moolenaare9a41262005-01-15 22:18:47 +00008417 if (argvars[2].v_type != VAR_UNKNOWN)
8418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008419 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008420 if (argvars[3].v_type != VAR_UNKNOWN)
8421 EMSG(_(e_invarg));
8422 }
8423
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008424 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008425 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008426 {
8427 if (!HASHITEM_EMPTY(hi))
8428 {
8429 --todo;
8430 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8431 ++n;
8432 }
8433 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008434 }
8435 }
8436 else
8437 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008438 rettv->vval.v_number = n;
8439}
8440
8441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8443 *
8444 * Checks the existence of a cscope connection.
8445 */
8446/*ARGSUSED*/
8447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008448f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008449 typval_T *argvars;
8450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
8452#ifdef FEAT_CSCOPE
8453 int num = 0;
8454 char_u *dbpath = NULL;
8455 char_u *prepend = NULL;
8456 char_u buf[NUMBUFLEN];
8457
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008458 if (argvars[0].v_type != VAR_UNKNOWN
8459 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008461 num = (int)get_tv_number(&argvars[0]);
8462 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008463 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 }
8466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008469 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#endif
8471}
8472
8473/*
8474 * "cursor(lnum, col)" function
8475 *
8476 * Moves the cursor to the specified line and column
8477 */
8478/*ARGSUSED*/
8479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008480f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 typval_T *argvars;
8482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483{
8484 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008485#ifdef FEAT_VIRTUALEDIT
8486 long coladd = 0;
8487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488
Bram Moolenaara5525202006-03-02 22:52:09 +00008489 if (argvars[1].v_type == VAR_UNKNOWN)
8490 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008491 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008492
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008493 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008494 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008495 line = pos.lnum;
8496 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008497#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008498 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008499#endif
8500 }
8501 else
8502 {
8503 line = get_tv_lnum(argvars);
8504 col = get_tv_number_chk(&argvars[1], NULL);
8505#ifdef FEAT_VIRTUALEDIT
8506 if (argvars[2].v_type != VAR_UNKNOWN)
8507 coladd = get_tv_number_chk(&argvars[2], NULL);
8508#endif
8509 }
8510 if (line < 0 || col < 0
8511#ifdef FEAT_VIRTUALEDIT
8512 || coladd < 0
8513#endif
8514 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 if (line > 0)
8517 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (col > 0)
8519 curwin->w_cursor.col = col - 1;
8520#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008521 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522#endif
8523
8524 /* Make sure the cursor is in a valid position. */
8525 check_cursor();
8526#ifdef FEAT_MBYTE
8527 /* Correct cursor for multi-byte character. */
8528 if (has_mbyte)
8529 mb_adjust_cursor();
8530#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008531
8532 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533}
8534
8535/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 */
8538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T *argvars;
8541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008543 int noref = 0;
8544
8545 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008546 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008547 if (noref < 0 || noref > 1)
8548 EMSG(_(e_invarg));
8549 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008550 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
8552
8553/*
8554 * "delete()" function
8555 */
8556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008558 typval_T *argvars;
8559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008564 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565}
8566
8567/*
8568 * "did_filetype()" function
8569 */
8570/*ARGSUSED*/
8571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 typval_T *argvars;
8574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#endif
8581}
8582
8583/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008584 * "diff_filler()" function
8585 */
8586/*ARGSUSED*/
8587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008589 typval_T *argvars;
8590 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008591{
8592#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008594#endif
8595}
8596
8597/*
8598 * "diff_hlID()" function
8599 */
8600/*ARGSUSED*/
8601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 typval_T *argvars;
8604 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008605{
8606#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008607 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008608 static linenr_T prev_lnum = 0;
8609 static int changedtick = 0;
8610 static int fnum = 0;
8611 static int change_start = 0;
8612 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008613 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008614 int filler_lines;
8615 int col;
8616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008617 if (lnum < 0) /* ignore type error in {lnum} arg */
8618 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008619 if (lnum != prev_lnum
8620 || changedtick != curbuf->b_changedtick
8621 || fnum != curbuf->b_fnum)
8622 {
8623 /* New line, buffer, change: need to get the values. */
8624 filler_lines = diff_check(curwin, lnum);
8625 if (filler_lines < 0)
8626 {
8627 if (filler_lines == -1)
8628 {
8629 change_start = MAXCOL;
8630 change_end = -1;
8631 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8632 hlID = HLF_ADD; /* added line */
8633 else
8634 hlID = HLF_CHD; /* changed line */
8635 }
8636 else
8637 hlID = HLF_ADD; /* added line */
8638 }
8639 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008640 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008641 prev_lnum = lnum;
8642 changedtick = curbuf->b_changedtick;
8643 fnum = curbuf->b_fnum;
8644 }
8645
8646 if (hlID == HLF_CHD || hlID == HLF_TXD)
8647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008648 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008649 if (col >= change_start && col <= change_end)
8650 hlID = HLF_TXD; /* changed text */
8651 else
8652 hlID = HLF_CHD; /* changed line */
8653 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008654 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008655#endif
8656}
8657
8658/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008659 * "empty({expr})" function
8660 */
8661 static void
8662f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 typval_T *argvars;
8664 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008665{
8666 int n;
8667
8668 switch (argvars[0].v_type)
8669 {
8670 case VAR_STRING:
8671 case VAR_FUNC:
8672 n = argvars[0].vval.v_string == NULL
8673 || *argvars[0].vval.v_string == NUL;
8674 break;
8675 case VAR_NUMBER:
8676 n = argvars[0].vval.v_number == 0;
8677 break;
8678 case VAR_LIST:
8679 n = argvars[0].vval.v_list == NULL
8680 || argvars[0].vval.v_list->lv_first == NULL;
8681 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008682 case VAR_DICT:
8683 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008684 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008685 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008686 default:
8687 EMSG2(_(e_intern2), "f_empty()");
8688 n = 0;
8689 }
8690
8691 rettv->vval.v_number = n;
8692}
8693
8694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 * "escape({string}, {chars})" function
8696 */
8697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008699 typval_T *argvars;
8700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 char_u buf[NUMBUFLEN];
8703
Bram Moolenaar758711c2005-02-02 23:11:38 +00008704 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8705 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707}
8708
8709/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008710 * "eval()" function
8711 */
8712/*ARGSUSED*/
8713 static void
8714f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 typval_T *argvars;
8716 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008717{
8718 char_u *s;
8719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 s = get_tv_string_chk(&argvars[0]);
8721 if (s != NULL)
8722 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008724 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8725 {
8726 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008727 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008728 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008729 else if (*s != NUL)
8730 EMSG(_(e_trailing));
8731}
8732
8733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 * "eventhandler()" function
8735 */
8736/*ARGSUSED*/
8737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008739 typval_T *argvars;
8740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743}
8744
8745/*
8746 * "executable()" function
8747 */
8748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 typval_T *argvars;
8751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754}
8755
8756/*
8757 * "exists()" function
8758 */
8759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008761 typval_T *argvars;
8762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
8764 char_u *p;
8765 char_u *name;
8766 int n = FALSE;
8767 int len = 0;
8768
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (*p == '$') /* environment variable */
8771 {
8772 /* first try "normal" environment variables (fast) */
8773 if (mch_getenv(p + 1) != NULL)
8774 n = TRUE;
8775 else
8776 {
8777 /* try expanding things like $VIM and ${HOME} */
8778 p = expand_env_save(p);
8779 if (p != NULL && *p != '$')
8780 n = TRUE;
8781 vim_free(p);
8782 }
8783 }
8784 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 else if (*p == '*') /* internal or user defined function */
8787 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008788 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
8790 else if (*p == ':')
8791 {
8792 n = cmd_exists(p + 1);
8793 }
8794 else if (*p == '#')
8795 {
8796#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008797 if (p[1] == '#')
8798 n = autocmd_supported(p + 2);
8799 else
8800 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801#endif
8802 }
8803 else /* internal variable */
8804 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008805 char_u *tofree;
8806 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008808 /* get_name_len() takes care of expanding curly braces */
8809 name = p;
8810 len = get_name_len(&p, &tofree, TRUE, FALSE);
8811 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008813 if (tofree != NULL)
8814 name = tofree;
8815 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8816 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008818 /* handle d.key, l[idx], f(expr) */
8819 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8820 if (n)
8821 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
8823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008825 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 }
8827
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
8831/*
8832 * "expand()" function
8833 */
8834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *argvars;
8837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 char_u *s;
8840 int len;
8841 char_u *errormsg;
8842 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8843 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008844 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->v_type = VAR_STRING;
8847 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 if (*s == '%' || *s == '#' || *s == '<')
8849 {
8850 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 --emsg_off;
8853 }
8854 else
8855 {
8856 /* When the optional second argument is non-zero, don't remove matches
8857 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008858 if (argvars[1].v_type != VAR_UNKNOWN
8859 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008861 if (!error)
8862 {
8863 ExpandInit(&xpc);
8864 xpc.xp_context = EXPAND_FILES;
8865 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8866 ExpandCleanup(&xpc);
8867 }
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 {
8947 EMSGN(_(e_invarg2), action);
8948 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);
10400 ExpandCleanup(&xpc);
10401}
10402
10403/*
10404 * "globpath()" function
10405 */
10406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010407f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 typval_T *argvars;
10409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410{
10411 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010412 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010415 if (file == NULL)
10416 rettv->vval.v_string = NULL;
10417 else
10418 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419}
10420
10421/*
10422 * "has()" function
10423 */
10424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 typval_T *argvars;
10427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428{
10429 int i;
10430 char_u *name;
10431 int n = FALSE;
10432 static char *(has_list[]) =
10433 {
10434#ifdef AMIGA
10435 "amiga",
10436# ifdef FEAT_ARP
10437 "arp",
10438# endif
10439#endif
10440#ifdef __BEOS__
10441 "beos",
10442#endif
10443#ifdef MSDOS
10444# ifdef DJGPP
10445 "dos32",
10446# else
10447 "dos16",
10448# endif
10449#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010450#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 "mac",
10452#endif
10453#if defined(MACOS_X_UNIX)
10454 "macunix",
10455#endif
10456#ifdef OS2
10457 "os2",
10458#endif
10459#ifdef __QNX__
10460 "qnx",
10461#endif
10462#ifdef RISCOS
10463 "riscos",
10464#endif
10465#ifdef UNIX
10466 "unix",
10467#endif
10468#ifdef VMS
10469 "vms",
10470#endif
10471#ifdef WIN16
10472 "win16",
10473#endif
10474#ifdef WIN32
10475 "win32",
10476#endif
10477#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10478 "win32unix",
10479#endif
10480#ifdef WIN64
10481 "win64",
10482#endif
10483#ifdef EBCDIC
10484 "ebcdic",
10485#endif
10486#ifndef CASE_INSENSITIVE_FILENAME
10487 "fname_case",
10488#endif
10489#ifdef FEAT_ARABIC
10490 "arabic",
10491#endif
10492#ifdef FEAT_AUTOCMD
10493 "autocmd",
10494#endif
10495#ifdef FEAT_BEVAL
10496 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010497# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10498 "balloon_multiline",
10499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500#endif
10501#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10502 "builtin_terms",
10503# ifdef ALL_BUILTIN_TCAPS
10504 "all_builtin_terms",
10505# endif
10506#endif
10507#ifdef FEAT_BYTEOFF
10508 "byte_offset",
10509#endif
10510#ifdef FEAT_CINDENT
10511 "cindent",
10512#endif
10513#ifdef FEAT_CLIENTSERVER
10514 "clientserver",
10515#endif
10516#ifdef FEAT_CLIPBOARD
10517 "clipboard",
10518#endif
10519#ifdef FEAT_CMDL_COMPL
10520 "cmdline_compl",
10521#endif
10522#ifdef FEAT_CMDHIST
10523 "cmdline_hist",
10524#endif
10525#ifdef FEAT_COMMENTS
10526 "comments",
10527#endif
10528#ifdef FEAT_CRYPT
10529 "cryptv",
10530#endif
10531#ifdef FEAT_CSCOPE
10532 "cscope",
10533#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010534#ifdef CURSOR_SHAPE
10535 "cursorshape",
10536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537#ifdef DEBUG
10538 "debug",
10539#endif
10540#ifdef FEAT_CON_DIALOG
10541 "dialog_con",
10542#endif
10543#ifdef FEAT_GUI_DIALOG
10544 "dialog_gui",
10545#endif
10546#ifdef FEAT_DIFF
10547 "diff",
10548#endif
10549#ifdef FEAT_DIGRAPHS
10550 "digraphs",
10551#endif
10552#ifdef FEAT_DND
10553 "dnd",
10554#endif
10555#ifdef FEAT_EMACS_TAGS
10556 "emacs_tags",
10557#endif
10558 "eval", /* always present, of course! */
10559#ifdef FEAT_EX_EXTRA
10560 "ex_extra",
10561#endif
10562#ifdef FEAT_SEARCH_EXTRA
10563 "extra_search",
10564#endif
10565#ifdef FEAT_FKMAP
10566 "farsi",
10567#endif
10568#ifdef FEAT_SEARCHPATH
10569 "file_in_path",
10570#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010571#if defined(UNIX) && !defined(USE_SYSTEM)
10572 "filterpipe",
10573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574#ifdef FEAT_FIND_ID
10575 "find_in_path",
10576#endif
10577#ifdef FEAT_FOLDING
10578 "folding",
10579#endif
10580#ifdef FEAT_FOOTER
10581 "footer",
10582#endif
10583#if !defined(USE_SYSTEM) && defined(UNIX)
10584 "fork",
10585#endif
10586#ifdef FEAT_GETTEXT
10587 "gettext",
10588#endif
10589#ifdef FEAT_GUI
10590 "gui",
10591#endif
10592#ifdef FEAT_GUI_ATHENA
10593# ifdef FEAT_GUI_NEXTAW
10594 "gui_neXtaw",
10595# else
10596 "gui_athena",
10597# endif
10598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599#ifdef FEAT_GUI_GTK
10600 "gui_gtk",
10601# ifdef HAVE_GTK2
10602 "gui_gtk2",
10603# endif
10604#endif
10605#ifdef FEAT_GUI_MAC
10606 "gui_mac",
10607#endif
10608#ifdef FEAT_GUI_MOTIF
10609 "gui_motif",
10610#endif
10611#ifdef FEAT_GUI_PHOTON
10612 "gui_photon",
10613#endif
10614#ifdef FEAT_GUI_W16
10615 "gui_win16",
10616#endif
10617#ifdef FEAT_GUI_W32
10618 "gui_win32",
10619#endif
10620#ifdef FEAT_HANGULIN
10621 "hangul_input",
10622#endif
10623#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10624 "iconv",
10625#endif
10626#ifdef FEAT_INS_EXPAND
10627 "insert_expand",
10628#endif
10629#ifdef FEAT_JUMPLIST
10630 "jumplist",
10631#endif
10632#ifdef FEAT_KEYMAP
10633 "keymap",
10634#endif
10635#ifdef FEAT_LANGMAP
10636 "langmap",
10637#endif
10638#ifdef FEAT_LIBCALL
10639 "libcall",
10640#endif
10641#ifdef FEAT_LINEBREAK
10642 "linebreak",
10643#endif
10644#ifdef FEAT_LISP
10645 "lispindent",
10646#endif
10647#ifdef FEAT_LISTCMDS
10648 "listcmds",
10649#endif
10650#ifdef FEAT_LOCALMAP
10651 "localmap",
10652#endif
10653#ifdef FEAT_MENU
10654 "menu",
10655#endif
10656#ifdef FEAT_SESSION
10657 "mksession",
10658#endif
10659#ifdef FEAT_MODIFY_FNAME
10660 "modify_fname",
10661#endif
10662#ifdef FEAT_MOUSE
10663 "mouse",
10664#endif
10665#ifdef FEAT_MOUSESHAPE
10666 "mouseshape",
10667#endif
10668#if defined(UNIX) || defined(VMS)
10669# ifdef FEAT_MOUSE_DEC
10670 "mouse_dec",
10671# endif
10672# ifdef FEAT_MOUSE_GPM
10673 "mouse_gpm",
10674# endif
10675# ifdef FEAT_MOUSE_JSB
10676 "mouse_jsbterm",
10677# endif
10678# ifdef FEAT_MOUSE_NET
10679 "mouse_netterm",
10680# endif
10681# ifdef FEAT_MOUSE_PTERM
10682 "mouse_pterm",
10683# endif
10684# ifdef FEAT_MOUSE_XTERM
10685 "mouse_xterm",
10686# endif
10687#endif
10688#ifdef FEAT_MBYTE
10689 "multi_byte",
10690#endif
10691#ifdef FEAT_MBYTE_IME
10692 "multi_byte_ime",
10693#endif
10694#ifdef FEAT_MULTI_LANG
10695 "multi_lang",
10696#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010697#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010698#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010699 "mzscheme",
10700#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702#ifdef FEAT_OLE
10703 "ole",
10704#endif
10705#ifdef FEAT_OSFILETYPE
10706 "osfiletype",
10707#endif
10708#ifdef FEAT_PATH_EXTRA
10709 "path_extra",
10710#endif
10711#ifdef FEAT_PERL
10712#ifndef DYNAMIC_PERL
10713 "perl",
10714#endif
10715#endif
10716#ifdef FEAT_PYTHON
10717#ifndef DYNAMIC_PYTHON
10718 "python",
10719#endif
10720#endif
10721#ifdef FEAT_POSTSCRIPT
10722 "postscript",
10723#endif
10724#ifdef FEAT_PRINTER
10725 "printer",
10726#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010727#ifdef FEAT_PROFILE
10728 "profile",
10729#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010730#ifdef FEAT_RELTIME
10731 "reltime",
10732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733#ifdef FEAT_QUICKFIX
10734 "quickfix",
10735#endif
10736#ifdef FEAT_RIGHTLEFT
10737 "rightleft",
10738#endif
10739#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10740 "ruby",
10741#endif
10742#ifdef FEAT_SCROLLBIND
10743 "scrollbind",
10744#endif
10745#ifdef FEAT_CMDL_INFO
10746 "showcmd",
10747 "cmdline_info",
10748#endif
10749#ifdef FEAT_SIGNS
10750 "signs",
10751#endif
10752#ifdef FEAT_SMARTINDENT
10753 "smartindent",
10754#endif
10755#ifdef FEAT_SNIFF
10756 "sniff",
10757#endif
10758#ifdef FEAT_STL_OPT
10759 "statusline",
10760#endif
10761#ifdef FEAT_SUN_WORKSHOP
10762 "sun_workshop",
10763#endif
10764#ifdef FEAT_NETBEANS_INTG
10765 "netbeans_intg",
10766#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010767#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010768 "spell",
10769#endif
10770#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 "syntax",
10772#endif
10773#if defined(USE_SYSTEM) || !defined(UNIX)
10774 "system",
10775#endif
10776#ifdef FEAT_TAG_BINS
10777 "tag_binary",
10778#endif
10779#ifdef FEAT_TAG_OLDSTATIC
10780 "tag_old_static",
10781#endif
10782#ifdef FEAT_TAG_ANYWHITE
10783 "tag_any_white",
10784#endif
10785#ifdef FEAT_TCL
10786# ifndef DYNAMIC_TCL
10787 "tcl",
10788# endif
10789#endif
10790#ifdef TERMINFO
10791 "terminfo",
10792#endif
10793#ifdef FEAT_TERMRESPONSE
10794 "termresponse",
10795#endif
10796#ifdef FEAT_TEXTOBJ
10797 "textobjects",
10798#endif
10799#ifdef HAVE_TGETENT
10800 "tgetent",
10801#endif
10802#ifdef FEAT_TITLE
10803 "title",
10804#endif
10805#ifdef FEAT_TOOLBAR
10806 "toolbar",
10807#endif
10808#ifdef FEAT_USR_CMDS
10809 "user-commands", /* was accidentally included in 5.4 */
10810 "user_commands",
10811#endif
10812#ifdef FEAT_VIMINFO
10813 "viminfo",
10814#endif
10815#ifdef FEAT_VERTSPLIT
10816 "vertsplit",
10817#endif
10818#ifdef FEAT_VIRTUALEDIT
10819 "virtualedit",
10820#endif
10821#ifdef FEAT_VISUAL
10822 "visual",
10823#endif
10824#ifdef FEAT_VISUALEXTRA
10825 "visualextra",
10826#endif
10827#ifdef FEAT_VREPLACE
10828 "vreplace",
10829#endif
10830#ifdef FEAT_WILDIGN
10831 "wildignore",
10832#endif
10833#ifdef FEAT_WILDMENU
10834 "wildmenu",
10835#endif
10836#ifdef FEAT_WINDOWS
10837 "windows",
10838#endif
10839#ifdef FEAT_WAK
10840 "winaltkeys",
10841#endif
10842#ifdef FEAT_WRITEBACKUP
10843 "writebackup",
10844#endif
10845#ifdef FEAT_XIM
10846 "xim",
10847#endif
10848#ifdef FEAT_XFONTSET
10849 "xfontset",
10850#endif
10851#ifdef USE_XSMP
10852 "xsmp",
10853#endif
10854#ifdef USE_XSMP_INTERACT
10855 "xsmp_interact",
10856#endif
10857#ifdef FEAT_XCLIPBOARD
10858 "xterm_clipboard",
10859#endif
10860#ifdef FEAT_XTERM_SAVE
10861 "xterm_save",
10862#endif
10863#if defined(UNIX) && defined(FEAT_X11)
10864 "X11",
10865#endif
10866 NULL
10867 };
10868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 for (i = 0; has_list[i] != NULL; ++i)
10871 if (STRICMP(name, has_list[i]) == 0)
10872 {
10873 n = TRUE;
10874 break;
10875 }
10876
10877 if (n == FALSE)
10878 {
10879 if (STRNICMP(name, "patch", 5) == 0)
10880 n = has_patch(atoi((char *)name + 5));
10881 else if (STRICMP(name, "vim_starting") == 0)
10882 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010883#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10884 else if (STRICMP(name, "balloon_multiline") == 0)
10885 n = multiline_balloon_available();
10886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887#ifdef DYNAMIC_TCL
10888 else if (STRICMP(name, "tcl") == 0)
10889 n = tcl_enabled(FALSE);
10890#endif
10891#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10892 else if (STRICMP(name, "iconv") == 0)
10893 n = iconv_enabled(FALSE);
10894#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010895#ifdef DYNAMIC_MZSCHEME
10896 else if (STRICMP(name, "mzscheme") == 0)
10897 n = mzscheme_enabled(FALSE);
10898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899#ifdef DYNAMIC_RUBY
10900 else if (STRICMP(name, "ruby") == 0)
10901 n = ruby_enabled(FALSE);
10902#endif
10903#ifdef DYNAMIC_PYTHON
10904 else if (STRICMP(name, "python") == 0)
10905 n = python_enabled(FALSE);
10906#endif
10907#ifdef DYNAMIC_PERL
10908 else if (STRICMP(name, "perl") == 0)
10909 n = perl_enabled(FALSE);
10910#endif
10911#ifdef FEAT_GUI
10912 else if (STRICMP(name, "gui_running") == 0)
10913 n = (gui.in_use || gui.starting);
10914# ifdef FEAT_GUI_W32
10915 else if (STRICMP(name, "gui_win32s") == 0)
10916 n = gui_is_win32s();
10917# endif
10918# ifdef FEAT_BROWSE
10919 else if (STRICMP(name, "browse") == 0)
10920 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10921# endif
10922#endif
10923#ifdef FEAT_SYN_HL
10924 else if (STRICMP(name, "syntax_items") == 0)
10925 n = syntax_present(curbuf);
10926#endif
10927#if defined(WIN3264)
10928 else if (STRICMP(name, "win95") == 0)
10929 n = mch_windows95();
10930#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010931#ifdef FEAT_NETBEANS_INTG
10932 else if (STRICMP(name, "netbeans_enabled") == 0)
10933 n = usingNetbeans;
10934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 }
10936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938}
10939
10940/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 * "has_key()" function
10942 */
10943 static void
10944f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010945 typval_T *argvars;
10946 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947{
10948 rettv->vval.v_number = 0;
10949 if (argvars[0].v_type != VAR_DICT)
10950 {
10951 EMSG(_(e_dictreq));
10952 return;
10953 }
10954 if (argvars[0].vval.v_dict == NULL)
10955 return;
10956
10957 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010958 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010959}
10960
10961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 * "hasmapto()" function
10963 */
10964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *argvars;
10967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968{
10969 char_u *name;
10970 char_u *mode;
10971 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010972 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010975 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 mode = (char_u *)"nvo";
10977 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010980 if (argvars[2].v_type != VAR_UNKNOWN)
10981 abbr = get_tv_number(&argvars[2]);
10982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983
Bram Moolenaar2c932302006-03-18 21:42:09 +000010984 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
10990/*
10991 * "histadd()" function
10992 */
10993/*ARGSUSED*/
10994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010996 typval_T *argvars;
10997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998{
10999#ifdef FEAT_CMDHIST
11000 int histype;
11001 char_u *str;
11002 char_u buf[NUMBUFLEN];
11003#endif
11004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 if (check_restricted() || check_secure())
11007 return;
11008#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11010 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 if (histype >= 0)
11012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 if (*str != NUL)
11015 {
11016 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 return;
11019 }
11020 }
11021#endif
11022}
11023
11024/*
11025 * "histdel()" function
11026 */
11027/*ARGSUSED*/
11028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 typval_T *argvars;
11031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
11033#ifdef FEAT_CMDHIST
11034 int n;
11035 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011036 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11039 if (str == NULL)
11040 n = 0;
11041 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011044 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 else
11049 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011050 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 get_tv_string_buf(&argvars[1], buf));
11052 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055#endif
11056}
11057
11058/*
11059 * "histget()" function
11060 */
11061/*ARGSUSED*/
11062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 typval_T *argvars;
11065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066{
11067#ifdef FEAT_CMDHIST
11068 int type;
11069 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11073 if (str == NULL)
11074 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 {
11077 type = get_histtype(str);
11078 if (argvars[1].v_type == VAR_UNKNOWN)
11079 idx = get_history_idx(type);
11080 else
11081 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11082 /* -1 on type error */
11083 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089}
11090
11091/*
11092 * "histnr()" function
11093 */
11094/*ARGSUSED*/
11095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 typval_T *argvars;
11098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099{
11100 int i;
11101
11102#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011103 char_u *history = get_tv_string_chk(&argvars[0]);
11104
11105 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 if (i >= HIST_CMD && i < HIST_COUNT)
11107 i = get_history_idx(i);
11108 else
11109#endif
11110 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112}
11113
11114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 * "highlightID(name)" function
11116 */
11117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011118f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 typval_T *argvars;
11120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123}
11124
11125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126 * "highlight_exists()" function
11127 */
11128 static void
11129f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011130 typval_T *argvars;
11131 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011132{
11133 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11134}
11135
11136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 * "hostname()" function
11138 */
11139/*ARGSUSED*/
11140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 typval_T *argvars;
11143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144{
11145 char_u hostname[256];
11146
11147 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->v_type = VAR_STRING;
11149 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150}
11151
11152/*
11153 * iconv() function
11154 */
11155/*ARGSUSED*/
11156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 typval_T *argvars;
11159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160{
11161#ifdef FEAT_MBYTE
11162 char_u buf1[NUMBUFLEN];
11163 char_u buf2[NUMBUFLEN];
11164 char_u *from, *to, *str;
11165 vimconv_T vimconv;
11166#endif
11167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 rettv->v_type = VAR_STRING;
11169 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170
11171#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 str = get_tv_string(&argvars[0]);
11173 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11174 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 vimconv.vc_type = CONV_NONE;
11176 convert_setup(&vimconv, from, to);
11177
11178 /* If the encodings are equal, no conversion needed. */
11179 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011180 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011182 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183
11184 convert_setup(&vimconv, NULL, NULL);
11185 vim_free(from);
11186 vim_free(to);
11187#endif
11188}
11189
11190/*
11191 * "indent()" function
11192 */
11193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011195 typval_T *argvars;
11196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197{
11198 linenr_T lnum;
11199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205}
11206
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011207/*
11208 * "index()" function
11209 */
11210 static void
11211f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 typval_T *argvars;
11213 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011214{
Bram Moolenaar33570922005-01-25 22:26:29 +000011215 list_T *l;
11216 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011217 long idx = 0;
11218 int ic = FALSE;
11219
11220 rettv->vval.v_number = -1;
11221 if (argvars[0].v_type != VAR_LIST)
11222 {
11223 EMSG(_(e_listreq));
11224 return;
11225 }
11226 l = argvars[0].vval.v_list;
11227 if (l != NULL)
11228 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011229 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011230 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 int error = FALSE;
11233
Bram Moolenaar758711c2005-02-02 23:11:38 +000011234 /* Start at specified item. Use the cached index that list_find()
11235 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011236 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011237 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011238 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 ic = get_tv_number_chk(&argvars[3], &error);
11240 if (error)
11241 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011242 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011243
Bram Moolenaar758711c2005-02-02 23:11:38 +000011244 for ( ; item != NULL; item = item->li_next, ++idx)
11245 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011246 {
11247 rettv->vval.v_number = idx;
11248 break;
11249 }
11250 }
11251}
11252
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253static int inputsecret_flag = 0;
11254
11255/*
11256 * "input()" function
11257 * Also handles inputsecret() when inputsecret is set.
11258 */
11259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011261 typval_T *argvars;
11262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 char_u *p = NULL;
11266 int c;
11267 char_u buf[NUMBUFLEN];
11268 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011270 int xp_type = EXPAND_NOTHING;
11271 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274
11275#ifdef NO_CONSOLE_INPUT
11276 /* While starting up, there is no place to enter text. */
11277 if (no_console_input())
11278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 return;
11281 }
11282#endif
11283
11284 cmd_silent = FALSE; /* Want to see the prompt. */
11285 if (prompt != NULL)
11286 {
11287 /* Only the part of the message after the last NL is considered as
11288 * prompt for the command line */
11289 p = vim_strrchr(prompt, '\n');
11290 if (p == NULL)
11291 p = prompt;
11292 else
11293 {
11294 ++p;
11295 c = *p;
11296 *p = NUL;
11297 msg_start();
11298 msg_clr_eos();
11299 msg_puts_attr(prompt, echo_attr);
11300 msg_didout = FALSE;
11301 msg_starthere();
11302 *p = c;
11303 }
11304 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 if (argvars[1].v_type != VAR_UNKNOWN)
11307 {
11308 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11309 if (defstr != NULL)
11310 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
Bram Moolenaar4463f292005-09-25 22:20:24 +000011312 if (argvars[2].v_type != VAR_UNKNOWN)
11313 {
11314 char_u *xp_name;
11315 int xp_namelen;
11316 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011317
Bram Moolenaar4463f292005-09-25 22:20:24 +000011318 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011319
Bram Moolenaar4463f292005-09-25 22:20:24 +000011320 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11321 if (xp_name == NULL)
11322 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011324 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011325
Bram Moolenaar4463f292005-09-25 22:20:24 +000011326 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11327 &xp_arg) == FAIL)
11328 return;
11329 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011330 }
11331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 if (defstr != NULL)
11333 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011334 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11335 xp_type, xp_arg);
11336
11337 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 /* since the user typed this, no need to wait for return */
11340 need_wait_return = FALSE;
11341 msg_didout = FALSE;
11342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 cmd_silent = cmd_silent_save;
11344}
11345
11346/*
11347 * "inputdialog()" function
11348 */
11349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *argvars;
11352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
11354#if defined(FEAT_GUI_TEXTDIALOG)
11355 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11356 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11357 {
11358 char_u *message;
11359 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 message = get_tv_string_chk(&argvars[0]);
11363 if (argvars[1].v_type != VAR_UNKNOWN
11364 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011365 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 else
11367 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 if (message != NULL && defstr != NULL
11369 && do_dialog(VIM_QUESTION, NULL, message,
11370 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 else
11373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 if (message != NULL && defstr != NULL
11375 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011376 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->vval.v_string = vim_strsave(
11378 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 }
11384 else
11385#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387}
11388
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011389/*
11390 * "inputlist()" function
11391 */
11392 static void
11393f_inputlist(argvars, rettv)
11394 typval_T *argvars;
11395 typval_T *rettv;
11396{
11397 listitem_T *li;
11398 int selected;
11399 int mouse_used;
11400
11401 rettv->vval.v_number = 0;
11402#ifdef NO_CONSOLE_INPUT
11403 /* While starting up, there is no place to enter text. */
11404 if (no_console_input())
11405 return;
11406#endif
11407 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11408 {
11409 EMSG2(_(e_listarg), "inputlist()");
11410 return;
11411 }
11412
11413 msg_start();
11414 lines_left = Rows; /* avoid more prompt */
11415 msg_scroll = TRUE;
11416 msg_clr_eos();
11417
11418 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11419 {
11420 msg_puts(get_tv_string(&li->li_tv));
11421 msg_putchar('\n');
11422 }
11423
11424 /* Ask for choice. */
11425 selected = prompt_for_number(&mouse_used);
11426 if (mouse_used)
11427 selected -= lines_left;
11428
11429 rettv->vval.v_number = selected;
11430}
11431
11432
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11434
11435/*
11436 * "inputrestore()" function
11437 */
11438/*ARGSUSED*/
11439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 typval_T *argvars;
11442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 if (ga_userinput.ga_len > 0)
11445 {
11446 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11448 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 }
11451 else if (p_verbose > 1)
11452 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011453 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 }
11456}
11457
11458/*
11459 * "inputsave()" function
11460 */
11461/*ARGSUSED*/
11462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T *argvars;
11465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466{
11467 /* Add an entry to the stack of typehead storage. */
11468 if (ga_grow(&ga_userinput, 1) == OK)
11469 {
11470 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11471 + ga_userinput.ga_len);
11472 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 }
11475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477}
11478
11479/*
11480 * "inputsecret()" function
11481 */
11482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011484 typval_T *argvars;
11485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
11487 ++cmdline_star;
11488 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 --cmdline_star;
11491 --inputsecret_flag;
11492}
11493
11494/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011495 * "insert()" function
11496 */
11497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 typval_T *argvars;
11500 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011501{
11502 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 listitem_T *item;
11504 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011505 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011506
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011507 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011508 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011509 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011510 else if ((l = argvars[0].vval.v_list) != NULL
11511 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011512 {
11513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 before = get_tv_number_chk(&argvars[2], &error);
11515 if (error)
11516 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011517
Bram Moolenaar758711c2005-02-02 23:11:38 +000011518 if (before == l->lv_len)
11519 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011520 else
11521 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011522 item = list_find(l, before);
11523 if (item == NULL)
11524 {
11525 EMSGN(_(e_listidx), before);
11526 l = NULL;
11527 }
11528 }
11529 if (l != NULL)
11530 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011531 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011532 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011533 }
11534 }
11535}
11536
11537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 * "isdirectory()" function
11539 */
11540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *argvars;
11543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546}
11547
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011548/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011549 * "islocked()" function
11550 */
11551 static void
11552f_islocked(argvars, rettv)
11553 typval_T *argvars;
11554 typval_T *rettv;
11555{
11556 lval_T lv;
11557 char_u *end;
11558 dictitem_T *di;
11559
11560 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011561 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11562 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011563 if (end != NULL && lv.ll_name != NULL)
11564 {
11565 if (*end != NUL)
11566 EMSG(_(e_trailing));
11567 else
11568 {
11569 if (lv.ll_tv == NULL)
11570 {
11571 if (check_changedtick(lv.ll_name))
11572 rettv->vval.v_number = 1; /* always locked */
11573 else
11574 {
11575 di = find_var(lv.ll_name, NULL);
11576 if (di != NULL)
11577 {
11578 /* Consider a variable locked when:
11579 * 1. the variable itself is locked
11580 * 2. the value of the variable is locked.
11581 * 3. the List or Dict value is locked.
11582 */
11583 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11584 || tv_islocked(&di->di_tv));
11585 }
11586 }
11587 }
11588 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011589 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011590 else if (lv.ll_newkey != NULL)
11591 EMSG2(_(e_dictkey), lv.ll_newkey);
11592 else if (lv.ll_list != NULL)
11593 /* List item. */
11594 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11595 else
11596 /* Dictionary item. */
11597 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11598 }
11599 }
11600
11601 clear_lval(&lv);
11602}
11603
Bram Moolenaar33570922005-01-25 22:26:29 +000011604static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011605
11606/*
11607 * Turn a dict into a list:
11608 * "what" == 0: list of keys
11609 * "what" == 1: list of values
11610 * "what" == 2: list of items
11611 */
11612 static void
11613dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011614 typval_T *argvars;
11615 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011616 int what;
11617{
Bram Moolenaar33570922005-01-25 22:26:29 +000011618 list_T *l2;
11619 dictitem_T *di;
11620 hashitem_T *hi;
11621 listitem_T *li;
11622 listitem_T *li2;
11623 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011624 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011625
11626 rettv->vval.v_number = 0;
11627 if (argvars[0].v_type != VAR_DICT)
11628 {
11629 EMSG(_(e_dictreq));
11630 return;
11631 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011632 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011633 return;
11634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011635 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011636 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011637
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011638 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011639 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011640 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011641 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011643 --todo;
11644 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011645
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011646 li = listitem_alloc();
11647 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011648 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011649 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011650
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011651 if (what == 0)
11652 {
11653 /* keys() */
11654 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011655 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011656 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11657 }
11658 else if (what == 1)
11659 {
11660 /* values() */
11661 copy_tv(&di->di_tv, &li->li_tv);
11662 }
11663 else
11664 {
11665 /* items() */
11666 l2 = list_alloc();
11667 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011668 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011669 li->li_tv.vval.v_list = l2;
11670 if (l2 == NULL)
11671 break;
11672 ++l2->lv_refcount;
11673
11674 li2 = listitem_alloc();
11675 if (li2 == NULL)
11676 break;
11677 list_append(l2, li2);
11678 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011679 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011680 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11681
11682 li2 = listitem_alloc();
11683 if (li2 == NULL)
11684 break;
11685 list_append(l2, li2);
11686 copy_tv(&di->di_tv, &li2->li_tv);
11687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011688 }
11689 }
11690}
11691
11692/*
11693 * "items(dict)" function
11694 */
11695 static void
11696f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 typval_T *argvars;
11698 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011699{
11700 dict_list(argvars, rettv, 2);
11701}
11702
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011704 * "join()" function
11705 */
11706 static void
11707f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011708 typval_T *argvars;
11709 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011710{
11711 garray_T ga;
11712 char_u *sep;
11713
11714 rettv->vval.v_number = 0;
11715 if (argvars[0].v_type != VAR_LIST)
11716 {
11717 EMSG(_(e_listreq));
11718 return;
11719 }
11720 if (argvars[0].vval.v_list == NULL)
11721 return;
11722 if (argvars[1].v_type == VAR_UNKNOWN)
11723 sep = (char_u *)" ";
11724 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011725 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011726
11727 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011728
11729 if (sep != NULL)
11730 {
11731 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011732 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 ga_append(&ga, NUL);
11734 rettv->vval.v_string = (char_u *)ga.ga_data;
11735 }
11736 else
11737 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011738}
11739
11740/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011741 * "keys()" function
11742 */
11743 static void
11744f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011745 typval_T *argvars;
11746 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011747{
11748 dict_list(argvars, rettv, 0);
11749}
11750
11751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 * "last_buffer_nr()" function.
11753 */
11754/*ARGSUSED*/
11755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011757 typval_T *argvars;
11758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759{
11760 int n = 0;
11761 buf_T *buf;
11762
11763 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11764 if (n < buf->b_fnum)
11765 n = buf->b_fnum;
11766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011768}
11769
11770/*
11771 * "len()" function
11772 */
11773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011774f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011775 typval_T *argvars;
11776 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011777{
11778 switch (argvars[0].v_type)
11779 {
11780 case VAR_STRING:
11781 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 rettv->vval.v_number = (varnumber_T)STRLEN(
11783 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011784 break;
11785 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011787 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011788 case VAR_DICT:
11789 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11790 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011791 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011792 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011793 break;
11794 }
11795}
11796
Bram Moolenaar33570922005-01-25 22:26:29 +000011797static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011798
11799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011800libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011801 typval_T *argvars;
11802 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011803 int type;
11804{
11805#ifdef FEAT_LIBCALL
11806 char_u *string_in;
11807 char_u **string_result;
11808 int nr_result;
11809#endif
11810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011816
11817 if (check_restricted() || check_secure())
11818 return;
11819
11820#ifdef FEAT_LIBCALL
11821 /* The first two args must be strings, otherwise its meaningless */
11822 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11823 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011824 string_in = NULL;
11825 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011826 string_in = argvars[2].vval.v_string;
11827 if (type == VAR_NUMBER)
11828 string_result = NULL;
11829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831 if (mch_libcall(argvars[0].vval.v_string,
11832 argvars[1].vval.v_string,
11833 string_in,
11834 argvars[2].vval.v_number,
11835 string_result,
11836 &nr_result) == OK
11837 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011839 }
11840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841}
11842
11843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011844 * "libcall()" function
11845 */
11846 static void
11847f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 typval_T *argvars;
11849 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850{
11851 libcall_common(argvars, rettv, VAR_STRING);
11852}
11853
11854/*
11855 * "libcallnr()" function
11856 */
11857 static void
11858f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011859 typval_T *argvars;
11860 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011861{
11862 libcall_common(argvars, rettv, VAR_NUMBER);
11863}
11864
11865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 * "line(string)" function
11867 */
11868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011870 typval_T *argvars;
11871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872{
11873 linenr_T lnum = 0;
11874 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011875 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011877 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 if (fp != NULL)
11879 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881}
11882
11883/*
11884 * "line2byte(lnum)" function
11885 */
11886/*ARGSUSED*/
11887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011889 typval_T *argvars;
11890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891{
11892#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894#else
11895 linenr_T lnum;
11896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11902 if (rettv->vval.v_number >= 0)
11903 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904#endif
11905}
11906
11907/*
11908 * "lispindent(lnum)" function
11909 */
11910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011912 typval_T *argvars;
11913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914{
11915#ifdef FEAT_LISP
11916 pos_T pos;
11917 linenr_T lnum;
11918
11919 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11922 {
11923 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 curwin->w_cursor = pos;
11926 }
11927 else
11928#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011929 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930}
11931
11932/*
11933 * "localtime()" function
11934 */
11935/*ARGSUSED*/
11936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *argvars;
11939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942}
11943
Bram Moolenaar33570922005-01-25 22:26:29 +000011944static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945
11946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T *argvars;
11949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 int exact;
11951{
11952 char_u *keys;
11953 char_u *which;
11954 char_u buf[NUMBUFLEN];
11955 char_u *keys_buf = NULL;
11956 char_u *rhs;
11957 int mode;
11958 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011959 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960
11961 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011962 rettv->v_type = VAR_STRING;
11963 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011965 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 if (*keys == NUL)
11967 return;
11968
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011972 if (argvars[2].v_type != VAR_UNKNOWN)
11973 abbr = get_tv_number(&argvars[2]);
11974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 else
11976 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011977 if (which == NULL)
11978 return;
11979
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 mode = get_map_mode(&which, 0);
11981
11982 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011983 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 vim_free(keys_buf);
11985 if (rhs != NULL)
11986 {
11987 ga_init(&ga);
11988 ga.ga_itemsize = 1;
11989 ga.ga_growsize = 40;
11990
11991 while (*rhs != NUL)
11992 ga_concat(&ga, str2special(&rhs, FALSE));
11993
11994 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 }
11997}
11998
11999/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012000 * "map()" function
12001 */
12002 static void
12003f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012004 typval_T *argvars;
12005 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012006{
12007 filter_map(argvars, rettv, TRUE);
12008}
12009
12010/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012011 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 */
12013 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012015 typval_T *argvars;
12016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012018 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019}
12020
12021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 */
12024 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012025f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012026 typval_T *argvars;
12027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012029 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030}
12031
Bram Moolenaar33570922005-01-25 22:26:29 +000012032static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033
12034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012036 typval_T *argvars;
12037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 int type;
12039{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012040 char_u *str = NULL;
12041 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 char_u *pat;
12043 regmatch_T regmatch;
12044 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012045 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 char_u *save_cpo;
12047 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012048 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012049 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012050 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012051 list_T *l = NULL;
12052 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012053 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012054 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055
12056 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12057 save_cpo = p_cpo;
12058 p_cpo = (char_u *)"";
12059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012060 rettv->vval.v_number = -1;
12061 if (type == 3)
12062 {
12063 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012064 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012065 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012066 }
12067 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069 rettv->v_type = VAR_STRING;
12070 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012073 if (argvars[0].v_type == VAR_LIST)
12074 {
12075 if ((l = argvars[0].vval.v_list) == NULL)
12076 goto theend;
12077 li = l->lv_first;
12078 }
12079 else
12080 expr = str = get_tv_string(&argvars[0]);
12081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12083 if (pat == NULL)
12084 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012085
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 int error = FALSE;
12089
12090 start = get_tv_number_chk(&argvars[2], &error);
12091 if (error)
12092 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012093 if (l != NULL)
12094 {
12095 li = list_find(l, start);
12096 if (li == NULL)
12097 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012098 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012099 }
12100 else
12101 {
12102 if (start < 0)
12103 start = 0;
12104 if (start > (long)STRLEN(str))
12105 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012106 /* When "count" argument is there ignore matches before "start",
12107 * otherwise skip part of the string. Differs when pattern is "^"
12108 * or "\<". */
12109 if (argvars[3].v_type != VAR_UNKNOWN)
12110 startcol = start;
12111 else
12112 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012113 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012114
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012115 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012116 nth = get_tv_number_chk(&argvars[3], &error);
12117 if (error)
12118 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 }
12120
12121 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12122 if (regmatch.regprog != NULL)
12123 {
12124 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012125
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012126 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012127 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012128 if (l != NULL)
12129 {
12130 if (li == NULL)
12131 {
12132 match = FALSE;
12133 break;
12134 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012135 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012136 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012137 if (str == NULL)
12138 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012139 }
12140
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012141 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012142
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012143 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012144 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012145 if (l == NULL && !match)
12146 break;
12147
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012148 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012149 if (l != NULL)
12150 {
12151 li = li->li_next;
12152 ++idx;
12153 }
12154 else
12155 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012156#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012157 startcol = (colnr_T)(regmatch.startp[0]
12158 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012159#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012160 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012161#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012162 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012163 }
12164
12165 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012167 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012168 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012169 int i;
12170
12171 /* return list with matched string and submatches */
12172 for (i = 0; i < NSUBEXP; ++i)
12173 {
12174 if (regmatch.endp[i] == NULL)
12175 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012176 if (list_append_string(rettv->vval.v_list,
12177 regmatch.startp[i],
12178 (int)(regmatch.endp[i] - regmatch.startp[i]))
12179 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012180 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012181 }
12182 }
12183 else if (type == 2)
12184 {
12185 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012186 if (l != NULL)
12187 copy_tv(&li->li_tv, rettv);
12188 else
12189 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012191 }
12192 else if (l != NULL)
12193 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 else
12195 {
12196 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012197 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 (varnumber_T)(regmatch.startp[0] - str);
12199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012202 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 }
12204 }
12205 vim_free(regmatch.regprog);
12206 }
12207
12208theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012209 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210 p_cpo = save_cpo;
12211}
12212
12213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214 * "match()" function
12215 */
12216 static void
12217f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012218 typval_T *argvars;
12219 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012220{
12221 find_some_match(argvars, rettv, 1);
12222}
12223
12224/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012225 * "matcharg()" function
12226 */
12227 static void
12228f_matcharg(argvars, rettv)
12229 typval_T *argvars;
12230 typval_T *rettv;
12231{
12232 if (rettv_list_alloc(rettv) == OK)
12233 {
12234#ifdef FEAT_SEARCH_EXTRA
12235 int mi = get_tv_number(&argvars[0]);
12236
12237 if (mi >= 1 && mi <= 3)
12238 {
12239 list_append_string(rettv->vval.v_list,
12240 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12241 list_append_string(rettv->vval.v_list,
12242 curwin->w_match_pat[mi - 1], -1);
12243 }
12244#endif
12245 }
12246}
12247
12248/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012249 * "matchend()" function
12250 */
12251 static void
12252f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012253 typval_T *argvars;
12254 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012255{
12256 find_some_match(argvars, rettv, 0);
12257}
12258
12259/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012260 * "matchlist()" function
12261 */
12262 static void
12263f_matchlist(argvars, rettv)
12264 typval_T *argvars;
12265 typval_T *rettv;
12266{
12267 find_some_match(argvars, rettv, 3);
12268}
12269
12270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012271 * "matchstr()" function
12272 */
12273 static void
12274f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012275 typval_T *argvars;
12276 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012277{
12278 find_some_match(argvars, rettv, 2);
12279}
12280
Bram Moolenaar33570922005-01-25 22:26:29 +000012281static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012282
12283 static void
12284max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 typval_T *argvars;
12286 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012287 int domax;
12288{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012289 long n = 0;
12290 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012292
12293 if (argvars[0].v_type == VAR_LIST)
12294 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012295 list_T *l;
12296 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012297
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012298 l = argvars[0].vval.v_list;
12299 if (l != NULL)
12300 {
12301 li = l->lv_first;
12302 if (li != NULL)
12303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012304 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012305 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012306 {
12307 li = li->li_next;
12308 if (li == NULL)
12309 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012310 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012311 if (domax ? i > n : i < n)
12312 n = i;
12313 }
12314 }
12315 }
12316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012317 else if (argvars[0].v_type == VAR_DICT)
12318 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012319 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012320 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012321 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012322 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012323
12324 d = argvars[0].vval.v_dict;
12325 if (d != NULL)
12326 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012327 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012328 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012330 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012332 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012333 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012334 if (first)
12335 {
12336 n = i;
12337 first = FALSE;
12338 }
12339 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012340 n = i;
12341 }
12342 }
12343 }
12344 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012345 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012346 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012347 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012348}
12349
12350/*
12351 * "max()" function
12352 */
12353 static void
12354f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 typval_T *argvars;
12356 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012357{
12358 max_min(argvars, rettv, TRUE);
12359}
12360
12361/*
12362 * "min()" function
12363 */
12364 static void
12365f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 typval_T *argvars;
12367 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012368{
12369 max_min(argvars, rettv, FALSE);
12370}
12371
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012372static int mkdir_recurse __ARGS((char_u *dir, int prot));
12373
12374/*
12375 * Create the directory in which "dir" is located, and higher levels when
12376 * needed.
12377 */
12378 static int
12379mkdir_recurse(dir, prot)
12380 char_u *dir;
12381 int prot;
12382{
12383 char_u *p;
12384 char_u *updir;
12385 int r = FAIL;
12386
12387 /* Get end of directory name in "dir".
12388 * We're done when it's "/" or "c:/". */
12389 p = gettail_sep(dir);
12390 if (p <= get_past_head(dir))
12391 return OK;
12392
12393 /* If the directory exists we're done. Otherwise: create it.*/
12394 updir = vim_strnsave(dir, (int)(p - dir));
12395 if (updir == NULL)
12396 return FAIL;
12397 if (mch_isdir(updir))
12398 r = OK;
12399 else if (mkdir_recurse(updir, prot) == OK)
12400 r = vim_mkdir_emsg(updir, prot);
12401 vim_free(updir);
12402 return r;
12403}
12404
12405#ifdef vim_mkdir
12406/*
12407 * "mkdir()" function
12408 */
12409 static void
12410f_mkdir(argvars, rettv)
12411 typval_T *argvars;
12412 typval_T *rettv;
12413{
12414 char_u *dir;
12415 char_u buf[NUMBUFLEN];
12416 int prot = 0755;
12417
12418 rettv->vval.v_number = FAIL;
12419 if (check_restricted() || check_secure())
12420 return;
12421
12422 dir = get_tv_string_buf(&argvars[0], buf);
12423 if (argvars[1].v_type != VAR_UNKNOWN)
12424 {
12425 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012426 prot = get_tv_number_chk(&argvars[2], NULL);
12427 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012428 mkdir_recurse(dir, prot);
12429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012430 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012431}
12432#endif
12433
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 * "mode()" function
12436 */
12437/*ARGSUSED*/
12438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012440 typval_T *argvars;
12441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
12443 char_u buf[2];
12444
12445#ifdef FEAT_VISUAL
12446 if (VIsual_active)
12447 {
12448 if (VIsual_select)
12449 buf[0] = VIsual_mode + 's' - 'v';
12450 else
12451 buf[0] = VIsual_mode;
12452 }
12453 else
12454#endif
12455 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12456 buf[0] = 'r';
12457 else if (State & INSERT)
12458 {
12459 if (State & REPLACE_FLAG)
12460 buf[0] = 'R';
12461 else
12462 buf[0] = 'i';
12463 }
12464 else if (State & CMDLINE)
12465 buf[0] = 'c';
12466 else
12467 buf[0] = 'n';
12468
12469 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012470 rettv->vval.v_string = vim_strsave(buf);
12471 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472}
12473
12474/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012475 * "nextnonblank()" function
12476 */
12477 static void
12478f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012479 typval_T *argvars;
12480 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012481{
12482 linenr_T lnum;
12483
12484 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012486 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 {
12488 lnum = 0;
12489 break;
12490 }
12491 if (*skipwhite(ml_get(lnum)) != NUL)
12492 break;
12493 }
12494 rettv->vval.v_number = lnum;
12495}
12496
12497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 * "nr2char()" function
12499 */
12500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012502 typval_T *argvars;
12503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
12505 char_u buf[NUMBUFLEN];
12506
12507#ifdef FEAT_MBYTE
12508 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 else
12511#endif
12512 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514 buf[1] = NUL;
12515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516 rettv->v_type = VAR_STRING;
12517 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012518}
12519
12520/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012521 * "pathshorten()" function
12522 */
12523 static void
12524f_pathshorten(argvars, rettv)
12525 typval_T *argvars;
12526 typval_T *rettv;
12527{
12528 char_u *p;
12529
12530 rettv->v_type = VAR_STRING;
12531 p = get_tv_string_chk(&argvars[0]);
12532 if (p == NULL)
12533 rettv->vval.v_string = NULL;
12534 else
12535 {
12536 p = vim_strsave(p);
12537 rettv->vval.v_string = p;
12538 if (p != NULL)
12539 shorten_dir(p);
12540 }
12541}
12542
12543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544 * "prevnonblank()" function
12545 */
12546 static void
12547f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012548 typval_T *argvars;
12549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012550{
12551 linenr_T lnum;
12552
12553 lnum = get_tv_lnum(argvars);
12554 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12555 lnum = 0;
12556 else
12557 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12558 --lnum;
12559 rettv->vval.v_number = lnum;
12560}
12561
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012562#ifdef HAVE_STDARG_H
12563/* This dummy va_list is here because:
12564 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12565 * - locally in the function results in a "used before set" warning
12566 * - using va_start() to initialize it gives "function with fixed args" error */
12567static va_list ap;
12568#endif
12569
Bram Moolenaar8c711452005-01-14 21:53:12 +000012570/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012571 * "printf()" function
12572 */
12573 static void
12574f_printf(argvars, rettv)
12575 typval_T *argvars;
12576 typval_T *rettv;
12577{
12578 rettv->v_type = VAR_STRING;
12579 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012580#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012581 {
12582 char_u buf[NUMBUFLEN];
12583 int len;
12584 char_u *s;
12585 int saved_did_emsg = did_emsg;
12586 char *fmt;
12587
12588 /* Get the required length, allocate the buffer and do it for real. */
12589 did_emsg = FALSE;
12590 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012591 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012592 if (!did_emsg)
12593 {
12594 s = alloc(len + 1);
12595 if (s != NULL)
12596 {
12597 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012598 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012599 }
12600 }
12601 did_emsg |= saved_did_emsg;
12602 }
12603#endif
12604}
12605
12606/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012607 * "pumvisible()" function
12608 */
12609/*ARGSUSED*/
12610 static void
12611f_pumvisible(argvars, rettv)
12612 typval_T *argvars;
12613 typval_T *rettv;
12614{
12615 rettv->vval.v_number = 0;
12616#ifdef FEAT_INS_EXPAND
12617 if (pum_visible())
12618 rettv->vval.v_number = 1;
12619#endif
12620}
12621
12622/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012623 * "range()" function
12624 */
12625 static void
12626f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012627 typval_T *argvars;
12628 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012629{
12630 long start;
12631 long end;
12632 long stride = 1;
12633 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012634 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012637 if (argvars[1].v_type == VAR_UNKNOWN)
12638 {
12639 end = start - 1;
12640 start = 0;
12641 }
12642 else
12643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012644 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012645 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012647 }
12648
12649 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012650 if (error)
12651 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012652 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012653 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012654 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012655 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012656 else
12657 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012658 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012659 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012660 if (list_append_number(rettv->vval.v_list,
12661 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012662 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012663 }
12664}
12665
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012666/*
12667 * "readfile()" function
12668 */
12669 static void
12670f_readfile(argvars, rettv)
12671 typval_T *argvars;
12672 typval_T *rettv;
12673{
12674 int binary = FALSE;
12675 char_u *fname;
12676 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012677 listitem_T *li;
12678#define FREAD_SIZE 200 /* optimized for text lines */
12679 char_u buf[FREAD_SIZE];
12680 int readlen; /* size of last fread() */
12681 int buflen; /* nr of valid chars in buf[] */
12682 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12683 int tolist; /* first byte in buf[] still to be put in list */
12684 int chop; /* how many CR to chop off */
12685 char_u *prev = NULL; /* previously read bytes, if any */
12686 int prevlen = 0; /* length of "prev" if not NULL */
12687 char_u *s;
12688 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012689 long maxline = MAXLNUM;
12690 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012691
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012692 if (argvars[1].v_type != VAR_UNKNOWN)
12693 {
12694 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12695 binary = TRUE;
12696 if (argvars[2].v_type != VAR_UNKNOWN)
12697 maxline = get_tv_number(&argvars[2]);
12698 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012699
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012700 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012701 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012702
12703 /* Always open the file in binary mode, library functions have a mind of
12704 * their own about CR-LF conversion. */
12705 fname = get_tv_string(&argvars[0]);
12706 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12707 {
12708 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12709 return;
12710 }
12711
12712 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012713 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012714 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012715 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012716 buflen = filtd + readlen;
12717 tolist = 0;
12718 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12719 {
12720 if (buf[filtd] == '\n' || readlen <= 0)
12721 {
12722 /* Only when in binary mode add an empty list item when the
12723 * last line ends in a '\n'. */
12724 if (!binary && readlen == 0 && filtd == 0)
12725 break;
12726
12727 /* Found end-of-line or end-of-file: add a text line to the
12728 * list. */
12729 chop = 0;
12730 if (!binary)
12731 while (filtd - chop - 1 >= tolist
12732 && buf[filtd - chop - 1] == '\r')
12733 ++chop;
12734 len = filtd - tolist - chop;
12735 if (prev == NULL)
12736 s = vim_strnsave(buf + tolist, len);
12737 else
12738 {
12739 s = alloc((unsigned)(prevlen + len + 1));
12740 if (s != NULL)
12741 {
12742 mch_memmove(s, prev, prevlen);
12743 vim_free(prev);
12744 prev = NULL;
12745 mch_memmove(s + prevlen, buf + tolist, len);
12746 s[prevlen + len] = NUL;
12747 }
12748 }
12749 tolist = filtd + 1;
12750
12751 li = listitem_alloc();
12752 if (li == NULL)
12753 {
12754 vim_free(s);
12755 break;
12756 }
12757 li->li_tv.v_type = VAR_STRING;
12758 li->li_tv.v_lock = 0;
12759 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012760 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012761
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012762 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012763 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012764 if (readlen <= 0)
12765 break;
12766 }
12767 else if (buf[filtd] == NUL)
12768 buf[filtd] = '\n';
12769 }
12770 if (readlen <= 0)
12771 break;
12772
12773 if (tolist == 0)
12774 {
12775 /* "buf" is full, need to move text to an allocated buffer */
12776 if (prev == NULL)
12777 {
12778 prev = vim_strnsave(buf, buflen);
12779 prevlen = buflen;
12780 }
12781 else
12782 {
12783 s = alloc((unsigned)(prevlen + buflen));
12784 if (s != NULL)
12785 {
12786 mch_memmove(s, prev, prevlen);
12787 mch_memmove(s + prevlen, buf, buflen);
12788 vim_free(prev);
12789 prev = s;
12790 prevlen += buflen;
12791 }
12792 }
12793 filtd = 0;
12794 }
12795 else
12796 {
12797 mch_memmove(buf, buf + tolist, buflen - tolist);
12798 filtd -= tolist;
12799 }
12800 }
12801
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012802 /*
12803 * For a negative line count use only the lines at the end of the file,
12804 * free the rest.
12805 */
12806 if (maxline < 0)
12807 while (cnt > -maxline)
12808 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012809 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012810 --cnt;
12811 }
12812
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012813 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012814 fclose(fd);
12815}
12816
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012817#if defined(FEAT_RELTIME)
12818static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12819
12820/*
12821 * Convert a List to proftime_T.
12822 * Return FAIL when there is something wrong.
12823 */
12824 static int
12825list2proftime(arg, tm)
12826 typval_T *arg;
12827 proftime_T *tm;
12828{
12829 long n1, n2;
12830 int error = FALSE;
12831
12832 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12833 || arg->vval.v_list->lv_len != 2)
12834 return FAIL;
12835 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12836 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12837# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012838 tm->HighPart = n1;
12839 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012840# else
12841 tm->tv_sec = n1;
12842 tm->tv_usec = n2;
12843# endif
12844 return error ? FAIL : OK;
12845}
12846#endif /* FEAT_RELTIME */
12847
12848/*
12849 * "reltime()" function
12850 */
12851 static void
12852f_reltime(argvars, rettv)
12853 typval_T *argvars;
12854 typval_T *rettv;
12855{
12856#ifdef FEAT_RELTIME
12857 proftime_T res;
12858 proftime_T start;
12859
12860 if (argvars[0].v_type == VAR_UNKNOWN)
12861 {
12862 /* No arguments: get current time. */
12863 profile_start(&res);
12864 }
12865 else if (argvars[1].v_type == VAR_UNKNOWN)
12866 {
12867 if (list2proftime(&argvars[0], &res) == FAIL)
12868 return;
12869 profile_end(&res);
12870 }
12871 else
12872 {
12873 /* Two arguments: compute the difference. */
12874 if (list2proftime(&argvars[0], &start) == FAIL
12875 || list2proftime(&argvars[1], &res) == FAIL)
12876 return;
12877 profile_sub(&res, &start);
12878 }
12879
12880 if (rettv_list_alloc(rettv) == OK)
12881 {
12882 long n1, n2;
12883
12884# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012885 n1 = res.HighPart;
12886 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012887# else
12888 n1 = res.tv_sec;
12889 n2 = res.tv_usec;
12890# endif
12891 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12892 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12893 }
12894#endif
12895}
12896
12897/*
12898 * "reltimestr()" function
12899 */
12900 static void
12901f_reltimestr(argvars, rettv)
12902 typval_T *argvars;
12903 typval_T *rettv;
12904{
12905#ifdef FEAT_RELTIME
12906 proftime_T tm;
12907#endif
12908
12909 rettv->v_type = VAR_STRING;
12910 rettv->vval.v_string = NULL;
12911#ifdef FEAT_RELTIME
12912 if (list2proftime(&argvars[0], &tm) == OK)
12913 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12914#endif
12915}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012916
Bram Moolenaar0d660222005-01-07 21:51:51 +000012917#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12918static void make_connection __ARGS((void));
12919static int check_connection __ARGS((void));
12920
12921 static void
12922make_connection()
12923{
12924 if (X_DISPLAY == NULL
12925# ifdef FEAT_GUI
12926 && !gui.in_use
12927# endif
12928 )
12929 {
12930 x_force_connect = TRUE;
12931 setup_term_clip();
12932 x_force_connect = FALSE;
12933 }
12934}
12935
12936 static int
12937check_connection()
12938{
12939 make_connection();
12940 if (X_DISPLAY == NULL)
12941 {
12942 EMSG(_("E240: No connection to Vim server"));
12943 return FAIL;
12944 }
12945 return OK;
12946}
12947#endif
12948
12949#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012950static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012951
12952 static void
12953remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012954 typval_T *argvars;
12955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956 int expr;
12957{
12958 char_u *server_name;
12959 char_u *keys;
12960 char_u *r = NULL;
12961 char_u buf[NUMBUFLEN];
12962# ifdef WIN32
12963 HWND w;
12964# else
12965 Window w;
12966# endif
12967
12968 if (check_restricted() || check_secure())
12969 return;
12970
12971# ifdef FEAT_X11
12972 if (check_connection() == FAIL)
12973 return;
12974# endif
12975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012976 server_name = get_tv_string_chk(&argvars[0]);
12977 if (server_name == NULL)
12978 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979 keys = get_tv_string_buf(&argvars[1], buf);
12980# ifdef WIN32
12981 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12982# else
12983 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12984 < 0)
12985# endif
12986 {
12987 if (r != NULL)
12988 EMSG(r); /* sending worked but evaluation failed */
12989 else
12990 EMSG2(_("E241: Unable to send to %s"), server_name);
12991 return;
12992 }
12993
12994 rettv->vval.v_string = r;
12995
12996 if (argvars[2].v_type != VAR_UNKNOWN)
12997 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012998 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012999 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013000 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001
13002 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013003 v.di_tv.v_type = VAR_STRING;
13004 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013005 idvar = get_tv_string_chk(&argvars[2]);
13006 if (idvar != NULL)
13007 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013008 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013009 }
13010}
13011#endif
13012
13013/*
13014 * "remote_expr()" function
13015 */
13016/*ARGSUSED*/
13017 static void
13018f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013021{
13022 rettv->v_type = VAR_STRING;
13023 rettv->vval.v_string = NULL;
13024#ifdef FEAT_CLIENTSERVER
13025 remote_common(argvars, rettv, TRUE);
13026#endif
13027}
13028
13029/*
13030 * "remote_foreground()" function
13031 */
13032/*ARGSUSED*/
13033 static void
13034f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013035 typval_T *argvars;
13036 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013037{
13038 rettv->vval.v_number = 0;
13039#ifdef FEAT_CLIENTSERVER
13040# ifdef WIN32
13041 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013042 {
13043 char_u *server_name = get_tv_string_chk(&argvars[0]);
13044
13045 if (server_name != NULL)
13046 serverForeground(server_name);
13047 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013048# else
13049 /* Send a foreground() expression to the server. */
13050 argvars[1].v_type = VAR_STRING;
13051 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13052 argvars[2].v_type = VAR_UNKNOWN;
13053 remote_common(argvars, rettv, TRUE);
13054 vim_free(argvars[1].vval.v_string);
13055# endif
13056#endif
13057}
13058
13059/*ARGSUSED*/
13060 static void
13061f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 typval_T *argvars;
13063 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013064{
13065#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013066 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013067 char_u *s = NULL;
13068# ifdef WIN32
13069 int n = 0;
13070# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013071 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013072
13073 if (check_restricted() || check_secure())
13074 {
13075 rettv->vval.v_number = -1;
13076 return;
13077 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013078 serverid = get_tv_string_chk(&argvars[0]);
13079 if (serverid == NULL)
13080 {
13081 rettv->vval.v_number = -1;
13082 return; /* type error; errmsg already given */
13083 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013084# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013085 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013086 if (n == 0)
13087 rettv->vval.v_number = -1;
13088 else
13089 {
13090 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13091 rettv->vval.v_number = (s != NULL);
13092 }
13093# else
13094 rettv->vval.v_number = 0;
13095 if (check_connection() == FAIL)
13096 return;
13097
13098 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100# endif
13101
13102 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013104 char_u *retvar;
13105
Bram Moolenaar33570922005-01-25 22:26:29 +000013106 v.di_tv.v_type = VAR_STRING;
13107 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 retvar = get_tv_string_chk(&argvars[1]);
13109 if (retvar != NULL)
13110 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013111 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013112 }
13113#else
13114 rettv->vval.v_number = -1;
13115#endif
13116}
13117
13118/*ARGSUSED*/
13119 static void
13120f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013121 typval_T *argvars;
13122 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013123{
13124 char_u *r = NULL;
13125
13126#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013127 char_u *serverid = get_tv_string_chk(&argvars[0]);
13128
13129 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013130 {
13131# ifdef WIN32
13132 /* The server's HWND is encoded in the 'id' parameter */
13133 int n = 0;
13134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013135 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013136 if (n != 0)
13137 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13138 if (r == NULL)
13139# else
13140 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013141 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013142# endif
13143 EMSG(_("E277: Unable to read a server reply"));
13144 }
13145#endif
13146 rettv->v_type = VAR_STRING;
13147 rettv->vval.v_string = r;
13148}
13149
13150/*
13151 * "remote_send()" function
13152 */
13153/*ARGSUSED*/
13154 static void
13155f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013156 typval_T *argvars;
13157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013158{
13159 rettv->v_type = VAR_STRING;
13160 rettv->vval.v_string = NULL;
13161#ifdef FEAT_CLIENTSERVER
13162 remote_common(argvars, rettv, FALSE);
13163#endif
13164}
13165
13166/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013167 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013168 */
13169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013171 typval_T *argvars;
13172 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013173{
Bram Moolenaar33570922005-01-25 22:26:29 +000013174 list_T *l;
13175 listitem_T *item, *item2;
13176 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013177 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013178 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013179 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 dict_T *d;
13181 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013182
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013183 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013184 if (argvars[0].v_type == VAR_DICT)
13185 {
13186 if (argvars[2].v_type != VAR_UNKNOWN)
13187 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013188 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013189 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013191 key = get_tv_string_chk(&argvars[1]);
13192 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013194 di = dict_find(d, key, -1);
13195 if (di == NULL)
13196 EMSG2(_(e_dictkey), key);
13197 else
13198 {
13199 *rettv = di->di_tv;
13200 init_tv(&di->di_tv);
13201 dictitem_remove(d, di);
13202 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013203 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013204 }
13205 }
13206 else if (argvars[0].v_type != VAR_LIST)
13207 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013208 else if ((l = argvars[0].vval.v_list) != NULL
13209 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 int error = FALSE;
13212
13213 idx = get_tv_number_chk(&argvars[1], &error);
13214 if (error)
13215 ; /* type error: do nothing, errmsg already given */
13216 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013217 EMSGN(_(e_listidx), idx);
13218 else
13219 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013220 if (argvars[2].v_type == VAR_UNKNOWN)
13221 {
13222 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013223 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013224 *rettv = item->li_tv;
13225 vim_free(item);
13226 }
13227 else
13228 {
13229 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013230 end = get_tv_number_chk(&argvars[2], &error);
13231 if (error)
13232 ; /* type error: do nothing */
13233 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013234 EMSGN(_(e_listidx), end);
13235 else
13236 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013237 int cnt = 0;
13238
13239 for (li = item; li != NULL; li = li->li_next)
13240 {
13241 ++cnt;
13242 if (li == item2)
13243 break;
13244 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013245 if (li == NULL) /* didn't find "item2" after "item" */
13246 EMSG(_(e_invrange));
13247 else
13248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013249 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013250 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013251 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013252 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013253 l->lv_first = item;
13254 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013255 item->li_prev = NULL;
13256 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013257 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013258 }
13259 }
13260 }
13261 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013262 }
13263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264}
13265
13266/*
13267 * "rename({from}, {to})" function
13268 */
13269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013271 typval_T *argvars;
13272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273{
13274 char_u buf[NUMBUFLEN];
13275
13276 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13280 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281}
13282
13283/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013284 * "repeat()" function
13285 */
13286/*ARGSUSED*/
13287 static void
13288f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013289 typval_T *argvars;
13290 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013291{
13292 char_u *p;
13293 int n;
13294 int slen;
13295 int len;
13296 char_u *r;
13297 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013298
13299 n = get_tv_number(&argvars[1]);
13300 if (argvars[0].v_type == VAR_LIST)
13301 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013302 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013303 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013304 if (list_extend(rettv->vval.v_list,
13305 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013306 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013307 }
13308 else
13309 {
13310 p = get_tv_string(&argvars[0]);
13311 rettv->v_type = VAR_STRING;
13312 rettv->vval.v_string = NULL;
13313
13314 slen = (int)STRLEN(p);
13315 len = slen * n;
13316 if (len <= 0)
13317 return;
13318
13319 r = alloc(len + 1);
13320 if (r != NULL)
13321 {
13322 for (i = 0; i < n; i++)
13323 mch_memmove(r + i * slen, p, (size_t)slen);
13324 r[len] = NUL;
13325 }
13326
13327 rettv->vval.v_string = r;
13328 }
13329}
13330
13331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 * "resolve()" function
13333 */
13334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013336 typval_T *argvars;
13337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338{
13339 char_u *p;
13340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342#ifdef FEAT_SHORTCUT
13343 {
13344 char_u *v = NULL;
13345
13346 v = mch_resolve_shortcut(p);
13347 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013348 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 }
13352#else
13353# ifdef HAVE_READLINK
13354 {
13355 char_u buf[MAXPATHL + 1];
13356 char_u *cpy;
13357 int len;
13358 char_u *remain = NULL;
13359 char_u *q;
13360 int is_relative_to_current = FALSE;
13361 int has_trailing_pathsep = FALSE;
13362 int limit = 100;
13363
13364 p = vim_strsave(p);
13365
13366 if (p[0] == '.' && (vim_ispathsep(p[1])
13367 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13368 is_relative_to_current = TRUE;
13369
13370 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013371 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 has_trailing_pathsep = TRUE;
13373
13374 q = getnextcomp(p);
13375 if (*q != NUL)
13376 {
13377 /* Separate the first path component in "p", and keep the
13378 * remainder (beginning with the path separator). */
13379 remain = vim_strsave(q - 1);
13380 q[-1] = NUL;
13381 }
13382
13383 for (;;)
13384 {
13385 for (;;)
13386 {
13387 len = readlink((char *)p, (char *)buf, MAXPATHL);
13388 if (len <= 0)
13389 break;
13390 buf[len] = NUL;
13391
13392 if (limit-- == 0)
13393 {
13394 vim_free(p);
13395 vim_free(remain);
13396 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398 goto fail;
13399 }
13400
13401 /* Ensure that the result will have a trailing path separator
13402 * if the argument has one. */
13403 if (remain == NULL && has_trailing_pathsep)
13404 add_pathsep(buf);
13405
13406 /* Separate the first path component in the link value and
13407 * concatenate the remainders. */
13408 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13409 if (*q != NUL)
13410 {
13411 if (remain == NULL)
13412 remain = vim_strsave(q - 1);
13413 else
13414 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013415 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 if (cpy != NULL)
13417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 vim_free(remain);
13419 remain = cpy;
13420 }
13421 }
13422 q[-1] = NUL;
13423 }
13424
13425 q = gettail(p);
13426 if (q > p && *q == NUL)
13427 {
13428 /* Ignore trailing path separator. */
13429 q[-1] = NUL;
13430 q = gettail(p);
13431 }
13432 if (q > p && !mch_isFullName(buf))
13433 {
13434 /* symlink is relative to directory of argument */
13435 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13436 if (cpy != NULL)
13437 {
13438 STRCPY(cpy, p);
13439 STRCPY(gettail(cpy), buf);
13440 vim_free(p);
13441 p = cpy;
13442 }
13443 }
13444 else
13445 {
13446 vim_free(p);
13447 p = vim_strsave(buf);
13448 }
13449 }
13450
13451 if (remain == NULL)
13452 break;
13453
13454 /* Append the first path component of "remain" to "p". */
13455 q = getnextcomp(remain + 1);
13456 len = q - remain - (*q != NUL);
13457 cpy = vim_strnsave(p, STRLEN(p) + len);
13458 if (cpy != NULL)
13459 {
13460 STRNCAT(cpy, remain, len);
13461 vim_free(p);
13462 p = cpy;
13463 }
13464 /* Shorten "remain". */
13465 if (*q != NUL)
13466 STRCPY(remain, q - 1);
13467 else
13468 {
13469 vim_free(remain);
13470 remain = NULL;
13471 }
13472 }
13473
13474 /* If the result is a relative path name, make it explicitly relative to
13475 * the current directory if and only if the argument had this form. */
13476 if (!vim_ispathsep(*p))
13477 {
13478 if (is_relative_to_current
13479 && *p != NUL
13480 && !(p[0] == '.'
13481 && (p[1] == NUL
13482 || vim_ispathsep(p[1])
13483 || (p[1] == '.'
13484 && (p[2] == NUL
13485 || vim_ispathsep(p[2]))))))
13486 {
13487 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013488 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489 if (cpy != NULL)
13490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 vim_free(p);
13492 p = cpy;
13493 }
13494 }
13495 else if (!is_relative_to_current)
13496 {
13497 /* Strip leading "./". */
13498 q = p;
13499 while (q[0] == '.' && vim_ispathsep(q[1]))
13500 q += 2;
13501 if (q > p)
13502 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13503 }
13504 }
13505
13506 /* Ensure that the result will have no trailing path separator
13507 * if the argument had none. But keep "/" or "//". */
13508 if (!has_trailing_pathsep)
13509 {
13510 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013511 if (after_pathsep(p, q))
13512 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 }
13514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 }
13517# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519# endif
13520#endif
13521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523
13524#ifdef HAVE_READLINK
13525fail:
13526#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528}
13529
13530/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013531 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 */
13533 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013534f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *argvars;
13536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537{
Bram Moolenaar33570922005-01-25 22:26:29 +000013538 list_T *l;
13539 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540
Bram Moolenaar0d660222005-01-07 21:51:51 +000013541 rettv->vval.v_number = 0;
13542 if (argvars[0].v_type != VAR_LIST)
13543 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013544 else if ((l = argvars[0].vval.v_list) != NULL
13545 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013546 {
13547 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013548 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013549 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013550 while (li != NULL)
13551 {
13552 ni = li->li_prev;
13553 list_append(l, li);
13554 li = ni;
13555 }
13556 rettv->vval.v_list = l;
13557 rettv->v_type = VAR_LIST;
13558 ++l->lv_refcount;
13559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560}
13561
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013562#define SP_NOMOVE 0x01 /* don't move cursor */
13563#define SP_REPEAT 0x02 /* repeat to find outer pair */
13564#define SP_RETCOUNT 0x04 /* return matchcount */
13565#define SP_SETPCMARK 0x08 /* set previous context mark */
13566#define SP_START 0x10 /* accept match at start position */
13567#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13568#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013569
Bram Moolenaar33570922005-01-25 22:26:29 +000013570static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571
13572/*
13573 * Get flags for a search function.
13574 * Possibly sets "p_ws".
13575 * Returns BACKWARD, FORWARD or zero (for an error).
13576 */
13577 static int
13578get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013579 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013580 int *flagsp;
13581{
13582 int dir = FORWARD;
13583 char_u *flags;
13584 char_u nbuf[NUMBUFLEN];
13585 int mask;
13586
13587 if (varp->v_type != VAR_UNKNOWN)
13588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013589 flags = get_tv_string_buf_chk(varp, nbuf);
13590 if (flags == NULL)
13591 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592 while (*flags != NUL)
13593 {
13594 switch (*flags)
13595 {
13596 case 'b': dir = BACKWARD; break;
13597 case 'w': p_ws = TRUE; break;
13598 case 'W': p_ws = FALSE; break;
13599 default: mask = 0;
13600 if (flagsp != NULL)
13601 switch (*flags)
13602 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013603 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013604 case 'e': mask = SP_END; break;
13605 case 'm': mask = SP_RETCOUNT; break;
13606 case 'n': mask = SP_NOMOVE; break;
13607 case 'p': mask = SP_SUBPAT; break;
13608 case 'r': mask = SP_REPEAT; break;
13609 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013610 }
13611 if (mask == 0)
13612 {
13613 EMSG2(_(e_invarg2), flags);
13614 dir = 0;
13615 }
13616 else
13617 *flagsp |= mask;
13618 }
13619 if (dir == 0)
13620 break;
13621 ++flags;
13622 }
13623 }
13624 return dir;
13625}
13626
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013628 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013630 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013631search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013633 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013634 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013636 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 char_u *pat;
13638 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013639 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 int save_p_ws = p_ws;
13641 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013642 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013643 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013644 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013645 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013648 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013649 if (dir == 0)
13650 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013651 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013652 if (flags & SP_START)
13653 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013654 if (flags & SP_END)
13655 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013656
13657 /* Optional extra argument: line number to stop searching. */
13658 if (argvars[1].v_type != VAR_UNKNOWN
13659 && argvars[2].v_type != VAR_UNKNOWN)
13660 {
13661 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13662 if (lnum_stop < 0)
13663 goto theend;
13664 }
13665
Bram Moolenaar231334e2005-07-25 20:46:57 +000013666 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013667 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013668 * Check to make sure only those flags are set.
13669 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13670 * flags cannot be set. Check for that condition also.
13671 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013672 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013673 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013676 goto theend;
13677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013679 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013680 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13681 options, RE_SEARCH, (linenr_T)lnum_stop);
13682 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013684 if (flags & SP_SUBPAT)
13685 retval = subpatnum;
13686 else
13687 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013688 if (flags & SP_SETPCMARK)
13689 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013691 if (match_pos != NULL)
13692 {
13693 /* Store the match cursor position */
13694 match_pos->lnum = pos.lnum;
13695 match_pos->col = pos.col + 1;
13696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 /* "/$" will put the cursor after the end of the line, may need to
13698 * correct that here */
13699 check_cursor();
13700 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013701
13702 /* If 'n' flag is used: restore cursor position. */
13703 if (flags & SP_NOMOVE)
13704 curwin->w_cursor = save_cursor;
13705theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013707
13708 return retval;
13709}
13710
13711/*
13712 * "search()" function
13713 */
13714 static void
13715f_search(argvars, rettv)
13716 typval_T *argvars;
13717 typval_T *rettv;
13718{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013719 int flags = 0;
13720
13721 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722}
13723
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013725 * "searchdecl()" function
13726 */
13727 static void
13728f_searchdecl(argvars, rettv)
13729 typval_T *argvars;
13730 typval_T *rettv;
13731{
13732 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013733 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013734 int error = FALSE;
13735 char_u *name;
13736
13737 rettv->vval.v_number = 1; /* default: FAIL */
13738
13739 name = get_tv_string_chk(&argvars[0]);
13740 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013741 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013742 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013743 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13744 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13745 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013746 if (!error && name != NULL)
13747 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013748 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013749}
13750
13751/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013752 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013754 static int
13755searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013756 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013757 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758{
13759 char_u *spat, *mpat, *epat;
13760 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 int dir;
13763 int flags = 0;
13764 char_u nbuf1[NUMBUFLEN];
13765 char_u nbuf2[NUMBUFLEN];
13766 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013767 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013768 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 spat = get_tv_string_chk(&argvars[0]);
13772 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13773 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13774 if (spat == NULL || mpat == NULL || epat == NULL)
13775 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 /* Handle the optional fourth argument: flags */
13778 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013779 if (dir == 0)
13780 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013781
13782 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013783 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13784 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013785 if ((flags & (SP_END | SP_SUBPAT)) != 0
13786 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013787 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013788 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013789 goto theend;
13790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013792 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013793 if (argvars[3].v_type == VAR_UNKNOWN
13794 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 skip = (char_u *)"";
13796 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013798 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013799 if (argvars[5].v_type != VAR_UNKNOWN)
13800 {
13801 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13802 if (lnum_stop < 0)
13803 goto theend;
13804 }
13805 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806 if (skip == NULL)
13807 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013809 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13810 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013811
13812theend:
13813 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013814
13815 return retval;
13816}
13817
13818/*
13819 * "searchpair()" function
13820 */
13821 static void
13822f_searchpair(argvars, rettv)
13823 typval_T *argvars;
13824 typval_T *rettv;
13825{
13826 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13827}
13828
13829/*
13830 * "searchpairpos()" function
13831 */
13832 static void
13833f_searchpairpos(argvars, rettv)
13834 typval_T *argvars;
13835 typval_T *rettv;
13836{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013837 pos_T match_pos;
13838 int lnum = 0;
13839 int col = 0;
13840
13841 rettv->vval.v_number = 0;
13842
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013843 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013844 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013845
13846 if (searchpair_cmn(argvars, &match_pos) > 0)
13847 {
13848 lnum = match_pos.lnum;
13849 col = match_pos.col;
13850 }
13851
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013852 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13853 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013854}
13855
13856/*
13857 * Search for a start/middle/end thing.
13858 * Used by searchpair(), see its documentation for the details.
13859 * Returns 0 or -1 for no match,
13860 */
13861 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013862do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013863 char_u *spat; /* start pattern */
13864 char_u *mpat; /* middle pattern */
13865 char_u *epat; /* end pattern */
13866 int dir; /* BACKWARD or FORWARD */
13867 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013868 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013869 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013870 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013871{
13872 char_u *save_cpo;
13873 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13874 long retval = 0;
13875 pos_T pos;
13876 pos_T firstpos;
13877 pos_T foundpos;
13878 pos_T save_cursor;
13879 pos_T save_pos;
13880 int n;
13881 int r;
13882 int nest = 1;
13883 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013884 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013885
13886 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13887 save_cpo = p_cpo;
13888 p_cpo = (char_u *)"";
13889
13890 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13891 * start/middle/end (pat3, for the top pair). */
13892 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13893 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13894 if (pat2 == NULL || pat3 == NULL)
13895 goto theend;
13896 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13897 if (*mpat == NUL)
13898 STRCPY(pat3, pat2);
13899 else
13900 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13901 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013902 if (flags & SP_START)
13903 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013904
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 save_cursor = curwin->w_cursor;
13906 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013907 clearpos(&firstpos);
13908 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 pat = pat3;
13910 for (;;)
13911 {
13912 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013913 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13915 /* didn't find it or found the first match again: FAIL */
13916 break;
13917
13918 if (firstpos.lnum == 0)
13919 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013920 if (equalpos(pos, foundpos))
13921 {
13922 /* Found the same position again. Can happen with a pattern that
13923 * has "\zs" at the end and searching backwards. Advance one
13924 * character and try again. */
13925 if (dir == BACKWARD)
13926 decl(&pos);
13927 else
13928 incl(&pos);
13929 }
13930 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931
13932 /* If the skip pattern matches, ignore this match. */
13933 if (*skip != NUL)
13934 {
13935 save_pos = curwin->w_cursor;
13936 curwin->w_cursor = pos;
13937 r = eval_to_bool(skip, &err, NULL, FALSE);
13938 curwin->w_cursor = save_pos;
13939 if (err)
13940 {
13941 /* Evaluating {skip} caused an error, break here. */
13942 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013943 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 break;
13945 }
13946 if (r)
13947 continue;
13948 }
13949
13950 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13951 {
13952 /* Found end when searching backwards or start when searching
13953 * forward: nested pair. */
13954 ++nest;
13955 pat = pat2; /* nested, don't search for middle */
13956 }
13957 else
13958 {
13959 /* Found end when searching forward or start when searching
13960 * backward: end of (nested) pair; or found middle in outer pair. */
13961 if (--nest == 1)
13962 pat = pat3; /* outer level, search for middle */
13963 }
13964
13965 if (nest == 0)
13966 {
13967 /* Found the match: return matchcount or line number. */
13968 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013969 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013971 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013972 if (flags & SP_SETPCMARK)
13973 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 curwin->w_cursor = pos;
13975 if (!(flags & SP_REPEAT))
13976 break;
13977 nest = 1; /* search for next unmatched */
13978 }
13979 }
13980
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013981 if (match_pos != NULL)
13982 {
13983 /* Store the match cursor position */
13984 match_pos->lnum = curwin->w_cursor.lnum;
13985 match_pos->col = curwin->w_cursor.col + 1;
13986 }
13987
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013989 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 curwin->w_cursor = save_cursor;
13991
13992theend:
13993 vim_free(pat2);
13994 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013996
13997 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998}
13999
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014000/*
14001 * "searchpos()" function
14002 */
14003 static void
14004f_searchpos(argvars, rettv)
14005 typval_T *argvars;
14006 typval_T *rettv;
14007{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014008 pos_T match_pos;
14009 int lnum = 0;
14010 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014011 int n;
14012 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014013
14014 rettv->vval.v_number = 0;
14015
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014016 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014017 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014018
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014019 n = search_cmn(argvars, &match_pos, &flags);
14020 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014021 {
14022 lnum = match_pos.lnum;
14023 col = match_pos.col;
14024 }
14025
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014026 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14027 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014028 if (flags & SP_SUBPAT)
14029 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014030}
14031
14032
Bram Moolenaar0d660222005-01-07 21:51:51 +000014033/*ARGSUSED*/
14034 static void
14035f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014036 typval_T *argvars;
14037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014039#ifdef FEAT_CLIENTSERVER
14040 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 char_u *server = get_tv_string_chk(&argvars[0]);
14042 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043
Bram Moolenaar0d660222005-01-07 21:51:51 +000014044 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 if (server == NULL || reply == NULL)
14046 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014047 if (check_restricted() || check_secure())
14048 return;
14049# ifdef FEAT_X11
14050 if (check_connection() == FAIL)
14051 return;
14052# endif
14053
14054 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056 EMSG(_("E258: Unable to send to client"));
14057 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059 rettv->vval.v_number = 0;
14060#else
14061 rettv->vval.v_number = -1;
14062#endif
14063}
14064
14065/*ARGSUSED*/
14066 static void
14067f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014068 typval_T *argvars;
14069 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014070{
14071 char_u *r = NULL;
14072
14073#ifdef FEAT_CLIENTSERVER
14074# ifdef WIN32
14075 r = serverGetVimNames();
14076# else
14077 make_connection();
14078 if (X_DISPLAY != NULL)
14079 r = serverGetVimNames(X_DISPLAY);
14080# endif
14081#endif
14082 rettv->v_type = VAR_STRING;
14083 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084}
14085
14086/*
14087 * "setbufvar()" function
14088 */
14089/*ARGSUSED*/
14090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014091f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014092 typval_T *argvars;
14093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094{
14095 buf_T *buf;
14096#ifdef FEAT_AUTOCMD
14097 aco_save_T aco;
14098#else
14099 buf_T *save_curbuf;
14100#endif
14101 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 char_u nbuf[NUMBUFLEN];
14104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 rettv->vval.v_number = 0;
14106
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 if (check_restricted() || check_secure())
14108 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14110 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 varp = &argvars[2];
14113
14114 if (buf != NULL && varname != NULL && varp != NULL)
14115 {
14116 /* set curbuf to be our buf, temporarily */
14117#ifdef FEAT_AUTOCMD
14118 aucmd_prepbuf(&aco, buf);
14119#else
14120 save_curbuf = curbuf;
14121 curbuf = buf;
14122#endif
14123
14124 if (*varname == '&')
14125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 long numval;
14127 char_u *strval;
14128 int error = FALSE;
14129
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 numval = get_tv_number_chk(varp, &error);
14132 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014133 if (!error && strval != NULL)
14134 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 }
14136 else
14137 {
14138 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14139 if (bufvarname != NULL)
14140 {
14141 STRCPY(bufvarname, "b:");
14142 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014143 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 vim_free(bufvarname);
14145 }
14146 }
14147
14148 /* reset notion of buffer */
14149#ifdef FEAT_AUTOCMD
14150 aucmd_restbuf(&aco);
14151#else
14152 curbuf = save_curbuf;
14153#endif
14154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155}
14156
14157/*
14158 * "setcmdpos()" function
14159 */
14160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 typval_T *argvars;
14163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 int pos = (int)get_tv_number(&argvars[0]) - 1;
14166
14167 if (pos >= 0)
14168 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169}
14170
14171/*
14172 * "setline()" function
14173 */
14174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178{
14179 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014180 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014181 list_T *l = NULL;
14182 listitem_T *li = NULL;
14183 long added = 0;
14184 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014186 lnum = get_tv_lnum(&argvars[0]);
14187 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014189 l = argvars[1].vval.v_list;
14190 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014192 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014194
14195 rettv->vval.v_number = 0; /* OK */
14196 for (;;)
14197 {
14198 if (l != NULL)
14199 {
14200 /* list argument, get next string */
14201 if (li == NULL)
14202 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014204 li = li->li_next;
14205 }
14206
14207 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014209 break;
14210 if (lnum <= curbuf->b_ml.ml_line_count)
14211 {
14212 /* existing line, replace it */
14213 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14214 {
14215 changed_bytes(lnum, 0);
14216 check_cursor_col();
14217 rettv->vval.v_number = 0; /* OK */
14218 }
14219 }
14220 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14221 {
14222 /* lnum is one past the last line, append the line */
14223 ++added;
14224 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14225 rettv->vval.v_number = 0; /* OK */
14226 }
14227
14228 if (l == NULL) /* only one string argument */
14229 break;
14230 ++lnum;
14231 }
14232
14233 if (added > 0)
14234 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235}
14236
14237/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014238 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014239 */
14240/*ARGSUSED*/
14241 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014242set_qf_ll_list(wp, list_arg, action_arg, rettv)
14243 win_T *wp;
14244 typval_T *list_arg;
14245 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014246 typval_T *rettv;
14247{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014248#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014249 char_u *act;
14250 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014251#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014252
Bram Moolenaar2641f772005-03-25 21:58:17 +000014253 rettv->vval.v_number = -1;
14254
14255#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014256 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014257 EMSG(_(e_listreq));
14258 else
14259 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014260 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014261
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014262 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014263 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014264 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 if (act == NULL)
14266 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014267 if (*act == 'a' || *act == 'r')
14268 action = *act;
14269 }
14270
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014271 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014272 rettv->vval.v_number = 0;
14273 }
14274#endif
14275}
14276
14277/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014278 * "setloclist()" function
14279 */
14280/*ARGSUSED*/
14281 static void
14282f_setloclist(argvars, rettv)
14283 typval_T *argvars;
14284 typval_T *rettv;
14285{
14286 win_T *win;
14287
14288 rettv->vval.v_number = -1;
14289
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014290 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014291 if (win != NULL)
14292 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14293}
14294
14295/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014296 * "setpos()" function
14297 */
14298/*ARGSUSED*/
14299 static void
14300f_setpos(argvars, rettv)
14301 typval_T *argvars;
14302 typval_T *rettv;
14303{
14304 pos_T pos;
14305 int fnum;
14306 char_u *name;
14307
14308 name = get_tv_string_chk(argvars);
14309 if (name != NULL)
14310 {
14311 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14312 {
14313 --pos.col;
14314 if (name[0] == '.') /* cursor */
14315 {
14316 if (fnum == curbuf->b_fnum)
14317 {
14318 curwin->w_cursor = pos;
14319 check_cursor();
14320 }
14321 else
14322 EMSG(_(e_invarg));
14323 }
14324 else if (name[0] == '\'') /* mark */
14325 (void)setmark_pos(name[1], &pos, fnum);
14326 else
14327 EMSG(_(e_invarg));
14328 }
14329 }
14330}
14331
14332/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014333 * "setqflist()" function
14334 */
14335/*ARGSUSED*/
14336 static void
14337f_setqflist(argvars, rettv)
14338 typval_T *argvars;
14339 typval_T *rettv;
14340{
14341 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14342}
14343
14344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 * "setreg()" function
14346 */
14347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014349 typval_T *argvars;
14350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351{
14352 int regname;
14353 char_u *strregname;
14354 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 int append;
14357 char_u yank_type;
14358 long block_len;
14359
14360 block_len = -1;
14361 yank_type = MAUTO;
14362 append = FALSE;
14363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 if (strregname == NULL)
14368 return; /* type error; errmsg already given */
14369 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 if (regname == 0 || regname == '@')
14371 regname = '"';
14372 else if (regname == '=')
14373 return;
14374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014375 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 stropt = get_tv_string_chk(&argvars[2]);
14378 if (stropt == NULL)
14379 return; /* type error */
14380 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 switch (*stropt)
14382 {
14383 case 'a': case 'A': /* append */
14384 append = TRUE;
14385 break;
14386 case 'v': case 'c': /* character-wise selection */
14387 yank_type = MCHAR;
14388 break;
14389 case 'V': case 'l': /* line-wise selection */
14390 yank_type = MLINE;
14391 break;
14392#ifdef FEAT_VISUAL
14393 case 'b': case Ctrl_V: /* block-wise selection */
14394 yank_type = MBLOCK;
14395 if (VIM_ISDIGIT(stropt[1]))
14396 {
14397 ++stropt;
14398 block_len = getdigits(&stropt) - 1;
14399 --stropt;
14400 }
14401 break;
14402#endif
14403 }
14404 }
14405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 strval = get_tv_string_chk(&argvars[1]);
14407 if (strval != NULL)
14408 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411}
14412
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014413/*
14414 * "settabwinvar()" function
14415 */
14416 static void
14417f_settabwinvar(argvars, rettv)
14418 typval_T *argvars;
14419 typval_T *rettv;
14420{
14421 setwinvar(argvars, rettv, 1);
14422}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423
14424/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014425 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 typval_T *argvars;
14430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014432 setwinvar(argvars, rettv, 0);
14433}
14434
14435/*
14436 * "setwinvar()" and "settabwinvar()" functions
14437 */
14438 static void
14439setwinvar(argvars, rettv, off)
14440 typval_T *argvars;
14441 typval_T *rettv;
14442 int off;
14443{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 win_T *win;
14445#ifdef FEAT_WINDOWS
14446 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014447 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448#endif
14449 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014450 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014452 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014454 rettv->vval.v_number = 0;
14455
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 if (check_restricted() || check_secure())
14457 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014458
14459#ifdef FEAT_WINDOWS
14460 if (off == 1)
14461 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14462 else
14463 tp = curtab;
14464#endif
14465 win = find_win_by_nr(&argvars[off], tp);
14466 varname = get_tv_string_chk(&argvars[off + 1]);
14467 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468
14469 if (win != NULL && varname != NULL && varp != NULL)
14470 {
14471#ifdef FEAT_WINDOWS
14472 /* set curwin to be our win, temporarily */
14473 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014474 save_curtab = curtab;
14475 goto_tabpage_tp(tp);
14476 if (!win_valid(win))
14477 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 curwin = win;
14479 curbuf = curwin->w_buffer;
14480#endif
14481
14482 if (*varname == '&')
14483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 long numval;
14485 char_u *strval;
14486 int error = FALSE;
14487
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014489 numval = get_tv_number_chk(varp, &error);
14490 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 if (!error && strval != NULL)
14492 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 }
14494 else
14495 {
14496 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14497 if (winvarname != NULL)
14498 {
14499 STRCPY(winvarname, "w:");
14500 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014501 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 vim_free(winvarname);
14503 }
14504 }
14505
14506#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014507 /* Restore current tabpage and window, if still valid (autocomands can
14508 * make them invalid). */
14509 if (valid_tabpage(save_curtab))
14510 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 if (win_valid(save_curwin))
14512 {
14513 curwin = save_curwin;
14514 curbuf = curwin->w_buffer;
14515 }
14516#endif
14517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518}
14519
14520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014521 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 */
14523 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014524f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014525 typval_T *argvars;
14526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529
Bram Moolenaar0d660222005-01-07 21:51:51 +000014530 p = get_tv_string(&argvars[0]);
14531 rettv->vval.v_string = vim_strsave(p);
14532 simplify_filename(rettv->vval.v_string); /* simplify in place */
14533 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534}
14535
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536static int
14537#ifdef __BORLANDC__
14538 _RTLENTRYF
14539#endif
14540 item_compare __ARGS((const void *s1, const void *s2));
14541static int
14542#ifdef __BORLANDC__
14543 _RTLENTRYF
14544#endif
14545 item_compare2 __ARGS((const void *s1, const void *s2));
14546
14547static int item_compare_ic;
14548static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014550#define ITEM_COMPARE_FAIL 999
14551
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555 static int
14556#ifdef __BORLANDC__
14557_RTLENTRYF
14558#endif
14559item_compare(s1, s2)
14560 const void *s1;
14561 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 char_u *p1, *p2;
14564 char_u *tofree1, *tofree2;
14565 int res;
14566 char_u numbuf1[NUMBUFLEN];
14567 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014569 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14570 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 if (item_compare_ic)
14572 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574 res = STRCMP(p1, p2);
14575 vim_free(tofree1);
14576 vim_free(tofree2);
14577 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578}
14579
14580 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581#ifdef __BORLANDC__
14582_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584item_compare2(s1, s2)
14585 const void *s1;
14586 const void *s2;
14587{
14588 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014589 typval_T rettv;
14590 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 /* shortcut after failure in previous call; compare all items equal */
14594 if (item_compare_func_err)
14595 return 0;
14596
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014597 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14598 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014599 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14600 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601
14602 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014603 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014604 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014605 clear_tv(&argv[0]);
14606 clear_tv(&argv[1]);
14607
14608 if (res == FAIL)
14609 res = ITEM_COMPARE_FAIL;
14610 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 /* return value has wrong type */
14612 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14613 if (item_compare_func_err)
14614 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014615 clear_tv(&rettv);
14616 return res;
14617}
14618
14619/*
14620 * "sort({list})" function
14621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014623f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014624 typval_T *argvars;
14625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626{
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 list_T *l;
14628 listitem_T *li;
14629 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014630 long len;
14631 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 rettv->vval.v_number = 0;
14634 if (argvars[0].v_type != VAR_LIST)
14635 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 else
14637 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014639 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640 return;
14641 rettv->vval.v_list = l;
14642 rettv->v_type = VAR_LIST;
14643 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645 len = list_len(l);
14646 if (len <= 1)
14647 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 item_compare_ic = FALSE;
14650 item_compare_func = NULL;
14651 if (argvars[1].v_type != VAR_UNKNOWN)
14652 {
14653 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655 else
14656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014657 int error = FALSE;
14658
14659 i = get_tv_number_chk(&argvars[1], &error);
14660 if (error)
14661 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662 if (i == 1)
14663 item_compare_ic = TRUE;
14664 else
14665 item_compare_func = get_tv_string(&argvars[1]);
14666 }
14667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671 if (ptrs == NULL)
14672 return;
14673 i = 0;
14674 for (li = l->lv_first; li != NULL; li = li->li_next)
14675 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014677 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678 /* test the compare function */
14679 if (item_compare_func != NULL
14680 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14681 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014682 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684 {
14685 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014686 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687 item_compare_func == NULL ? item_compare : item_compare2);
14688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014689 if (!item_compare_func_err)
14690 {
14691 /* Clear the List and append the items in the sorted order. */
14692 l->lv_first = l->lv_last = NULL;
14693 l->lv_len = 0;
14694 for (i = 0; i < len; ++i)
14695 list_append(l, ptrs[i]);
14696 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014697 }
14698
14699 vim_free(ptrs);
14700 }
14701}
14702
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014703/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014704 * "soundfold({word})" function
14705 */
14706 static void
14707f_soundfold(argvars, rettv)
14708 typval_T *argvars;
14709 typval_T *rettv;
14710{
14711 char_u *s;
14712
14713 rettv->v_type = VAR_STRING;
14714 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014715#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014716 rettv->vval.v_string = eval_soundfold(s);
14717#else
14718 rettv->vval.v_string = vim_strsave(s);
14719#endif
14720}
14721
14722/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014723 * "spellbadword()" function
14724 */
14725/* ARGSUSED */
14726 static void
14727f_spellbadword(argvars, rettv)
14728 typval_T *argvars;
14729 typval_T *rettv;
14730{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014731 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014732 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014733 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014734
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014735 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014736 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014737
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014738#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014739 if (argvars[0].v_type == VAR_UNKNOWN)
14740 {
14741 /* Find the start and length of the badly spelled word. */
14742 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14743 if (len != 0)
14744 word = ml_get_cursor();
14745 }
14746 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14747 {
14748 char_u *str = get_tv_string_chk(&argvars[0]);
14749 int capcol = -1;
14750
14751 if (str != NULL)
14752 {
14753 /* Check the argument for spelling. */
14754 while (*str != NUL)
14755 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014756 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014757 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014758 {
14759 word = str;
14760 break;
14761 }
14762 str += len;
14763 }
14764 }
14765 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014766#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014767
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014768 list_append_string(rettv->vval.v_list, word, len);
14769 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014770 attr == HLF_SPB ? "bad" :
14771 attr == HLF_SPR ? "rare" :
14772 attr == HLF_SPL ? "local" :
14773 attr == HLF_SPC ? "caps" :
14774 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014775}
14776
14777/*
14778 * "spellsuggest()" function
14779 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014780/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014781 static void
14782f_spellsuggest(argvars, rettv)
14783 typval_T *argvars;
14784 typval_T *rettv;
14785{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014786#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014787 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014788 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014789 int maxcount;
14790 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014791 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014792 listitem_T *li;
14793 int need_capital = FALSE;
14794#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014795
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014796 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014797 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014798
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014799#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014800 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14801 {
14802 str = get_tv_string(&argvars[0]);
14803 if (argvars[1].v_type != VAR_UNKNOWN)
14804 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014805 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014806 if (maxcount <= 0)
14807 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014808 if (argvars[2].v_type != VAR_UNKNOWN)
14809 {
14810 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14811 if (typeerr)
14812 return;
14813 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014814 }
14815 else
14816 maxcount = 25;
14817
Bram Moolenaar4770d092006-01-12 23:22:24 +000014818 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014819
14820 for (i = 0; i < ga.ga_len; ++i)
14821 {
14822 str = ((char_u **)ga.ga_data)[i];
14823
14824 li = listitem_alloc();
14825 if (li == NULL)
14826 vim_free(str);
14827 else
14828 {
14829 li->li_tv.v_type = VAR_STRING;
14830 li->li_tv.v_lock = 0;
14831 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014832 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014833 }
14834 }
14835 ga_clear(&ga);
14836 }
14837#endif
14838}
14839
Bram Moolenaar0d660222005-01-07 21:51:51 +000014840 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014841f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014842 typval_T *argvars;
14843 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014844{
14845 char_u *str;
14846 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014847 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014848 regmatch_T regmatch;
14849 char_u patbuf[NUMBUFLEN];
14850 char_u *save_cpo;
14851 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014852 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014853 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014855
14856 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14857 save_cpo = p_cpo;
14858 p_cpo = (char_u *)"";
14859
14860 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014861 if (argvars[1].v_type != VAR_UNKNOWN)
14862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014863 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14864 if (pat == NULL)
14865 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014867 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014868 }
14869 if (pat == NULL || *pat == NUL)
14870 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014871
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014872 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 if (typeerr)
14875 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876
Bram Moolenaar0d660222005-01-07 21:51:51 +000014877 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14878 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014881 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014882 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014883 if (*str == NUL)
14884 match = FALSE; /* empty item at the end */
14885 else
14886 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014887 if (match)
14888 end = regmatch.startp[0];
14889 else
14890 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014891 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14892 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014893 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014894 if (list_append_string(rettv->vval.v_list, str,
14895 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014896 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014897 }
14898 if (!match)
14899 break;
14900 /* Advance to just after the match. */
14901 if (regmatch.endp[0] > str)
14902 col = 0;
14903 else
14904 {
14905 /* Don't get stuck at the same match. */
14906#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014907 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014908#else
14909 col = 1;
14910#endif
14911 }
14912 str = regmatch.endp[0];
14913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914
Bram Moolenaar0d660222005-01-07 21:51:51 +000014915 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917
Bram Moolenaar0d660222005-01-07 21:51:51 +000014918 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919}
14920
Bram Moolenaar2c932302006-03-18 21:42:09 +000014921/*
14922 * "str2nr()" function
14923 */
14924 static void
14925f_str2nr(argvars, rettv)
14926 typval_T *argvars;
14927 typval_T *rettv;
14928{
14929 int base = 10;
14930 char_u *p;
14931 long n;
14932
14933 if (argvars[1].v_type != VAR_UNKNOWN)
14934 {
14935 base = get_tv_number(&argvars[1]);
14936 if (base != 8 && base != 10 && base != 16)
14937 {
14938 EMSG(_(e_invarg));
14939 return;
14940 }
14941 }
14942
14943 p = skipwhite(get_tv_string(&argvars[0]));
14944 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14945 rettv->vval.v_number = n;
14946}
14947
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948#ifdef HAVE_STRFTIME
14949/*
14950 * "strftime({format}[, {time}])" function
14951 */
14952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 typval_T *argvars;
14955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956{
14957 char_u result_buf[256];
14958 struct tm *curtime;
14959 time_t seconds;
14960 char_u *p;
14961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014962 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014965 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 seconds = time(NULL);
14967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014968 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 curtime = localtime(&seconds);
14970 /* MSVC returns NULL for an invalid value of seconds. */
14971 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014972 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973 else
14974 {
14975# ifdef FEAT_MBYTE
14976 vimconv_T conv;
14977 char_u *enc;
14978
14979 conv.vc_type = CONV_NONE;
14980 enc = enc_locale();
14981 convert_setup(&conv, p_enc, enc);
14982 if (conv.vc_type != CONV_NONE)
14983 p = string_convert(&conv, p, NULL);
14984# endif
14985 if (p != NULL)
14986 (void)strftime((char *)result_buf, sizeof(result_buf),
14987 (char *)p, curtime);
14988 else
14989 result_buf[0] = NUL;
14990
14991# ifdef FEAT_MBYTE
14992 if (conv.vc_type != CONV_NONE)
14993 vim_free(p);
14994 convert_setup(&conv, enc, p_enc);
14995 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 else
14998# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000
15001# ifdef FEAT_MBYTE
15002 /* Release conversion descriptors */
15003 convert_setup(&conv, NULL, NULL);
15004 vim_free(enc);
15005# endif
15006 }
15007}
15008#endif
15009
15010/*
15011 * "stridx()" function
15012 */
15013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015015 typval_T *argvars;
15016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017{
15018 char_u buf[NUMBUFLEN];
15019 char_u *needle;
15020 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015021 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015025 needle = get_tv_string_chk(&argvars[1]);
15026 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015028 if (needle == NULL || haystack == NULL)
15029 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030
Bram Moolenaar33570922005-01-25 22:26:29 +000015031 if (argvars[2].v_type != VAR_UNKNOWN)
15032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015033 int error = FALSE;
15034
15035 start_idx = get_tv_number_chk(&argvars[2], &error);
15036 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015038 if (start_idx >= 0)
15039 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015040 }
15041
15042 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15043 if (pos != NULL)
15044 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045}
15046
15047/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015048 * "string()" function
15049 */
15050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015052 typval_T *argvars;
15053 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015054{
15055 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015056 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015058 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015059 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015060 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015061 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062}
15063
15064/*
15065 * "strlen()" function
15066 */
15067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 typval_T *argvars;
15070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015072 rettv->vval.v_number = (varnumber_T)(STRLEN(
15073 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074}
15075
15076/*
15077 * "strpart()" function
15078 */
15079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015080f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015081 typval_T *argvars;
15082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083{
15084 char_u *p;
15085 int n;
15086 int len;
15087 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 slen = (int)STRLEN(p);
15092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093 n = get_tv_number_chk(&argvars[1], &error);
15094 if (error)
15095 len = 0;
15096 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015097 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 else
15099 len = slen - n; /* default len: all bytes that are available. */
15100
15101 /*
15102 * Only return the overlap between the specified part and the actual
15103 * string.
15104 */
15105 if (n < 0)
15106 {
15107 len += n;
15108 n = 0;
15109 }
15110 else if (n > slen)
15111 n = slen;
15112 if (len < 0)
15113 len = 0;
15114 else if (n + len > slen)
15115 len = slen - n;
15116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015117 rettv->v_type = VAR_STRING;
15118 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119}
15120
15121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 * "strridx()" function
15123 */
15124 static void
15125f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015126 typval_T *argvars;
15127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128{
15129 char_u buf[NUMBUFLEN];
15130 char_u *needle;
15131 char_u *haystack;
15132 char_u *rest;
15133 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015134 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015136 needle = get_tv_string_chk(&argvars[1]);
15137 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015138 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139
15140 rettv->vval.v_number = -1;
15141 if (needle == NULL || haystack == NULL)
15142 return; /* type error; errmsg already given */
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)
15403 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015404 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15405 err = TRUE;
15406 if (fclose(fd) != 0)
15407 err = TRUE;
15408 if (err)
15409 {
15410 EMSG(_("E677: Error writing temp file"));
15411 goto done;
15412 }
15413 }
15414
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015415 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15416 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015417
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418#ifdef USE_CR
15419 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015420 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 {
15422 char_u *s;
15423
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015424 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425 {
15426 if (*s == CAR)
15427 *s = NL;
15428 }
15429 }
15430#else
15431# ifdef USE_CRNL
15432 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015433 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 {
15435 char_u *s, *d;
15436
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015437 d = res;
15438 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 {
15440 if (s[0] == CAR && s[1] == NL)
15441 ++s;
15442 *d++ = *s;
15443 }
15444 *d = NUL;
15445 }
15446# endif
15447#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015448
15449done:
15450 if (infile != NULL)
15451 {
15452 mch_remove(infile);
15453 vim_free(infile);
15454 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455 rettv->v_type = VAR_STRING;
15456 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457}
15458
15459/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015460 * "tabpagebuflist()" function
15461 */
15462/* ARGSUSED */
15463 static void
15464f_tabpagebuflist(argvars, rettv)
15465 typval_T *argvars;
15466 typval_T *rettv;
15467{
15468#ifndef FEAT_WINDOWS
15469 rettv->vval.v_number = 0;
15470#else
15471 tabpage_T *tp;
15472 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015473
15474 if (argvars[0].v_type == VAR_UNKNOWN)
15475 wp = firstwin;
15476 else
15477 {
15478 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15479 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015480 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015481 }
15482 if (wp == NULL)
15483 rettv->vval.v_number = 0;
15484 else
15485 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015486 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015487 rettv->vval.v_number = 0;
15488 else
15489 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015490 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015491 if (list_append_number(rettv->vval.v_list,
15492 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015493 break;
15494 }
15495 }
15496#endif
15497}
15498
15499
15500/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015501 * "tabpagenr()" function
15502 */
15503/* ARGSUSED */
15504 static void
15505f_tabpagenr(argvars, rettv)
15506 typval_T *argvars;
15507 typval_T *rettv;
15508{
15509 int nr = 1;
15510#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015511 char_u *arg;
15512
15513 if (argvars[0].v_type != VAR_UNKNOWN)
15514 {
15515 arg = get_tv_string_chk(&argvars[0]);
15516 nr = 0;
15517 if (arg != NULL)
15518 {
15519 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015520 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015521 else
15522 EMSG2(_(e_invexpr2), arg);
15523 }
15524 }
15525 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015526 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015527#endif
15528 rettv->vval.v_number = nr;
15529}
15530
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015531
15532#ifdef FEAT_WINDOWS
15533static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15534
15535/*
15536 * Common code for tabpagewinnr() and winnr().
15537 */
15538 static int
15539get_winnr(tp, argvar)
15540 tabpage_T *tp;
15541 typval_T *argvar;
15542{
15543 win_T *twin;
15544 int nr = 1;
15545 win_T *wp;
15546 char_u *arg;
15547
15548 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15549 if (argvar->v_type != VAR_UNKNOWN)
15550 {
15551 arg = get_tv_string_chk(argvar);
15552 if (arg == NULL)
15553 nr = 0; /* type error; errmsg already given */
15554 else if (STRCMP(arg, "$") == 0)
15555 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15556 else if (STRCMP(arg, "#") == 0)
15557 {
15558 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15559 if (twin == NULL)
15560 nr = 0;
15561 }
15562 else
15563 {
15564 EMSG2(_(e_invexpr2), arg);
15565 nr = 0;
15566 }
15567 }
15568
15569 if (nr > 0)
15570 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15571 wp != twin; wp = wp->w_next)
15572 ++nr;
15573 return nr;
15574}
15575#endif
15576
15577/*
15578 * "tabpagewinnr()" function
15579 */
15580/* ARGSUSED */
15581 static void
15582f_tabpagewinnr(argvars, rettv)
15583 typval_T *argvars;
15584 typval_T *rettv;
15585{
15586 int nr = 1;
15587#ifdef FEAT_WINDOWS
15588 tabpage_T *tp;
15589
15590 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15591 if (tp == NULL)
15592 nr = 0;
15593 else
15594 nr = get_winnr(tp, &argvars[1]);
15595#endif
15596 rettv->vval.v_number = nr;
15597}
15598
15599
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015600/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015601 * "tagfiles()" function
15602 */
15603/*ARGSUSED*/
15604 static void
15605f_tagfiles(argvars, rettv)
15606 typval_T *argvars;
15607 typval_T *rettv;
15608{
15609 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015610 tagname_T tn;
15611 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015612
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015613 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015614 {
15615 rettv->vval.v_number = 0;
15616 return;
15617 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015618
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015619 for (first = TRUE; ; first = FALSE)
15620 if (get_tagfname(&tn, first, fname) == FAIL
15621 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015622 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015623 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015624}
15625
15626/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015627 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015628 */
15629 static void
15630f_taglist(argvars, rettv)
15631 typval_T *argvars;
15632 typval_T *rettv;
15633{
15634 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015635
15636 tag_pattern = get_tv_string(&argvars[0]);
15637
15638 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015639 if (*tag_pattern == NUL)
15640 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015641
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015642 if (rettv_list_alloc(rettv) == OK)
15643 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015644}
15645
15646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 * "tempname()" function
15648 */
15649/*ARGSUSED*/
15650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015651f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015652 typval_T *argvars;
15653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654{
15655 static int x = 'A';
15656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015657 rettv->v_type = VAR_STRING;
15658 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659
15660 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15661 * names. Skip 'I' and 'O', they are used for shell redirection. */
15662 do
15663 {
15664 if (x == 'Z')
15665 x = '0';
15666 else if (x == '9')
15667 x = 'A';
15668 else
15669 {
15670#ifdef EBCDIC
15671 if (x == 'I')
15672 x = 'J';
15673 else if (x == 'R')
15674 x = 'S';
15675 else
15676#endif
15677 ++x;
15678 }
15679 } while (x == 'I' || x == 'O');
15680}
15681
15682/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015683 * "test(list)" function: Just checking the walls...
15684 */
15685/*ARGSUSED*/
15686 static void
15687f_test(argvars, rettv)
15688 typval_T *argvars;
15689 typval_T *rettv;
15690{
15691 /* Used for unit testing. Change the code below to your liking. */
15692#if 0
15693 listitem_T *li;
15694 list_T *l;
15695 char_u *bad, *good;
15696
15697 if (argvars[0].v_type != VAR_LIST)
15698 return;
15699 l = argvars[0].vval.v_list;
15700 if (l == NULL)
15701 return;
15702 li = l->lv_first;
15703 if (li == NULL)
15704 return;
15705 bad = get_tv_string(&li->li_tv);
15706 li = li->li_next;
15707 if (li == NULL)
15708 return;
15709 good = get_tv_string(&li->li_tv);
15710 rettv->vval.v_number = test_edit_score(bad, good);
15711#endif
15712}
15713
15714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 * "tolower(string)" function
15716 */
15717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015718f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015719 typval_T *argvars;
15720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721{
15722 char_u *p;
15723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015724 p = vim_strsave(get_tv_string(&argvars[0]));
15725 rettv->v_type = VAR_STRING;
15726 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727
15728 if (p != NULL)
15729 while (*p != NUL)
15730 {
15731#ifdef FEAT_MBYTE
15732 int l;
15733
15734 if (enc_utf8)
15735 {
15736 int c, lc;
15737
15738 c = utf_ptr2char(p);
15739 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015740 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 /* TODO: reallocate string when byte count changes. */
15742 if (utf_char2len(lc) == l)
15743 utf_char2bytes(lc, p);
15744 p += l;
15745 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015746 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 p += l; /* skip multi-byte character */
15748 else
15749#endif
15750 {
15751 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15752 ++p;
15753 }
15754 }
15755}
15756
15757/*
15758 * "toupper(string)" function
15759 */
15760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 typval_T *argvars;
15763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015766 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767}
15768
15769/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015770 * "tr(string, fromstr, tostr)" function
15771 */
15772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 typval_T *argvars;
15775 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015776{
15777 char_u *instr;
15778 char_u *fromstr;
15779 char_u *tostr;
15780 char_u *p;
15781#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015782 int inlen;
15783 int fromlen;
15784 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015785 int idx;
15786 char_u *cpstr;
15787 int cplen;
15788 int first = TRUE;
15789#endif
15790 char_u buf[NUMBUFLEN];
15791 char_u buf2[NUMBUFLEN];
15792 garray_T ga;
15793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015794 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015795 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15796 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015797
15798 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015799 rettv->v_type = VAR_STRING;
15800 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 if (fromstr == NULL || tostr == NULL)
15802 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015803 ga_init2(&ga, (int)sizeof(char), 80);
15804
15805#ifdef FEAT_MBYTE
15806 if (!has_mbyte)
15807#endif
15808 /* not multi-byte: fromstr and tostr must be the same length */
15809 if (STRLEN(fromstr) != STRLEN(tostr))
15810 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015811#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015812error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015813#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015814 EMSG2(_(e_invarg2), fromstr);
15815 ga_clear(&ga);
15816 return;
15817 }
15818
15819 /* fromstr and tostr have to contain the same number of chars */
15820 while (*instr != NUL)
15821 {
15822#ifdef FEAT_MBYTE
15823 if (has_mbyte)
15824 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015825 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015826 cpstr = instr;
15827 cplen = inlen;
15828 idx = 0;
15829 for (p = fromstr; *p != NUL; p += fromlen)
15830 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015831 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015832 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15833 {
15834 for (p = tostr; *p != NUL; p += tolen)
15835 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015836 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015837 if (idx-- == 0)
15838 {
15839 cplen = tolen;
15840 cpstr = p;
15841 break;
15842 }
15843 }
15844 if (*p == NUL) /* tostr is shorter than fromstr */
15845 goto error;
15846 break;
15847 }
15848 ++idx;
15849 }
15850
15851 if (first && cpstr == instr)
15852 {
15853 /* Check that fromstr and tostr have the same number of
15854 * (multi-byte) characters. Done only once when a character
15855 * of instr doesn't appear in fromstr. */
15856 first = FALSE;
15857 for (p = tostr; *p != NUL; p += tolen)
15858 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015859 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015860 --idx;
15861 }
15862 if (idx != 0)
15863 goto error;
15864 }
15865
15866 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015867 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015868 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015869
15870 instr += inlen;
15871 }
15872 else
15873#endif
15874 {
15875 /* When not using multi-byte chars we can do it faster. */
15876 p = vim_strchr(fromstr, *instr);
15877 if (p != NULL)
15878 ga_append(&ga, tostr[p - fromstr]);
15879 else
15880 ga_append(&ga, *instr);
15881 ++instr;
15882 }
15883 }
15884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015886}
15887
15888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 * "type(expr)" function
15890 */
15891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015892f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015893 typval_T *argvars;
15894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015896 int n;
15897
15898 switch (argvars[0].v_type)
15899 {
15900 case VAR_NUMBER: n = 0; break;
15901 case VAR_STRING: n = 1; break;
15902 case VAR_FUNC: n = 2; break;
15903 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015904 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015905 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15906 }
15907 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908}
15909
15910/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015911 * "values(dict)" function
15912 */
15913 static void
15914f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015915 typval_T *argvars;
15916 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015917{
15918 dict_list(argvars, rettv, 1);
15919}
15920
15921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 * "virtcol(string)" function
15923 */
15924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015925f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015926 typval_T *argvars;
15927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928{
15929 colnr_T vcol = 0;
15930 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015931 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015933 fp = var2fpos(&argvars[0], FALSE, &fnum);
15934 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15935 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 {
15937 getvvcol(curwin, fp, NULL, NULL, &vcol);
15938 ++vcol;
15939 }
15940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015941 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942}
15943
15944/*
15945 * "visualmode()" function
15946 */
15947/*ARGSUSED*/
15948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015949f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015950 typval_T *argvars;
15951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952{
15953#ifdef FEAT_VISUAL
15954 char_u str[2];
15955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015956 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 str[0] = curbuf->b_visual_mode_eval;
15958 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015959 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960
15961 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015962 if ((argvars[0].v_type == VAR_NUMBER
15963 && argvars[0].vval.v_number != 0)
15964 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015965 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 curbuf->b_visual_mode_eval = NUL;
15967#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015968 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969#endif
15970}
15971
15972/*
15973 * "winbufnr(nr)" function
15974 */
15975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015976f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015977 typval_T *argvars;
15978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979{
15980 win_T *wp;
15981
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015982 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015986 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987}
15988
15989/*
15990 * "wincol()" function
15991 */
15992/*ARGSUSED*/
15993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015994f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 typval_T *argvars;
15996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997{
15998 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015999 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000}
16001
16002/*
16003 * "winheight(nr)" function
16004 */
16005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016006f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016007 typval_T *argvars;
16008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009{
16010 win_T *wp;
16011
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016012 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016016 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017}
16018
16019/*
16020 * "winline()" function
16021 */
16022/*ARGSUSED*/
16023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016024f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016025 typval_T *argvars;
16026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027{
16028 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016029 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030}
16031
16032/*
16033 * "winnr()" function
16034 */
16035/* ARGSUSED */
16036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016037f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016038 typval_T *argvars;
16039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040{
16041 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016042
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016044 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016046 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047}
16048
16049/*
16050 * "winrestcmd()" function
16051 */
16052/* ARGSUSED */
16053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016055 typval_T *argvars;
16056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057{
16058#ifdef FEAT_WINDOWS
16059 win_T *wp;
16060 int winnr = 1;
16061 garray_T ga;
16062 char_u buf[50];
16063
16064 ga_init2(&ga, (int)sizeof(char), 70);
16065 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16066 {
16067 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16068 ga_concat(&ga, buf);
16069# ifdef FEAT_VERTSPLIT
16070 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16071 ga_concat(&ga, buf);
16072# endif
16073 ++winnr;
16074 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016075 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016077 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016081 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082}
16083
16084/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016085 * "winrestview()" function
16086 */
16087/* ARGSUSED */
16088 static void
16089f_winrestview(argvars, rettv)
16090 typval_T *argvars;
16091 typval_T *rettv;
16092{
16093 dict_T *dict;
16094
16095 if (argvars[0].v_type != VAR_DICT
16096 || (dict = argvars[0].vval.v_dict) == NULL)
16097 EMSG(_(e_invarg));
16098 else
16099 {
16100 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16101 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16102#ifdef FEAT_VIRTUALEDIT
16103 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16104#endif
16105 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016106 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016107
16108 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16109#ifdef FEAT_DIFF
16110 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16111#endif
16112 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16113 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16114
16115 check_cursor();
16116 changed_cline_bef_curs();
16117 invalidate_botline();
16118 redraw_later(VALID);
16119
16120 if (curwin->w_topline == 0)
16121 curwin->w_topline = 1;
16122 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16123 curwin->w_topline = curbuf->b_ml.ml_line_count;
16124#ifdef FEAT_DIFF
16125 check_topfill(curwin, TRUE);
16126#endif
16127 }
16128}
16129
16130/*
16131 * "winsaveview()" function
16132 */
16133/* ARGSUSED */
16134 static void
16135f_winsaveview(argvars, rettv)
16136 typval_T *argvars;
16137 typval_T *rettv;
16138{
16139 dict_T *dict;
16140
16141 dict = dict_alloc();
16142 if (dict == NULL)
16143 return;
16144 rettv->v_type = VAR_DICT;
16145 rettv->vval.v_dict = dict;
16146 ++dict->dv_refcount;
16147
16148 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16149 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16150#ifdef FEAT_VIRTUALEDIT
16151 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16152#endif
16153 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16154
16155 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16156#ifdef FEAT_DIFF
16157 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16158#endif
16159 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16160 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16161}
16162
16163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 * "winwidth(nr)" function
16165 */
16166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 typval_T *argvars;
16169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170{
16171 win_T *wp;
16172
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016173 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 else
16177#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016180 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181#endif
16182}
16183
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016185 * "writefile()" function
16186 */
16187 static void
16188f_writefile(argvars, rettv)
16189 typval_T *argvars;
16190 typval_T *rettv;
16191{
16192 int binary = FALSE;
16193 char_u *fname;
16194 FILE *fd;
16195 listitem_T *li;
16196 char_u *s;
16197 int ret = 0;
16198 int c;
16199
16200 if (argvars[0].v_type != VAR_LIST)
16201 {
16202 EMSG2(_(e_listarg), "writefile()");
16203 return;
16204 }
16205 if (argvars[0].vval.v_list == NULL)
16206 return;
16207
16208 if (argvars[2].v_type != VAR_UNKNOWN
16209 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16210 binary = TRUE;
16211
16212 /* Always open the file in binary mode, library functions have a mind of
16213 * their own about CR-LF conversion. */
16214 fname = get_tv_string(&argvars[1]);
16215 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16216 {
16217 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16218 ret = -1;
16219 }
16220 else
16221 {
16222 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16223 li = li->li_next)
16224 {
16225 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16226 {
16227 if (*s == '\n')
16228 c = putc(NUL, fd);
16229 else
16230 c = putc(*s, fd);
16231 if (c == EOF)
16232 {
16233 ret = -1;
16234 break;
16235 }
16236 }
16237 if (!binary || li->li_next != NULL)
16238 if (putc('\n', fd) == EOF)
16239 {
16240 ret = -1;
16241 break;
16242 }
16243 if (ret < 0)
16244 {
16245 EMSG(_(e_write));
16246 break;
16247 }
16248 }
16249 fclose(fd);
16250 }
16251
16252 rettv->vval.v_number = ret;
16253}
16254
16255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016257 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 */
16259 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016260var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016261 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016263 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016265 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016267 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268
Bram Moolenaara5525202006-03-02 22:52:09 +000016269 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016270 if (varp->v_type == VAR_LIST)
16271 {
16272 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016273 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016274 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016275
16276 l = varp->vval.v_list;
16277 if (l == NULL)
16278 return NULL;
16279
16280 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016281 pos.lnum = list_find_nr(l, 0L, &error);
16282 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016283 return NULL; /* invalid line number */
16284
16285 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016286 pos.col = list_find_nr(l, 1L, &error);
16287 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016288 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016289 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016290 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016291 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016292 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016293 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016294
Bram Moolenaara5525202006-03-02 22:52:09 +000016295#ifdef FEAT_VIRTUALEDIT
16296 /* Get the virtual offset. Defaults to zero. */
16297 pos.coladd = list_find_nr(l, 2L, &error);
16298 if (error)
16299 pos.coladd = 0;
16300#endif
16301
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016302 return &pos;
16303 }
16304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 name = get_tv_string_chk(varp);
16306 if (name == NULL)
16307 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 if (name[0] == '.') /* cursor */
16309 return &curwin->w_cursor;
16310 if (name[0] == '\'') /* mark */
16311 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016312 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16314 return NULL;
16315 return pp;
16316 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016317
16318#ifdef FEAT_VIRTUALEDIT
16319 pos.coladd = 0;
16320#endif
16321
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016322 if (name[0] == 'w' && lnum)
16323 {
16324 pos.col = 0;
16325 if (name[1] == '0') /* "w0": first visible line */
16326 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016327 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016328 pos.lnum = curwin->w_topline;
16329 return &pos;
16330 }
16331 else if (name[1] == '$') /* "w$": last visible line */
16332 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016333 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016334 pos.lnum = curwin->w_botline - 1;
16335 return &pos;
16336 }
16337 }
16338 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 {
16340 if (lnum)
16341 {
16342 pos.lnum = curbuf->b_ml.ml_line_count;
16343 pos.col = 0;
16344 }
16345 else
16346 {
16347 pos.lnum = curwin->w_cursor.lnum;
16348 pos.col = (colnr_T)STRLEN(ml_get_curline());
16349 }
16350 return &pos;
16351 }
16352 return NULL;
16353}
16354
16355/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016356 * Convert list in "arg" into a position and optional file number.
16357 * When "fnump" is NULL there is no file number, only 3 items.
16358 * Note that the column is passed on as-is, the caller may want to decrement
16359 * it to use 1 for the first column.
16360 * Return FAIL when conversion is not possible, doesn't check the position for
16361 * validity.
16362 */
16363 static int
16364list2fpos(arg, posp, fnump)
16365 typval_T *arg;
16366 pos_T *posp;
16367 int *fnump;
16368{
16369 list_T *l = arg->vval.v_list;
16370 long i = 0;
16371 long n;
16372
16373 /* List must be: [fnum, lnum, col, coladd] */
16374 if (arg->v_type != VAR_LIST || l == NULL
16375 || l->lv_len != (fnump == NULL ? 3 : 4))
16376 return FAIL;
16377
16378 if (fnump != NULL)
16379 {
16380 n = list_find_nr(l, i++, NULL); /* fnum */
16381 if (n < 0)
16382 return FAIL;
16383 if (n == 0)
16384 n = curbuf->b_fnum; /* current buffer */
16385 *fnump = n;
16386 }
16387
16388 n = list_find_nr(l, i++, NULL); /* lnum */
16389 if (n < 0)
16390 return FAIL;
16391 posp->lnum = n;
16392
16393 n = list_find_nr(l, i++, NULL); /* col */
16394 if (n < 0)
16395 return FAIL;
16396 posp->col = n;
16397
16398#ifdef FEAT_VIRTUALEDIT
16399 n = list_find_nr(l, i, NULL);
16400 if (n < 0)
16401 return FAIL;
16402 posp->coladd = n;
16403#endif
16404
16405 return OK;
16406}
16407
16408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 * Get the length of an environment variable name.
16410 * Advance "arg" to the first character after the name.
16411 * Return 0 for error.
16412 */
16413 static int
16414get_env_len(arg)
16415 char_u **arg;
16416{
16417 char_u *p;
16418 int len;
16419
16420 for (p = *arg; vim_isIDc(*p); ++p)
16421 ;
16422 if (p == *arg) /* no name found */
16423 return 0;
16424
16425 len = (int)(p - *arg);
16426 *arg = p;
16427 return len;
16428}
16429
16430/*
16431 * Get the length of the name of a function or internal variable.
16432 * "arg" is advanced to the first non-white character after the name.
16433 * Return 0 if something is wrong.
16434 */
16435 static int
16436get_id_len(arg)
16437 char_u **arg;
16438{
16439 char_u *p;
16440 int len;
16441
16442 /* Find the end of the name. */
16443 for (p = *arg; eval_isnamec(*p); ++p)
16444 ;
16445 if (p == *arg) /* no name found */
16446 return 0;
16447
16448 len = (int)(p - *arg);
16449 *arg = skipwhite(p);
16450
16451 return len;
16452}
16453
16454/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016455 * Get the length of the name of a variable or function.
16456 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016458 * Return -1 if curly braces expansion failed.
16459 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 * If the name contains 'magic' {}'s, expand them and return the
16461 * expanded name in an allocated string via 'alias' - caller must free.
16462 */
16463 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016464get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 char_u **arg;
16466 char_u **alias;
16467 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016468 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469{
16470 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 char_u *p;
16472 char_u *expr_start;
16473 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474
16475 *alias = NULL; /* default to no alias */
16476
16477 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16478 && (*arg)[2] == (int)KE_SNR)
16479 {
16480 /* hard coded <SNR>, already translated */
16481 *arg += 3;
16482 return get_id_len(arg) + 3;
16483 }
16484 len = eval_fname_script(*arg);
16485 if (len > 0)
16486 {
16487 /* literal "<SID>", "s:" or "<SNR>" */
16488 *arg += len;
16489 }
16490
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016492 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016494 p = find_name_end(*arg, &expr_start, &expr_end,
16495 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 if (expr_start != NULL)
16497 {
16498 char_u *temp_string;
16499
16500 if (!evaluate)
16501 {
16502 len += (int)(p - *arg);
16503 *arg = skipwhite(p);
16504 return len;
16505 }
16506
16507 /*
16508 * Include any <SID> etc in the expanded string:
16509 * Thus the -len here.
16510 */
16511 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16512 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016513 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514 *alias = temp_string;
16515 *arg = skipwhite(p);
16516 return (int)STRLEN(temp_string);
16517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518
16519 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016520 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 EMSG2(_(e_invexpr2), *arg);
16522
16523 return len;
16524}
16525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016526/*
16527 * Find the end of a variable or function name, taking care of magic braces.
16528 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16529 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016530 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016531 * Return a pointer to just after the name. Equal to "arg" if there is no
16532 * valid name.
16533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016535find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 char_u *arg;
16537 char_u **expr_start;
16538 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016539 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016541 int mb_nest = 0;
16542 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 char_u *p;
16544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016545 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016547 *expr_start = NULL;
16548 *expr_end = NULL;
16549 }
16550
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016551 /* Quick check for valid starting character. */
16552 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16553 return arg;
16554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016555 for (p = arg; *p != NUL
16556 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016557 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016558 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016559 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016560 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016561 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016562 if (*p == '\'')
16563 {
16564 /* skip over 'string' to avoid counting [ and ] inside it. */
16565 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16566 ;
16567 if (*p == NUL)
16568 break;
16569 }
16570 else if (*p == '"')
16571 {
16572 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16573 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16574 if (*p == '\\' && p[1] != NUL)
16575 ++p;
16576 if (*p == NUL)
16577 break;
16578 }
16579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016580 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016582 if (*p == '[')
16583 ++br_nest;
16584 else if (*p == ']')
16585 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016588 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016590 if (*p == '{')
16591 {
16592 mb_nest++;
16593 if (expr_start != NULL && *expr_start == NULL)
16594 *expr_start = p;
16595 }
16596 else if (*p == '}')
16597 {
16598 mb_nest--;
16599 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16600 *expr_end = p;
16601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 }
16604
16605 return p;
16606}
16607
16608/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016609 * Expands out the 'magic' {}'s in a variable/function name.
16610 * Note that this can call itself recursively, to deal with
16611 * constructs like foo{bar}{baz}{bam}
16612 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16613 * "in_start" ^
16614 * "expr_start" ^
16615 * "expr_end" ^
16616 * "in_end" ^
16617 *
16618 * Returns a new allocated string, which the caller must free.
16619 * Returns NULL for failure.
16620 */
16621 static char_u *
16622make_expanded_name(in_start, expr_start, expr_end, in_end)
16623 char_u *in_start;
16624 char_u *expr_start;
16625 char_u *expr_end;
16626 char_u *in_end;
16627{
16628 char_u c1;
16629 char_u *retval = NULL;
16630 char_u *temp_result;
16631 char_u *nextcmd = NULL;
16632
16633 if (expr_end == NULL || in_end == NULL)
16634 return NULL;
16635 *expr_start = NUL;
16636 *expr_end = NUL;
16637 c1 = *in_end;
16638 *in_end = NUL;
16639
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016640 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641 if (temp_result != NULL && nextcmd == NULL)
16642 {
16643 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16644 + (in_end - expr_end) + 1));
16645 if (retval != NULL)
16646 {
16647 STRCPY(retval, in_start);
16648 STRCAT(retval, temp_result);
16649 STRCAT(retval, expr_end + 1);
16650 }
16651 }
16652 vim_free(temp_result);
16653
16654 *in_end = c1; /* put char back for error messages */
16655 *expr_start = '{';
16656 *expr_end = '}';
16657
16658 if (retval != NULL)
16659 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016660 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016661 if (expr_start != NULL)
16662 {
16663 /* Further expansion! */
16664 temp_result = make_expanded_name(retval, expr_start,
16665 expr_end, temp_result);
16666 vim_free(retval);
16667 retval = temp_result;
16668 }
16669 }
16670
16671 return retval;
16672}
16673
16674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016676 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 */
16678 static int
16679eval_isnamec(c)
16680 int c;
16681{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016682 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16683}
16684
16685/*
16686 * Return TRUE if character "c" can be used as the first character in a
16687 * variable or function name (excluding '{' and '}').
16688 */
16689 static int
16690eval_isnamec1(c)
16691 int c;
16692{
16693 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694}
16695
16696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 * Set number v: variable to "val".
16698 */
16699 void
16700set_vim_var_nr(idx, val)
16701 int idx;
16702 long val;
16703{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016704 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705}
16706
16707/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016708 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 */
16710 long
16711get_vim_var_nr(idx)
16712 int idx;
16713{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016714 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715}
16716
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016717#if defined(FEAT_AUTOCMD) || defined(PROTO)
16718/*
16719 * Get string v: variable value. Uses a static buffer, can only be used once.
16720 */
16721 char_u *
16722get_vim_var_str(idx)
16723 int idx;
16724{
16725 return get_tv_string(&vimvars[idx].vv_tv);
16726}
16727#endif
16728
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729/*
16730 * Set v:count, v:count1 and v:prevcount.
16731 */
16732 void
16733set_vcount(count, count1)
16734 long count;
16735 long count1;
16736{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016737 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16738 vimvars[VV_COUNT].vv_nr = count;
16739 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740}
16741
16742/*
16743 * Set string v: variable to a copy of "val".
16744 */
16745 void
16746set_vim_var_string(idx, val, len)
16747 int idx;
16748 char_u *val;
16749 int len; /* length of "val" to use or -1 (whole string) */
16750{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016751 /* Need to do this (at least) once, since we can't initialize a union.
16752 * Will always be invoked when "v:progname" is set. */
16753 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16754
Bram Moolenaare9a41262005-01-15 22:18:47 +000016755 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016757 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016759 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016761 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762}
16763
16764/*
16765 * Set v:register if needed.
16766 */
16767 void
16768set_reg_var(c)
16769 int c;
16770{
16771 char_u regname;
16772
16773 if (c == 0 || c == ' ')
16774 regname = '"';
16775 else
16776 regname = c;
16777 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016778 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779 set_vim_var_string(VV_REG, &regname, 1);
16780}
16781
16782/*
16783 * Get or set v:exception. If "oldval" == NULL, return the current value.
16784 * Otherwise, restore the value to "oldval" and return NULL.
16785 * Must always be called in pairs to save and restore v:exception! Does not
16786 * take care of memory allocations.
16787 */
16788 char_u *
16789v_exception(oldval)
16790 char_u *oldval;
16791{
16792 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016793 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794
Bram Moolenaare9a41262005-01-15 22:18:47 +000016795 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 return NULL;
16797}
16798
16799/*
16800 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16801 * Otherwise, restore the value to "oldval" and return NULL.
16802 * Must always be called in pairs to save and restore v:throwpoint! Does not
16803 * take care of memory allocations.
16804 */
16805 char_u *
16806v_throwpoint(oldval)
16807 char_u *oldval;
16808{
16809 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016810 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811
Bram Moolenaare9a41262005-01-15 22:18:47 +000016812 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 return NULL;
16814}
16815
16816#if defined(FEAT_AUTOCMD) || defined(PROTO)
16817/*
16818 * Set v:cmdarg.
16819 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16820 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16821 * Must always be called in pairs!
16822 */
16823 char_u *
16824set_cmdarg(eap, oldarg)
16825 exarg_T *eap;
16826 char_u *oldarg;
16827{
16828 char_u *oldval;
16829 char_u *newval;
16830 unsigned len;
16831
Bram Moolenaare9a41262005-01-15 22:18:47 +000016832 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016833 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016835 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016836 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016837 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 }
16839
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016840 if (eap->force_bin == FORCE_BIN)
16841 len = 6;
16842 else if (eap->force_bin == FORCE_NOBIN)
16843 len = 8;
16844 else
16845 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016846
16847 if (eap->read_edit)
16848 len += 7;
16849
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016850 if (eap->force_ff != 0)
16851 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16852# ifdef FEAT_MBYTE
16853 if (eap->force_enc != 0)
16854 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016855 if (eap->bad_char != 0)
16856 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016857# endif
16858
16859 newval = alloc(len + 1);
16860 if (newval == NULL)
16861 return NULL;
16862
16863 if (eap->force_bin == FORCE_BIN)
16864 sprintf((char *)newval, " ++bin");
16865 else if (eap->force_bin == FORCE_NOBIN)
16866 sprintf((char *)newval, " ++nobin");
16867 else
16868 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016869
16870 if (eap->read_edit)
16871 STRCAT(newval, " ++edit");
16872
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016873 if (eap->force_ff != 0)
16874 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16875 eap->cmd + eap->force_ff);
16876# ifdef FEAT_MBYTE
16877 if (eap->force_enc != 0)
16878 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16879 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016880 if (eap->bad_char != 0)
16881 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16882 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016883# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016884 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016885 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886}
16887#endif
16888
16889/*
16890 * Get the value of internal variable "name".
16891 * Return OK or FAIL.
16892 */
16893 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016894get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016895 char_u *name;
16896 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016898 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899{
16900 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016901 typval_T *tv = NULL;
16902 typval_T atv;
16903 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905
16906 /* truncate the name, so that we can use strcmp() */
16907 cc = name[len];
16908 name[len] = NUL;
16909
16910 /*
16911 * Check for "b:changedtick".
16912 */
16913 if (STRCMP(name, "b:changedtick") == 0)
16914 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016915 atv.v_type = VAR_NUMBER;
16916 atv.vval.v_number = curbuf->b_changedtick;
16917 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 }
16919
16920 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 * Check for user-defined variables.
16922 */
16923 else
16924 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016925 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 }
16929
Bram Moolenaare9a41262005-01-15 22:18:47 +000016930 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016932 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 ret = FAIL;
16935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016936 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016937 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938
16939 name[len] = cc;
16940
16941 return ret;
16942}
16943
16944/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016945 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16946 * Also handle function call with Funcref variable: func(expr)
16947 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16948 */
16949 static int
16950handle_subscript(arg, rettv, evaluate, verbose)
16951 char_u **arg;
16952 typval_T *rettv;
16953 int evaluate; /* do more than finding the end */
16954 int verbose; /* give error messages */
16955{
16956 int ret = OK;
16957 dict_T *selfdict = NULL;
16958 char_u *s;
16959 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016960 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016961
16962 while (ret == OK
16963 && (**arg == '['
16964 || (**arg == '.' && rettv->v_type == VAR_DICT)
16965 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16966 && !vim_iswhite(*(*arg - 1)))
16967 {
16968 if (**arg == '(')
16969 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016970 /* need to copy the funcref so that we can clear rettv */
16971 functv = *rettv;
16972 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016973
16974 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016975 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016976 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016977 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16978 &len, evaluate, selfdict);
16979
16980 /* Clear the funcref afterwards, so that deleting it while
16981 * evaluating the arguments is possible (see test55). */
16982 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016983
16984 /* Stop the expression evaluation when immediately aborting on
16985 * error, or when an interrupt occurred or an exception was thrown
16986 * but not caught. */
16987 if (aborting())
16988 {
16989 if (ret == OK)
16990 clear_tv(rettv);
16991 ret = FAIL;
16992 }
16993 dict_unref(selfdict);
16994 selfdict = NULL;
16995 }
16996 else /* **arg == '[' || **arg == '.' */
16997 {
16998 dict_unref(selfdict);
16999 if (rettv->v_type == VAR_DICT)
17000 {
17001 selfdict = rettv->vval.v_dict;
17002 if (selfdict != NULL)
17003 ++selfdict->dv_refcount;
17004 }
17005 else
17006 selfdict = NULL;
17007 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17008 {
17009 clear_tv(rettv);
17010 ret = FAIL;
17011 }
17012 }
17013 }
17014 dict_unref(selfdict);
17015 return ret;
17016}
17017
17018/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017019 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17020 * value).
17021 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017022 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017023alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017024{
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017026}
17027
17028/*
17029 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 * The string "s" must have been allocated, it is consumed.
17031 * Return NULL for out of memory, the variable otherwise.
17032 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017033 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 char_u *s;
17036{
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017039 rettv = alloc_tv();
17040 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017042 rettv->v_type = VAR_STRING;
17043 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 }
17045 else
17046 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048}
17049
17050/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017051 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017053 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017054free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017055 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056{
17057 if (varp != NULL)
17058 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017059 switch (varp->v_type)
17060 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017061 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017062 func_unref(varp->vval.v_string);
17063 /*FALLTHROUGH*/
17064 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017065 vim_free(varp->vval.v_string);
17066 break;
17067 case VAR_LIST:
17068 list_unref(varp->vval.v_list);
17069 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017070 case VAR_DICT:
17071 dict_unref(varp->vval.v_dict);
17072 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017073 case VAR_NUMBER:
17074 case VAR_UNKNOWN:
17075 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017076 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017077 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017078 break;
17079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 vim_free(varp);
17081 }
17082}
17083
17084/*
17085 * Free the memory for a variable value and set the value to NULL or 0.
17086 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017087 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017088clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017089 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090{
17091 if (varp != NULL)
17092 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017093 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017095 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017096 func_unref(varp->vval.v_string);
17097 /*FALLTHROUGH*/
17098 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017099 vim_free(varp->vval.v_string);
17100 varp->vval.v_string = NULL;
17101 break;
17102 case VAR_LIST:
17103 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017104 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017105 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017106 case VAR_DICT:
17107 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017108 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017109 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017110 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017111 varp->vval.v_number = 0;
17112 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017113 case VAR_UNKNOWN:
17114 break;
17115 default:
17116 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017118 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 }
17120}
17121
17122/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 * Set the value of a variable to NULL without freeing items.
17124 */
17125 static void
17126init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017127 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128{
17129 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017130 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131}
17132
17133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 * Get the number value of a variable.
17135 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 * For incompatible types, return 0.
17137 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17138 * caller of incompatible types: it sets *denote to TRUE if "denote"
17139 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 */
17141 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017142get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017145 int error = FALSE;
17146
17147 return get_tv_number_chk(varp, &error); /* return 0L on error */
17148}
17149
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017150 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017151get_tv_number_chk(varp, denote)
17152 typval_T *varp;
17153 int *denote;
17154{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017155 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017157 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017159 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017160 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017161 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017162 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017163 break;
17164 case VAR_STRING:
17165 if (varp->vval.v_string != NULL)
17166 vim_str2nr(varp->vval.v_string, NULL, NULL,
17167 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017169 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017170 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017171 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017172 case VAR_DICT:
17173 EMSG(_("E728: Using a Dictionary as a number"));
17174 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017175 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017176 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 if (denote == NULL) /* useful for values that must be unsigned */
17180 n = -1;
17181 else
17182 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017183 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184}
17185
17186/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017187 * Get the lnum from the first argument.
17188 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017189 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 */
17191 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017193 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194{
Bram Moolenaar33570922005-01-25 22:26:29 +000017195 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 linenr_T lnum;
17197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017198 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 if (lnum == 0) /* no valid number, try using line() */
17200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 rettv.v_type = VAR_NUMBER;
17202 f_line(argvars, &rettv);
17203 lnum = rettv.vval.v_number;
17204 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 }
17206 return lnum;
17207}
17208
17209/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017210 * Get the lnum from the first argument.
17211 * Also accepts "$", then "buf" is used.
17212 * Returns 0 on error.
17213 */
17214 static linenr_T
17215get_tv_lnum_buf(argvars, buf)
17216 typval_T *argvars;
17217 buf_T *buf;
17218{
17219 if (argvars[0].v_type == VAR_STRING
17220 && argvars[0].vval.v_string != NULL
17221 && argvars[0].vval.v_string[0] == '$'
17222 && buf != NULL)
17223 return buf->b_ml.ml_line_count;
17224 return get_tv_number_chk(&argvars[0], NULL);
17225}
17226
17227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 * Get the string value of a variable.
17229 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017230 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17231 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 * If the String variable has never been set, return an empty string.
17233 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17235 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 */
17237 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240{
17241 static char_u mybuf[NUMBUFLEN];
17242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244}
17245
17246 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017247get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017248 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017249 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017251 char_u *res = get_tv_string_buf_chk(varp, buf);
17252
17253 return res != NULL ? res : (char_u *)"";
17254}
17255
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017256 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017257get_tv_string_chk(varp)
17258 typval_T *varp;
17259{
17260 static char_u mybuf[NUMBUFLEN];
17261
17262 return get_tv_string_buf_chk(varp, mybuf);
17263}
17264
17265 static char_u *
17266get_tv_string_buf_chk(varp, buf)
17267 typval_T *varp;
17268 char_u *buf;
17269{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017270 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017272 case VAR_NUMBER:
17273 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17274 return buf;
17275 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017276 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017277 break;
17278 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017279 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017280 break;
17281 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017282 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017283 break;
17284 case VAR_STRING:
17285 if (varp->vval.v_string != NULL)
17286 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017287 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017288 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017289 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017292 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293}
17294
17295/*
17296 * Find variable "name" in the list of variables.
17297 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017298 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017299 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017300 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017303find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017308 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309
Bram Moolenaara7043832005-01-21 11:56:39 +000017310 ht = find_var_ht(name, &varname);
17311 if (htp != NULL)
17312 *htp = ht;
17313 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017315 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316}
17317
17318/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017320 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017322 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017323find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017324 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017325 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017326 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017327{
Bram Moolenaar33570922005-01-25 22:26:29 +000017328 hashitem_T *hi;
17329
17330 if (*varname == NUL)
17331 {
17332 /* Must be something like "s:", otherwise "ht" would be NULL. */
17333 switch (varname[-2])
17334 {
17335 case 's': return &SCRIPT_SV(current_SID).sv_var;
17336 case 'g': return &globvars_var;
17337 case 'v': return &vimvars_var;
17338 case 'b': return &curbuf->b_bufvar;
17339 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017340#ifdef FEAT_WINDOWS
17341 case 't': return &curtab->tp_winvar;
17342#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017343 case 'l': return current_funccal == NULL
17344 ? NULL : &current_funccal->l_vars_var;
17345 case 'a': return current_funccal == NULL
17346 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017347 }
17348 return NULL;
17349 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017350
17351 hi = hash_find(ht, varname);
17352 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017353 {
17354 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017355 * worked find the variable again. Don't auto-load a script if it was
17356 * loaded already, otherwise it would be loaded every time when
17357 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017358 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017359 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017360 hi = hash_find(ht, varname);
17361 if (HASHITEM_EMPTY(hi))
17362 return NULL;
17363 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017364 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017365}
17366
17367/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017369 * Set "varname" to the start of name without ':'.
17370 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017372find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 char_u *name;
17374 char_u **varname;
17375{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017376 hashitem_T *hi;
17377
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 if (name[1] != ':')
17379 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017380 /* The name must not start with a colon or #. */
17381 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 return NULL;
17383 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017384
17385 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017386 hi = hash_find(&compat_hashtab, name);
17387 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017388 return &compat_hashtab;
17389
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 return &globvarht; /* global variable */
17392 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 }
17394 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017395 if (*name == 'g') /* global variable */
17396 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017397 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17398 */
17399 if (vim_strchr(name + 2, ':') != NULL
17400 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017401 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017403 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017405 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017406#ifdef FEAT_WINDOWS
17407 if (*name == 't') /* tab page variable */
17408 return &curtab->tp_vars.dv_hashtab;
17409#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 if (*name == 'v') /* v: variable */
17411 return &vimvarht;
17412 if (*name == 'a' && current_funccal != NULL) /* function argument */
17413 return &current_funccal->l_avars.dv_hashtab;
17414 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17415 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 if (*name == 's' /* script variable */
17417 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17418 return &SCRIPT_VARS(current_SID);
17419 return NULL;
17420}
17421
17422/*
17423 * Get the string value of a (global/local) variable.
17424 * Returns NULL when it doesn't exist.
17425 */
17426 char_u *
17427get_var_value(name)
17428 char_u *name;
17429{
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431
Bram Moolenaara7043832005-01-21 11:56:39 +000017432 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 if (v == NULL)
17434 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436}
17437
17438/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017439 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 * sourcing this script and when executing functions defined in the script.
17441 */
17442 void
17443new_script_vars(id)
17444 scid_T id;
17445{
Bram Moolenaara7043832005-01-21 11:56:39 +000017446 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017447 hashtab_T *ht;
17448 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017449
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17451 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017452 /* Re-allocating ga_data means that an ht_array pointing to
17453 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017454 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017455 for (i = 1; i <= ga_scripts.ga_len; ++i)
17456 {
17457 ht = &SCRIPT_VARS(i);
17458 if (ht->ht_mask == HT_INIT_SIZE - 1)
17459 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017460 sv = &SCRIPT_SV(i);
17461 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017462 }
17463
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 while (ga_scripts.ga_len < id)
17465 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017466 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17467 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 }
17470 }
17471}
17472
17473/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017474 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17475 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 */
17477 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017478init_var_dict(dict, dict_var)
17479 dict_T *dict;
17480 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481{
Bram Moolenaar33570922005-01-25 22:26:29 +000017482 hash_init(&dict->dv_hashtab);
17483 dict->dv_refcount = 99999;
17484 dict_var->di_tv.vval.v_dict = dict;
17485 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017486 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17488 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489}
17490
17491/*
17492 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017493 * Frees all allocated variables and the value they contain.
17494 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 */
17496 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017497vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 hashtab_T *ht;
17499{
17500 vars_clear_ext(ht, TRUE);
17501}
17502
17503/*
17504 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17505 */
17506 static void
17507vars_clear_ext(ht, free_val)
17508 hashtab_T *ht;
17509 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510{
Bram Moolenaara7043832005-01-21 11:56:39 +000017511 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 hashitem_T *hi;
17513 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017516 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017517 for (hi = ht->ht_array; todo > 0; ++hi)
17518 {
17519 if (!HASHITEM_EMPTY(hi))
17520 {
17521 --todo;
17522
Bram Moolenaar33570922005-01-25 22:26:29 +000017523 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017524 * ht_array might change then. hash_clear() takes care of it
17525 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 v = HI2DI(hi);
17527 if (free_val)
17528 clear_tv(&v->di_tv);
17529 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17530 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017531 }
17532 }
17533 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017534 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535}
17536
Bram Moolenaara7043832005-01-21 11:56:39 +000017537/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017538 * Delete a variable from hashtab "ht" at item "hi".
17539 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017542delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 hashtab_T *ht;
17544 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545{
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017547
17548 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 clear_tv(&di->di_tv);
17550 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551}
17552
17553/*
17554 * List the value of one internal variable.
17555 */
17556 static void
17557list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017558 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 char_u *prefix;
17560{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017561 char_u *tofree;
17562 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017563 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017564
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017565 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017566 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017567 s == NULL ? (char_u *)"" : s);
17568 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569}
17570
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 static void
17572list_one_var_a(prefix, name, type, string)
17573 char_u *prefix;
17574 char_u *name;
17575 int type;
17576 char_u *string;
17577{
17578 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17579 if (name != NULL) /* "a:" vars don't have a name stored */
17580 msg_puts(name);
17581 msg_putchar(' ');
17582 msg_advance(22);
17583 if (type == VAR_NUMBER)
17584 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017585 else if (type == VAR_FUNC)
17586 msg_putchar('*');
17587 else if (type == VAR_LIST)
17588 {
17589 msg_putchar('[');
17590 if (*string == '[')
17591 ++string;
17592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017593 else if (type == VAR_DICT)
17594 {
17595 msg_putchar('{');
17596 if (*string == '{')
17597 ++string;
17598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 else
17600 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017601
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017603
17604 if (type == VAR_FUNC)
17605 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606}
17607
17608/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 * If the variable already exists, the value is updated.
17611 * Otherwise the variable is created.
17612 */
17613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017614set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618{
Bram Moolenaar33570922005-01-25 22:26:29 +000017619 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017622 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017624 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017625 {
17626 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17627 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17628 ? name[2] : name[0]))
17629 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017630 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017631 return;
17632 }
17633 if (function_exists(name))
17634 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017635 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017636 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017637 return;
17638 }
17639 }
17640
Bram Moolenaara7043832005-01-21 11:56:39 +000017641 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017643 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017644 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017645 return;
17646 }
17647
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017648 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017649 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017651 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017652 if (var_check_ro(v->di_flags, name)
17653 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017654 return;
17655 if (v->di_tv.v_type != tv->v_type
17656 && !((v->di_tv.v_type == VAR_STRING
17657 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017658 && (tv->v_type == VAR_STRING
17659 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017660 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017661 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017662 return;
17663 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017664
17665 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017666 * Handle setting internal v: variables separately: we don't change
17667 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 */
17669 if (ht == &vimvarht)
17670 {
17671 if (v->di_tv.v_type == VAR_STRING)
17672 {
17673 vim_free(v->di_tv.vval.v_string);
17674 if (copy || tv->v_type != VAR_STRING)
17675 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17676 else
17677 {
17678 /* Take over the string to avoid an extra alloc/free. */
17679 v->di_tv.vval.v_string = tv->vval.v_string;
17680 tv->vval.v_string = NULL;
17681 }
17682 }
17683 else if (v->di_tv.v_type != VAR_NUMBER)
17684 EMSG2(_(e_intern2), "set_var()");
17685 else
17686 v->di_tv.vval.v_number = get_tv_number(tv);
17687 return;
17688 }
17689
17690 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 }
17692 else /* add a new variable */
17693 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017694 /* Make sure the variable name is valid. */
17695 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017696 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17697 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017698 {
17699 EMSG2(_(e_illvar), varname);
17700 return;
17701 }
17702
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017703 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17704 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017705 if (v == NULL)
17706 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017707 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017708 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017710 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 return;
17712 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017713 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017716 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017717 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017718 else
17719 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017720 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017721 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017722 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724}
17725
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017726/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017727 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17728 * Also give an error message.
17729 */
17730 static int
17731var_check_ro(flags, name)
17732 int flags;
17733 char_u *name;
17734{
17735 if (flags & DI_FLAGS_RO)
17736 {
17737 EMSG2(_(e_readonlyvar), name);
17738 return TRUE;
17739 }
17740 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17741 {
17742 EMSG2(_(e_readonlysbx), name);
17743 return TRUE;
17744 }
17745 return FALSE;
17746}
17747
17748/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017749 * Return TRUE if typeval "tv" is set to be locked (immutable).
17750 * Also give an error message, using "name".
17751 */
17752 static int
17753tv_check_lock(lock, name)
17754 int lock;
17755 char_u *name;
17756{
17757 if (lock & VAR_LOCKED)
17758 {
17759 EMSG2(_("E741: Value is locked: %s"),
17760 name == NULL ? (char_u *)_("Unknown") : name);
17761 return TRUE;
17762 }
17763 if (lock & VAR_FIXED)
17764 {
17765 EMSG2(_("E742: Cannot change value of %s"),
17766 name == NULL ? (char_u *)_("Unknown") : name);
17767 return TRUE;
17768 }
17769 return FALSE;
17770}
17771
17772/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017773 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017774 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017775 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017778copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017779 typval_T *from;
17780 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017782 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017783 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017784 switch (from->v_type)
17785 {
17786 case VAR_NUMBER:
17787 to->vval.v_number = from->vval.v_number;
17788 break;
17789 case VAR_STRING:
17790 case VAR_FUNC:
17791 if (from->vval.v_string == NULL)
17792 to->vval.v_string = NULL;
17793 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017794 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017795 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017796 if (from->v_type == VAR_FUNC)
17797 func_ref(to->vval.v_string);
17798 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017799 break;
17800 case VAR_LIST:
17801 if (from->vval.v_list == NULL)
17802 to->vval.v_list = NULL;
17803 else
17804 {
17805 to->vval.v_list = from->vval.v_list;
17806 ++to->vval.v_list->lv_refcount;
17807 }
17808 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017809 case VAR_DICT:
17810 if (from->vval.v_dict == NULL)
17811 to->vval.v_dict = NULL;
17812 else
17813 {
17814 to->vval.v_dict = from->vval.v_dict;
17815 ++to->vval.v_dict->dv_refcount;
17816 }
17817 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017818 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017819 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017820 break;
17821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822}
17823
17824/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017825 * Make a copy of an item.
17826 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017827 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17828 * reference to an already copied list/dict can be used.
17829 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017830 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017831 static int
17832item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017833 typval_T *from;
17834 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017835 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017836 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017837{
17838 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017839 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017840
Bram Moolenaar33570922005-01-25 22:26:29 +000017841 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017842 {
17843 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017844 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017845 }
17846 ++recurse;
17847
17848 switch (from->v_type)
17849 {
17850 case VAR_NUMBER:
17851 case VAR_STRING:
17852 case VAR_FUNC:
17853 copy_tv(from, to);
17854 break;
17855 case VAR_LIST:
17856 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017857 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017858 if (from->vval.v_list == NULL)
17859 to->vval.v_list = NULL;
17860 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17861 {
17862 /* use the copy made earlier */
17863 to->vval.v_list = from->vval.v_list->lv_copylist;
17864 ++to->vval.v_list->lv_refcount;
17865 }
17866 else
17867 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17868 if (to->vval.v_list == NULL)
17869 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017870 break;
17871 case VAR_DICT:
17872 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017873 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017874 if (from->vval.v_dict == NULL)
17875 to->vval.v_dict = NULL;
17876 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17877 {
17878 /* use the copy made earlier */
17879 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17880 ++to->vval.v_dict->dv_refcount;
17881 }
17882 else
17883 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17884 if (to->vval.v_dict == NULL)
17885 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017886 break;
17887 default:
17888 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017889 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017890 }
17891 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017892 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017893}
17894
17895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 * ":echo expr1 ..." print each argument separated with a space, add a
17897 * newline at the end.
17898 * ":echon expr1 ..." print each argument plain.
17899 */
17900 void
17901ex_echo(eap)
17902 exarg_T *eap;
17903{
17904 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017905 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017906 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 char_u *p;
17908 int needclr = TRUE;
17909 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017910 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911
17912 if (eap->skip)
17913 ++emsg_skip;
17914 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17915 {
17916 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017917 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 {
17919 /*
17920 * Report the invalid expression unless the expression evaluation
17921 * has been cancelled due to an aborting error, an interrupt, or an
17922 * exception.
17923 */
17924 if (!aborting())
17925 EMSG2(_(e_invexpr2), p);
17926 break;
17927 }
17928 if (!eap->skip)
17929 {
17930 if (atstart)
17931 {
17932 atstart = FALSE;
17933 /* Call msg_start() after eval1(), evaluating the expression
17934 * may cause a message to appear. */
17935 if (eap->cmdidx == CMD_echo)
17936 msg_start();
17937 }
17938 else if (eap->cmdidx == CMD_echo)
17939 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017940 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017941 if (p != NULL)
17942 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017944 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017946 if (*p != TAB && needclr)
17947 {
17948 /* remove any text still there from the command */
17949 msg_clr_eos();
17950 needclr = FALSE;
17951 }
17952 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 }
17954 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017955 {
17956#ifdef FEAT_MBYTE
17957 if (has_mbyte)
17958 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017959 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017960
17961 (void)msg_outtrans_len_attr(p, i, echo_attr);
17962 p += i - 1;
17963 }
17964 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017966 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017969 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017971 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 arg = skipwhite(arg);
17973 }
17974 eap->nextcmd = check_nextcmd(arg);
17975
17976 if (eap->skip)
17977 --emsg_skip;
17978 else
17979 {
17980 /* remove text that may still be there from the command */
17981 if (needclr)
17982 msg_clr_eos();
17983 if (eap->cmdidx == CMD_echo)
17984 msg_end();
17985 }
17986}
17987
17988/*
17989 * ":echohl {name}".
17990 */
17991 void
17992ex_echohl(eap)
17993 exarg_T *eap;
17994{
17995 int id;
17996
17997 id = syn_name2id(eap->arg);
17998 if (id == 0)
17999 echo_attr = 0;
18000 else
18001 echo_attr = syn_id2attr(id);
18002}
18003
18004/*
18005 * ":execute expr1 ..." execute the result of an expression.
18006 * ":echomsg expr1 ..." Print a message
18007 * ":echoerr expr1 ..." Print an error
18008 * Each gets spaces around each argument and a newline at the end for
18009 * echo commands
18010 */
18011 void
18012ex_execute(eap)
18013 exarg_T *eap;
18014{
18015 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 int ret = OK;
18018 char_u *p;
18019 garray_T ga;
18020 int len;
18021 int save_did_emsg;
18022
18023 ga_init2(&ga, 1, 80);
18024
18025 if (eap->skip)
18026 ++emsg_skip;
18027 while (*arg != NUL && *arg != '|' && *arg != '\n')
18028 {
18029 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018030 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 {
18032 /*
18033 * Report the invalid expression unless the expression evaluation
18034 * has been cancelled due to an aborting error, an interrupt, or an
18035 * exception.
18036 */
18037 if (!aborting())
18038 EMSG2(_(e_invexpr2), p);
18039 ret = FAIL;
18040 break;
18041 }
18042
18043 if (!eap->skip)
18044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018045 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 len = (int)STRLEN(p);
18047 if (ga_grow(&ga, len + 2) == FAIL)
18048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 ret = FAIL;
18051 break;
18052 }
18053 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 ga.ga_len += len;
18057 }
18058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 arg = skipwhite(arg);
18061 }
18062
18063 if (ret != FAIL && ga.ga_data != NULL)
18064 {
18065 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018068 out_flush();
18069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 else if (eap->cmdidx == CMD_echoerr)
18071 {
18072 /* We don't want to abort following commands, restore did_emsg. */
18073 save_did_emsg = did_emsg;
18074 EMSG((char_u *)ga.ga_data);
18075 if (!force_abort)
18076 did_emsg = save_did_emsg;
18077 }
18078 else if (eap->cmdidx == CMD_execute)
18079 do_cmdline((char_u *)ga.ga_data,
18080 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18081 }
18082
18083 ga_clear(&ga);
18084
18085 if (eap->skip)
18086 --emsg_skip;
18087
18088 eap->nextcmd = check_nextcmd(arg);
18089}
18090
18091/*
18092 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18093 * "arg" points to the "&" or '+' when called, to "option" when returning.
18094 * Returns NULL when no option name found. Otherwise pointer to the char
18095 * after the option name.
18096 */
18097 static char_u *
18098find_option_end(arg, opt_flags)
18099 char_u **arg;
18100 int *opt_flags;
18101{
18102 char_u *p = *arg;
18103
18104 ++p;
18105 if (*p == 'g' && p[1] == ':')
18106 {
18107 *opt_flags = OPT_GLOBAL;
18108 p += 2;
18109 }
18110 else if (*p == 'l' && p[1] == ':')
18111 {
18112 *opt_flags = OPT_LOCAL;
18113 p += 2;
18114 }
18115 else
18116 *opt_flags = 0;
18117
18118 if (!ASCII_ISALPHA(*p))
18119 return NULL;
18120 *arg = p;
18121
18122 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18123 p += 4; /* termcap option */
18124 else
18125 while (ASCII_ISALPHA(*p))
18126 ++p;
18127 return p;
18128}
18129
18130/*
18131 * ":function"
18132 */
18133 void
18134ex_function(eap)
18135 exarg_T *eap;
18136{
18137 char_u *theline;
18138 int j;
18139 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 char_u *name = NULL;
18142 char_u *p;
18143 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018144 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 garray_T newargs;
18146 garray_T newlines;
18147 int varargs = FALSE;
18148 int mustend = FALSE;
18149 int flags = 0;
18150 ufunc_T *fp;
18151 int indent;
18152 int nesting;
18153 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018154 dictitem_T *v;
18155 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018156 static int func_nr = 0; /* number for nameless function */
18157 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018158 hashtab_T *ht;
18159 int todo;
18160 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018161 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162
18163 /*
18164 * ":function" without argument: list functions.
18165 */
18166 if (ends_excmd(*eap->arg))
18167 {
18168 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018169 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018170 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018171 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018172 {
18173 if (!HASHITEM_EMPTY(hi))
18174 {
18175 --todo;
18176 fp = HI2UF(hi);
18177 if (!isdigit(*fp->uf_name))
18178 list_func_head(fp, FALSE);
18179 }
18180 }
18181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 eap->nextcmd = check_nextcmd(eap->arg);
18183 return;
18184 }
18185
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018186 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018187 * ":function /pat": list functions matching pattern.
18188 */
18189 if (*eap->arg == '/')
18190 {
18191 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18192 if (!eap->skip)
18193 {
18194 regmatch_T regmatch;
18195
18196 c = *p;
18197 *p = NUL;
18198 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18199 *p = c;
18200 if (regmatch.regprog != NULL)
18201 {
18202 regmatch.rm_ic = p_ic;
18203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018204 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018205 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18206 {
18207 if (!HASHITEM_EMPTY(hi))
18208 {
18209 --todo;
18210 fp = HI2UF(hi);
18211 if (!isdigit(*fp->uf_name)
18212 && vim_regexec(&regmatch, fp->uf_name, 0))
18213 list_func_head(fp, FALSE);
18214 }
18215 }
18216 }
18217 }
18218 if (*p == '/')
18219 ++p;
18220 eap->nextcmd = check_nextcmd(p);
18221 return;
18222 }
18223
18224 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018225 * Get the function name. There are these situations:
18226 * func normal function name
18227 * "name" == func, "fudi.fd_dict" == NULL
18228 * dict.func new dictionary entry
18229 * "name" == NULL, "fudi.fd_dict" set,
18230 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18231 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018232 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018233 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18234 * dict.func existing dict entry that's not a Funcref
18235 * "name" == NULL, "fudi.fd_dict" set,
18236 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018239 name = trans_function_name(&p, eap->skip, 0, &fudi);
18240 paren = (vim_strchr(p, '(') != NULL);
18241 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 {
18243 /*
18244 * Return on an invalid expression in braces, unless the expression
18245 * evaluation has been cancelled due to an aborting error, an
18246 * interrupt, or an exception.
18247 */
18248 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018249 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018250 if (!eap->skip && fudi.fd_newkey != NULL)
18251 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018252 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 else
18256 eap->skip = TRUE;
18257 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018258
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 /* An error in a function call during evaluation of an expression in magic
18260 * braces should not cause the function not to be defined. */
18261 saved_did_emsg = did_emsg;
18262 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263
18264 /*
18265 * ":function func" with only function name: list function.
18266 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018267 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 {
18269 if (!ends_excmd(*skipwhite(p)))
18270 {
18271 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018272 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 }
18274 eap->nextcmd = check_nextcmd(p);
18275 if (eap->nextcmd != NULL)
18276 *p = NUL;
18277 if (!eap->skip && !got_int)
18278 {
18279 fp = find_func(name);
18280 if (fp != NULL)
18281 {
18282 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018283 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018285 if (FUNCLINE(fp, j) == NULL)
18286 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 msg_putchar('\n');
18288 msg_outnum((long)(j + 1));
18289 if (j < 9)
18290 msg_putchar(' ');
18291 if (j < 99)
18292 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018293 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 out_flush(); /* show a line at a time */
18295 ui_breakcheck();
18296 }
18297 if (!got_int)
18298 {
18299 msg_putchar('\n');
18300 msg_puts((char_u *)" endfunction");
18301 }
18302 }
18303 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018304 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018306 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 }
18308
18309 /*
18310 * ":function name(arg1, arg2)" Define function.
18311 */
18312 p = skipwhite(p);
18313 if (*p != '(')
18314 {
18315 if (!eap->skip)
18316 {
18317 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018318 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 }
18320 /* attempt to continue by skipping some text */
18321 if (vim_strchr(p, '(') != NULL)
18322 p = vim_strchr(p, '(');
18323 }
18324 p = skipwhite(p + 1);
18325
18326 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18327 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18328
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018329 if (!eap->skip)
18330 {
18331 /* Check the name of the function. */
18332 if (name != NULL)
18333 arg = name;
18334 else
18335 arg = fudi.fd_newkey;
18336 if (arg != NULL)
18337 {
18338 if (*arg == K_SPECIAL)
18339 j = 3;
18340 else
18341 j = 0;
18342 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18343 : eval_isnamec(arg[j])))
18344 ++j;
18345 if (arg[j] != NUL)
18346 emsg_funcname(_(e_invarg2), arg);
18347 }
18348 }
18349
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 /*
18351 * Isolate the arguments: "arg1, arg2, ...)"
18352 */
18353 while (*p != ')')
18354 {
18355 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18356 {
18357 varargs = TRUE;
18358 p += 3;
18359 mustend = TRUE;
18360 }
18361 else
18362 {
18363 arg = p;
18364 while (ASCII_ISALNUM(*p) || *p == '_')
18365 ++p;
18366 if (arg == p || isdigit(*arg)
18367 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18368 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18369 {
18370 if (!eap->skip)
18371 EMSG2(_("E125: Illegal argument: %s"), arg);
18372 break;
18373 }
18374 if (ga_grow(&newargs, 1) == FAIL)
18375 goto erret;
18376 c = *p;
18377 *p = NUL;
18378 arg = vim_strsave(arg);
18379 if (arg == NULL)
18380 goto erret;
18381 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18382 *p = c;
18383 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 if (*p == ',')
18385 ++p;
18386 else
18387 mustend = TRUE;
18388 }
18389 p = skipwhite(p);
18390 if (mustend && *p != ')')
18391 {
18392 if (!eap->skip)
18393 EMSG2(_(e_invarg2), eap->arg);
18394 break;
18395 }
18396 }
18397 ++p; /* skip the ')' */
18398
Bram Moolenaare9a41262005-01-15 22:18:47 +000018399 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 for (;;)
18401 {
18402 p = skipwhite(p);
18403 if (STRNCMP(p, "range", 5) == 0)
18404 {
18405 flags |= FC_RANGE;
18406 p += 5;
18407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018408 else if (STRNCMP(p, "dict", 4) == 0)
18409 {
18410 flags |= FC_DICT;
18411 p += 4;
18412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 else if (STRNCMP(p, "abort", 5) == 0)
18414 {
18415 flags |= FC_ABORT;
18416 p += 5;
18417 }
18418 else
18419 break;
18420 }
18421
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018422 /* When there is a line break use what follows for the function body.
18423 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18424 if (*p == '\n')
18425 line_arg = p + 1;
18426 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 EMSG(_(e_trailing));
18428
18429 /*
18430 * Read the body of the function, until ":endfunction" is found.
18431 */
18432 if (KeyTyped)
18433 {
18434 /* Check if the function already exists, don't let the user type the
18435 * whole function before telling him it doesn't work! For a script we
18436 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018437 if (!eap->skip && !eap->forceit)
18438 {
18439 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18440 EMSG(_(e_funcdict));
18441 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018442 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018445 if (!eap->skip && did_emsg)
18446 goto erret;
18447
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 msg_putchar('\n'); /* don't overwrite the function name */
18449 cmdline_row = msg_row;
18450 }
18451
18452 indent = 2;
18453 nesting = 0;
18454 for (;;)
18455 {
18456 msg_scroll = TRUE;
18457 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018458 sourcing_lnum_off = sourcing_lnum;
18459
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018460 if (line_arg != NULL)
18461 {
18462 /* Use eap->arg, split up in parts by line breaks. */
18463 theline = line_arg;
18464 p = vim_strchr(theline, '\n');
18465 if (p == NULL)
18466 line_arg += STRLEN(line_arg);
18467 else
18468 {
18469 *p = NUL;
18470 line_arg = p + 1;
18471 }
18472 }
18473 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 theline = getcmdline(':', 0L, indent);
18475 else
18476 theline = eap->getline(':', eap->cookie, indent);
18477 if (KeyTyped)
18478 lines_left = Rows - 1;
18479 if (theline == NULL)
18480 {
18481 EMSG(_("E126: Missing :endfunction"));
18482 goto erret;
18483 }
18484
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018485 /* Detect line continuation: sourcing_lnum increased more than one. */
18486 if (sourcing_lnum > sourcing_lnum_off + 1)
18487 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18488 else
18489 sourcing_lnum_off = 0;
18490
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 if (skip_until != NULL)
18492 {
18493 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18494 * don't check for ":endfunc". */
18495 if (STRCMP(theline, skip_until) == 0)
18496 {
18497 vim_free(skip_until);
18498 skip_until = NULL;
18499 }
18500 }
18501 else
18502 {
18503 /* skip ':' and blanks*/
18504 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18505 ;
18506
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018507 /* Check for "endfunction". */
18508 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018510 if (line_arg == NULL)
18511 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 break;
18513 }
18514
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018515 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 * at "end". */
18517 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18518 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018519 else if (STRNCMP(p, "if", 2) == 0
18520 || STRNCMP(p, "wh", 2) == 0
18521 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 || STRNCMP(p, "try", 3) == 0)
18523 indent += 2;
18524
18525 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018526 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018528 if (*p == '!')
18529 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 p += eval_fname_script(p);
18531 if (ASCII_ISALPHA(*p))
18532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018533 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 if (*skipwhite(p) == '(')
18535 {
18536 ++nesting;
18537 indent += 2;
18538 }
18539 }
18540 }
18541
18542 /* Check for ":append" or ":insert". */
18543 p = skip_range(p, NULL);
18544 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18545 || (p[0] == 'i'
18546 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18547 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18548 skip_until = vim_strsave((char_u *)".");
18549
18550 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18551 arg = skipwhite(skiptowhite(p));
18552 if (arg[0] == '<' && arg[1] =='<'
18553 && ((p[0] == 'p' && p[1] == 'y'
18554 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18555 || (p[0] == 'p' && p[1] == 'e'
18556 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18557 || (p[0] == 't' && p[1] == 'c'
18558 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18559 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18560 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018561 || (p[0] == 'm' && p[1] == 'z'
18562 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 ))
18564 {
18565 /* ":python <<" continues until a dot, like ":append" */
18566 p = skipwhite(arg + 2);
18567 if (*p == NUL)
18568 skip_until = vim_strsave((char_u *)".");
18569 else
18570 skip_until = vim_strsave(p);
18571 }
18572 }
18573
18574 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018575 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018576 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018577 if (line_arg == NULL)
18578 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018580 }
18581
18582 /* Copy the line to newly allocated memory. get_one_sourceline()
18583 * allocates 250 bytes per line, this saves 80% on average. The cost
18584 * is an extra alloc/free. */
18585 p = vim_strsave(theline);
18586 if (p != NULL)
18587 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018588 if (line_arg == NULL)
18589 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018590 theline = p;
18591 }
18592
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018593 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18594
18595 /* Add NULL lines for continuation lines, so that the line count is
18596 * equal to the index in the growarray. */
18597 while (sourcing_lnum_off-- > 0)
18598 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018599
18600 /* Check for end of eap->arg. */
18601 if (line_arg != NULL && *line_arg == NUL)
18602 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 }
18604
18605 /* Don't define the function when skipping commands or when an error was
18606 * detected. */
18607 if (eap->skip || did_emsg)
18608 goto erret;
18609
18610 /*
18611 * If there are no errors, add the function
18612 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018613 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018614 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018615 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018616 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018617 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018618 emsg_funcname("E707: Function name conflicts with variable: %s",
18619 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018620 goto erret;
18621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018622
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018623 fp = find_func(name);
18624 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018626 if (!eap->forceit)
18627 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018628 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018629 goto erret;
18630 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018631 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018632 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018633 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018634 name);
18635 goto erret;
18636 }
18637 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018638 ga_clear_strings(&(fp->uf_args));
18639 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018640 vim_free(name);
18641 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 }
18644 else
18645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018646 char numbuf[20];
18647
18648 fp = NULL;
18649 if (fudi.fd_newkey == NULL && !eap->forceit)
18650 {
18651 EMSG(_(e_funcdict));
18652 goto erret;
18653 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018654 if (fudi.fd_di == NULL)
18655 {
18656 /* Can't add a function to a locked dictionary */
18657 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18658 goto erret;
18659 }
18660 /* Can't change an existing function if it is locked */
18661 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18662 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018663
18664 /* Give the function a sequential number. Can only be used with a
18665 * Funcref! */
18666 vim_free(name);
18667 sprintf(numbuf, "%d", ++func_nr);
18668 name = vim_strsave((char_u *)numbuf);
18669 if (name == NULL)
18670 goto erret;
18671 }
18672
18673 if (fp == NULL)
18674 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018675 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018676 {
18677 int slen, plen;
18678 char_u *scriptname;
18679
18680 /* Check that the autoload name matches the script name. */
18681 j = FAIL;
18682 if (sourcing_name != NULL)
18683 {
18684 scriptname = autoload_name(name);
18685 if (scriptname != NULL)
18686 {
18687 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018688 plen = (int)STRLEN(p);
18689 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018690 if (slen > plen && fnamecmp(p,
18691 sourcing_name + slen - plen) == 0)
18692 j = OK;
18693 vim_free(scriptname);
18694 }
18695 }
18696 if (j == FAIL)
18697 {
18698 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18699 goto erret;
18700 }
18701 }
18702
18703 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 if (fp == NULL)
18705 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018706
18707 if (fudi.fd_dict != NULL)
18708 {
18709 if (fudi.fd_di == NULL)
18710 {
18711 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018712 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018713 if (fudi.fd_di == NULL)
18714 {
18715 vim_free(fp);
18716 goto erret;
18717 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018718 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18719 {
18720 vim_free(fudi.fd_di);
18721 goto erret;
18722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018723 }
18724 else
18725 /* overwrite existing dict entry */
18726 clear_tv(&fudi.fd_di->di_tv);
18727 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018728 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018729 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018730 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018731
18732 /* behave like "dict" was used */
18733 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018734 }
18735
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018737 STRCPY(fp->uf_name, name);
18738 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018740 fp->uf_args = newargs;
18741 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018742#ifdef FEAT_PROFILE
18743 fp->uf_tml_count = NULL;
18744 fp->uf_tml_total = NULL;
18745 fp->uf_tml_self = NULL;
18746 fp->uf_profiling = FALSE;
18747 if (prof_def_func())
18748 func_do_profile(fp);
18749#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018750 fp->uf_varargs = varargs;
18751 fp->uf_flags = flags;
18752 fp->uf_calls = 0;
18753 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018754 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755
18756erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 ga_clear_strings(&newargs);
18758 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018759ret_free:
18760 vim_free(skip_until);
18761 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764}
18765
18766/*
18767 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018768 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018770 * flags:
18771 * TFN_INT: internal function name OK
18772 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 * Advances "pp" to just after the function name (if no error).
18774 */
18775 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018776trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 char_u **pp;
18778 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018779 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018780 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018782 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 char_u *start;
18784 char_u *end;
18785 int lead;
18786 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018788 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018789
18790 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018793
18794 /* Check for hard coded <SNR>: already translated function ID (from a user
18795 * command). */
18796 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18797 && (*pp)[2] == (int)KE_SNR)
18798 {
18799 *pp += 3;
18800 len = get_id_len(pp) + 3;
18801 return vim_strnsave(start, len);
18802 }
18803
18804 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18805 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018807 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018809
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018810 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18811 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018812 if (end == start)
18813 {
18814 if (!skip)
18815 EMSG(_("E129: Function name required"));
18816 goto theend;
18817 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018818 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018819 {
18820 /*
18821 * Report an invalid expression in braces, unless the expression
18822 * evaluation has been cancelled due to an aborting error, an
18823 * interrupt, or an exception.
18824 */
18825 if (!aborting())
18826 {
18827 if (end != NULL)
18828 EMSG2(_(e_invarg2), start);
18829 }
18830 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018831 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018832 goto theend;
18833 }
18834
18835 if (lv.ll_tv != NULL)
18836 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018837 if (fdp != NULL)
18838 {
18839 fdp->fd_dict = lv.ll_dict;
18840 fdp->fd_newkey = lv.ll_newkey;
18841 lv.ll_newkey = NULL;
18842 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018843 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018844 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18845 {
18846 name = vim_strsave(lv.ll_tv->vval.v_string);
18847 *pp = end;
18848 }
18849 else
18850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018851 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18852 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018853 EMSG(_(e_funcref));
18854 else
18855 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018856 name = NULL;
18857 }
18858 goto theend;
18859 }
18860
18861 if (lv.ll_name == NULL)
18862 {
18863 /* Error found, but continue after the function name. */
18864 *pp = end;
18865 goto theend;
18866 }
18867
18868 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018869 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018870 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018871 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18872 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18873 {
18874 /* When there was "s:" already or the name expanded to get a
18875 * leading "s:" then remove it. */
18876 lv.ll_name += 2;
18877 len -= 2;
18878 lead = 2;
18879 }
18880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018881 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018882 {
18883 if (lead == 2) /* skip over "s:" */
18884 lv.ll_name += 2;
18885 len = (int)(end - lv.ll_name);
18886 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018887
18888 /*
18889 * Copy the function name to allocated memory.
18890 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18891 * Accept <SNR>123_name() outside a script.
18892 */
18893 if (skip)
18894 lead = 0; /* do nothing */
18895 else if (lead > 0)
18896 {
18897 lead = 3;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018898 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018899 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018900 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018901 if (current_SID <= 0)
18902 {
18903 EMSG(_(e_usingsid));
18904 goto theend;
18905 }
18906 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18907 lead += (int)STRLEN(sid_buf);
18908 }
18909 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018910 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018911 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018912 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018913 goto theend;
18914 }
18915 name = alloc((unsigned)(len + lead + 1));
18916 if (name != NULL)
18917 {
18918 if (lead > 0)
18919 {
18920 name[0] = K_SPECIAL;
18921 name[1] = KS_EXTRA;
18922 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018923 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018924 STRCPY(name + 3, sid_buf);
18925 }
18926 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18927 name[len + lead] = NUL;
18928 }
18929 *pp = end;
18930
18931theend:
18932 clear_lval(&lv);
18933 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934}
18935
18936/*
18937 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18938 * Return 2 if "p" starts with "s:".
18939 * Return 0 otherwise.
18940 */
18941 static int
18942eval_fname_script(p)
18943 char_u *p;
18944{
18945 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18946 || STRNICMP(p + 1, "SNR>", 4) == 0))
18947 return 5;
18948 if (p[0] == 's' && p[1] == ':')
18949 return 2;
18950 return 0;
18951}
18952
18953/*
18954 * Return TRUE if "p" starts with "<SID>" or "s:".
18955 * Only works if eval_fname_script() returned non-zero for "p"!
18956 */
18957 static int
18958eval_fname_sid(p)
18959 char_u *p;
18960{
18961 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18962}
18963
18964/*
18965 * List the head of the function: "name(arg1, arg2)".
18966 */
18967 static void
18968list_func_head(fp, indent)
18969 ufunc_T *fp;
18970 int indent;
18971{
18972 int j;
18973
18974 msg_start();
18975 if (indent)
18976 MSG_PUTS(" ");
18977 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018978 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 {
18980 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018981 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 }
18983 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018984 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018986 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 {
18988 if (j)
18989 MSG_PUTS(", ");
18990 msg_puts(FUNCARG(fp, j));
18991 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018992 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993 {
18994 if (j)
18995 MSG_PUTS(", ");
18996 MSG_PUTS("...");
18997 }
18998 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018999 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019000 if (p_verbose > 0)
19001 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002}
19003
19004/*
19005 * Find a function by name, return pointer to it in ufuncs.
19006 * Return NULL for unknown function.
19007 */
19008 static ufunc_T *
19009find_func(name)
19010 char_u *name;
19011{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019012 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019014 hi = hash_find(&func_hashtab, name);
19015 if (!HASHITEM_EMPTY(hi))
19016 return HI2UF(hi);
19017 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018}
19019
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019020#if defined(EXITFREE) || defined(PROTO)
19021 void
19022free_all_functions()
19023{
19024 hashitem_T *hi;
19025
19026 /* Need to start all over every time, because func_free() may change the
19027 * hash table. */
19028 while (func_hashtab.ht_used > 0)
19029 for (hi = func_hashtab.ht_array; ; ++hi)
19030 if (!HASHITEM_EMPTY(hi))
19031 {
19032 func_free(HI2UF(hi));
19033 break;
19034 }
19035}
19036#endif
19037
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019038/*
19039 * Return TRUE if a function "name" exists.
19040 */
19041 static int
19042function_exists(name)
19043 char_u *name;
19044{
19045 char_u *p = name;
19046 int n = FALSE;
19047
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019048 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019049 if (p != NULL)
19050 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019051 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019052 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019053 else
19054 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019055 vim_free(p);
19056 }
19057 return n;
19058}
19059
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019060/*
19061 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019062 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019063 */
19064 static int
19065builtin_function(name)
19066 char_u *name;
19067{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019068 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19069 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019070}
19071
Bram Moolenaar05159a02005-02-26 23:04:13 +000019072#if defined(FEAT_PROFILE) || defined(PROTO)
19073/*
19074 * Start profiling function "fp".
19075 */
19076 static void
19077func_do_profile(fp)
19078 ufunc_T *fp;
19079{
19080 fp->uf_tm_count = 0;
19081 profile_zero(&fp->uf_tm_self);
19082 profile_zero(&fp->uf_tm_total);
19083 if (fp->uf_tml_count == NULL)
19084 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19085 (sizeof(int) * fp->uf_lines.ga_len));
19086 if (fp->uf_tml_total == NULL)
19087 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19088 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19089 if (fp->uf_tml_self == NULL)
19090 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19091 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19092 fp->uf_tml_idx = -1;
19093 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19094 || fp->uf_tml_self == NULL)
19095 return; /* out of memory */
19096
19097 fp->uf_profiling = TRUE;
19098}
19099
19100/*
19101 * Dump the profiling results for all functions in file "fd".
19102 */
19103 void
19104func_dump_profile(fd)
19105 FILE *fd;
19106{
19107 hashitem_T *hi;
19108 int todo;
19109 ufunc_T *fp;
19110 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019111 ufunc_T **sorttab;
19112 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019114 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019115 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19116
Bram Moolenaar05159a02005-02-26 23:04:13 +000019117 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19118 {
19119 if (!HASHITEM_EMPTY(hi))
19120 {
19121 --todo;
19122 fp = HI2UF(hi);
19123 if (fp->uf_profiling)
19124 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019125 if (sorttab != NULL)
19126 sorttab[st_len++] = fp;
19127
Bram Moolenaar05159a02005-02-26 23:04:13 +000019128 if (fp->uf_name[0] == K_SPECIAL)
19129 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19130 else
19131 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19132 if (fp->uf_tm_count == 1)
19133 fprintf(fd, "Called 1 time\n");
19134 else
19135 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19136 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19137 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19138 fprintf(fd, "\n");
19139 fprintf(fd, "count total (s) self (s)\n");
19140
19141 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19142 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019143 if (FUNCLINE(fp, i) == NULL)
19144 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019145 prof_func_line(fd, fp->uf_tml_count[i],
19146 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019147 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19148 }
19149 fprintf(fd, "\n");
19150 }
19151 }
19152 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019153
19154 if (sorttab != NULL && st_len > 0)
19155 {
19156 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19157 prof_total_cmp);
19158 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19159 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19160 prof_self_cmp);
19161 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19162 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019163}
Bram Moolenaar73830342005-02-28 22:48:19 +000019164
19165 static void
19166prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19167 FILE *fd;
19168 ufunc_T **sorttab;
19169 int st_len;
19170 char *title;
19171 int prefer_self; /* when equal print only self time */
19172{
19173 int i;
19174 ufunc_T *fp;
19175
19176 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19177 fprintf(fd, "count total (s) self (s) function\n");
19178 for (i = 0; i < 20 && i < st_len; ++i)
19179 {
19180 fp = sorttab[i];
19181 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19182 prefer_self);
19183 if (fp->uf_name[0] == K_SPECIAL)
19184 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19185 else
19186 fprintf(fd, " %s()\n", fp->uf_name);
19187 }
19188 fprintf(fd, "\n");
19189}
19190
19191/*
19192 * Print the count and times for one function or function line.
19193 */
19194 static void
19195prof_func_line(fd, count, total, self, prefer_self)
19196 FILE *fd;
19197 int count;
19198 proftime_T *total;
19199 proftime_T *self;
19200 int prefer_self; /* when equal print only self time */
19201{
19202 if (count > 0)
19203 {
19204 fprintf(fd, "%5d ", count);
19205 if (prefer_self && profile_equal(total, self))
19206 fprintf(fd, " ");
19207 else
19208 fprintf(fd, "%s ", profile_msg(total));
19209 if (!prefer_self && profile_equal(total, self))
19210 fprintf(fd, " ");
19211 else
19212 fprintf(fd, "%s ", profile_msg(self));
19213 }
19214 else
19215 fprintf(fd, " ");
19216}
19217
19218/*
19219 * Compare function for total time sorting.
19220 */
19221 static int
19222#ifdef __BORLANDC__
19223_RTLENTRYF
19224#endif
19225prof_total_cmp(s1, s2)
19226 const void *s1;
19227 const void *s2;
19228{
19229 ufunc_T *p1, *p2;
19230
19231 p1 = *(ufunc_T **)s1;
19232 p2 = *(ufunc_T **)s2;
19233 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19234}
19235
19236/*
19237 * Compare function for self time sorting.
19238 */
19239 static int
19240#ifdef __BORLANDC__
19241_RTLENTRYF
19242#endif
19243prof_self_cmp(s1, s2)
19244 const void *s1;
19245 const void *s2;
19246{
19247 ufunc_T *p1, *p2;
19248
19249 p1 = *(ufunc_T **)s1;
19250 p2 = *(ufunc_T **)s2;
19251 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19252}
19253
Bram Moolenaar05159a02005-02-26 23:04:13 +000019254#endif
19255
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019256/* The names of packages that once were loaded is remembered. */
19257static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19258
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019259/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019260 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019261 * Return TRUE if a package was loaded.
19262 */
19263 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019264script_autoload(name, reload)
19265 char_u *name;
19266 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019267{
19268 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019269 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019270 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019271 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019272
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019273 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019274 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019275 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019276 return FALSE;
19277
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019278 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019279
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019280 /* Find the name in the list of previously loaded package names. Skip
19281 * "autoload/", it's always the same. */
19282 for (i = 0; i < ga_loaded.ga_len; ++i)
19283 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19284 break;
19285 if (!reload && i < ga_loaded.ga_len)
19286 ret = FALSE; /* was loaded already */
19287 else
19288 {
19289 /* Remember the name if it wasn't loaded already. */
19290 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19291 {
19292 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19293 tofree = NULL;
19294 }
19295
19296 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019297 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019298 ret = TRUE;
19299 }
19300
19301 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019302 return ret;
19303}
19304
19305/*
19306 * Return the autoload script name for a function or variable name.
19307 * Returns NULL when out of memory.
19308 */
19309 static char_u *
19310autoload_name(name)
19311 char_u *name;
19312{
19313 char_u *p;
19314 char_u *scriptname;
19315
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019316 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019317 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19318 if (scriptname == NULL)
19319 return FALSE;
19320 STRCPY(scriptname, "autoload/");
19321 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019322 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019323 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019324 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019325 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019326 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019327}
19328
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19330
19331/*
19332 * Function given to ExpandGeneric() to obtain the list of user defined
19333 * function names.
19334 */
19335 char_u *
19336get_user_func_name(xp, idx)
19337 expand_T *xp;
19338 int idx;
19339{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019340 static long_u done;
19341 static hashitem_T *hi;
19342 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343
19344 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019346 done = 0;
19347 hi = func_hashtab.ht_array;
19348 }
19349 if (done < func_hashtab.ht_used)
19350 {
19351 if (done++ > 0)
19352 ++hi;
19353 while (HASHITEM_EMPTY(hi))
19354 ++hi;
19355 fp = HI2UF(hi);
19356
19357 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19358 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359
19360 cat_func_name(IObuff, fp);
19361 if (xp->xp_context != EXPAND_USER_FUNC)
19362 {
19363 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019364 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 STRCAT(IObuff, ")");
19366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 return IObuff;
19368 }
19369 return NULL;
19370}
19371
19372#endif /* FEAT_CMDL_COMPL */
19373
19374/*
19375 * Copy the function name of "fp" to buffer "buf".
19376 * "buf" must be able to hold the function name plus three bytes.
19377 * Takes care of script-local function names.
19378 */
19379 static void
19380cat_func_name(buf, fp)
19381 char_u *buf;
19382 ufunc_T *fp;
19383{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019384 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 {
19386 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019387 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 }
19389 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019390 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391}
19392
19393/*
19394 * ":delfunction {name}"
19395 */
19396 void
19397ex_delfunction(eap)
19398 exarg_T *eap;
19399{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019400 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 char_u *p;
19402 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019403 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404
19405 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019406 name = trans_function_name(&p, eap->skip, 0, &fudi);
19407 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019409 {
19410 if (fudi.fd_dict != NULL && !eap->skip)
19411 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414 if (!ends_excmd(*skipwhite(p)))
19415 {
19416 vim_free(name);
19417 EMSG(_(e_trailing));
19418 return;
19419 }
19420 eap->nextcmd = check_nextcmd(p);
19421 if (eap->nextcmd != NULL)
19422 *p = NUL;
19423
19424 if (!eap->skip)
19425 fp = find_func(name);
19426 vim_free(name);
19427
19428 if (!eap->skip)
19429 {
19430 if (fp == NULL)
19431 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019432 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 return;
19434 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019435 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 {
19437 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19438 return;
19439 }
19440
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019441 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019443 /* Delete the dict item that refers to the function, it will
19444 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019445 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019447 else
19448 func_free(fp);
19449 }
19450}
19451
19452/*
19453 * Free a function and remove it from the list of functions.
19454 */
19455 static void
19456func_free(fp)
19457 ufunc_T *fp;
19458{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019459 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019460
19461 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019462 ga_clear_strings(&(fp->uf_args));
19463 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019464#ifdef FEAT_PROFILE
19465 vim_free(fp->uf_tml_count);
19466 vim_free(fp->uf_tml_total);
19467 vim_free(fp->uf_tml_self);
19468#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019470 /* remove the function from the function hashtable */
19471 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19472 if (HASHITEM_EMPTY(hi))
19473 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019474 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019475 hash_remove(&func_hashtab, hi);
19476
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019477 vim_free(fp);
19478}
19479
19480/*
19481 * Unreference a Function: decrement the reference count and free it when it
19482 * becomes zero. Only for numbered functions.
19483 */
19484 static void
19485func_unref(name)
19486 char_u *name;
19487{
19488 ufunc_T *fp;
19489
19490 if (name != NULL && isdigit(*name))
19491 {
19492 fp = find_func(name);
19493 if (fp == NULL)
19494 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019495 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019496 {
19497 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019498 * when "uf_calls" becomes zero. */
19499 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019500 func_free(fp);
19501 }
19502 }
19503}
19504
19505/*
19506 * Count a reference to a Function.
19507 */
19508 static void
19509func_ref(name)
19510 char_u *name;
19511{
19512 ufunc_T *fp;
19513
19514 if (name != NULL && isdigit(*name))
19515 {
19516 fp = find_func(name);
19517 if (fp == NULL)
19518 EMSG2(_(e_intern2), "func_ref()");
19519 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019520 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 }
19522}
19523
19524/*
19525 * Call a user function.
19526 */
19527 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019528call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 ufunc_T *fp; /* pointer to function */
19530 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019531 typval_T *argvars; /* arguments */
19532 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 linenr_T firstline; /* first line of range */
19534 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019535 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536{
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 char_u *save_sourcing_name;
19538 linenr_T save_sourcing_lnum;
19539 scid_T save_current_SID;
19540 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019541 int save_did_emsg;
19542 static int depth = 0;
19543 dictitem_T *v;
19544 int fixvar_idx = 0; /* index in fixvar[] */
19545 int i;
19546 int ai;
19547 char_u numbuf[NUMBUFLEN];
19548 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019549#ifdef FEAT_PROFILE
19550 proftime_T wait_start;
19551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552
19553 /* If depth of calling is getting too high, don't execute the function */
19554 if (depth >= p_mfd)
19555 {
19556 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019557 rettv->v_type = VAR_NUMBER;
19558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559 return;
19560 }
19561 ++depth;
19562
19563 line_breakcheck(); /* check for CTRL-C hit */
19564
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019565 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019566 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019568 fc.rettv = rettv;
19569 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 fc.linenr = 0;
19571 fc.returned = FALSE;
19572 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019574 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 fc.dbg_tick = debug_tick;
19576
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 /*
19578 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19579 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19580 * each argument variable and saves a lot of time.
19581 */
19582 /*
19583 * Init l: variables.
19584 */
19585 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019586 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019587 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019588 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19589 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019590 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019591 name = v->di_key;
19592 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19594 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19595 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019596 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019597 v->di_tv.vval.v_dict = selfdict;
19598 ++selfdict->dv_refcount;
19599 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019600
Bram Moolenaar33570922005-01-25 22:26:29 +000019601 /*
19602 * Init a: variables.
19603 * Set a:0 to "argcount".
19604 * Set a:000 to a list with room for the "..." arguments.
19605 */
19606 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19607 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019608 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019609 v = &fc.fixvar[fixvar_idx++].var;
19610 STRCPY(v->di_key, "000");
19611 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19612 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19613 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019614 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 v->di_tv.vval.v_list = &fc.l_varlist;
19616 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19617 fc.l_varlist.lv_refcount = 99999;
19618
19619 /*
19620 * Set a:firstline to "firstline" and a:lastline to "lastline".
19621 * Set a:name to named arguments.
19622 * Set a:N to the "..." arguments.
19623 */
19624 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19625 (varnumber_T)firstline);
19626 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19627 (varnumber_T)lastline);
19628 for (i = 0; i < argcount; ++i)
19629 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019630 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019631 if (ai < 0)
19632 /* named argument a:name */
19633 name = FUNCARG(fp, i);
19634 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019635 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 /* "..." argument a:1, a:2, etc. */
19637 sprintf((char *)numbuf, "%d", ai + 1);
19638 name = numbuf;
19639 }
19640 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19641 {
19642 v = &fc.fixvar[fixvar_idx++].var;
19643 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19644 }
19645 else
19646 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019647 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19648 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019649 if (v == NULL)
19650 break;
19651 v->di_flags = DI_FLAGS_RO;
19652 }
19653 STRCPY(v->di_key, name);
19654 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19655
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019656 /* Note: the values are copied directly to avoid alloc/free.
19657 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019658 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019659 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019660
19661 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19662 {
19663 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19664 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019665 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019666 }
19667 }
19668
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 /* Don't redraw while executing the function. */
19670 ++RedrawingDisabled;
19671 save_sourcing_name = sourcing_name;
19672 save_sourcing_lnum = sourcing_lnum;
19673 sourcing_lnum = 1;
19674 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019675 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 if (sourcing_name != NULL)
19677 {
19678 if (save_sourcing_name != NULL
19679 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19680 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19681 else
19682 STRCPY(sourcing_name, "function ");
19683 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19684
19685 if (p_verbose >= 12)
19686 {
19687 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019688 verbose_enter_scroll();
19689
Bram Moolenaar555b2802005-05-19 21:08:39 +000019690 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 if (p_verbose >= 14)
19692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019694 char_u numbuf[NUMBUFLEN];
19695 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696
19697 msg_puts((char_u *)"(");
19698 for (i = 0; i < argcount; ++i)
19699 {
19700 if (i > 0)
19701 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019702 if (argvars[i].v_type == VAR_NUMBER)
19703 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704 else
19705 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019706 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019707 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019709 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 }
19711 }
19712 msg_puts((char_u *)")");
19713 }
19714 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019715
19716 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 --no_wait_return;
19718 }
19719 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019720#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019721 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019722 {
19723 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19724 func_do_profile(fp);
19725 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019726 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019727 {
19728 ++fp->uf_tm_count;
19729 profile_start(&fp->uf_tm_start);
19730 profile_zero(&fp->uf_tm_children);
19731 }
19732 script_prof_save(&wait_start);
19733 }
19734#endif
19735
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019737 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 save_did_emsg = did_emsg;
19739 did_emsg = FALSE;
19740
19741 /* call do_cmdline() to execute the lines */
19742 do_cmdline(NULL, get_func_line, (void *)&fc,
19743 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19744
19745 --RedrawingDisabled;
19746
19747 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019748 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019750 clear_tv(rettv);
19751 rettv->v_type = VAR_NUMBER;
19752 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 }
19754
Bram Moolenaar05159a02005-02-26 23:04:13 +000019755#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019756 if (do_profiling == PROF_YES && (fp->uf_profiling
19757 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019758 {
19759 profile_end(&fp->uf_tm_start);
19760 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19761 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019762 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019763 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019764 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019765 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19766 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019767 }
19768 }
19769#endif
19770
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 /* when being verbose, mention the return value */
19772 if (p_verbose >= 12)
19773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019775 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019778 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019780 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19781 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019782 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019784 char_u buf[MSG_BUF_LEN];
19785 char_u numbuf[NUMBUFLEN];
19786 char_u *tofree;
19787
Bram Moolenaar555b2802005-05-19 21:08:39 +000019788 /* The value may be very long. Skip the middle part, so that we
19789 * have some idea how it starts and ends. smsg() would always
19790 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019791 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019792 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019793 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019794 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 }
19796 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019797
19798 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 --no_wait_return;
19800 }
19801
19802 vim_free(sourcing_name);
19803 sourcing_name = save_sourcing_name;
19804 sourcing_lnum = save_sourcing_lnum;
19805 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019806#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019807 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019808 script_prof_restore(&wait_start);
19809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810
19811 if (p_verbose >= 12 && sourcing_name != NULL)
19812 {
19813 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019814 verbose_enter_scroll();
19815
Bram Moolenaar555b2802005-05-19 21:08:39 +000019816 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019818
19819 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 --no_wait_return;
19821 }
19822
19823 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019824 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825
Bram Moolenaar33570922005-01-25 22:26:29 +000019826 /* The a: variables typevals were not alloced, only free the allocated
19827 * variables. */
19828 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19829
19830 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 --depth;
19832}
19833
19834/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019835 * Add a number variable "name" to dict "dp" with value "nr".
19836 */
19837 static void
19838add_nr_var(dp, v, name, nr)
19839 dict_T *dp;
19840 dictitem_T *v;
19841 char *name;
19842 varnumber_T nr;
19843{
19844 STRCPY(v->di_key, name);
19845 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19846 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19847 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019848 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 v->di_tv.vval.v_number = nr;
19850}
19851
19852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 * ":return [expr]"
19854 */
19855 void
19856ex_return(eap)
19857 exarg_T *eap;
19858{
19859 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 int returning = FALSE;
19862
19863 if (current_funccal == NULL)
19864 {
19865 EMSG(_("E133: :return not inside a function"));
19866 return;
19867 }
19868
19869 if (eap->skip)
19870 ++emsg_skip;
19871
19872 eap->nextcmd = NULL;
19873 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019874 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 {
19876 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019877 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019879 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 }
19881 /* It's safer to return also on error. */
19882 else if (!eap->skip)
19883 {
19884 /*
19885 * Return unless the expression evaluation has been cancelled due to an
19886 * aborting error, an interrupt, or an exception.
19887 */
19888 if (!aborting())
19889 returning = do_return(eap, FALSE, TRUE, NULL);
19890 }
19891
19892 /* When skipping or the return gets pending, advance to the next command
19893 * in this line (!returning). Otherwise, ignore the rest of the line.
19894 * Following lines will be ignored by get_func_line(). */
19895 if (returning)
19896 eap->nextcmd = NULL;
19897 else if (eap->nextcmd == NULL) /* no argument */
19898 eap->nextcmd = check_nextcmd(arg);
19899
19900 if (eap->skip)
19901 --emsg_skip;
19902}
19903
19904/*
19905 * Return from a function. Possibly makes the return pending. Also called
19906 * for a pending return at the ":endtry" or after returning from an extra
19907 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019908 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019909 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 * FALSE when the return gets pending.
19911 */
19912 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019913do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 exarg_T *eap;
19915 int reanimate;
19916 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019917 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918{
19919 int idx;
19920 struct condstack *cstack = eap->cstack;
19921
19922 if (reanimate)
19923 /* Undo the return. */
19924 current_funccal->returned = FALSE;
19925
19926 /*
19927 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19928 * not in its finally clause (which then is to be executed next) is found.
19929 * In this case, make the ":return" pending for execution at the ":endtry".
19930 * Otherwise, return normally.
19931 */
19932 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19933 if (idx >= 0)
19934 {
19935 cstack->cs_pending[idx] = CSTP_RETURN;
19936
19937 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019938 /* A pending return again gets pending. "rettv" points to an
19939 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 else
19943 {
19944 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019945 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019947 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 {
19951 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019952 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019953 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 else
19955 EMSG(_(e_outofmem));
19956 }
19957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019958 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959
19960 if (reanimate)
19961 {
19962 /* The pending return value could be overwritten by a ":return"
19963 * without argument in a finally clause; reset the default
19964 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019965 current_funccal->rettv->v_type = VAR_NUMBER;
19966 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 }
19968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 }
19971 else
19972 {
19973 current_funccal->returned = TRUE;
19974
19975 /* If the return is carried out now, store the return value. For
19976 * a return immediately after reanimation, the value is already
19977 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019980 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019981 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019983 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 }
19985 }
19986
19987 return idx < 0;
19988}
19989
19990/*
19991 * Free the variable with a pending return value.
19992 */
19993 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994discard_pending_return(rettv)
19995 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998}
19999
20000/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020001 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 * is an allocated string. Used by report_pending() for verbose messages.
20003 */
20004 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005get_return_cmd(rettv)
20006 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020008 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020009 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020010 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020012 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020013 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020014 if (s == NULL)
20015 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016
20017 STRCPY(IObuff, ":return ");
20018 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20019 if (STRLEN(s) + 8 >= IOSIZE)
20020 STRCPY(IObuff + IOSIZE - 4, "...");
20021 vim_free(tofree);
20022 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023}
20024
20025/*
20026 * Get next function line.
20027 * Called by do_cmdline() to get the next line.
20028 * Returns allocated string, or NULL for end of function.
20029 */
20030/* ARGSUSED */
20031 char_u *
20032get_func_line(c, cookie, indent)
20033 int c; /* not used */
20034 void *cookie;
20035 int indent; /* not used */
20036{
Bram Moolenaar33570922005-01-25 22:26:29 +000020037 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020038 ufunc_T *fp = fcp->func;
20039 char_u *retval;
20040 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041
20042 /* If breakpoints have been added/deleted need to check for it. */
20043 if (fcp->dbg_tick != debug_tick)
20044 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020045 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 sourcing_lnum);
20047 fcp->dbg_tick = debug_tick;
20048 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020049#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020050 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020051 func_line_end(cookie);
20052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053
Bram Moolenaar05159a02005-02-26 23:04:13 +000020054 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020055 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20056 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 retval = NULL;
20058 else
20059 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020060 /* Skip NULL lines (continuation lines). */
20061 while (fcp->linenr < gap->ga_len
20062 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20063 ++fcp->linenr;
20064 if (fcp->linenr >= gap->ga_len)
20065 retval = NULL;
20066 else
20067 {
20068 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20069 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020070#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020071 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020072 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020073#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 }
20076
20077 /* Did we encounter a breakpoint? */
20078 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20079 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020080 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020082 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 sourcing_lnum);
20084 fcp->dbg_tick = debug_tick;
20085 }
20086
20087 return retval;
20088}
20089
Bram Moolenaar05159a02005-02-26 23:04:13 +000020090#if defined(FEAT_PROFILE) || defined(PROTO)
20091/*
20092 * Called when starting to read a function line.
20093 * "sourcing_lnum" must be correct!
20094 * When skipping lines it may not actually be executed, but we won't find out
20095 * until later and we need to store the time now.
20096 */
20097 void
20098func_line_start(cookie)
20099 void *cookie;
20100{
20101 funccall_T *fcp = (funccall_T *)cookie;
20102 ufunc_T *fp = fcp->func;
20103
20104 if (fp->uf_profiling && sourcing_lnum >= 1
20105 && sourcing_lnum <= fp->uf_lines.ga_len)
20106 {
20107 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020108 /* Skip continuation lines. */
20109 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20110 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020111 fp->uf_tml_execed = FALSE;
20112 profile_start(&fp->uf_tml_start);
20113 profile_zero(&fp->uf_tml_children);
20114 profile_get_wait(&fp->uf_tml_wait);
20115 }
20116}
20117
20118/*
20119 * Called when actually executing a function line.
20120 */
20121 void
20122func_line_exec(cookie)
20123 void *cookie;
20124{
20125 funccall_T *fcp = (funccall_T *)cookie;
20126 ufunc_T *fp = fcp->func;
20127
20128 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20129 fp->uf_tml_execed = TRUE;
20130}
20131
20132/*
20133 * Called when done with a function line.
20134 */
20135 void
20136func_line_end(cookie)
20137 void *cookie;
20138{
20139 funccall_T *fcp = (funccall_T *)cookie;
20140 ufunc_T *fp = fcp->func;
20141
20142 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20143 {
20144 if (fp->uf_tml_execed)
20145 {
20146 ++fp->uf_tml_count[fp->uf_tml_idx];
20147 profile_end(&fp->uf_tml_start);
20148 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020149 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020150 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20151 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020152 }
20153 fp->uf_tml_idx = -1;
20154 }
20155}
20156#endif
20157
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158/*
20159 * Return TRUE if the currently active function should be ended, because a
20160 * return was encountered or an error occured. Used inside a ":while".
20161 */
20162 int
20163func_has_ended(cookie)
20164 void *cookie;
20165{
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167
20168 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20169 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020170 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 || fcp->returned);
20172}
20173
20174/*
20175 * return TRUE if cookie indicates a function which "abort"s on errors.
20176 */
20177 int
20178func_has_abort(cookie)
20179 void *cookie;
20180{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020181 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182}
20183
20184#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20185typedef enum
20186{
20187 VAR_FLAVOUR_DEFAULT,
20188 VAR_FLAVOUR_SESSION,
20189 VAR_FLAVOUR_VIMINFO
20190} var_flavour_T;
20191
20192static var_flavour_T var_flavour __ARGS((char_u *varname));
20193
20194 static var_flavour_T
20195var_flavour(varname)
20196 char_u *varname;
20197{
20198 char_u *p = varname;
20199
20200 if (ASCII_ISUPPER(*p))
20201 {
20202 while (*(++p))
20203 if (ASCII_ISLOWER(*p))
20204 return VAR_FLAVOUR_SESSION;
20205 return VAR_FLAVOUR_VIMINFO;
20206 }
20207 else
20208 return VAR_FLAVOUR_DEFAULT;
20209}
20210#endif
20211
20212#if defined(FEAT_VIMINFO) || defined(PROTO)
20213/*
20214 * Restore global vars that start with a capital from the viminfo file
20215 */
20216 int
20217read_viminfo_varlist(virp, writing)
20218 vir_T *virp;
20219 int writing;
20220{
20221 char_u *tab;
20222 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224
20225 if (!writing && (find_viminfo_parameter('!') != NULL))
20226 {
20227 tab = vim_strchr(virp->vir_line + 1, '\t');
20228 if (tab != NULL)
20229 {
20230 *tab++ = '\0'; /* isolate the variable name */
20231 if (*tab == 'S') /* string var */
20232 is_string = TRUE;
20233
20234 tab = vim_strchr(tab, '\t');
20235 if (tab != NULL)
20236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 if (is_string)
20238 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020239 tv.v_type = VAR_STRING;
20240 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 }
20243 else
20244 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020245 tv.v_type = VAR_NUMBER;
20246 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020248 set_var(virp->vir_line + 1, &tv, FALSE);
20249 if (is_string)
20250 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 }
20252 }
20253 }
20254
20255 return viminfo_readline(virp);
20256}
20257
20258/*
20259 * Write global vars that start with a capital to the viminfo file
20260 */
20261 void
20262write_viminfo_varlist(fp)
20263 FILE *fp;
20264{
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 hashitem_T *hi;
20266 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020267 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020268 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020269 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020270 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020271 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272
20273 if (find_viminfo_parameter('!') == NULL)
20274 return;
20275
20276 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020277
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020278 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020279 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020281 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020283 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020284 this_var = HI2DI(hi);
20285 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020288 {
20289 case VAR_STRING: s = "STR"; break;
20290 case VAR_NUMBER: s = "NUM"; break;
20291 default: continue;
20292 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020293 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020294 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020295 if (p != NULL)
20296 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020297 vim_free(tofree);
20298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 }
20300 }
20301}
20302#endif
20303
20304#if defined(FEAT_SESSION) || defined(PROTO)
20305 int
20306store_session_globals(fd)
20307 FILE *fd;
20308{
Bram Moolenaar33570922005-01-25 22:26:29 +000020309 hashitem_T *hi;
20310 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020311 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 char_u *p, *t;
20313
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020314 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020317 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020319 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020320 this_var = HI2DI(hi);
20321 if ((this_var->di_tv.v_type == VAR_NUMBER
20322 || this_var->di_tv.v_type == VAR_STRING)
20323 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020324 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020325 /* Escape special characters with a backslash. Turn a LF and
20326 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020327 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020328 (char_u *)"\\\"\n\r");
20329 if (p == NULL) /* out of memory */
20330 break;
20331 for (t = p; *t != NUL; ++t)
20332 if (*t == '\n')
20333 *t = 'n';
20334 else if (*t == '\r')
20335 *t = 'r';
20336 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020337 this_var->di_key,
20338 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20339 : ' ',
20340 p,
20341 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20342 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020343 || put_eol(fd) == FAIL)
20344 {
20345 vim_free(p);
20346 return FAIL;
20347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 vim_free(p);
20349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 }
20351 }
20352 return OK;
20353}
20354#endif
20355
Bram Moolenaar661b1822005-07-28 22:36:45 +000020356/*
20357 * Display script name where an item was last set.
20358 * Should only be invoked when 'verbose' is non-zero.
20359 */
20360 void
20361last_set_msg(scriptID)
20362 scid_T scriptID;
20363{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020364 char_u *p;
20365
Bram Moolenaar661b1822005-07-28 22:36:45 +000020366 if (scriptID != 0)
20367 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020368 p = home_replace_save(NULL, get_scriptname(scriptID));
20369 if (p != NULL)
20370 {
20371 verbose_enter();
20372 MSG_PUTS(_("\n\tLast set from "));
20373 MSG_PUTS(p);
20374 vim_free(p);
20375 verbose_leave();
20376 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020377 }
20378}
20379
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380#endif /* FEAT_EVAL */
20381
20382#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20383
20384
20385#ifdef WIN3264
20386/*
20387 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20388 */
20389static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20390static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20391static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20392
20393/*
20394 * Get the short pathname of a file.
20395 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20396 */
20397 static int
20398get_short_pathname(fnamep, bufp, fnamelen)
20399 char_u **fnamep;
20400 char_u **bufp;
20401 int *fnamelen;
20402{
20403 int l,len;
20404 char_u *newbuf;
20405
20406 len = *fnamelen;
20407
20408 l = GetShortPathName(*fnamep, *fnamep, len);
20409 if (l > len - 1)
20410 {
20411 /* If that doesn't work (not enough space), then save the string
20412 * and try again with a new buffer big enough
20413 */
20414 newbuf = vim_strnsave(*fnamep, l);
20415 if (newbuf == NULL)
20416 return 0;
20417
20418 vim_free(*bufp);
20419 *fnamep = *bufp = newbuf;
20420
20421 l = GetShortPathName(*fnamep,*fnamep,l+1);
20422
20423 /* Really should always succeed, as the buffer is big enough */
20424 }
20425
20426 *fnamelen = l;
20427 return 1;
20428}
20429
20430/*
20431 * Create a short path name. Returns the length of the buffer it needs.
20432 * Doesn't copy over the end of the buffer passed in.
20433 */
20434 static int
20435shortpath_for_invalid_fname(fname, bufp, fnamelen)
20436 char_u **fname;
20437 char_u **bufp;
20438 int *fnamelen;
20439{
20440 char_u *s, *p, *pbuf2, *pbuf3;
20441 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020442 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443
20444 /* Make a copy */
20445 len2 = *fnamelen;
20446 pbuf2 = vim_strnsave(*fname, len2);
20447 pbuf3 = NULL;
20448
20449 s = pbuf2 + len2 - 1; /* Find the end */
20450 slen = 1;
20451 plen = len2;
20452
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020453 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 {
20455 --s;
20456 ++slen;
20457 --plen;
20458 }
20459
20460 do
20461 {
20462 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020463 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 {
20465 --s;
20466 ++slen;
20467 --plen;
20468 }
20469 if (s <= pbuf2)
20470 break;
20471
20472 /* Remeber the character that is about to be blatted */
20473 ch = *s;
20474 *s = 0; /* get_short_pathname requires a null-terminated string */
20475
20476 /* Try it in situ */
20477 p = pbuf2;
20478 if (!get_short_pathname(&p, &pbuf3, &plen))
20479 {
20480 vim_free(pbuf2);
20481 return -1;
20482 }
20483 *s = ch; /* Preserve the string */
20484 } while (plen == 0);
20485
20486 if (plen > 0)
20487 {
20488 /* Remeber the length of the new string. */
20489 *fnamelen = len = plen + slen;
20490 vim_free(*bufp);
20491 if (len > len2)
20492 {
20493 /* If there's not enough space in the currently allocated string,
20494 * then copy it to a buffer big enough.
20495 */
20496 *fname= *bufp = vim_strnsave(p, len);
20497 if (*fname == NULL)
20498 return -1;
20499 }
20500 else
20501 {
20502 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20503 *fname = *bufp = pbuf2;
20504 if (p != pbuf2)
20505 strncpy(*fname, p, plen);
20506 pbuf2 = NULL;
20507 }
20508 /* Concat the next bit */
20509 strncpy(*fname + plen, s, slen);
20510 (*fname)[len] = '\0';
20511 }
20512 vim_free(pbuf3);
20513 vim_free(pbuf2);
20514 return 0;
20515}
20516
20517/*
20518 * Get a pathname for a partial path.
20519 */
20520 static int
20521shortpath_for_partial(fnamep, bufp, fnamelen)
20522 char_u **fnamep;
20523 char_u **bufp;
20524 int *fnamelen;
20525{
20526 int sepcount, len, tflen;
20527 char_u *p;
20528 char_u *pbuf, *tfname;
20529 int hasTilde;
20530
20531 /* Count up the path seperators from the RHS.. so we know which part
20532 * of the path to return.
20533 */
20534 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020535 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 if (vim_ispathsep(*p))
20537 ++sepcount;
20538
20539 /* Need full path first (use expand_env() to remove a "~/") */
20540 hasTilde = (**fnamep == '~');
20541 if (hasTilde)
20542 pbuf = tfname = expand_env_save(*fnamep);
20543 else
20544 pbuf = tfname = FullName_save(*fnamep, FALSE);
20545
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020546 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547
20548 if (!get_short_pathname(&tfname, &pbuf, &len))
20549 return -1;
20550
20551 if (len == 0)
20552 {
20553 /* Don't have a valid filename, so shorten the rest of the
20554 * path if we can. This CAN give us invalid 8.3 filenames, but
20555 * there's not a lot of point in guessing what it might be.
20556 */
20557 len = tflen;
20558 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20559 return -1;
20560 }
20561
20562 /* Count the paths backward to find the beginning of the desired string. */
20563 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020564 {
20565#ifdef FEAT_MBYTE
20566 if (has_mbyte)
20567 p -= mb_head_off(tfname, p);
20568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 if (vim_ispathsep(*p))
20570 {
20571 if (sepcount == 0 || (hasTilde && sepcount == 1))
20572 break;
20573 else
20574 sepcount --;
20575 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 if (hasTilde)
20578 {
20579 --p;
20580 if (p >= tfname)
20581 *p = '~';
20582 else
20583 return -1;
20584 }
20585 else
20586 ++p;
20587
20588 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20589 vim_free(*bufp);
20590 *fnamelen = (int)STRLEN(p);
20591 *bufp = pbuf;
20592 *fnamep = p;
20593
20594 return 0;
20595}
20596#endif /* WIN3264 */
20597
20598/*
20599 * Adjust a filename, according to a string of modifiers.
20600 * *fnamep must be NUL terminated when called. When returning, the length is
20601 * determined by *fnamelen.
20602 * Returns valid flags.
20603 * When there is an error, *fnamep is set to NULL.
20604 */
20605 int
20606modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20607 char_u *src; /* string with modifiers */
20608 int *usedlen; /* characters after src that are used */
20609 char_u **fnamep; /* file name so far */
20610 char_u **bufp; /* buffer for allocated file name or NULL */
20611 int *fnamelen; /* length of fnamep */
20612{
20613 int valid = 0;
20614 char_u *tail;
20615 char_u *s, *p, *pbuf;
20616 char_u dirname[MAXPATHL];
20617 int c;
20618 int has_fullname = 0;
20619#ifdef WIN3264
20620 int has_shortname = 0;
20621#endif
20622
20623repeat:
20624 /* ":p" - full path/file_name */
20625 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20626 {
20627 has_fullname = 1;
20628
20629 valid |= VALID_PATH;
20630 *usedlen += 2;
20631
20632 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20633 if ((*fnamep)[0] == '~'
20634#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20635 && ((*fnamep)[1] == '/'
20636# ifdef BACKSLASH_IN_FILENAME
20637 || (*fnamep)[1] == '\\'
20638# endif
20639 || (*fnamep)[1] == NUL)
20640
20641#endif
20642 )
20643 {
20644 *fnamep = expand_env_save(*fnamep);
20645 vim_free(*bufp); /* free any allocated file name */
20646 *bufp = *fnamep;
20647 if (*fnamep == NULL)
20648 return -1;
20649 }
20650
20651 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020652 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 {
20654 if (vim_ispathsep(*p)
20655 && p[1] == '.'
20656 && (p[2] == NUL
20657 || vim_ispathsep(p[2])
20658 || (p[2] == '.'
20659 && (p[3] == NUL || vim_ispathsep(p[3])))))
20660 break;
20661 }
20662
20663 /* FullName_save() is slow, don't use it when not needed. */
20664 if (*p != NUL || !vim_isAbsName(*fnamep))
20665 {
20666 *fnamep = FullName_save(*fnamep, *p != NUL);
20667 vim_free(*bufp); /* free any allocated file name */
20668 *bufp = *fnamep;
20669 if (*fnamep == NULL)
20670 return -1;
20671 }
20672
20673 /* Append a path separator to a directory. */
20674 if (mch_isdir(*fnamep))
20675 {
20676 /* Make room for one or two extra characters. */
20677 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20678 vim_free(*bufp); /* free any allocated file name */
20679 *bufp = *fnamep;
20680 if (*fnamep == NULL)
20681 return -1;
20682 add_pathsep(*fnamep);
20683 }
20684 }
20685
20686 /* ":." - path relative to the current directory */
20687 /* ":~" - path relative to the home directory */
20688 /* ":8" - shortname path - postponed till after */
20689 while (src[*usedlen] == ':'
20690 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20691 {
20692 *usedlen += 2;
20693 if (c == '8')
20694 {
20695#ifdef WIN3264
20696 has_shortname = 1; /* Postpone this. */
20697#endif
20698 continue;
20699 }
20700 pbuf = NULL;
20701 /* Need full path first (use expand_env() to remove a "~/") */
20702 if (!has_fullname)
20703 {
20704 if (c == '.' && **fnamep == '~')
20705 p = pbuf = expand_env_save(*fnamep);
20706 else
20707 p = pbuf = FullName_save(*fnamep, FALSE);
20708 }
20709 else
20710 p = *fnamep;
20711
20712 has_fullname = 0;
20713
20714 if (p != NULL)
20715 {
20716 if (c == '.')
20717 {
20718 mch_dirname(dirname, MAXPATHL);
20719 s = shorten_fname(p, dirname);
20720 if (s != NULL)
20721 {
20722 *fnamep = s;
20723 if (pbuf != NULL)
20724 {
20725 vim_free(*bufp); /* free any allocated file name */
20726 *bufp = pbuf;
20727 pbuf = NULL;
20728 }
20729 }
20730 }
20731 else
20732 {
20733 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20734 /* Only replace it when it starts with '~' */
20735 if (*dirname == '~')
20736 {
20737 s = vim_strsave(dirname);
20738 if (s != NULL)
20739 {
20740 *fnamep = s;
20741 vim_free(*bufp);
20742 *bufp = s;
20743 }
20744 }
20745 }
20746 vim_free(pbuf);
20747 }
20748 }
20749
20750 tail = gettail(*fnamep);
20751 *fnamelen = (int)STRLEN(*fnamep);
20752
20753 /* ":h" - head, remove "/file_name", can be repeated */
20754 /* Don't remove the first "/" or "c:\" */
20755 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20756 {
20757 valid |= VALID_HEAD;
20758 *usedlen += 2;
20759 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020760 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 --tail;
20762 *fnamelen = (int)(tail - *fnamep);
20763#ifdef VMS
20764 if (*fnamelen > 0)
20765 *fnamelen += 1; /* the path separator is part of the path */
20766#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020767 while (tail > s && !after_pathsep(s, tail))
20768 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 }
20770
20771 /* ":8" - shortname */
20772 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20773 {
20774 *usedlen += 2;
20775#ifdef WIN3264
20776 has_shortname = 1;
20777#endif
20778 }
20779
20780#ifdef WIN3264
20781 /* Check shortname after we have done 'heads' and before we do 'tails'
20782 */
20783 if (has_shortname)
20784 {
20785 pbuf = NULL;
20786 /* Copy the string if it is shortened by :h */
20787 if (*fnamelen < (int)STRLEN(*fnamep))
20788 {
20789 p = vim_strnsave(*fnamep, *fnamelen);
20790 if (p == 0)
20791 return -1;
20792 vim_free(*bufp);
20793 *bufp = *fnamep = p;
20794 }
20795
20796 /* Split into two implementations - makes it easier. First is where
20797 * there isn't a full name already, second is where there is.
20798 */
20799 if (!has_fullname && !vim_isAbsName(*fnamep))
20800 {
20801 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20802 return -1;
20803 }
20804 else
20805 {
20806 int l;
20807
20808 /* Simple case, already have the full-name
20809 * Nearly always shorter, so try first time. */
20810 l = *fnamelen;
20811 if (!get_short_pathname(fnamep, bufp, &l))
20812 return -1;
20813
20814 if (l == 0)
20815 {
20816 /* Couldn't find the filename.. search the paths.
20817 */
20818 l = *fnamelen;
20819 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20820 return -1;
20821 }
20822 *fnamelen = l;
20823 }
20824 }
20825#endif /* WIN3264 */
20826
20827 /* ":t" - tail, just the basename */
20828 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20829 {
20830 *usedlen += 2;
20831 *fnamelen -= (int)(tail - *fnamep);
20832 *fnamep = tail;
20833 }
20834
20835 /* ":e" - extension, can be repeated */
20836 /* ":r" - root, without extension, can be repeated */
20837 while (src[*usedlen] == ':'
20838 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20839 {
20840 /* find a '.' in the tail:
20841 * - for second :e: before the current fname
20842 * - otherwise: The last '.'
20843 */
20844 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20845 s = *fnamep - 2;
20846 else
20847 s = *fnamep + *fnamelen - 1;
20848 for ( ; s > tail; --s)
20849 if (s[0] == '.')
20850 break;
20851 if (src[*usedlen + 1] == 'e') /* :e */
20852 {
20853 if (s > tail)
20854 {
20855 *fnamelen += (int)(*fnamep - (s + 1));
20856 *fnamep = s + 1;
20857#ifdef VMS
20858 /* cut version from the extension */
20859 s = *fnamep + *fnamelen - 1;
20860 for ( ; s > *fnamep; --s)
20861 if (s[0] == ';')
20862 break;
20863 if (s > *fnamep)
20864 *fnamelen = s - *fnamep;
20865#endif
20866 }
20867 else if (*fnamep <= tail)
20868 *fnamelen = 0;
20869 }
20870 else /* :r */
20871 {
20872 if (s > tail) /* remove one extension */
20873 *fnamelen = (int)(s - *fnamep);
20874 }
20875 *usedlen += 2;
20876 }
20877
20878 /* ":s?pat?foo?" - substitute */
20879 /* ":gs?pat?foo?" - global substitute */
20880 if (src[*usedlen] == ':'
20881 && (src[*usedlen + 1] == 's'
20882 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20883 {
20884 char_u *str;
20885 char_u *pat;
20886 char_u *sub;
20887 int sep;
20888 char_u *flags;
20889 int didit = FALSE;
20890
20891 flags = (char_u *)"";
20892 s = src + *usedlen + 2;
20893 if (src[*usedlen + 1] == 'g')
20894 {
20895 flags = (char_u *)"g";
20896 ++s;
20897 }
20898
20899 sep = *s++;
20900 if (sep)
20901 {
20902 /* find end of pattern */
20903 p = vim_strchr(s, sep);
20904 if (p != NULL)
20905 {
20906 pat = vim_strnsave(s, (int)(p - s));
20907 if (pat != NULL)
20908 {
20909 s = p + 1;
20910 /* find end of substitution */
20911 p = vim_strchr(s, sep);
20912 if (p != NULL)
20913 {
20914 sub = vim_strnsave(s, (int)(p - s));
20915 str = vim_strnsave(*fnamep, *fnamelen);
20916 if (sub != NULL && str != NULL)
20917 {
20918 *usedlen = (int)(p + 1 - src);
20919 s = do_string_sub(str, pat, sub, flags);
20920 if (s != NULL)
20921 {
20922 *fnamep = s;
20923 *fnamelen = (int)STRLEN(s);
20924 vim_free(*bufp);
20925 *bufp = s;
20926 didit = TRUE;
20927 }
20928 }
20929 vim_free(sub);
20930 vim_free(str);
20931 }
20932 vim_free(pat);
20933 }
20934 }
20935 /* after using ":s", repeat all the modifiers */
20936 if (didit)
20937 goto repeat;
20938 }
20939 }
20940
20941 return valid;
20942}
20943
20944/*
20945 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20946 * "flags" can be "g" to do a global substitute.
20947 * Returns an allocated string, NULL for error.
20948 */
20949 char_u *
20950do_string_sub(str, pat, sub, flags)
20951 char_u *str;
20952 char_u *pat;
20953 char_u *sub;
20954 char_u *flags;
20955{
20956 int sublen;
20957 regmatch_T regmatch;
20958 int i;
20959 int do_all;
20960 char_u *tail;
20961 garray_T ga;
20962 char_u *ret;
20963 char_u *save_cpo;
20964
20965 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20966 save_cpo = p_cpo;
20967 p_cpo = (char_u *)"";
20968
20969 ga_init2(&ga, 1, 200);
20970
20971 do_all = (flags[0] == 'g');
20972
20973 regmatch.rm_ic = p_ic;
20974 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20975 if (regmatch.regprog != NULL)
20976 {
20977 tail = str;
20978 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20979 {
20980 /*
20981 * Get some space for a temporary buffer to do the substitution
20982 * into. It will contain:
20983 * - The text up to where the match is.
20984 * - The substituted text.
20985 * - The text after the match.
20986 */
20987 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20988 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20989 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20990 {
20991 ga_clear(&ga);
20992 break;
20993 }
20994
20995 /* copy the text up to where the match is */
20996 i = (int)(regmatch.startp[0] - tail);
20997 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20998 /* add the substituted text */
20999 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21000 + ga.ga_len + i, TRUE, TRUE, FALSE);
21001 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 /* avoid getting stuck on a match with an empty string */
21003 if (tail == regmatch.endp[0])
21004 {
21005 if (*tail == NUL)
21006 break;
21007 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21008 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 }
21010 else
21011 {
21012 tail = regmatch.endp[0];
21013 if (*tail == NUL)
21014 break;
21015 }
21016 if (!do_all)
21017 break;
21018 }
21019
21020 if (ga.ga_data != NULL)
21021 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21022
21023 vim_free(regmatch.regprog);
21024 }
21025
21026 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21027 ga_clear(&ga);
21028 p_cpo = save_cpo;
21029
21030 return ret;
21031}
21032
21033#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */