blob: bfb9946e316a107b16eed589457ba80e8d3cd849 [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));
372static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000373static void list_script_vars __ARGS((void));
374static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000375static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
376static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
377static int check_changedtick __ARGS((char_u *arg));
378static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
379static void clear_lval __ARGS((lval_T *lp));
380static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
381static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
382static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
383static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
384static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
385static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
386static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
387static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
388static void item_lock __ARGS((typval_T *tv, int deep, int lock));
389static int tv_islocked __ARGS((typval_T *tv));
390
Bram Moolenaar33570922005-01-25 22:26:29 +0000391static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
392static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000400static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000405static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static listitem_T *listitem_alloc __ARGS((void));
407static void listitem_free __ARGS((listitem_T *item));
408static void listitem_remove __ARGS((list_T *l, listitem_T *item));
409static long list_len __ARGS((list_T *l));
410static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
411static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
412static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000414static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_append __ARGS((list_T *l, listitem_T *item));
417static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000418static int list_append_string __ARGS((list_T *l, char_u *str, int len));
419static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
421static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
422static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000423static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
426static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000427static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
428static void set_ref_in_list __ARGS((list_T *l, int copyID));
429static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static void dict_unref __ARGS((dict_T *d));
431static void dict_free __ARGS((dict_T *d));
432static dictitem_T *dictitem_alloc __ARGS((char_u *key));
433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
435static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000436static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int dict_add __ARGS((dict_T *d, dictitem_T *item));
438static long dict_len __ARGS((dict_T *d));
439static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static 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));
449static 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 +0000450static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451
452static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000468static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000472#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000473static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000474static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
482static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000508static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000510static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000516static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000524static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000525static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000548static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000554static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000571static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000575#ifdef vim_mkdir
576static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
577#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000582static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000583static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000585static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000597static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000606static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000607static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000608static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000613static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000614static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000617static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618#ifdef HAVE_STRFTIME
619static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
621static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000633static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000634static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000635static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000636static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000637static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000639static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000653static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000656static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000658static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
659static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static int get_env_len __ARGS((char_u **arg));
661static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000662static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000663static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
664#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
665#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
666 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000667static 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 +0000668static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000669static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000670static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
671static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static typval_T *alloc_tv __ARGS((void));
673static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void init_tv __ARGS((typval_T *varp));
675static long get_tv_number __ARGS((typval_T *varp));
676static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000677static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static char_u *get_tv_string __ARGS((typval_T *varp));
679static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000682static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
684static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
685static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
686static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
687static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
688static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
689static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000690static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000692static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
694static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
695static int eval_fname_script __ARGS((char_u *p));
696static int eval_fname_sid __ARGS((char_u *p));
697static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static ufunc_T *find_func __ARGS((char_u *name));
699static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000700static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701#ifdef FEAT_PROFILE
702static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000703static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
704static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
705static int
706# ifdef __BORLANDC__
707 _RTLENTRYF
708# endif
709 prof_total_cmp __ARGS((const void *s1, const void *s2));
710static int
711# ifdef __BORLANDC__
712 _RTLENTRYF
713# endif
714 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000715#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000717static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000718static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void func_free __ARGS((ufunc_T *fp));
720static void func_unref __ARGS((char_u *name));
721static void func_ref __ARGS((char_u *name));
722static 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));
723static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000724static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000725static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000726static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000728/* Character used as separated in autoload function/variable names. */
729#define AUTOLOAD_CHAR '#'
730
Bram Moolenaar33570922005-01-25 22:26:29 +0000731/*
732 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000733 */
734 void
735eval_init()
736{
Bram Moolenaar33570922005-01-25 22:26:29 +0000737 int i;
738 struct vimvar *p;
739
740 init_var_dict(&globvardict, &globvars_var);
741 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000742 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000743 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000744
745 for (i = 0; i < VV_LEN; ++i)
746 {
747 p = &vimvars[i];
748 STRCPY(p->vv_di.di_key, p->vv_name);
749 if (p->vv_flags & VV_RO)
750 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
751 else if (p->vv_flags & VV_RO_SBX)
752 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
753 else
754 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755
756 /* add to v: scope dict, unless the value is not always available */
757 if (p->vv_type != VAR_UNKNOWN)
758 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000759 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000760 /* add to compat scope dict */
761 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000763}
764
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765#if defined(EXITFREE) || defined(PROTO)
766 void
767eval_clear()
768{
769 int i;
770 struct vimvar *p;
771
772 for (i = 0; i < VV_LEN; ++i)
773 {
774 p = &vimvars[i];
775 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000776 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000777 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000778 p->vv_di.di_tv.vval.v_string = NULL;
779 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000780 }
781 hash_clear(&vimvarht);
782 hash_clear(&compat_hashtab);
783
784 /* script-local variables */
785 for (i = 1; i <= ga_scripts.ga_len; ++i)
786 vars_clear(&SCRIPT_VARS(i));
787 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000788 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789
790 /* global variables */
791 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000792
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000793 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000795 hash_clear(&func_hashtab);
796
797 /* unreferenced lists and dicts */
798 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000799}
800#endif
801
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * Return the name of the executed function.
804 */
805 char_u *
806func_name(cookie)
807 void *cookie;
808{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000809 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/*
813 * Return the address holding the next breakpoint line for a funccall cookie.
814 */
815 linenr_T *
816func_breakpoint(cookie)
817 void *cookie;
818{
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821
822/*
823 * Return the address holding the debug tick for a funccall cookie.
824 */
825 int *
826func_dbg_tick(cookie)
827 void *cookie;
828{
Bram Moolenaar33570922005-01-25 22:26:29 +0000829 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
832/*
833 * Return the nesting level for a funccall cookie.
834 */
835 int
836func_level(cookie)
837 void *cookie;
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841
842/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000843funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845/*
846 * Return TRUE when a function was ended by a ":return" command.
847 */
848 int
849current_func_returned()
850{
851 return current_funccal->returned;
852}
853
854
855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 * Set an internal variable to a string value. Creates the variable if it does
857 * not already exist.
858 */
859 void
860set_internal_string_var(name, value)
861 char_u *name;
862 char_u *value;
863{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000864 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 val = vim_strsave(value);
868 if (val != NULL)
869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000870 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000871 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000873 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876 }
877}
878
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000879static lval_T *redir_lval = NULL;
880static char_u *redir_endp = NULL;
881static char_u *redir_varname = NULL;
882
883/*
884 * Start recording command output to a variable
885 * Returns OK if successfully completed the setup. FAIL otherwise.
886 */
887 int
888var_redir_start(name, append)
889 char_u *name;
890 int append; /* append to an existing variable */
891{
892 int save_emsg;
893 int err;
894 typval_T tv;
895
896 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000897 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898 {
899 EMSG(_(e_invarg));
900 return FAIL;
901 }
902
903 redir_varname = vim_strsave(name);
904 if (redir_varname == NULL)
905 return FAIL;
906
907 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
908 if (redir_lval == NULL)
909 {
910 var_redir_stop();
911 return FAIL;
912 }
913
914 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000915 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
916 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
918 {
919 if (redir_endp != NULL && *redir_endp != NUL)
920 /* Trailing characters are present after the variable name */
921 EMSG(_(e_trailing));
922 else
923 EMSG(_(e_invarg));
924 var_redir_stop();
925 return FAIL;
926 }
927
928 /* check if we can write to the variable: set it to or append an empty
929 * string */
930 save_emsg = did_emsg;
931 did_emsg = FALSE;
932 tv.v_type = VAR_STRING;
933 tv.vval.v_string = (char_u *)"";
934 if (append)
935 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
936 else
937 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
938 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000939 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 if (err)
941 {
942 var_redir_stop();
943 return FAIL;
944 }
945 if (redir_lval->ll_newkey != NULL)
946 {
947 /* Dictionary item was created, don't do it again. */
948 vim_free(redir_lval->ll_newkey);
949 redir_lval->ll_newkey = NULL;
950 }
951
952 return OK;
953}
954
955/*
956 * Append "value[len]" to the variable set by var_redir_start().
957 */
958 void
959var_redir_str(value, len)
960 char_u *value;
961 int len;
962{
963 char_u *val;
964 typval_T tv;
965 int save_emsg;
966 int err;
967
968 if (redir_lval == NULL)
969 return;
970
971 if (len == -1)
972 /* Append the entire string */
973 val = vim_strsave(value);
974 else
975 /* Append only the specified number of characters */
976 val = vim_strnsave(value, len);
977 if (val == NULL)
978 return;
979
980 tv.v_type = VAR_STRING;
981 tv.vval.v_string = val;
982
983 save_emsg = did_emsg;
984 did_emsg = FALSE;
985 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
986 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000987 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000988 if (err)
989 var_redir_stop();
990
991 vim_free(tv.vval.v_string);
992}
993
994/*
995 * Stop redirecting command output to a variable.
996 */
997 void
998var_redir_stop()
999{
1000 if (redir_lval != NULL)
1001 {
1002 clear_lval(redir_lval);
1003 vim_free(redir_lval);
1004 redir_lval = NULL;
1005 }
1006 vim_free(redir_varname);
1007 redir_varname = NULL;
1008}
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010# if defined(FEAT_MBYTE) || defined(PROTO)
1011 int
1012eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1013 char_u *enc_from;
1014 char_u *enc_to;
1015 char_u *fname_from;
1016 char_u *fname_to;
1017{
1018 int err = FALSE;
1019
1020 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1021 set_vim_var_string(VV_CC_TO, enc_to, -1);
1022 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1023 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1024 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1025 err = TRUE;
1026 set_vim_var_string(VV_CC_FROM, NULL, -1);
1027 set_vim_var_string(VV_CC_TO, NULL, -1);
1028 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1029 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1030
1031 if (err)
1032 return FAIL;
1033 return OK;
1034}
1035# endif
1036
1037# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1038 int
1039eval_printexpr(fname, args)
1040 char_u *fname;
1041 char_u *args;
1042{
1043 int err = FALSE;
1044
1045 set_vim_var_string(VV_FNAME_IN, fname, -1);
1046 set_vim_var_string(VV_CMDARG, args, -1);
1047 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1048 err = TRUE;
1049 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1050 set_vim_var_string(VV_CMDARG, NULL, -1);
1051
1052 if (err)
1053 {
1054 mch_remove(fname);
1055 return FAIL;
1056 }
1057 return OK;
1058}
1059# endif
1060
1061# if defined(FEAT_DIFF) || defined(PROTO)
1062 void
1063eval_diff(origfile, newfile, outfile)
1064 char_u *origfile;
1065 char_u *newfile;
1066 char_u *outfile;
1067{
1068 int err = FALSE;
1069
1070 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1071 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1072 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1073 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1074 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1075 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1076 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1077}
1078
1079 void
1080eval_patch(origfile, difffile, outfile)
1081 char_u *origfile;
1082 char_u *difffile;
1083 char_u *outfile;
1084{
1085 int err;
1086
1087 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1088 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1089 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1090 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1091 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1092 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1093 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1094}
1095# endif
1096
1097/*
1098 * Top level evaluation function, returning a boolean.
1099 * Sets "error" to TRUE if there was an error.
1100 * Return TRUE or FALSE.
1101 */
1102 int
1103eval_to_bool(arg, error, nextcmd, skip)
1104 char_u *arg;
1105 int *error;
1106 char_u **nextcmd;
1107 int skip; /* only parse, don't execute */
1108{
Bram Moolenaar33570922005-01-25 22:26:29 +00001109 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 int retval = FALSE;
1111
1112 if (skip)
1113 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 else
1117 {
1118 *error = FALSE;
1119 if (!skip)
1120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001121 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124 }
1125 if (skip)
1126 --emsg_skip;
1127
1128 return retval;
1129}
1130
1131/*
1132 * Top level evaluation function, returning a string. If "skip" is TRUE,
1133 * only parsing to "nextcmd" is done, without reporting errors. Return
1134 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1135 */
1136 char_u *
1137eval_to_string_skip(arg, nextcmd, skip)
1138 char_u *arg;
1139 char_u **nextcmd;
1140 int skip; /* only parse, don't execute */
1141{
Bram Moolenaar33570922005-01-25 22:26:29 +00001142 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 char_u *retval;
1144
1145 if (skip)
1146 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 retval = NULL;
1149 else
1150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 retval = vim_strsave(get_tv_string(&tv));
1152 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154 if (skip)
1155 --emsg_skip;
1156
1157 return retval;
1158}
1159
1160/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001161 * Skip over an expression at "*pp".
1162 * Return FAIL for an error, OK otherwise.
1163 */
1164 int
1165skip_expr(pp)
1166 char_u **pp;
1167{
Bram Moolenaar33570922005-01-25 22:26:29 +00001168 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001169
1170 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001172}
1173
1174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * Top level evaluation function, returning a string.
1176 * Return pointer to allocated memory, or NULL for failure.
1177 */
1178 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001179eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 char_u *arg;
1181 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001182 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
Bram Moolenaar33570922005-01-25 22:26:29 +00001184 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001186 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001188 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 retval = NULL;
1190 else
1191 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001192 if (dolist && tv.v_type == VAR_LIST)
1193 {
1194 ga_init2(&ga, (int)sizeof(char), 80);
1195 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1196 ga_append(&ga, NUL);
1197 retval = (char_u *)ga.ga_data;
1198 }
1199 else
1200 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203
1204 return retval;
1205}
1206
1207/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001208 * Call eval_to_string() without using current local variables and using
1209 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
1211 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001212eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 char_u *arg;
1214 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001215 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
1217 char_u *retval;
1218 void *save_funccalp;
1219
1220 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001221 if (use_sandbox)
1222 ++sandbox;
1223 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001224 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001225 if (use_sandbox)
1226 --sandbox;
1227 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 restore_funccal(save_funccalp);
1229 return retval;
1230}
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * Top level evaluation function, returning a number.
1234 * Evaluates "expr" silently.
1235 * Returns -1 for an error.
1236 */
1237 int
1238eval_to_number(expr)
1239 char_u *expr;
1240{
Bram Moolenaar33570922005-01-25 22:26:29 +00001241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001243 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 ++emsg_off;
1246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001247 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 retval = -1;
1249 else
1250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001251 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001252 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 --emsg_off;
1255
1256 return retval;
1257}
1258
Bram Moolenaara40058a2005-07-11 22:42:07 +00001259/*
1260 * Prepare v: variable "idx" to be used.
1261 * Save the current typeval in "save_tv".
1262 * When not used yet add the variable to the v: hashtable.
1263 */
1264 static void
1265prepare_vimvar(idx, save_tv)
1266 int idx;
1267 typval_T *save_tv;
1268{
1269 *save_tv = vimvars[idx].vv_tv;
1270 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1271 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1272}
1273
1274/*
1275 * Restore v: variable "idx" to typeval "save_tv".
1276 * When no longer defined, remove the variable from the v: hashtable.
1277 */
1278 static void
1279restore_vimvar(idx, save_tv)
1280 int idx;
1281 typval_T *save_tv;
1282{
1283 hashitem_T *hi;
1284
1285 clear_tv(&vimvars[idx].vv_tv);
1286 vimvars[idx].vv_tv = *save_tv;
1287 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1288 {
1289 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1290 if (HASHITEM_EMPTY(hi))
1291 EMSG2(_(e_intern2), "restore_vimvar()");
1292 else
1293 hash_remove(&vimvarht, hi);
1294 }
1295}
1296
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001297#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001298/*
1299 * Evaluate an expression to a list with suggestions.
1300 * For the "expr:" part of 'spellsuggest'.
1301 */
1302 list_T *
1303eval_spell_expr(badword, expr)
1304 char_u *badword;
1305 char_u *expr;
1306{
1307 typval_T save_val;
1308 typval_T rettv;
1309 list_T *list = NULL;
1310 char_u *p = skipwhite(expr);
1311
1312 /* Set "v:val" to the bad word. */
1313 prepare_vimvar(VV_VAL, &save_val);
1314 vimvars[VV_VAL].vv_type = VAR_STRING;
1315 vimvars[VV_VAL].vv_str = badword;
1316 if (p_verbose == 0)
1317 ++emsg_off;
1318
1319 if (eval1(&p, &rettv, TRUE) == OK)
1320 {
1321 if (rettv.v_type != VAR_LIST)
1322 clear_tv(&rettv);
1323 else
1324 list = rettv.vval.v_list;
1325 }
1326
1327 if (p_verbose == 0)
1328 --emsg_off;
1329 vimvars[VV_VAL].vv_str = NULL;
1330 restore_vimvar(VV_VAL, &save_val);
1331
1332 return list;
1333}
1334
1335/*
1336 * "list" is supposed to contain two items: a word and a number. Return the
1337 * word in "pp" and the number as the return value.
1338 * Return -1 if anything isn't right.
1339 * Used to get the good word and score from the eval_spell_expr() result.
1340 */
1341 int
1342get_spellword(list, pp)
1343 list_T *list;
1344 char_u **pp;
1345{
1346 listitem_T *li;
1347
1348 li = list->lv_first;
1349 if (li == NULL)
1350 return -1;
1351 *pp = get_tv_string(&li->li_tv);
1352
1353 li = li->li_next;
1354 if (li == NULL)
1355 return -1;
1356 return get_tv_number(&li->li_tv);
1357}
1358#endif
1359
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001360/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001361 * Top level evaluation function.
1362 * Returns an allocated typval_T with the result.
1363 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001364 */
1365 typval_T *
1366eval_expr(arg, nextcmd)
1367 char_u *arg;
1368 char_u **nextcmd;
1369{
1370 typval_T *tv;
1371
1372 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001373 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001374 {
1375 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001376 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001377 }
1378
1379 return tv;
1380}
1381
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1384/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001385 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001387 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001389 static int
1390call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 char_u *func;
1392 int argc;
1393 char_u **argv;
1394 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 long n;
1399 int len;
1400 int i;
1401 int doesrange;
1402 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001403 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar33570922005-01-25 22:26:29 +00001405 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 for (i = 0; i < argc; i++)
1410 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001411 /* Pass a NULL or empty argument as an empty string */
1412 if (argv[i] == NULL || *argv[i] == NUL)
1413 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001414 argvars[i].v_type = VAR_STRING;
1415 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001416 continue;
1417 }
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 /* Recognize a number argument, the others must be strings. */
1420 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1421 if (len != 0 && len == (int)STRLEN(argv[i]))
1422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001423 argvars[i].v_type = VAR_NUMBER;
1424 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 else
1427 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001428 argvars[i].v_type = VAR_STRING;
1429 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 }
1432
1433 if (safe)
1434 {
1435 save_funccalp = save_funccal();
1436 ++sandbox;
1437 }
1438
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001439 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1440 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (safe)
1444 {
1445 --sandbox;
1446 restore_funccal(save_funccalp);
1447 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001448 vim_free(argvars);
1449
1450 if (ret == FAIL)
1451 clear_tv(rettv);
1452
1453 return ret;
1454}
1455
1456/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001457 * Call vimL function "func" and return the result as a string.
1458 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459 * Uses argv[argc] for the function arguments.
1460 */
1461 void *
1462call_func_retstr(func, argc, argv, safe)
1463 char_u *func;
1464 int argc;
1465 char_u **argv;
1466 int safe; /* use the sandbox */
1467{
1468 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001469 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470
1471 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1472 return NULL;
1473
1474 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 return retval;
1477}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001479#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001481 * Call vimL function "func" and return the result as a number.
1482 * Returns -1 when calling the function fails.
1483 * Uses argv[argc] for the function arguments.
1484 */
1485 long
1486call_func_retnr(func, argc, argv, safe)
1487 char_u *func;
1488 int argc;
1489 char_u **argv;
1490 int safe; /* use the sandbox */
1491{
1492 typval_T rettv;
1493 long retval;
1494
1495 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1496 return -1;
1497
1498 retval = get_tv_number_chk(&rettv, NULL);
1499 clear_tv(&rettv);
1500 return retval;
1501}
1502#endif
1503
1504/*
1505 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506 * Uses argv[argc] for the function arguments.
1507 */
1508 void *
1509call_func_retlist(func, argc, argv, safe)
1510 char_u *func;
1511 int argc;
1512 char_u **argv;
1513 int safe; /* use the sandbox */
1514{
1515 typval_T rettv;
1516
1517 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1518 return NULL;
1519
1520 if (rettv.v_type != VAR_LIST)
1521 {
1522 clear_tv(&rettv);
1523 return NULL;
1524 }
1525
1526 return rettv.vval.v_list;
1527}
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529#endif
1530
1531/*
1532 * Save the current function call pointer, and set it to NULL.
1533 * Used when executing autocommands and for ":source".
1534 */
1535 void *
1536save_funccal()
1537{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001538 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 current_funccal = NULL;
1541 return (void *)fc;
1542}
1543
1544 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001545restore_funccal(vfc)
1546 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001548 funccall_T *fc = (funccall_T *)vfc;
1549
1550 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551}
1552
Bram Moolenaar05159a02005-02-26 23:04:13 +00001553#if defined(FEAT_PROFILE) || defined(PROTO)
1554/*
1555 * Prepare profiling for entering a child or something else that is not
1556 * counted for the script/function itself.
1557 * Should always be called in pair with prof_child_exit().
1558 */
1559 void
1560prof_child_enter(tm)
1561 proftime_T *tm; /* place to store waittime */
1562{
1563 funccall_T *fc = current_funccal;
1564
1565 if (fc != NULL && fc->func->uf_profiling)
1566 profile_start(&fc->prof_child);
1567 script_prof_save(tm);
1568}
1569
1570/*
1571 * Take care of time spent in a child.
1572 * Should always be called after prof_child_enter().
1573 */
1574 void
1575prof_child_exit(tm)
1576 proftime_T *tm; /* where waittime was stored */
1577{
1578 funccall_T *fc = current_funccal;
1579
1580 if (fc != NULL && fc->func->uf_profiling)
1581 {
1582 profile_end(&fc->prof_child);
1583 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1584 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1585 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1586 }
1587 script_prof_restore(tm);
1588}
1589#endif
1590
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592#ifdef FEAT_FOLDING
1593/*
1594 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1595 * it in "*cp". Doesn't give error messages.
1596 */
1597 int
1598eval_foldexpr(arg, cp)
1599 char_u *arg;
1600 int *cp;
1601{
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 int retval;
1604 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001605 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1606 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001609 if (use_sandbox)
1610 ++sandbox;
1611 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 retval = 0;
1615 else
1616 {
1617 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 if (tv.v_type == VAR_NUMBER)
1619 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001620 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 retval = 0;
1622 else
1623 {
1624 /* If the result is a string, check if there is a non-digit before
1625 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (!VIM_ISDIGIT(*s) && *s != '-')
1628 *cp = *s++;
1629 retval = atol((char *)s);
1630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
1633 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001634 if (use_sandbox)
1635 --sandbox;
1636 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 return retval;
1639}
1640#endif
1641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001643 * ":let" list all variable values
1644 * ":let var1 var2" list variable values
1645 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 * ":let var += expr" assignment command.
1647 * ":let var -= expr" assignment command.
1648 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 */
1651 void
1652ex_let(eap)
1653 exarg_T *eap;
1654{
1655 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001657 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 int var_count = 0;
1660 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 expr = skip_var_list(arg, &var_count, &semicolon);
1664 if (expr == NULL)
1665 return;
1666 expr = vim_strchr(expr, '=');
1667 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001669 /*
1670 * ":let" without "=": list variables
1671 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 if (*arg == '[')
1673 EMSG(_(e_invarg));
1674 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 /* ":let var1 var2" */
1676 arg = list_arg_vars(eap, arg);
1677 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001680 list_glob_vars();
1681 list_buf_vars();
1682 list_win_vars();
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001683 list_script_vars();
1684 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001685 list_vim_vars();
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 eap->nextcmd = check_nextcmd(arg);
1688 }
1689 else
1690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 op[0] = '=';
1692 op[1] = NUL;
1693 if (expr > arg)
1694 {
1695 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1696 op[0] = expr[-1]; /* +=, -= or .= */
1697 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if (eap->skip)
1701 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (eap->skip)
1704 {
1705 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001706 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 --emsg_skip;
1708 }
1709 else if (i != FAIL)
1710 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 }
1716}
1717
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718/*
1719 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1720 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 * When "nextchars" is not NULL it points to a string with characters that
1722 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1723 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 * Returns OK or FAIL;
1725 */
1726 static int
1727ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1728 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001729 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 int copy; /* copy values from "tv", don't move */
1731 int semicolon; /* from skip_var_list() */
1732 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001733 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734{
1735 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001736 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001738 listitem_T *item;
1739 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740
1741 if (*arg != '[')
1742 {
1743 /*
1744 * ":let var = expr" or ":for var in list"
1745 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 return FAIL;
1748 return OK;
1749 }
1750
1751 /*
1752 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1753 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001754 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 {
1756 EMSG(_(e_listreq));
1757 return FAIL;
1758 }
1759
1760 i = list_len(l);
1761 if (semicolon == 0 && var_count < i)
1762 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001763 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 return FAIL;
1765 }
1766 if (var_count - semicolon > i)
1767 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001768 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 return FAIL;
1770 }
1771
1772 item = l->lv_first;
1773 while (*arg != ']')
1774 {
1775 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 item = item->li_next;
1778 if (arg == NULL)
1779 return FAIL;
1780
1781 arg = skipwhite(arg);
1782 if (*arg == ';')
1783 {
1784 /* Put the rest of the list (may be empty) in the var after ';'.
1785 * Create a new list for this. */
1786 l = list_alloc();
1787 if (l == NULL)
1788 return FAIL;
1789 while (item != NULL)
1790 {
1791 list_append_tv(l, &item->li_tv);
1792 item = item->li_next;
1793 }
1794
1795 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001796 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797 ltv.vval.v_list = l;
1798 l->lv_refcount = 1;
1799
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1801 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 clear_tv(&ltv);
1803 if (arg == NULL)
1804 return FAIL;
1805 break;
1806 }
1807 else if (*arg != ',' && *arg != ']')
1808 {
1809 EMSG2(_(e_intern2), "ex_let_vars()");
1810 return FAIL;
1811 }
1812 }
1813
1814 return OK;
1815}
1816
1817/*
1818 * Skip over assignable variable "var" or list of variables "[var, var]".
1819 * Used for ":let varvar = expr" and ":for varvar in expr".
1820 * For "[var, var]" increment "*var_count" for each variable.
1821 * for "[var, var; var]" set "semicolon".
1822 * Return NULL for an error.
1823 */
1824 static char_u *
1825skip_var_list(arg, var_count, semicolon)
1826 char_u *arg;
1827 int *var_count;
1828 int *semicolon;
1829{
1830 char_u *p, *s;
1831
1832 if (*arg == '[')
1833 {
1834 /* "[var, var]": find the matching ']'. */
1835 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001836 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 {
1838 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1839 s = skip_var_one(p);
1840 if (s == p)
1841 {
1842 EMSG2(_(e_invarg2), p);
1843 return NULL;
1844 }
1845 ++*var_count;
1846
1847 p = skipwhite(s);
1848 if (*p == ']')
1849 break;
1850 else if (*p == ';')
1851 {
1852 if (*semicolon == 1)
1853 {
1854 EMSG(_("Double ; in list of variables"));
1855 return NULL;
1856 }
1857 *semicolon = 1;
1858 }
1859 else if (*p != ',')
1860 {
1861 EMSG2(_(e_invarg2), p);
1862 return NULL;
1863 }
1864 }
1865 return p + 1;
1866 }
1867 else
1868 return skip_var_one(arg);
1869}
1870
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001871/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001872 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1873 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001874 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 static char_u *
1876skip_var_one(arg)
1877 char_u *arg;
1878{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001879 if (*arg == '@' && arg[1] != NUL)
1880 return arg + 2;
1881 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1882 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883}
1884
Bram Moolenaara7043832005-01-21 11:56:39 +00001885/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 * List variables for hashtab "ht" with prefix "prefix".
1887 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001890list_hashtable_vars(ht, prefix, empty)
1891 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001892 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001894{
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 hashitem_T *hi;
1896 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001897 int todo;
1898
1899 todo = ht->ht_used;
1900 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1901 {
1902 if (!HASHITEM_EMPTY(hi))
1903 {
1904 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 di = HI2DI(hi);
1906 if (empty || di->di_tv.v_type != VAR_STRING
1907 || di->di_tv.vval.v_string != NULL)
1908 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 }
1910 }
1911}
1912
1913/*
1914 * List global variables.
1915 */
1916 static void
1917list_glob_vars()
1918{
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001920}
1921
1922/*
1923 * List buffer variables.
1924 */
1925 static void
1926list_buf_vars()
1927{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001928 char_u numbuf[NUMBUFLEN];
1929
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001931
1932 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1933 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934}
1935
1936/*
1937 * List window variables.
1938 */
1939 static void
1940list_win_vars()
1941{
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001943}
1944
1945/*
1946 * List Vim variables.
1947 */
1948 static void
1949list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950{
Bram Moolenaar33570922005-01-25 22:26:29 +00001951 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952}
1953
1954/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001955 * List script-local variables, if there is a script.
1956 */
1957 static void
1958list_script_vars()
1959{
1960 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1961 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1962}
1963
1964/*
1965 * List function variables, if there is a function.
1966 */
1967 static void
1968list_func_vars()
1969{
1970 if (current_funccal != NULL)
1971 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
1972 (char_u *)"l:", FALSE);
1973}
1974
1975/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 * List variables in "arg".
1977 */
1978 static char_u *
1979list_arg_vars(eap, arg)
1980 exarg_T *eap;
1981 char_u *arg;
1982{
1983 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001986 char_u *name_start;
1987 char_u *arg_subsc;
1988 char_u *tofree;
1989 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990
1991 while (!ends_excmd(*arg) && !got_int)
1992 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001995 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1997 {
1998 emsg_severe = TRUE;
1999 EMSG(_(e_trailing));
2000 break;
2001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002005 /* get_name_len() takes care of expanding curly braces */
2006 name_start = name = arg;
2007 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2008 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 /* This is mainly to keep test 49 working: when expanding
2011 * curly braces fails overrule the exception error message. */
2012 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002014 emsg_severe = TRUE;
2015 EMSG2(_(e_invarg2), arg);
2016 break;
2017 }
2018 error = TRUE;
2019 }
2020 else
2021 {
2022 if (tofree != NULL)
2023 name = tofree;
2024 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 else
2027 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002028 /* handle d.key, l[idx], f(expr) */
2029 arg_subsc = arg;
2030 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002031 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002033 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002035 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002036 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002037 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002038 case 'g': list_glob_vars(); break;
2039 case 'b': list_buf_vars(); break;
2040 case 'w': list_win_vars(); break;
2041 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002042 case 's': list_script_vars(); break;
2043 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002044 default:
2045 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002046 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002047 }
2048 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 {
2050 char_u numbuf[NUMBUFLEN];
2051 char_u *tf;
2052 int c;
2053 char_u *s;
2054
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002055 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 c = *arg;
2057 *arg = NUL;
2058 list_one_var_a((char_u *)"",
2059 arg == arg_subsc ? name : name_start,
2060 tv.v_type, s == NULL ? (char_u *)"" : s);
2061 *arg = c;
2062 vim_free(tf);
2063 }
2064 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 }
2067 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068
2069 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071
2072 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
2074
2075 return arg;
2076}
2077
2078/*
2079 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2080 * Returns a pointer to the char just after the var name.
2081 * Returns NULL if there is an error.
2082 */
2083 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002087 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002088 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090{
2091 int c1;
2092 char_u *name;
2093 char_u *p;
2094 char_u *arg_end = NULL;
2095 int len;
2096 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098
2099 /*
2100 * ":let $VAR = expr": Set environment variable.
2101 */
2102 if (*arg == '$')
2103 {
2104 /* Find the end of the name. */
2105 ++arg;
2106 name = arg;
2107 len = get_env_len(&arg);
2108 if (len == 0)
2109 EMSG2(_(e_invarg2), name - 1);
2110 else
2111 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 if (op != NULL && (*op == '+' || *op == '-'))
2113 EMSG2(_(e_letwrong), op);
2114 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 EMSG(_(e_letunexp));
2117 else
2118 {
2119 c1 = name[len];
2120 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002121 p = get_tv_string_chk(tv);
2122 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002123 {
2124 int mustfree = FALSE;
2125 char_u *s = vim_getenv(name, &mustfree);
2126
2127 if (s != NULL)
2128 {
2129 p = tofree = concat_str(s, p);
2130 if (mustfree)
2131 vim_free(s);
2132 }
2133 }
2134 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002135 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137 if (STRICMP(name, "HOME") == 0)
2138 init_homedir();
2139 else if (didset_vim && STRICMP(name, "VIM") == 0)
2140 didset_vim = FALSE;
2141 else if (didset_vimruntime
2142 && STRICMP(name, "VIMRUNTIME") == 0)
2143 didset_vimruntime = FALSE;
2144 arg_end = arg;
2145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002147 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 }
2149 }
2150 }
2151
2152 /*
2153 * ":let &option = expr": Set option value.
2154 * ":let &l:option = expr": Set local option value.
2155 * ":let &g:option = expr": Set global option value.
2156 */
2157 else if (*arg == '&')
2158 {
2159 /* Find the end of the name. */
2160 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161 if (p == NULL || (endchars != NULL
2162 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 EMSG(_(e_letunexp));
2164 else
2165 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 long n;
2167 int opt_type;
2168 long numval;
2169 char_u *stringval = NULL;
2170 char_u *s;
2171
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 c1 = *p;
2173 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174
2175 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 s = get_tv_string_chk(tv); /* != NULL if number or string */
2177 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 {
2179 opt_type = get_option_value(arg, &numval,
2180 &stringval, opt_flags);
2181 if ((opt_type == 1 && *op == '.')
2182 || (opt_type == 0 && *op != '.'))
2183 EMSG2(_(e_letwrong), op);
2184 else
2185 {
2186 if (opt_type == 1) /* number */
2187 {
2188 if (*op == '+')
2189 n = numval + n;
2190 else
2191 n = numval - n;
2192 }
2193 else if (opt_type == 0 && stringval != NULL) /* string */
2194 {
2195 s = concat_str(stringval, s);
2196 vim_free(stringval);
2197 stringval = s;
2198 }
2199 }
2200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002201 if (s != NULL)
2202 {
2203 set_option_value(arg, n, s, opt_flags);
2204 arg_end = p;
2205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
2209 }
2210
2211 /*
2212 * ":let @r = expr": Set register contents.
2213 */
2214 else if (*arg == '@')
2215 {
2216 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 if (op != NULL && (*op == '+' || *op == '-'))
2218 EMSG2(_(e_letwrong), op);
2219 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002220 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 EMSG(_(e_letunexp));
2222 else
2223 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224 char_u *tofree = NULL;
2225 char_u *s;
2226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002227 p = get_tv_string_chk(tv);
2228 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002230 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002231 if (s != NULL)
2232 {
2233 p = tofree = concat_str(s, p);
2234 vim_free(s);
2235 }
2236 }
2237 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002238 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 arg_end = arg + 1;
2241 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 }
2244 }
2245
2246 /*
2247 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002250 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002254 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2258 EMSG(_(e_letunexp));
2259 else
2260 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002261 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 arg_end = p;
2263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 }
2267
2268 else
2269 EMSG2(_(e_invarg2), arg);
2270
2271 return arg_end;
2272}
2273
2274/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2276 */
2277 static int
2278check_changedtick(arg)
2279 char_u *arg;
2280{
2281 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2282 {
2283 EMSG2(_(e_readonlyvar), arg);
2284 return TRUE;
2285 }
2286 return FALSE;
2287}
2288
2289/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 * Get an lval: variable, Dict item or List item that can be assigned a value
2291 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2292 * "name.key", "name.key[expr]" etc.
2293 * Indexing only works if "name" is an existing List or Dictionary.
2294 * "name" points to the start of the name.
2295 * If "rettv" is not NULL it points to the value to be assigned.
2296 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2297 * wrong; must end in space or cmd separator.
2298 *
2299 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 */
2303 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002304get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002306 typval_T *rettv;
2307 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 int unlet;
2309 int skip;
2310 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002311 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 char_u *expr_start, *expr_end;
2315 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 dictitem_T *v;
2317 typval_T var1;
2318 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002321 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002322 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002323 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327
2328 if (skip)
2329 {
2330 /* When skipping just find the end of the name. */
2331 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002332 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 }
2334
2335 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002336 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (expr_start != NULL)
2338 {
2339 /* Don't expand the name when we already know there is an error. */
2340 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2341 && *p != '[' && *p != '.')
2342 {
2343 EMSG(_(e_trailing));
2344 return NULL;
2345 }
2346
2347 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2348 if (lp->ll_exp_name == NULL)
2349 {
2350 /* Report an invalid expression in braces, unless the
2351 * expression evaluation has been cancelled due to an
2352 * aborting error, an interrupt, or an exception. */
2353 if (!aborting() && !quiet)
2354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002355 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 EMSG2(_(e_invarg2), name);
2357 return NULL;
2358 }
2359 }
2360 lp->ll_name = lp->ll_exp_name;
2361 }
2362 else
2363 lp->ll_name = name;
2364
2365 /* Without [idx] or .key we are done. */
2366 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2367 return p;
2368
2369 cc = *p;
2370 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002371 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 if (v == NULL && !quiet)
2373 EMSG2(_(e_undefvar), lp->ll_name);
2374 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 if (v == NULL)
2376 return NULL;
2377
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 /*
2379 * Loop until no more [idx] or .key is following.
2380 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002381 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2385 && !(lp->ll_tv->v_type == VAR_DICT
2386 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 if (!quiet)
2389 EMSG(_("E689: Can only index a List or Dictionary"));
2390 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 if (!quiet)
2395 EMSG(_("E708: [:] must come last"));
2396 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002398
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 len = -1;
2400 if (*p == '.')
2401 {
2402 key = p + 1;
2403 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2404 ;
2405 if (len == 0)
2406 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 if (!quiet)
2408 EMSG(_(e_emptykey));
2409 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 }
2411 p = key + len;
2412 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002413 else
2414 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002416 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 if (*p == ':')
2418 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002419 else
2420 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 empty1 = FALSE;
2422 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 if (get_tv_string_chk(&var1) == NULL)
2425 {
2426 /* not a number or string */
2427 clear_tv(&var1);
2428 return NULL;
2429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
2431
2432 /* Optionally get the second index [ :expr]. */
2433 if (*p == ':')
2434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002439 if (!empty1)
2440 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002442 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (rettv != NULL && (rettv->v_type != VAR_LIST
2444 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
2447 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 if (!empty1)
2449 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452 p = skipwhite(p + 1);
2453 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 else
2456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 if (!empty1)
2461 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (get_tv_string_chk(&var2) == NULL)
2465 {
2466 /* not a number or string */
2467 if (!empty1)
2468 clear_tv(&var1);
2469 clear_tv(&var2);
2470 return NULL;
2471 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002477
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (!quiet)
2481 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 if (!empty1)
2483 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 }
2488
2489 /* Skip to past ']'. */
2490 ++p;
2491 }
2492
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 {
2495 if (len == -1)
2496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (*key == NUL)
2500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (!quiet)
2502 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
2506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 lp->ll_list = NULL;
2508 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002509 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002512 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 if (len == -1)
2518 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 }
2521 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 if (len == -1)
2526 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 p = NULL;
2529 break;
2530 }
2531 if (len == -1)
2532 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 }
2535 else
2536 {
2537 /*
2538 * Get the number and item for the only or first index of the List.
2539 */
2540 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 else
2543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 clear_tv(&var1);
2546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 lp->ll_list = lp->ll_tv->vval.v_list;
2549 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2550 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (!quiet)
2553 EMSGN(_(e_listidx), lp->ll_n1);
2554 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 }
2558
2559 /*
2560 * May need to find the item or absolute index for the second
2561 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * When no index given: "lp->ll_empty2" is TRUE.
2563 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002567 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 if (ni == NULL)
2573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (!quiet)
2575 EMSGN(_(e_listidx), lp->ll_n2);
2576 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 }
2580
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2582 if (lp->ll_n1 < 0)
2583 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2584 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002585 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (!quiet)
2587 EMSGN(_(e_listidx), lp->ll_n2);
2588 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002589 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002590 }
2591
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 }
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 return p;
2597}
2598
2599/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 */
2602 static void
2603clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605{
2606 vim_free(lp->ll_exp_name);
2607 vim_free(lp->ll_newkey);
2608}
2609
2610/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002611 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002613 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 */
2615 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002619 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622{
2623 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 listitem_T *ri;
2625 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626
2627 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 cc = *endp;
2632 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 if (op != NULL && *op != '=')
2634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002637 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002638 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2639 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 {
2641 if (tv_op(&tv, rettv, op) == OK)
2642 set_var(lp->ll_name, &tv, FALSE);
2643 clear_tv(&tv);
2644 }
2645 }
2646 else
2647 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002651 else if (tv_check_lock(lp->ll_newkey == NULL
2652 ? lp->ll_tv->v_lock
2653 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2654 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 else if (lp->ll_range)
2656 {
2657 /*
2658 * Assign the List values to the list items.
2659 */
2660 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 if (op != NULL && *op != '=')
2663 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2664 else
2665 {
2666 clear_tv(&lp->ll_li->li_tv);
2667 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2668 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 ri = ri->li_next;
2670 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2671 break;
2672 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002675 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 ri = NULL;
2678 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 lp->ll_li = lp->ll_li->li_next;
2682 ++lp->ll_n1;
2683 }
2684 if (ri != NULL)
2685 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 else if (lp->ll_empty2
2687 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 : lp->ll_n1 != lp->ll_n2)
2689 EMSG(_("E711: List value has not enough items"));
2690 }
2691 else
2692 {
2693 /*
2694 * Assign to a List or Dictionary item.
2695 */
2696 if (lp->ll_newkey != NULL)
2697 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 if (op != NULL && *op != '=')
2699 {
2700 EMSG2(_(e_letwrong), op);
2701 return;
2702 }
2703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002705 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (di == NULL)
2707 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002708 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2709 {
2710 vim_free(di);
2711 return;
2712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 else if (op != NULL && *op != '=')
2716 {
2717 tv_op(lp->ll_tv, rettv, op);
2718 return;
2719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 /*
2724 * Assign the value to the variable or list item.
2725 */
2726 if (copy)
2727 copy_tv(rettv, lp->ll_tv);
2728 else
2729 {
2730 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002731 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002733 }
2734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002735}
2736
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2739 * Returns OK or FAIL.
2740 */
2741 static int
2742tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002743 typval_T *tv1;
2744 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 char_u *op;
2746{
2747 long n;
2748 char_u numbuf[NUMBUFLEN];
2749 char_u *s;
2750
2751 /* Can't do anything with a Funcref or a Dict on the right. */
2752 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2753 {
2754 switch (tv1->v_type)
2755 {
2756 case VAR_DICT:
2757 case VAR_FUNC:
2758 break;
2759
2760 case VAR_LIST:
2761 if (*op != '+' || tv2->v_type != VAR_LIST)
2762 break;
2763 /* List += List */
2764 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2765 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2766 return OK;
2767
2768 case VAR_NUMBER:
2769 case VAR_STRING:
2770 if (tv2->v_type == VAR_LIST)
2771 break;
2772 if (*op == '+' || *op == '-')
2773 {
2774 /* nr += nr or nr -= nr*/
2775 n = get_tv_number(tv1);
2776 if (*op == '+')
2777 n += get_tv_number(tv2);
2778 else
2779 n -= get_tv_number(tv2);
2780 clear_tv(tv1);
2781 tv1->v_type = VAR_NUMBER;
2782 tv1->vval.v_number = n;
2783 }
2784 else
2785 {
2786 /* str .= str */
2787 s = get_tv_string(tv1);
2788 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2789 clear_tv(tv1);
2790 tv1->v_type = VAR_STRING;
2791 tv1->vval.v_string = s;
2792 }
2793 return OK;
2794 }
2795 }
2796
2797 EMSG2(_(e_letwrong), op);
2798 return FAIL;
2799}
2800
2801/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802 * Add a watcher to a list.
2803 */
2804 static void
2805list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002806 list_T *l;
2807 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002808{
2809 lw->lw_next = l->lv_watch;
2810 l->lv_watch = lw;
2811}
2812
2813/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002814 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815 * No warning when it isn't found...
2816 */
2817 static void
2818list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 list_T *l;
2820 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002821{
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823
2824 lwp = &l->lv_watch;
2825 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2826 {
2827 if (lw == lwrem)
2828 {
2829 *lwp = lw->lw_next;
2830 break;
2831 }
2832 lwp = &lw->lw_next;
2833 }
2834}
2835
2836/*
2837 * Just before removing an item from a list: advance watchers to the next
2838 * item.
2839 */
2840 static void
2841list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002842 list_T *l;
2843 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002844{
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846
2847 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2848 if (lw->lw_item == item)
2849 lw->lw_item = item->li_next;
2850}
2851
2852/*
2853 * Evaluate the expression used in a ":for var in expr" command.
2854 * "arg" points to "var".
2855 * Set "*errp" to TRUE for an error, FALSE otherwise;
2856 * Return a pointer that holds the info. Null when there is an error.
2857 */
2858 void *
2859eval_for_line(arg, errp, nextcmdp, skip)
2860 char_u *arg;
2861 int *errp;
2862 char_u **nextcmdp;
2863 int skip;
2864{
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002866 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 typval_T tv;
2868 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002869
2870 *errp = TRUE; /* default: there is an error */
2871
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002873 if (fi == NULL)
2874 return NULL;
2875
2876 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2877 if (expr == NULL)
2878 return fi;
2879
2880 expr = skipwhite(expr);
2881 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2882 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002883 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884 return fi;
2885 }
2886
2887 if (skip)
2888 ++emsg_skip;
2889 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2890 {
2891 *errp = FALSE;
2892 if (!skip)
2893 {
2894 l = tv.vval.v_list;
2895 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002896 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002898 clear_tv(&tv);
2899 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900 else
2901 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002902 /* No need to increment the refcount, it's already set for the
2903 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 fi->fi_list = l;
2905 list_add_watch(l, &fi->fi_lw);
2906 fi->fi_lw.lw_item = l->lv_first;
2907 }
2908 }
2909 }
2910 if (skip)
2911 --emsg_skip;
2912
2913 return fi;
2914}
2915
2916/*
2917 * Use the first item in a ":for" list. Advance to the next.
2918 * Assign the values to the variable (list). "arg" points to the first one.
2919 * Return TRUE when a valid item was found, FALSE when at end of list or
2920 * something wrong.
2921 */
2922 int
2923next_for_item(fi_void, arg)
2924 void *fi_void;
2925 char_u *arg;
2926{
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930
2931 item = fi->fi_lw.lw_item;
2932 if (item == NULL)
2933 result = FALSE;
2934 else
2935 {
2936 fi->fi_lw.lw_item = item->li_next;
2937 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2938 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2939 }
2940 return result;
2941}
2942
2943/*
2944 * Free the structure used to store info used by ":for".
2945 */
2946 void
2947free_for_info(fi_void)
2948 void *fi_void;
2949{
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002951
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002952 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002953 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002954 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002955 list_unref(fi->fi_list);
2956 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957 vim_free(fi);
2958}
2959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2961
2962 void
2963set_context_for_expression(xp, arg, cmdidx)
2964 expand_T *xp;
2965 char_u *arg;
2966 cmdidx_T cmdidx;
2967{
2968 int got_eq = FALSE;
2969 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002970 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 if (cmdidx == CMD_let)
2973 {
2974 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002975 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 {
2977 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002978 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 {
2980 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982 if (vim_iswhite(*p))
2983 break;
2984 }
2985 return;
2986 }
2987 }
2988 else
2989 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2990 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 while ((xp->xp_pattern = vim_strpbrk(arg,
2992 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2993 {
2994 c = *xp->xp_pattern;
2995 if (c == '&')
2996 {
2997 c = xp->xp_pattern[1];
2998 if (c == '&')
2999 {
3000 ++xp->xp_pattern;
3001 xp->xp_context = cmdidx != CMD_let || got_eq
3002 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3003 }
3004 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003005 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003007 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3008 xp->xp_pattern += 2;
3009
3010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012 else if (c == '$')
3013 {
3014 /* environment variable */
3015 xp->xp_context = EXPAND_ENV_VARS;
3016 }
3017 else if (c == '=')
3018 {
3019 got_eq = TRUE;
3020 xp->xp_context = EXPAND_EXPRESSION;
3021 }
3022 else if (c == '<'
3023 && xp->xp_context == EXPAND_FUNCTIONS
3024 && vim_strchr(xp->xp_pattern, '(') == NULL)
3025 {
3026 /* Function name can start with "<SNR>" */
3027 break;
3028 }
3029 else if (cmdidx != CMD_let || got_eq)
3030 {
3031 if (c == '"') /* string */
3032 {
3033 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3034 if (c == '\\' && xp->xp_pattern[1] != NUL)
3035 ++xp->xp_pattern;
3036 xp->xp_context = EXPAND_NOTHING;
3037 }
3038 else if (c == '\'') /* literal string */
3039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003040 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3042 /* skip */ ;
3043 xp->xp_context = EXPAND_NOTHING;
3044 }
3045 else if (c == '|')
3046 {
3047 if (xp->xp_pattern[1] == '|')
3048 {
3049 ++xp->xp_pattern;
3050 xp->xp_context = EXPAND_EXPRESSION;
3051 }
3052 else
3053 xp->xp_context = EXPAND_COMMANDS;
3054 }
3055 else
3056 xp->xp_context = EXPAND_EXPRESSION;
3057 }
3058 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059 /* Doesn't look like something valid, expand as an expression
3060 * anyway. */
3061 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 arg = xp->xp_pattern;
3063 if (*arg != NUL)
3064 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3065 /* skip */ ;
3066 }
3067 xp->xp_pattern = arg;
3068}
3069
3070#endif /* FEAT_CMDL_COMPL */
3071
3072/*
3073 * ":1,25call func(arg1, arg2)" function call.
3074 */
3075 void
3076ex_call(eap)
3077 exarg_T *eap;
3078{
3079 char_u *arg = eap->arg;
3080 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003084 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 linenr_T lnum;
3086 int doesrange;
3087 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003090 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3091 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 if (tofree == NULL)
3093 return;
3094
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 /* Increase refcount on dictionary, it could get deleted when evaluating
3096 * the arguments. */
3097 if (fudi.fd_dict != NULL)
3098 ++fudi.fd_dict->dv_refcount;
3099
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3101 len = STRLEN(tofree);
3102 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar532c7802005-01-27 14:44:31 +00003104 /* Skip white space to allow ":call func ()". Not good, but required for
3105 * backward compatibility. */
3106 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 if (*startarg != '(')
3110 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003111 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 goto end;
3113 }
3114
3115 /*
3116 * When skipping, evaluate the function once, to find the end of the
3117 * arguments.
3118 * When the function takes a range, this is discovered after the first
3119 * call, and the loop is broken.
3120 */
3121 if (eap->skip)
3122 {
3123 ++emsg_skip;
3124 lnum = eap->line2; /* do it once, also with an invalid range */
3125 }
3126 else
3127 lnum = eap->line1;
3128 for ( ; lnum <= eap->line2; ++lnum)
3129 {
3130 if (!eap->skip && eap->addr_count > 0)
3131 {
3132 curwin->w_cursor.lnum = lnum;
3133 curwin->w_cursor.col = 0;
3134 }
3135 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003137 eap->line1, eap->line2, &doesrange,
3138 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 failed = TRUE;
3141 break;
3142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003143 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (doesrange || eap->skip)
3145 break;
3146 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003147 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003148 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003149 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (aborting())
3151 break;
3152 }
3153 if (eap->skip)
3154 --emsg_skip;
3155
3156 if (!failed)
3157 {
3158 /* Check for trailing illegal characters and a following command. */
3159 if (!ends_excmd(*arg))
3160 {
3161 emsg_severe = TRUE;
3162 EMSG(_(e_trailing));
3163 }
3164 else
3165 eap->nextcmd = check_nextcmd(arg);
3166 }
3167
3168end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003169 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171}
3172
3173/*
3174 * ":unlet[!] var1 ... " command.
3175 */
3176 void
3177ex_unlet(eap)
3178 exarg_T *eap;
3179{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003180 ex_unletlock(eap, eap->arg, 0);
3181}
3182
3183/*
3184 * ":lockvar" and ":unlockvar" commands
3185 */
3186 void
3187ex_lockvar(eap)
3188 exarg_T *eap;
3189{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003191 int deep = 2;
3192
3193 if (eap->forceit)
3194 deep = -1;
3195 else if (vim_isdigit(*arg))
3196 {
3197 deep = getdigits(&arg);
3198 arg = skipwhite(arg);
3199 }
3200
3201 ex_unletlock(eap, arg, deep);
3202}
3203
3204/*
3205 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3206 */
3207 static void
3208ex_unletlock(eap, argstart, deep)
3209 exarg_T *eap;
3210 char_u *argstart;
3211 int deep;
3212{
3213 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003216 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 do
3219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003221 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3222 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223 if (lv.ll_name == NULL)
3224 error = TRUE; /* error but continue parsing */
3225 if (name_end == NULL || (!vim_iswhite(*name_end)
3226 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 if (name_end != NULL)
3229 {
3230 emsg_severe = TRUE;
3231 EMSG(_(e_trailing));
3232 }
3233 if (!(eap->skip || error))
3234 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 break;
3236 }
3237
3238 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003239 {
3240 if (eap->cmdidx == CMD_unlet)
3241 {
3242 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3243 error = TRUE;
3244 }
3245 else
3246 {
3247 if (do_lock_var(&lv, name_end, deep,
3248 eap->cmdidx == CMD_lockvar) == FAIL)
3249 error = TRUE;
3250 }
3251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 if (!eap->skip)
3254 clear_lval(&lv);
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 arg = skipwhite(name_end);
3257 } while (!ends_excmd(*arg));
3258
3259 eap->nextcmd = check_nextcmd(arg);
3260}
3261
Bram Moolenaar8c711452005-01-14 21:53:12 +00003262 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003263do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003266 int forceit;
3267{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003268 int ret = OK;
3269 int cc;
3270
3271 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003273 cc = *name_end;
3274 *name_end = NUL;
3275
3276 /* Normal name or expanded name. */
3277 if (check_changedtick(lp->ll_name))
3278 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003280 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003281 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003282 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003283 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3284 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003285 else if (lp->ll_range)
3286 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003288
3289 /* Delete a range of List items. */
3290 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3291 {
3292 li = lp->ll_li->li_next;
3293 listitem_remove(lp->ll_list, lp->ll_li);
3294 lp->ll_li = li;
3295 ++lp->ll_n1;
3296 }
3297 }
3298 else
3299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003300 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003301 /* unlet a List item. */
3302 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003303 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003304 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003305 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 }
3307
3308 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309}
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311/*
3312 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003313 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
3315 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003316do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003318 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 hashtab_T *ht;
3321 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003322 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 ht = find_var_ht(name, &varname);
3325 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 hi = hash_find(ht, varname);
3328 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003329 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003330 if (var_check_ro(HI2DI(hi)->di_flags, name))
3331 return FAIL;
3332 delete_var(ht, hi);
3333 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003336 if (forceit)
3337 return OK;
3338 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 return FAIL;
3340}
3341
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003342/*
3343 * Lock or unlock variable indicated by "lp".
3344 * "deep" is the levels to go (-1 for unlimited);
3345 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3346 */
3347 static int
3348do_lock_var(lp, name_end, deep, lock)
3349 lval_T *lp;
3350 char_u *name_end;
3351 int deep;
3352 int lock;
3353{
3354 int ret = OK;
3355 int cc;
3356 dictitem_T *di;
3357
3358 if (deep == 0) /* nothing to do */
3359 return OK;
3360
3361 if (lp->ll_tv == NULL)
3362 {
3363 cc = *name_end;
3364 *name_end = NUL;
3365
3366 /* Normal name or expanded name. */
3367 if (check_changedtick(lp->ll_name))
3368 ret = FAIL;
3369 else
3370 {
3371 di = find_var(lp->ll_name, NULL);
3372 if (di == NULL)
3373 ret = FAIL;
3374 else
3375 {
3376 if (lock)
3377 di->di_flags |= DI_FLAGS_LOCK;
3378 else
3379 di->di_flags &= ~DI_FLAGS_LOCK;
3380 item_lock(&di->di_tv, deep, lock);
3381 }
3382 }
3383 *name_end = cc;
3384 }
3385 else if (lp->ll_range)
3386 {
3387 listitem_T *li = lp->ll_li;
3388
3389 /* (un)lock a range of List items. */
3390 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3391 {
3392 item_lock(&li->li_tv, deep, lock);
3393 li = li->li_next;
3394 ++lp->ll_n1;
3395 }
3396 }
3397 else if (lp->ll_list != NULL)
3398 /* (un)lock a List item. */
3399 item_lock(&lp->ll_li->li_tv, deep, lock);
3400 else
3401 /* un(lock) a Dictionary item. */
3402 item_lock(&lp->ll_di->di_tv, deep, lock);
3403
3404 return ret;
3405}
3406
3407/*
3408 * Lock or unlock an item. "deep" is nr of levels to go.
3409 */
3410 static void
3411item_lock(tv, deep, lock)
3412 typval_T *tv;
3413 int deep;
3414 int lock;
3415{
3416 static int recurse = 0;
3417 list_T *l;
3418 listitem_T *li;
3419 dict_T *d;
3420 hashitem_T *hi;
3421 int todo;
3422
3423 if (recurse >= DICT_MAXNEST)
3424 {
3425 EMSG(_("E743: variable nested too deep for (un)lock"));
3426 return;
3427 }
3428 if (deep == 0)
3429 return;
3430 ++recurse;
3431
3432 /* lock/unlock the item itself */
3433 if (lock)
3434 tv->v_lock |= VAR_LOCKED;
3435 else
3436 tv->v_lock &= ~VAR_LOCKED;
3437
3438 switch (tv->v_type)
3439 {
3440 case VAR_LIST:
3441 if ((l = tv->vval.v_list) != NULL)
3442 {
3443 if (lock)
3444 l->lv_lock |= VAR_LOCKED;
3445 else
3446 l->lv_lock &= ~VAR_LOCKED;
3447 if (deep < 0 || deep > 1)
3448 /* recursive: lock/unlock the items the List contains */
3449 for (li = l->lv_first; li != NULL; li = li->li_next)
3450 item_lock(&li->li_tv, deep - 1, lock);
3451 }
3452 break;
3453 case VAR_DICT:
3454 if ((d = tv->vval.v_dict) != NULL)
3455 {
3456 if (lock)
3457 d->dv_lock |= VAR_LOCKED;
3458 else
3459 d->dv_lock &= ~VAR_LOCKED;
3460 if (deep < 0 || deep > 1)
3461 {
3462 /* recursive: lock/unlock the items the List contains */
3463 todo = d->dv_hashtab.ht_used;
3464 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3465 {
3466 if (!HASHITEM_EMPTY(hi))
3467 {
3468 --todo;
3469 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3470 }
3471 }
3472 }
3473 }
3474 }
3475 --recurse;
3476}
3477
Bram Moolenaara40058a2005-07-11 22:42:07 +00003478/*
3479 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3480 * it refers to a List or Dictionary that is locked.
3481 */
3482 static int
3483tv_islocked(tv)
3484 typval_T *tv;
3485{
3486 return (tv->v_lock & VAR_LOCKED)
3487 || (tv->v_type == VAR_LIST
3488 && tv->vval.v_list != NULL
3489 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3490 || (tv->v_type == VAR_DICT
3491 && tv->vval.v_dict != NULL
3492 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3493}
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3496/*
3497 * Delete all "menutrans_" variables.
3498 */
3499 void
3500del_menutrans_vars()
3501{
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003503 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 hash_lock(&globvarht);
3506 todo = globvarht.ht_used;
3507 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003508 {
3509 if (!HASHITEM_EMPTY(hi))
3510 {
3511 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003512 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3513 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003514 }
3515 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003516 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517}
3518#endif
3519
3520#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3521
3522/*
3523 * Local string buffer for the next two functions to store a variable name
3524 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3525 * get_user_var_name().
3526 */
3527
3528static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3529
3530static char_u *varnamebuf = NULL;
3531static int varnamebuflen = 0;
3532
3533/*
3534 * Function to concatenate a prefix and a variable name.
3535 */
3536 static char_u *
3537cat_prefix_varname(prefix, name)
3538 int prefix;
3539 char_u *name;
3540{
3541 int len;
3542
3543 len = (int)STRLEN(name) + 3;
3544 if (len > varnamebuflen)
3545 {
3546 vim_free(varnamebuf);
3547 len += 10; /* some additional space */
3548 varnamebuf = alloc(len);
3549 if (varnamebuf == NULL)
3550 {
3551 varnamebuflen = 0;
3552 return NULL;
3553 }
3554 varnamebuflen = len;
3555 }
3556 *varnamebuf = prefix;
3557 varnamebuf[1] = ':';
3558 STRCPY(varnamebuf + 2, name);
3559 return varnamebuf;
3560}
3561
3562/*
3563 * Function given to ExpandGeneric() to obtain the list of user defined
3564 * (global/buffer/window/built-in) variable names.
3565 */
3566/*ARGSUSED*/
3567 char_u *
3568get_user_var_name(xp, idx)
3569 expand_T *xp;
3570 int idx;
3571{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003572 static long_u gdone;
3573 static long_u bdone;
3574 static long_u wdone;
3575 static int vidx;
3576 static hashitem_T *hi;
3577 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003580 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003581
3582 /* Global variables */
3583 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003585 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003587 else
3588 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003589 while (HASHITEM_EMPTY(hi))
3590 ++hi;
3591 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3592 return cat_prefix_varname('g', hi->hi_key);
3593 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003595
3596 /* b: variables */
3597 ht = &curbuf->b_vars.dv_hashtab;
3598 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003600 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003601 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003602 else
3603 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003604 while (HASHITEM_EMPTY(hi))
3605 ++hi;
3606 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003610 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return (char_u *)"b:changedtick";
3612 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003613
3614 /* w: variables */
3615 ht = &curwin->w_vars.dv_hashtab;
3616 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003618 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003619 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003620 else
3621 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003622 while (HASHITEM_EMPTY(hi))
3623 ++hi;
3624 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003626
3627 /* v: variables */
3628 if (vidx < VV_LEN)
3629 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 vim_free(varnamebuf);
3632 varnamebuf = NULL;
3633 varnamebuflen = 0;
3634 return NULL;
3635}
3636
3637#endif /* FEAT_CMDL_COMPL */
3638
3639/*
3640 * types for expressions.
3641 */
3642typedef enum
3643{
3644 TYPE_UNKNOWN = 0
3645 , TYPE_EQUAL /* == */
3646 , TYPE_NEQUAL /* != */
3647 , TYPE_GREATER /* > */
3648 , TYPE_GEQUAL /* >= */
3649 , TYPE_SMALLER /* < */
3650 , TYPE_SEQUAL /* <= */
3651 , TYPE_MATCH /* =~ */
3652 , TYPE_NOMATCH /* !~ */
3653} exptype_T;
3654
3655/*
3656 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3659 */
3660
3661/*
3662 * Handle zero level expression.
3663 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003664 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003665 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 * Return OK or FAIL.
3667 */
3668 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 char_u **nextcmd;
3673 int evaluate;
3674{
3675 int ret;
3676 char_u *p;
3677
3678 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003679 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (ret == FAIL || !ends_excmd(*p))
3681 {
3682 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003683 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /*
3685 * Report the invalid expression unless the expression evaluation has
3686 * been cancelled due to an aborting error, an interrupt, or an
3687 * exception.
3688 */
3689 if (!aborting())
3690 EMSG2(_(e_invexpr2), arg);
3691 ret = FAIL;
3692 }
3693 if (nextcmd != NULL)
3694 *nextcmd = check_nextcmd(p);
3695
3696 return ret;
3697}
3698
3699/*
3700 * Handle top level expression:
3701 * expr1 ? expr0 : expr0
3702 *
3703 * "arg" must point to the first non-white of the expression.
3704 * "arg" is advanced to the next non-white after the recognized expression.
3705 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003706 * Note: "rettv.v_lock" is not set.
3707 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 * Return OK or FAIL.
3709 */
3710 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 int evaluate;
3715{
3716 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 /*
3720 * Get the first variable.
3721 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 return FAIL;
3724
3725 if ((*arg)[0] == '?')
3726 {
3727 result = FALSE;
3728 if (evaluate)
3729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003730 int error = FALSE;
3731
3732 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 if (error)
3736 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 /*
3740 * Get the second variable.
3741 */
3742 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 return FAIL;
3745
3746 /*
3747 * Check for the ":".
3748 */
3749 if ((*arg)[0] != ':')
3750 {
3751 EMSG(_("E109: Missing ':' after '?'"));
3752 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 return FAIL;
3755 }
3756
3757 /*
3758 * Get the third variable.
3759 */
3760 *arg = skipwhite(*arg + 1);
3761 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3762 {
3763 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return FAIL;
3766 }
3767 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770
3771 return OK;
3772}
3773
3774/*
3775 * Handle first level expression:
3776 * expr2 || expr2 || expr2 logical OR
3777 *
3778 * "arg" must point to the first non-white of the expression.
3779 * "arg" is advanced to the next non-white after the recognized expression.
3780 *
3781 * Return OK or FAIL.
3782 */
3783 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 int evaluate;
3788{
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 long result;
3791 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /*
3795 * Get the first variable.
3796 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return FAIL;
3799
3800 /*
3801 * Repeat until there is no following "||".
3802 */
3803 first = TRUE;
3804 result = FALSE;
3805 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3806 {
3807 if (evaluate && first)
3808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003811 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 if (error)
3813 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 first = FALSE;
3815 }
3816
3817 /*
3818 * Get the second variable.
3819 */
3820 *arg = skipwhite(*arg + 2);
3821 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3822 return FAIL;
3823
3824 /*
3825 * Compute the result.
3826 */
3827 if (evaluate && !result)
3828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (error)
3833 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 if (evaluate)
3836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 rettv->v_type = VAR_NUMBER;
3838 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
3841
3842 return OK;
3843}
3844
3845/*
3846 * Handle second level expression:
3847 * expr3 && expr3 && expr3 logical AND
3848 *
3849 * "arg" must point to the first non-white of the expression.
3850 * "arg" is advanced to the next non-white after the recognized expression.
3851 *
3852 * Return OK or FAIL.
3853 */
3854 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 int evaluate;
3859{
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 long result;
3862 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
3865 /*
3866 * Get the first variable.
3867 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return FAIL;
3870
3871 /*
3872 * Repeat until there is no following "&&".
3873 */
3874 first = TRUE;
3875 result = TRUE;
3876 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3877 {
3878 if (evaluate && first)
3879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 if (error)
3884 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 first = FALSE;
3886 }
3887
3888 /*
3889 * Get the second variable.
3890 */
3891 *arg = skipwhite(*arg + 2);
3892 if (eval4(arg, &var2, evaluate && result) == FAIL)
3893 return FAIL;
3894
3895 /*
3896 * Compute the result.
3897 */
3898 if (evaluate && result)
3899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003902 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 if (error)
3904 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906 if (evaluate)
3907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 rettv->v_type = VAR_NUMBER;
3909 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 }
3912
3913 return OK;
3914}
3915
3916/*
3917 * Handle third level expression:
3918 * var1 == var2
3919 * var1 =~ var2
3920 * var1 != var2
3921 * var1 !~ var2
3922 * var1 > var2
3923 * var1 >= var2
3924 * var1 < var2
3925 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003926 * var1 is var2
3927 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 *
3929 * "arg" must point to the first non-white of the expression.
3930 * "arg" is advanced to the next non-white after the recognized expression.
3931 *
3932 * Return OK or FAIL.
3933 */
3934 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003935eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 int evaluate;
3939{
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 char_u *p;
3942 int i;
3943 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003944 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 int len = 2;
3946 long n1, n2;
3947 char_u *s1, *s2;
3948 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3949 regmatch_T regmatch;
3950 int ic;
3951 char_u *save_cpo;
3952
3953 /*
3954 * Get the first variable.
3955 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return FAIL;
3958
3959 p = *arg;
3960 switch (p[0])
3961 {
3962 case '=': if (p[1] == '=')
3963 type = TYPE_EQUAL;
3964 else if (p[1] == '~')
3965 type = TYPE_MATCH;
3966 break;
3967 case '!': if (p[1] == '=')
3968 type = TYPE_NEQUAL;
3969 else if (p[1] == '~')
3970 type = TYPE_NOMATCH;
3971 break;
3972 case '>': if (p[1] != '=')
3973 {
3974 type = TYPE_GREATER;
3975 len = 1;
3976 }
3977 else
3978 type = TYPE_GEQUAL;
3979 break;
3980 case '<': if (p[1] != '=')
3981 {
3982 type = TYPE_SMALLER;
3983 len = 1;
3984 }
3985 else
3986 type = TYPE_SEQUAL;
3987 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 case 'i': if (p[1] == 's')
3989 {
3990 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3991 len = 5;
3992 if (!vim_isIDc(p[len]))
3993 {
3994 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3995 type_is = TRUE;
3996 }
3997 }
3998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000
4001 /*
4002 * If there is a comparitive operator, use it.
4003 */
4004 if (type != TYPE_UNKNOWN)
4005 {
4006 /* extra question mark appended: ignore case */
4007 if (p[len] == '?')
4008 {
4009 ic = TRUE;
4010 ++len;
4011 }
4012 /* extra '#' appended: match case */
4013 else if (p[len] == '#')
4014 {
4015 ic = FALSE;
4016 ++len;
4017 }
4018 /* nothing appened: use 'ignorecase' */
4019 else
4020 ic = p_ic;
4021
4022 /*
4023 * Get the second variable.
4024 */
4025 *arg = skipwhite(p + len);
4026 if (eval5(arg, &var2, evaluate) == FAIL)
4027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030 }
4031
4032 if (evaluate)
4033 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004034 if (type_is && rettv->v_type != var2.v_type)
4035 {
4036 /* For "is" a different type always means FALSE, for "notis"
4037 * it means TRUE. */
4038 n1 = (type == TYPE_NEQUAL);
4039 }
4040 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4041 {
4042 if (type_is)
4043 {
4044 n1 = (rettv->v_type == var2.v_type
4045 && rettv->vval.v_list == var2.vval.v_list);
4046 if (type == TYPE_NEQUAL)
4047 n1 = !n1;
4048 }
4049 else if (rettv->v_type != var2.v_type
4050 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4051 {
4052 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004053 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004054 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004055 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004056 clear_tv(rettv);
4057 clear_tv(&var2);
4058 return FAIL;
4059 }
4060 else
4061 {
4062 /* Compare two Lists for being equal or unequal. */
4063 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4064 if (type == TYPE_NEQUAL)
4065 n1 = !n1;
4066 }
4067 }
4068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004069 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4070 {
4071 if (type_is)
4072 {
4073 n1 = (rettv->v_type == var2.v_type
4074 && rettv->vval.v_dict == var2.vval.v_dict);
4075 if (type == TYPE_NEQUAL)
4076 n1 = !n1;
4077 }
4078 else if (rettv->v_type != var2.v_type
4079 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4080 {
4081 if (rettv->v_type != var2.v_type)
4082 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4083 else
4084 EMSG(_("E736: Invalid operation for Dictionary"));
4085 clear_tv(rettv);
4086 clear_tv(&var2);
4087 return FAIL;
4088 }
4089 else
4090 {
4091 /* Compare two Dictionaries for being equal or unequal. */
4092 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4093 if (type == TYPE_NEQUAL)
4094 n1 = !n1;
4095 }
4096 }
4097
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004098 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4099 {
4100 if (rettv->v_type != var2.v_type
4101 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4102 {
4103 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004104 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004105 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004106 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004107 clear_tv(rettv);
4108 clear_tv(&var2);
4109 return FAIL;
4110 }
4111 else
4112 {
4113 /* Compare two Funcrefs for being equal or unequal. */
4114 if (rettv->vval.v_string == NULL
4115 || var2.vval.v_string == NULL)
4116 n1 = FALSE;
4117 else
4118 n1 = STRCMP(rettv->vval.v_string,
4119 var2.vval.v_string) == 0;
4120 if (type == TYPE_NEQUAL)
4121 n1 = !n1;
4122 }
4123 }
4124
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 /*
4126 * If one of the two variables is a number, compare as a number.
4127 * When using "=~" or "!~", always compare as string.
4128 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004129 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 n1 = get_tv_number(rettv);
4133 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 switch (type)
4135 {
4136 case TYPE_EQUAL: n1 = (n1 == n2); break;
4137 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4138 case TYPE_GREATER: n1 = (n1 > n2); break;
4139 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4140 case TYPE_SMALLER: n1 = (n1 < n2); break;
4141 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4142 case TYPE_UNKNOWN:
4143 case TYPE_MATCH:
4144 case TYPE_NOMATCH: break; /* avoid gcc warning */
4145 }
4146 }
4147 else
4148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149 s1 = get_tv_string_buf(rettv, buf1);
4150 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4152 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4153 else
4154 i = 0;
4155 n1 = FALSE;
4156 switch (type)
4157 {
4158 case TYPE_EQUAL: n1 = (i == 0); break;
4159 case TYPE_NEQUAL: n1 = (i != 0); break;
4160 case TYPE_GREATER: n1 = (i > 0); break;
4161 case TYPE_GEQUAL: n1 = (i >= 0); break;
4162 case TYPE_SMALLER: n1 = (i < 0); break;
4163 case TYPE_SEQUAL: n1 = (i <= 0); break;
4164
4165 case TYPE_MATCH:
4166 case TYPE_NOMATCH:
4167 /* avoid 'l' flag in 'cpoptions' */
4168 save_cpo = p_cpo;
4169 p_cpo = (char_u *)"";
4170 regmatch.regprog = vim_regcomp(s2,
4171 RE_MAGIC + RE_STRING);
4172 regmatch.rm_ic = ic;
4173 if (regmatch.regprog != NULL)
4174 {
4175 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4176 vim_free(regmatch.regprog);
4177 if (type == TYPE_NOMATCH)
4178 n1 = !n1;
4179 }
4180 p_cpo = save_cpo;
4181 break;
4182
4183 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4184 }
4185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
4187 clear_tv(&var2);
4188 rettv->v_type = VAR_NUMBER;
4189 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 }
4192
4193 return OK;
4194}
4195
4196/*
4197 * Handle fourth level expression:
4198 * + number addition
4199 * - number subtraction
4200 * . string concatenation
4201 *
4202 * "arg" must point to the first non-white of the expression.
4203 * "arg" is advanced to the next non-white after the recognized expression.
4204 *
4205 * Return OK or FAIL.
4206 */
4207 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 int evaluate;
4212{
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T var2;
4214 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 int op;
4216 long n1, n2;
4217 char_u *s1, *s2;
4218 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4219 char_u *p;
4220
4221 /*
4222 * Get the first variable.
4223 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return FAIL;
4226
4227 /*
4228 * Repeat computing, until no '+', '-' or '.' is following.
4229 */
4230 for (;;)
4231 {
4232 op = **arg;
4233 if (op != '+' && op != '-' && op != '.')
4234 break;
4235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 if (op != '+' || rettv->v_type != VAR_LIST)
4237 {
4238 /* For "list + ...", an illegal use of the first operand as
4239 * a number cannot be determined before evaluating the 2nd
4240 * operand: if this is also a list, all is ok.
4241 * For "something . ...", "something - ..." or "non-list + ...",
4242 * we know that the first operand needs to be a string or number
4243 * without evaluating the 2nd operand. So check before to avoid
4244 * side effects after an error. */
4245 if (evaluate && get_tv_string_chk(rettv) == NULL)
4246 {
4247 clear_tv(rettv);
4248 return FAIL;
4249 }
4250 }
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 /*
4253 * Get the second variable.
4254 */
4255 *arg = skipwhite(*arg + 1);
4256 if (eval6(arg, &var2, evaluate) == FAIL)
4257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260 }
4261
4262 if (evaluate)
4263 {
4264 /*
4265 * Compute the result.
4266 */
4267 if (op == '.')
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4270 s2 = get_tv_string_buf_chk(&var2, buf2);
4271 if (s2 == NULL) /* type error ? */
4272 {
4273 clear_tv(rettv);
4274 clear_tv(&var2);
4275 return FAIL;
4276 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004277 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(rettv);
4279 rettv->v_type = VAR_STRING;
4280 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004282 else if (op == '+' && rettv->v_type == VAR_LIST
4283 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 {
4285 /* concatenate Lists */
4286 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4287 &var3) == FAIL)
4288 {
4289 clear_tv(rettv);
4290 clear_tv(&var2);
4291 return FAIL;
4292 }
4293 clear_tv(rettv);
4294 *rettv = var3;
4295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 else
4297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 int error = FALSE;
4299
4300 n1 = get_tv_number_chk(rettv, &error);
4301 if (error)
4302 {
4303 /* This can only happen for "list + non-list".
4304 * For "non-list + ..." or "something - ...", we returned
4305 * before evaluating the 2nd operand. */
4306 clear_tv(rettv);
4307 return FAIL;
4308 }
4309 n2 = get_tv_number_chk(&var2, &error);
4310 if (error)
4311 {
4312 clear_tv(rettv);
4313 clear_tv(&var2);
4314 return FAIL;
4315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (op == '+')
4318 n1 = n1 + n2;
4319 else
4320 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 rettv->v_type = VAR_NUMBER;
4322 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 }
4327 return OK;
4328}
4329
4330/*
4331 * Handle fifth level expression:
4332 * * number multiplication
4333 * / number division
4334 * % number modulo
4335 *
4336 * "arg" must point to the first non-white of the expression.
4337 * "arg" is advanced to the next non-white after the recognized expression.
4338 *
4339 * Return OK or FAIL.
4340 */
4341 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int evaluate;
4346{
Bram Moolenaar33570922005-01-25 22:26:29 +00004347 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 int op;
4349 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351
4352 /*
4353 * Get the first variable.
4354 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 return FAIL;
4357
4358 /*
4359 * Repeat computing, until no '*', '/' or '%' is following.
4360 */
4361 for (;;)
4362 {
4363 op = **arg;
4364 if (op != '*' && op != '/' && op != '%')
4365 break;
4366
4367 if (evaluate)
4368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004369 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004371 if (error)
4372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 else
4375 n1 = 0;
4376
4377 /*
4378 * Get the second variable.
4379 */
4380 *arg = skipwhite(*arg + 1);
4381 if (eval7(arg, &var2, evaluate) == FAIL)
4382 return FAIL;
4383
4384 if (evaluate)
4385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 if (error)
4389 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /*
4392 * Compute the result.
4393 */
4394 if (op == '*')
4395 n1 = n1 * n2;
4396 else if (op == '/')
4397 {
4398 if (n2 == 0) /* give an error message? */
4399 n1 = 0x7fffffffL;
4400 else
4401 n1 = n1 / n2;
4402 }
4403 else
4404 {
4405 if (n2 == 0) /* give an error message? */
4406 n1 = 0;
4407 else
4408 n1 = n1 % n2;
4409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 rettv->v_type = VAR_NUMBER;
4411 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 }
4414
4415 return OK;
4416}
4417
4418/*
4419 * Handle sixth level expression:
4420 * number number constant
4421 * "string" string contstant
4422 * 'string' literal string contstant
4423 * &option-name option value
4424 * @r register contents
4425 * identifier variable value
4426 * function() function call
4427 * $VAR environment variable
4428 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004429 * [expr, expr] List
4430 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 *
4432 * Also handle:
4433 * ! in front logical NOT
4434 * - in front unary minus
4435 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 * trailing [] subscript in String or List
4437 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 *
4439 * "arg" must point to the first non-white of the expression.
4440 * "arg" is advanced to the next non-white after the recognized expression.
4441 *
4442 * Return OK or FAIL.
4443 */
4444 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 int evaluate;
4449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 long n;
4451 int len;
4452 char_u *s;
4453 int val;
4454 char_u *start_leader, *end_leader;
4455 int ret = OK;
4456 char_u *alias;
4457
4458 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 /*
4465 * Skip '!' and '-' characters. They are handled later.
4466 */
4467 start_leader = *arg;
4468 while (**arg == '!' || **arg == '-' || **arg == '+')
4469 *arg = skipwhite(*arg + 1);
4470 end_leader = *arg;
4471
4472 switch (**arg)
4473 {
4474 /*
4475 * Number constant.
4476 */
4477 case '0':
4478 case '1':
4479 case '2':
4480 case '3':
4481 case '4':
4482 case '5':
4483 case '6':
4484 case '7':
4485 case '8':
4486 case '9':
4487 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4488 *arg += len;
4489 if (evaluate)
4490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004491 rettv->v_type = VAR_NUMBER;
4492 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 break;
4495
4496 /*
4497 * String constant: "string".
4498 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 break;
4501
4502 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 break;
4507
4508 /*
4509 * List: [expr, expr]
4510 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004511 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 break;
4513
4514 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515 * Dictionary: {key: val, key: val}
4516 */
4517 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4518 break;
4519
4520 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004521 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004523 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 break;
4525
4526 /*
4527 * Environment variable: $VAR.
4528 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 break;
4531
4532 /*
4533 * Register contents: @r.
4534 */
4535 case '@': ++*arg;
4536 if (evaluate)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004539 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541 if (**arg != NUL)
4542 ++*arg;
4543 break;
4544
4545 /*
4546 * nested expression: (expression).
4547 */
4548 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (**arg == ')')
4551 ++*arg;
4552 else if (ret == OK)
4553 {
4554 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 ret = FAIL;
4557 }
4558 break;
4559
Bram Moolenaar8c711452005-01-14 21:53:12 +00004560 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 break;
4562 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563
4564 if (ret == NOTDONE)
4565 {
4566 /*
4567 * Must be a variable or function name.
4568 * Can also be a curly-braces kind of name: {expr}.
4569 */
4570 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004571 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 if (alias != NULL)
4573 s = alias;
4574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004575 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004576 ret = FAIL;
4577 else
4578 {
4579 if (**arg == '(') /* recursive! */
4580 {
4581 /* If "s" is the name of a variable of type VAR_FUNC
4582 * use its contents. */
4583 s = deref_func_name(s, &len);
4584
4585 /* Invoke the function. */
4586 ret = get_func_tv(s, len, rettv, arg,
4587 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004588 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004589 /* Stop the expression evaluation when immediately
4590 * aborting on error, or when an interrupt occurred or
4591 * an exception was thrown but not caught. */
4592 if (aborting())
4593 {
4594 if (ret == OK)
4595 clear_tv(rettv);
4596 ret = FAIL;
4597 }
4598 }
4599 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004600 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004601 else
4602 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 }
4604
4605 if (alias != NULL)
4606 vim_free(alias);
4607 }
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 *arg = skipwhite(*arg);
4610
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004611 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4612 * expr(expr). */
4613 if (ret == OK)
4614 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 /*
4617 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4618 */
4619 if (ret == OK && evaluate && end_leader > start_leader)
4620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004621 int error = FALSE;
4622
4623 val = get_tv_number_chk(rettv, &error);
4624 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 clear_tv(rettv);
4627 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 else
4630 {
4631 while (end_leader > start_leader)
4632 {
4633 --end_leader;
4634 if (*end_leader == '!')
4635 val = !val;
4636 else if (*end_leader == '-')
4637 val = -val;
4638 }
4639 clear_tv(rettv);
4640 rettv->v_type = VAR_NUMBER;
4641 rettv->vval.v_number = val;
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644
4645 return ret;
4646}
4647
4648/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 * Evaluate an "[expr]" or "[expr:expr]" index.
4650 * "*arg" points to the '['.
4651 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4652 */
4653 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004654eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004656 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004658 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659{
4660 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 long len = -1;
4664 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004670 if (verbose)
4671 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672 return FAIL;
4673 }
4674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 /*
4678 * dict.name
4679 */
4680 key = *arg + 1;
4681 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4682 ;
4683 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 }
4687 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 /*
4690 * something[idx]
4691 *
4692 * Get the (first) variable from inside the [].
4693 */
4694 *arg = skipwhite(*arg + 1);
4695 if (**arg == ':')
4696 empty1 = TRUE;
4697 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4698 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4700 {
4701 /* not a number or string */
4702 clear_tv(&var1);
4703 return FAIL;
4704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705
4706 /*
4707 * Get the second variable from inside the [:].
4708 */
4709 if (**arg == ':')
4710 {
4711 range = TRUE;
4712 *arg = skipwhite(*arg + 1);
4713 if (**arg == ']')
4714 empty2 = TRUE;
4715 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 if (!empty1)
4718 clear_tv(&var1);
4719 return FAIL;
4720 }
4721 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4722 {
4723 /* not a number or string */
4724 if (!empty1)
4725 clear_tv(&var1);
4726 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727 return FAIL;
4728 }
4729 }
4730
4731 /* Check for the ']'. */
4732 if (**arg != ']')
4733 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004734 if (verbose)
4735 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 clear_tv(&var1);
4737 if (range)
4738 clear_tv(&var2);
4739 return FAIL;
4740 }
4741 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 }
4743
4744 if (evaluate)
4745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 n1 = 0;
4747 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 n1 = get_tv_number(&var1);
4750 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 }
4752 if (range)
4753 {
4754 if (empty2)
4755 n2 = -1;
4756 else
4757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 n2 = get_tv_number(&var2);
4759 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 }
4761 }
4762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 {
4765 case VAR_NUMBER:
4766 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 len = (long)STRLEN(s);
4769 if (range)
4770 {
4771 /* The resulting variable is a substring. If the indexes
4772 * are out of range the result is empty. */
4773 if (n1 < 0)
4774 {
4775 n1 = len + n1;
4776 if (n1 < 0)
4777 n1 = 0;
4778 }
4779 if (n2 < 0)
4780 n2 = len + n2;
4781 else if (n2 >= len)
4782 n2 = len;
4783 if (n1 >= len || n2 < 0 || n1 > n2)
4784 s = NULL;
4785 else
4786 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4787 }
4788 else
4789 {
4790 /* The resulting variable is a string of a single
4791 * character. If the index is too big or negative the
4792 * result is empty. */
4793 if (n1 >= len || n1 < 0)
4794 s = NULL;
4795 else
4796 s = vim_strnsave(s + n1, 1);
4797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 clear_tv(rettv);
4799 rettv->v_type = VAR_STRING;
4800 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 break;
4802
4803 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 if (n1 < 0)
4806 n1 = len + n1;
4807 if (!empty1 && (n1 < 0 || n1 >= len))
4808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004809 if (verbose)
4810 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 return FAIL;
4812 }
4813 if (range)
4814 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004815 list_T *l;
4816 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817
4818 if (n2 < 0)
4819 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004822 if (verbose)
4823 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 return FAIL;
4825 }
4826 l = list_alloc();
4827 if (l == NULL)
4828 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 n1 <= n2; ++n1)
4831 {
4832 if (list_append_tv(l, &item->li_tv) == FAIL)
4833 {
4834 list_free(l);
4835 return FAIL;
4836 }
4837 item = item->li_next;
4838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 clear_tv(rettv);
4840 rettv->v_type = VAR_LIST;
4841 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004842 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843 }
4844 else
4845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(rettv);
4849 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 }
4851 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852
4853 case VAR_DICT:
4854 if (range)
4855 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004856 if (verbose)
4857 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 if (len == -1)
4859 clear_tv(&var1);
4860 return FAIL;
4861 }
4862 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004863 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004864
4865 if (len == -1)
4866 {
4867 key = get_tv_string(&var1);
4868 if (*key == NUL)
4869 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004870 if (verbose)
4871 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004872 clear_tv(&var1);
4873 return FAIL;
4874 }
4875 }
4876
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004877 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004878
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004879 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004880 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881 if (len == -1)
4882 clear_tv(&var1);
4883 if (item == NULL)
4884 return FAIL;
4885
4886 copy_tv(&item->di_tv, &var1);
4887 clear_tv(rettv);
4888 *rettv = var1;
4889 }
4890 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004891 }
4892 }
4893
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 return OK;
4895}
4896
4897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 * Get an option value.
4899 * "arg" points to the '&' or '+' before the option name.
4900 * "arg" is advanced to character after the option name.
4901 * Return OK or FAIL.
4902 */
4903 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004906 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 int evaluate;
4908{
4909 char_u *option_end;
4910 long numval;
4911 char_u *stringval;
4912 int opt_type;
4913 int c;
4914 int working = (**arg == '+'); /* has("+option") */
4915 int ret = OK;
4916 int opt_flags;
4917
4918 /*
4919 * Isolate the option name and find its value.
4920 */
4921 option_end = find_option_end(arg, &opt_flags);
4922 if (option_end == NULL)
4923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 EMSG2(_("E112: Option name missing: %s"), *arg);
4926 return FAIL;
4927 }
4928
4929 if (!evaluate)
4930 {
4931 *arg = option_end;
4932 return OK;
4933 }
4934
4935 c = *option_end;
4936 *option_end = NUL;
4937 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 if (opt_type == -3) /* invalid name */
4941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 EMSG2(_("E113: Unknown option: %s"), *arg);
4944 ret = FAIL;
4945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 if (opt_type == -2) /* hidden string option */
4949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 rettv->v_type = VAR_STRING;
4951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 else if (opt_type == -1) /* hidden number option */
4954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 rettv->v_type = VAR_NUMBER;
4956 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 else if (opt_type == 1) /* number option */
4959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960 rettv->v_type = VAR_NUMBER;
4961 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else /* string option */
4964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 rettv->v_type = VAR_STRING;
4966 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 }
4969 else if (working && (opt_type == -2 || opt_type == -1))
4970 ret = FAIL;
4971
4972 *option_end = c; /* put back for error messages */
4973 *arg = option_end;
4974
4975 return ret;
4976}
4977
4978/*
4979 * Allocate a variable for a string constant.
4980 * Return OK or FAIL.
4981 */
4982 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 int evaluate;
4987{
4988 char_u *p;
4989 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 int extra = 0;
4991
4992 /*
4993 * Find the end of the string, skipping backslashed characters.
4994 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 if (*p == '\\' && p[1] != NUL)
4998 {
4999 ++p;
5000 /* A "\<x>" form occupies at least 4 characters, and produces up
5001 * to 6 characters: reserve space for 2 extra */
5002 if (*p == '<')
5003 extra += 2;
5004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006
5007 if (*p != '"')
5008 {
5009 EMSG2(_("E114: Missing quote: %s"), *arg);
5010 return FAIL;
5011 }
5012
5013 /* If only parsing, set *arg and return here */
5014 if (!evaluate)
5015 {
5016 *arg = p + 1;
5017 return OK;
5018 }
5019
5020 /*
5021 * Copy the string into allocated memory, handling backslashed
5022 * characters.
5023 */
5024 name = alloc((unsigned)(p - *arg + extra));
5025 if (name == NULL)
5026 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 rettv->v_type = VAR_STRING;
5028 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 if (*p == '\\')
5033 {
5034 switch (*++p)
5035 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 case 'b': *name++ = BS; ++p; break;
5037 case 'e': *name++ = ESC; ++p; break;
5038 case 'f': *name++ = FF; ++p; break;
5039 case 'n': *name++ = NL; ++p; break;
5040 case 'r': *name++ = CAR; ++p; break;
5041 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 case 'X': /* hex: "\x1", "\x12" */
5044 case 'x':
5045 case 'u': /* Unicode: "\u0023" */
5046 case 'U':
5047 if (vim_isxdigit(p[1]))
5048 {
5049 int n, nr;
5050 int c = toupper(*p);
5051
5052 if (c == 'X')
5053 n = 2;
5054 else
5055 n = 4;
5056 nr = 0;
5057 while (--n >= 0 && vim_isxdigit(p[1]))
5058 {
5059 ++p;
5060 nr = (nr << 4) + hex2nr(*p);
5061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063#ifdef FEAT_MBYTE
5064 /* For "\u" store the number according to
5065 * 'encoding'. */
5066 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else
5069#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073
5074 /* octal: "\1", "\12", "\123" */
5075 case '0':
5076 case '1':
5077 case '2':
5078 case '3':
5079 case '4':
5080 case '5':
5081 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 case '7': *name = *p++ - '0';
5083 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 *name = (*name << 3) + *p++ - '0';
5086 if (*p >= '0' && *p <= '7')
5087 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (extra != 0)
5095 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098 }
5099 /* FALLTHROUGH */
5100
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 }
5104 }
5105 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 *arg = p + 1;
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 return OK;
5113}
5114
5115/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 * Return OK or FAIL.
5118 */
5119 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 int evaluate;
5124{
5125 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005127 int reduce = 0;
5128
5129 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131 */
5132 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5133 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005135 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005137 break;
5138 ++reduce;
5139 ++p;
5140 }
5141 }
5142
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005146 return FAIL;
5147 }
5148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005150 if (!evaluate)
5151 {
5152 *arg = p + 1;
5153 return OK;
5154 }
5155
5156 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005158 */
5159 str = alloc((unsigned)((p - *arg) - reduce));
5160 if (str == NULL)
5161 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 rettv->v_type = VAR_STRING;
5163 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005164
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005166 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005168 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005170 break;
5171 ++p;
5172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005176 *arg = p + 1;
5177
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005178 return OK;
5179}
5180
5181/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182 * Allocate a variable for a List and fill it from "*arg".
5183 * Return OK or FAIL.
5184 */
5185 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 int evaluate;
5190{
Bram Moolenaar33570922005-01-25 22:26:29 +00005191 list_T *l = NULL;
5192 typval_T tv;
5193 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194
5195 if (evaluate)
5196 {
5197 l = list_alloc();
5198 if (l == NULL)
5199 return FAIL;
5200 }
5201
5202 *arg = skipwhite(*arg + 1);
5203 while (**arg != ']' && **arg != NUL)
5204 {
5205 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5206 goto failret;
5207 if (evaluate)
5208 {
5209 item = listitem_alloc();
5210 if (item != NULL)
5211 {
5212 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005213 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214 list_append(l, item);
5215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005216 else
5217 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218 }
5219
5220 if (**arg == ']')
5221 break;
5222 if (**arg != ',')
5223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 goto failret;
5226 }
5227 *arg = skipwhite(*arg + 1);
5228 }
5229
5230 if (**arg != ']')
5231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233failret:
5234 if (evaluate)
5235 list_free(l);
5236 return FAIL;
5237 }
5238
5239 *arg = skipwhite(*arg + 1);
5240 if (evaluate)
5241 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005242 rettv->v_type = VAR_LIST;
5243 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 ++l->lv_refcount;
5245 }
5246
5247 return OK;
5248}
5249
5250/*
5251 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005252 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005254 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255list_alloc()
5256{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257 list_T *l;
5258
5259 l = (list_T *)alloc_clear(sizeof(list_T));
5260 if (l != NULL)
5261 {
5262 /* Prepend the list to the list of lists for garbage collection. */
5263 if (first_list != NULL)
5264 first_list->lv_used_prev = l;
5265 l->lv_used_prev = NULL;
5266 l->lv_used_next = first_list;
5267 first_list = l;
5268 }
5269 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270}
5271
5272/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005273 * Allocate an empty list for a return value.
5274 * Returns OK or FAIL.
5275 */
5276 static int
5277rettv_list_alloc(rettv)
5278 typval_T *rettv;
5279{
5280 list_T *l = list_alloc();
5281
5282 if (l == NULL)
5283 return FAIL;
5284
5285 rettv->vval.v_list = l;
5286 rettv->v_type = VAR_LIST;
5287 ++l->lv_refcount;
5288 return OK;
5289}
5290
5291/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 * Unreference a list: decrement the reference count and free it when it
5293 * becomes zero.
5294 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005295 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005299 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5300 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301}
5302
5303/*
5304 * Free a list, including all items it points to.
5305 * Ignores the reference count.
5306 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005307 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310{
Bram Moolenaar33570922005-01-25 22:26:29 +00005311 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312
Bram Moolenaard9fba312005-06-26 22:34:35 +00005313 /* Avoid that recursive reference to the list frees us again. */
5314 l->lv_refcount = DEL_REFCOUNT;
5315
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005316 /* Remove the list from the list of lists for garbage collection. */
5317 if (l->lv_used_prev == NULL)
5318 first_list = l->lv_used_next;
5319 else
5320 l->lv_used_prev->lv_used_next = l->lv_used_next;
5321 if (l->lv_used_next != NULL)
5322 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5323
Bram Moolenaard9fba312005-06-26 22:34:35 +00005324 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005326 /* Remove the item before deleting it. */
5327 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 listitem_free(item);
5329 }
5330 vim_free(l);
5331}
5332
5333/*
5334 * Allocate a list item.
5335 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337listitem_alloc()
5338{
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340}
5341
5342/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005343 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 */
5345 static void
5346listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 vim_free(item);
5351}
5352
5353/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005354 * Remove a list item from a List and free it. Also clears the value.
5355 */
5356 static void
5357listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 list_T *l;
5359 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005360{
5361 list_remove(l, item, item);
5362 listitem_free(item);
5363}
5364
5365/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 * Get the number of items in a list.
5367 */
5368 static long
5369list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005370 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 if (l == NULL)
5373 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005374 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375}
5376
5377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005378 * Return TRUE when two lists have exactly the same values.
5379 */
5380 static int
5381list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005382 list_T *l1;
5383 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005384 int ic; /* ignore case for strings */
5385{
Bram Moolenaar33570922005-01-25 22:26:29 +00005386 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005387
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005388 if (list_len(l1) != list_len(l2))
5389 return FALSE;
5390
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005391 for (item1 = l1->lv_first, item2 = l2->lv_first;
5392 item1 != NULL && item2 != NULL;
5393 item1 = item1->li_next, item2 = item2->li_next)
5394 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5395 return FALSE;
5396 return item1 == NULL && item2 == NULL;
5397}
5398
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005399#if defined(FEAT_PYTHON) || defined(PROTO)
5400/*
5401 * Return the dictitem that an entry in a hashtable points to.
5402 */
5403 dictitem_T *
5404dict_lookup(hi)
5405 hashitem_T *hi;
5406{
5407 return HI2DI(hi);
5408}
5409#endif
5410
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005411/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005412 * Return TRUE when two dictionaries have exactly the same key/values.
5413 */
5414 static int
5415dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 dict_T *d1;
5417 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005418 int ic; /* ignore case for strings */
5419{
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 hashitem_T *hi;
5421 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005422 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005423
5424 if (dict_len(d1) != dict_len(d2))
5425 return FALSE;
5426
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 todo = d1->dv_hashtab.ht_used;
5428 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005430 if (!HASHITEM_EMPTY(hi))
5431 {
5432 item2 = dict_find(d2, hi->hi_key, -1);
5433 if (item2 == NULL)
5434 return FALSE;
5435 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5436 return FALSE;
5437 --todo;
5438 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005439 }
5440 return TRUE;
5441}
5442
5443/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005444 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005445 * Compares the items just like "==" would compare them, but strings and
5446 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005447 */
5448 static int
5449tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005450 typval_T *tv1;
5451 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005452 int ic; /* ignore case */
5453{
5454 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005455 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005456
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005457 if (tv1->v_type != tv2->v_type)
5458 return FALSE;
5459
5460 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005461 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005462 case VAR_LIST:
5463 /* recursive! */
5464 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5465
5466 case VAR_DICT:
5467 /* recursive! */
5468 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5469
5470 case VAR_FUNC:
5471 return (tv1->vval.v_string != NULL
5472 && tv2->vval.v_string != NULL
5473 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5474
5475 case VAR_NUMBER:
5476 return tv1->vval.v_number == tv2->vval.v_number;
5477
5478 case VAR_STRING:
5479 s1 = get_tv_string_buf(tv1, buf1);
5480 s2 = get_tv_string_buf(tv2, buf2);
5481 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005482 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005483
5484 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485 return TRUE;
5486}
5487
5488/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 * Locate item with index "n" in list "l" and return it.
5490 * A negative index is counted from the end; -1 is the last item.
5491 * Returns NULL when "n" is out of range.
5492 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005493 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 long n;
5497{
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 long idx;
5500
5501 if (l == NULL)
5502 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005503
5504 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005506 n = l->lv_len + n;
5507
5508 /* Check for index out of range. */
5509 if (n < 0 || n >= l->lv_len)
5510 return NULL;
5511
5512 /* When there is a cached index may start search from there. */
5513 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005515 if (n < l->lv_idx / 2)
5516 {
5517 /* closest to the start of the list */
5518 item = l->lv_first;
5519 idx = 0;
5520 }
5521 else if (n > (l->lv_idx + l->lv_len) / 2)
5522 {
5523 /* closest to the end of the list */
5524 item = l->lv_last;
5525 idx = l->lv_len - 1;
5526 }
5527 else
5528 {
5529 /* closest to the cached index */
5530 item = l->lv_idx_item;
5531 idx = l->lv_idx;
5532 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 }
5534 else
5535 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005536 if (n < l->lv_len / 2)
5537 {
5538 /* closest to the start of the list */
5539 item = l->lv_first;
5540 idx = 0;
5541 }
5542 else
5543 {
5544 /* closest to the end of the list */
5545 item = l->lv_last;
5546 idx = l->lv_len - 1;
5547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005549
5550 while (n > idx)
5551 {
5552 /* search forward */
5553 item = item->li_next;
5554 ++idx;
5555 }
5556 while (n < idx)
5557 {
5558 /* search backward */
5559 item = item->li_prev;
5560 --idx;
5561 }
5562
5563 /* cache the used index */
5564 l->lv_idx = idx;
5565 l->lv_idx_item = item;
5566
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 return item;
5568}
5569
5570/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005571 * Get list item "l[idx]" as a number.
5572 */
5573 static long
5574list_find_nr(l, idx, errorp)
5575 list_T *l;
5576 long idx;
5577 int *errorp; /* set to TRUE when something wrong */
5578{
5579 listitem_T *li;
5580
5581 li = list_find(l, idx);
5582 if (li == NULL)
5583 {
5584 if (errorp != NULL)
5585 *errorp = TRUE;
5586 return -1L;
5587 }
5588 return get_tv_number_chk(&li->li_tv, errorp);
5589}
5590
5591/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005592 * Locate "item" list "l" and return its index.
5593 * Returns -1 when "item" is not in the list.
5594 */
5595 static long
5596list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 list_T *l;
5598 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005599{
5600 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005602
5603 if (l == NULL)
5604 return -1;
5605 idx = 0;
5606 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5607 ++idx;
5608 if (li == NULL)
5609 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005610 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005611}
5612
5613/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 * Append item "item" to the end of list "l".
5615 */
5616 static void
5617list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 list_T *l;
5619 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620{
5621 if (l->lv_last == NULL)
5622 {
5623 /* empty list */
5624 l->lv_first = item;
5625 l->lv_last = item;
5626 item->li_prev = NULL;
5627 }
5628 else
5629 {
5630 l->lv_last->li_next = item;
5631 item->li_prev = l->lv_last;
5632 l->lv_last = item;
5633 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 item->li_next = NULL;
5636}
5637
5638/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005640 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641 */
5642 static int
5643list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 list_T *l;
5645 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005647 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648
Bram Moolenaar05159a02005-02-26 23:04:13 +00005649 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005651 copy_tv(tv, &li->li_tv);
5652 list_append(l, li);
5653 return OK;
5654}
5655
5656/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005657 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005658 * Return FAIL when out of memory.
5659 */
5660 int
5661list_append_dict(list, dict)
5662 list_T *list;
5663 dict_T *dict;
5664{
5665 listitem_T *li = listitem_alloc();
5666
5667 if (li == NULL)
5668 return FAIL;
5669 li->li_tv.v_type = VAR_DICT;
5670 li->li_tv.v_lock = 0;
5671 li->li_tv.vval.v_dict = dict;
5672 list_append(list, li);
5673 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 return OK;
5675}
5676
5677/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005678 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005679 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005680 * Returns FAIL when out of memory.
5681 */
5682 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005683list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005684 list_T *l;
5685 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005686 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005687{
5688 listitem_T *li = listitem_alloc();
5689
5690 if (li == NULL)
5691 return FAIL;
5692 list_append(l, li);
5693 li->li_tv.v_type = VAR_STRING;
5694 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005695 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5696 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005697 return FAIL;
5698 return OK;
5699}
5700
5701/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005702 * Append "n" to list "l".
5703 * Returns FAIL when out of memory.
5704 */
5705 static int
5706list_append_number(l, n)
5707 list_T *l;
5708 varnumber_T n;
5709{
5710 listitem_T *li;
5711
5712 li = listitem_alloc();
5713 if (li == NULL)
5714 return FAIL;
5715 li->li_tv.v_type = VAR_NUMBER;
5716 li->li_tv.v_lock = 0;
5717 li->li_tv.vval.v_number = n;
5718 list_append(l, li);
5719 return OK;
5720}
5721
5722/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005724 * If "item" is NULL append at the end.
5725 * Return FAIL when out of memory.
5726 */
5727 static int
5728list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 list_T *l;
5730 typval_T *tv;
5731 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005732{
Bram Moolenaar33570922005-01-25 22:26:29 +00005733 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005734
5735 if (ni == NULL)
5736 return FAIL;
5737 copy_tv(tv, &ni->li_tv);
5738 if (item == NULL)
5739 /* Append new item at end of list. */
5740 list_append(l, ni);
5741 else
5742 {
5743 /* Insert new item before existing item. */
5744 ni->li_prev = item->li_prev;
5745 ni->li_next = item;
5746 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005747 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005748 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005749 ++l->lv_idx;
5750 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005751 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005752 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005753 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005754 l->lv_idx_item = NULL;
5755 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005756 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005757 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005758 }
5759 return OK;
5760}
5761
5762/*
5763 * Extend "l1" with "l2".
5764 * If "bef" is NULL append at the end, otherwise insert before this item.
5765 * Returns FAIL when out of memory.
5766 */
5767 static int
5768list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005769 list_T *l1;
5770 list_T *l2;
5771 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005772{
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005774
5775 for (item = l2->lv_first; item != NULL; item = item->li_next)
5776 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5777 return FAIL;
5778 return OK;
5779}
5780
5781/*
5782 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5783 * Return FAIL when out of memory.
5784 */
5785 static int
5786list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 list_T *l1;
5788 list_T *l2;
5789 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005790{
Bram Moolenaar33570922005-01-25 22:26:29 +00005791 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005792
5793 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005794 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005795 if (l == NULL)
5796 return FAIL;
5797 tv->v_type = VAR_LIST;
5798 tv->vval.v_list = l;
5799
5800 /* append all items from the second list */
5801 return list_extend(l, l2, NULL);
5802}
5803
5804/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005805 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005807 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 * Returns NULL when out of memory.
5809 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005811list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005814 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815{
Bram Moolenaar33570922005-01-25 22:26:29 +00005816 list_T *copy;
5817 listitem_T *item;
5818 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819
5820 if (orig == NULL)
5821 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822
5823 copy = list_alloc();
5824 if (copy != NULL)
5825 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005826 if (copyID != 0)
5827 {
5828 /* Do this before adding the items, because one of the items may
5829 * refer back to this list. */
5830 orig->lv_copyID = copyID;
5831 orig->lv_copylist = copy;
5832 }
5833 for (item = orig->lv_first; item != NULL && !got_int;
5834 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 {
5836 ni = listitem_alloc();
5837 if (ni == NULL)
5838 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005839 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005840 {
5841 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5842 {
5843 vim_free(ni);
5844 break;
5845 }
5846 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005848 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 list_append(copy, ni);
5850 }
5851 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005852 if (item != NULL)
5853 {
5854 list_unref(copy);
5855 copy = NULL;
5856 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 }
5858
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 return copy;
5860}
5861
5862/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005864 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005867list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 list_T *l;
5869 listitem_T *item;
5870 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005871{
Bram Moolenaar33570922005-01-25 22:26:29 +00005872 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005874 /* notify watchers */
5875 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005877 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005878 list_fix_watch(l, ip);
5879 if (ip == item2)
5880 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882
5883 if (item2->li_next == NULL)
5884 l->lv_last = item->li_prev;
5885 else
5886 item2->li_next->li_prev = item->li_prev;
5887 if (item->li_prev == NULL)
5888 l->lv_first = item2->li_next;
5889 else
5890 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005891 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892}
5893
5894/*
5895 * Return an allocated string with the string representation of a list.
5896 * May return NULL.
5897 */
5898 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005899list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005901 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902{
5903 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904
5905 if (tv->vval.v_list == NULL)
5906 return NULL;
5907 ga_init2(&ga, (int)sizeof(char), 80);
5908 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005909 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005910 {
5911 vim_free(ga.ga_data);
5912 return NULL;
5913 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 ga_append(&ga, ']');
5915 ga_append(&ga, NUL);
5916 return (char_u *)ga.ga_data;
5917}
5918
5919/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 * Join list "l" into a string in "*gap", using separator "sep".
5921 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005924 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005925list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 char_u *sep;
5929 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931{
5932 int first = TRUE;
5933 char_u *tofree;
5934 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005935 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 char_u *s;
5937
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005938 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005939 {
5940 if (first)
5941 first = FALSE;
5942 else
5943 ga_concat(gap, sep);
5944
5945 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005946 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005947 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005948 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 if (s != NULL)
5950 ga_concat(gap, s);
5951 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005952 if (s == NULL)
5953 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005955 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956}
5957
5958/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 * Garbage collection for lists and dictionaries.
5960 *
5961 * We use reference counts to be able to free most items right away when they
5962 * are no longer used. But for composite items it's possible that it becomes
5963 * unused while the reference count is > 0: When there is a recursive
5964 * reference. Example:
5965 * :let l = [1, 2, 3]
5966 * :let d = {9: l}
5967 * :let l[1] = d
5968 *
5969 * Since this is quite unusual we handle this with garbage collection: every
5970 * once in a while find out which lists and dicts are not referenced from any
5971 * variable.
5972 *
5973 * Here is a good reference text about garbage collection (refers to Python
5974 * but it applies to all reference-counting mechanisms):
5975 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005976 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005977
5978/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005979 * Do garbage collection for lists and dicts.
5980 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005981 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 int
5983garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005984{
5985 dict_T *dd;
5986 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 int copyID = ++current_copyID;
5988 buf_T *buf;
5989 win_T *wp;
5990 int i;
5991 funccall_T *fc;
5992 int did_free = FALSE;
5993
5994 /*
5995 * 1. Go through all accessible variables and mark all lists and dicts
5996 * with copyID.
5997 */
5998 /* script-local variables */
5999 for (i = 1; i <= ga_scripts.ga_len; ++i)
6000 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6001
6002 /* buffer-local variables */
6003 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6004 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6005
6006 /* window-local variables */
6007 FOR_ALL_WINDOWS(wp)
6008 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6009
6010 /* global variables */
6011 set_ref_in_ht(&globvarht, copyID);
6012
6013 /* function-local variables */
6014 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6015 {
6016 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6017 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6018 }
6019
6020 /*
6021 * 2. Go through the list of dicts and free items without the copyID.
6022 */
6023 for (dd = first_dict; dd != NULL; )
6024 if (dd->dv_copyID != copyID)
6025 {
6026 dict_free(dd);
6027 did_free = TRUE;
6028
6029 /* restart, next dict may also have been freed */
6030 dd = first_dict;
6031 }
6032 else
6033 dd = dd->dv_used_next;
6034
6035 /*
6036 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006037 * But don't free a list that has a watcher (used in a for loop), these
6038 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 */
6040 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006041 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006042 {
6043 list_free(ll);
6044 did_free = TRUE;
6045
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006046 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 ll = first_list;
6048 }
6049 else
6050 ll = ll->lv_used_next;
6051
6052 return did_free;
6053}
6054
6055/*
6056 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6057 */
6058 static void
6059set_ref_in_ht(ht, copyID)
6060 hashtab_T *ht;
6061 int copyID;
6062{
6063 int todo;
6064 hashitem_T *hi;
6065
6066 todo = ht->ht_used;
6067 for (hi = ht->ht_array; todo > 0; ++hi)
6068 if (!HASHITEM_EMPTY(hi))
6069 {
6070 --todo;
6071 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6072 }
6073}
6074
6075/*
6076 * Mark all lists and dicts referenced through list "l" with "copyID".
6077 */
6078 static void
6079set_ref_in_list(l, copyID)
6080 list_T *l;
6081 int copyID;
6082{
6083 listitem_T *li;
6084
6085 for (li = l->lv_first; li != NULL; li = li->li_next)
6086 set_ref_in_item(&li->li_tv, copyID);
6087}
6088
6089/*
6090 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6091 */
6092 static void
6093set_ref_in_item(tv, copyID)
6094 typval_T *tv;
6095 int copyID;
6096{
6097 dict_T *dd;
6098 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006099
6100 switch (tv->v_type)
6101 {
6102 case VAR_DICT:
6103 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006104 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006105 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006106 /* Didn't see this dict yet. */
6107 dd->dv_copyID = copyID;
6108 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006109 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006111
6112 case VAR_LIST:
6113 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 /* Didn't see this list yet. */
6117 ll->lv_copyID = copyID;
6118 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006119 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006120 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006121 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006122 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006123}
6124
6125/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006126 * Allocate an empty header for a dictionary.
6127 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006128 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006129dict_alloc()
6130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006134 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006135 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006136 /* Add the list to the hashtable for garbage collection. */
6137 if (first_dict != NULL)
6138 first_dict->dv_used_prev = d;
6139 d->dv_used_next = first_dict;
6140 d->dv_used_prev = NULL;
6141
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006143 d->dv_lock = 0;
6144 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006145 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006146 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006148}
6149
6150/*
6151 * Unreference a Dictionary: decrement the reference count and free it when it
6152 * becomes zero.
6153 */
6154 static void
6155dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006157{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006158 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6159 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006160}
6161
6162/*
6163 * Free a Dictionary, including all items it contains.
6164 * Ignores the reference count.
6165 */
6166 static void
6167dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006168 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006169{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006170 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006172 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173
Bram Moolenaard9fba312005-06-26 22:34:35 +00006174 /* Avoid that recursive reference to the dict frees us again. */
6175 d->dv_refcount = DEL_REFCOUNT;
6176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006177 /* Remove the dict from the list of dicts for garbage collection. */
6178 if (d->dv_used_prev == NULL)
6179 first_dict = d->dv_used_next;
6180 else
6181 d->dv_used_prev->dv_used_next = d->dv_used_next;
6182 if (d->dv_used_next != NULL)
6183 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6184
6185 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006186 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 todo = d->dv_hashtab.ht_used;
6188 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006190 if (!HASHITEM_EMPTY(hi))
6191 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006192 /* Remove the item before deleting it, just in case there is
6193 * something recursive causing trouble. */
6194 di = HI2DI(hi);
6195 hash_remove(&d->dv_hashtab, hi);
6196 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 --todo;
6198 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006199 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006201 vim_free(d);
6202}
6203
6204/*
6205 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006206 * The "key" is copied to the new item.
6207 * Note that the value of the item "di_tv" still needs to be initialized!
6208 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006209 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211dictitem_alloc(key)
6212 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006213{
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215
Bram Moolenaar33570922005-01-25 22:26:29 +00006216 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006219 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006220 di->di_flags = 0;
6221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223}
6224
6225/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006226 * Make a copy of a Dictionary item.
6227 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006229dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231{
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006233
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006235 if (di != NULL)
6236 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006237 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006238 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006239 copy_tv(&org->di_tv, &di->di_tv);
6240 }
6241 return di;
6242}
6243
6244/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006245 * Remove item "item" from Dictionary "dict" and free it.
6246 */
6247 static void
6248dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 dict_T *dict;
6250 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251{
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006255 if (HASHITEM_EMPTY(hi))
6256 EMSG2(_(e_intern2), "dictitem_remove()");
6257 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006259 dictitem_free(item);
6260}
6261
6262/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 * Free a dict item. Also clears the value.
6264 */
6265 static void
6266dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006267 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006269 clear_tv(&item->di_tv);
6270 vim_free(item);
6271}
6272
6273/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6275 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006276 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277 * Returns NULL when out of memory.
6278 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006280dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006282 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006283 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006284{
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 dict_T *copy;
6286 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289
6290 if (orig == NULL)
6291 return NULL;
6292
6293 copy = dict_alloc();
6294 if (copy != NULL)
6295 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006296 if (copyID != 0)
6297 {
6298 orig->dv_copyID = copyID;
6299 orig->dv_copydict = copy;
6300 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006302 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006304 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006306 --todo;
6307
6308 di = dictitem_alloc(hi->hi_key);
6309 if (di == NULL)
6310 break;
6311 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006312 {
6313 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6314 copyID) == FAIL)
6315 {
6316 vim_free(di);
6317 break;
6318 }
6319 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 else
6321 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6322 if (dict_add(copy, di) == FAIL)
6323 {
6324 dictitem_free(di);
6325 break;
6326 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006327 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006328 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006329
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006331 if (todo > 0)
6332 {
6333 dict_unref(copy);
6334 copy = NULL;
6335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006336 }
6337
6338 return copy;
6339}
6340
6341/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 dict_T *d;
6348 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006349{
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351}
6352
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 * Add a number or string entry to dictionary "d".
6355 * When "str" is NULL use number "nr", otherwise use "str".
6356 * Returns FAIL when out of memory and when key already exists.
6357 */
6358 int
6359dict_add_nr_str(d, key, nr, str)
6360 dict_T *d;
6361 char *key;
6362 long nr;
6363 char_u *str;
6364{
6365 dictitem_T *item;
6366
6367 item = dictitem_alloc((char_u *)key);
6368 if (item == NULL)
6369 return FAIL;
6370 item->di_tv.v_lock = 0;
6371 if (str == NULL)
6372 {
6373 item->di_tv.v_type = VAR_NUMBER;
6374 item->di_tv.vval.v_number = nr;
6375 }
6376 else
6377 {
6378 item->di_tv.v_type = VAR_STRING;
6379 item->di_tv.vval.v_string = vim_strsave(str);
6380 }
6381 if (dict_add(d, item) == FAIL)
6382 {
6383 dictitem_free(item);
6384 return FAIL;
6385 }
6386 return OK;
6387}
6388
6389/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 * Get the number of items in a Dictionary.
6391 */
6392 static long
6393dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 if (d == NULL)
6397 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006399}
6400
6401/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 * Find item "key[len]" in Dictionary "d".
6403 * If "len" is negative use strlen(key).
6404 * Returns NULL when not found.
6405 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409 char_u *key;
6410 int len;
6411{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412#define AKEYLEN 200
6413 char_u buf[AKEYLEN];
6414 char_u *akey;
6415 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 if (len < 0)
6419 akey = key;
6420 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006421 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006422 tofree = akey = vim_strnsave(key, len);
6423 if (akey == NULL)
6424 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006426 else
6427 {
6428 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006429 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006430 akey = buf;
6431 }
6432
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006434 vim_free(tofree);
6435 if (HASHITEM_EMPTY(hi))
6436 return NULL;
6437 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438}
6439
6440/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006441 * Get a string item from a dictionary.
6442 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006443 * Returns NULL if the entry doesn't exist or out of memory.
6444 */
6445 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006446get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006447 dict_T *d;
6448 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006449 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006450{
6451 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006452 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006453
6454 di = dict_find(d, key, -1);
6455 if (di == NULL)
6456 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006457 s = get_tv_string(&di->di_tv);
6458 if (save && s != NULL)
6459 s = vim_strsave(s);
6460 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006461}
6462
6463/*
6464 * Get a number item from a dictionary.
6465 * Returns 0 if the entry doesn't exist or out of memory.
6466 */
6467 long
6468get_dict_number(d, key)
6469 dict_T *d;
6470 char_u *key;
6471{
6472 dictitem_T *di;
6473
6474 di = dict_find(d, key, -1);
6475 if (di == NULL)
6476 return 0;
6477 return get_tv_number(&di->di_tv);
6478}
6479
6480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006481 * Return an allocated string with the string representation of a Dictionary.
6482 * May return NULL.
6483 */
6484 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006485dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006488{
6489 garray_T ga;
6490 int first = TRUE;
6491 char_u *tofree;
6492 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006496 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 return NULL;
6500 ga_init2(&ga, (int)sizeof(char), 80);
6501 ga_append(&ga, '{');
6502
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006506 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006508 --todo;
6509
6510 if (first)
6511 first = FALSE;
6512 else
6513 ga_concat(&ga, (char_u *)", ");
6514
6515 tofree = string_quote(hi->hi_key, FALSE);
6516 if (tofree != NULL)
6517 {
6518 ga_concat(&ga, tofree);
6519 vim_free(tofree);
6520 }
6521 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006522 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006523 if (s != NULL)
6524 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 if (s == NULL)
6527 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006530 if (todo > 0)
6531 {
6532 vim_free(ga.ga_data);
6533 return NULL;
6534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535
6536 ga_append(&ga, '}');
6537 ga_append(&ga, NUL);
6538 return (char_u *)ga.ga_data;
6539}
6540
6541/*
6542 * Allocate a variable for a Dictionary and fill it from "*arg".
6543 * Return OK or FAIL. Returns NOTDONE for {expr}.
6544 */
6545 static int
6546get_dict_tv(arg, rettv, evaluate)
6547 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006549 int evaluate;
6550{
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 dict_T *d = NULL;
6552 typval_T tvkey;
6553 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006554 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006557 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558
6559 /*
6560 * First check if it's not a curly-braces thing: {expr}.
6561 * Must do this without evaluating, otherwise a function may be called
6562 * twice. Unfortunately this means we need to call eval1() twice for the
6563 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006564 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006565 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006566 if (*start != '}')
6567 {
6568 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6569 return FAIL;
6570 if (*start == '}')
6571 return NOTDONE;
6572 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573
6574 if (evaluate)
6575 {
6576 d = dict_alloc();
6577 if (d == NULL)
6578 return FAIL;
6579 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006580 tvkey.v_type = VAR_UNKNOWN;
6581 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582
6583 *arg = skipwhite(*arg + 1);
6584 while (**arg != '}' && **arg != NUL)
6585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006586 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587 goto failret;
6588 if (**arg != ':')
6589 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006590 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 goto failret;
6593 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006594 key = get_tv_string_buf_chk(&tvkey, buf);
6595 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006597 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6598 if (key != NULL)
6599 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006600 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601 goto failret;
6602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603
6604 *arg = skipwhite(*arg + 1);
6605 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6606 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006607 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608 goto failret;
6609 }
6610 if (evaluate)
6611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006612 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613 if (item != NULL)
6614 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006615 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006616 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 clear_tv(&tv);
6618 goto failret;
6619 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006620 item = dictitem_alloc(key);
6621 clear_tv(&tvkey);
6622 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006624 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006625 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006626 if (dict_add(d, item) == FAIL)
6627 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006628 }
6629 }
6630
6631 if (**arg == '}')
6632 break;
6633 if (**arg != ',')
6634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006635 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 goto failret;
6637 }
6638 *arg = skipwhite(*arg + 1);
6639 }
6640
6641 if (**arg != '}')
6642 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006643 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644failret:
6645 if (evaluate)
6646 dict_free(d);
6647 return FAIL;
6648 }
6649
6650 *arg = skipwhite(*arg + 1);
6651 if (evaluate)
6652 {
6653 rettv->v_type = VAR_DICT;
6654 rettv->vval.v_dict = d;
6655 ++d->dv_refcount;
6656 }
6657
6658 return OK;
6659}
6660
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 * Return a string with the string representation of a variable.
6663 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006665 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006666 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 * May return NULL;
6668 */
6669 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006670echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006676 static int recurse = 0;
6677 char_u *r = NULL;
6678
Bram Moolenaar33570922005-01-25 22:26:29 +00006679 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006680 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006681 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 *tofree = NULL;
6683 return NULL;
6684 }
6685 ++recurse;
6686
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 switch (tv->v_type)
6688 {
6689 case VAR_FUNC:
6690 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006691 r = tv->vval.v_string;
6692 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006695 if (tv->vval.v_list == NULL)
6696 {
6697 *tofree = NULL;
6698 r = NULL;
6699 }
6700 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6701 {
6702 *tofree = NULL;
6703 r = (char_u *)"[...]";
6704 }
6705 else
6706 {
6707 tv->vval.v_list->lv_copyID = copyID;
6708 *tofree = list2string(tv, copyID);
6709 r = *tofree;
6710 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006712
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006714 if (tv->vval.v_dict == NULL)
6715 {
6716 *tofree = NULL;
6717 r = NULL;
6718 }
6719 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6720 {
6721 *tofree = NULL;
6722 r = (char_u *)"{...}";
6723 }
6724 else
6725 {
6726 tv->vval.v_dict->dv_copyID = copyID;
6727 *tofree = dict2string(tv, copyID);
6728 r = *tofree;
6729 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006730 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732 case VAR_STRING:
6733 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006734 *tofree = NULL;
6735 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006736 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006740 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006742
6743 --recurse;
6744 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006745}
6746
6747/*
6748 * Return a string with the string representation of a variable.
6749 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6750 * "numbuf" is used for a number.
6751 * Puts quotes around strings, so that they can be parsed back by eval().
6752 * May return NULL;
6753 */
6754 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006755tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006757 char_u **tofree;
6758 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006759 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760{
6761 switch (tv->v_type)
6762 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763 case VAR_FUNC:
6764 *tofree = string_quote(tv->vval.v_string, TRUE);
6765 return *tofree;
6766 case VAR_STRING:
6767 *tofree = string_quote(tv->vval.v_string, FALSE);
6768 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006770 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006772 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006776 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777}
6778
6779/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 * Return string "str" in ' quotes, doubling ' characters.
6781 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006783 */
6784 static char_u *
6785string_quote(str, function)
6786 char_u *str;
6787 int function;
6788{
Bram Moolenaar33570922005-01-25 22:26:29 +00006789 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 char_u *p, *r, *s;
6791
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 len = (function ? 13 : 3);
6793 if (str != NULL)
6794 {
6795 len += STRLEN(str);
6796 for (p = str; *p != NUL; mb_ptr_adv(p))
6797 if (*p == '\'')
6798 ++len;
6799 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 s = r = alloc(len);
6801 if (r != NULL)
6802 {
6803 if (function)
6804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006805 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 r += 10;
6807 }
6808 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006809 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 if (str != NULL)
6811 for (p = str; *p != NUL; )
6812 {
6813 if (*p == '\'')
6814 *r++ = '\'';
6815 MB_COPY_CHAR(p, r);
6816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006818 if (function)
6819 *r++ = ')';
6820 *r++ = NUL;
6821 }
6822 return s;
6823}
6824
6825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 * Get the value of an environment variable.
6827 * "arg" is pointing to the '$'. It is advanced to after the name.
6828 * If the environment variable was not set, silently assume it is empty.
6829 * Always return OK.
6830 */
6831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006832get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 int evaluate;
6836{
6837 char_u *string = NULL;
6838 int len;
6839 int cc;
6840 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006841 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842
6843 ++*arg;
6844 name = *arg;
6845 len = get_env_len(arg);
6846 if (evaluate)
6847 {
6848 if (len != 0)
6849 {
6850 cc = name[len];
6851 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006852 /* first try vim_getenv(), fast for normal environment vars */
6853 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006855 {
6856 if (!mustfree)
6857 string = vim_strsave(string);
6858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 else
6860 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006861 if (mustfree)
6862 vim_free(string);
6863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 /* next try expanding things like $VIM and ${HOME} */
6865 string = expand_env_save(name - 1);
6866 if (string != NULL && *string == '$')
6867 {
6868 vim_free(string);
6869 string = NULL;
6870 }
6871 }
6872 name[len] = cc;
6873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006874 rettv->v_type = VAR_STRING;
6875 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
6877
6878 return OK;
6879}
6880
6881/*
6882 * Array with names and number of arguments of all internal functions
6883 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6884 */
6885static struct fst
6886{
6887 char *f_name; /* function name */
6888 char f_min_argc; /* minimal number of arguments */
6889 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006890 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006891 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892} functions[] =
6893{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006894 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {"append", 2, 2, f_append},
6896 {"argc", 0, 0, f_argc},
6897 {"argidx", 0, 0, f_argidx},
6898 {"argv", 1, 1, f_argv},
6899 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006900 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"bufexists", 1, 1, f_bufexists},
6902 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6903 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6904 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6905 {"buflisted", 1, 1, f_buflisted},
6906 {"bufloaded", 1, 1, f_bufloaded},
6907 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006908 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {"bufwinnr", 1, 1, f_bufwinnr},
6910 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006911 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006912 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006913 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"char2nr", 1, 1, f_char2nr},
6915 {"cindent", 1, 1, f_cindent},
6916 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006917#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006918 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006919 {"complete_add", 1, 1, f_complete_add},
6920 {"complete_check", 0, 0, f_complete_check},
6921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006923 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006924 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006926 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006927 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {"delete", 1, 1, f_delete},
6929 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006930 {"diff_filler", 1, 1, f_diff_filler},
6931 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006932 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006934 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {"eventhandler", 0, 0, f_eventhandler},
6936 {"executable", 1, 1, f_executable},
6937 {"exists", 1, 1, f_exists},
6938 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006939 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6941 {"filereadable", 1, 1, f_filereadable},
6942 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006943 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006944 {"finddir", 1, 3, f_finddir},
6945 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {"fnamemodify", 2, 2, f_fnamemodify},
6947 {"foldclosed", 1, 1, f_foldclosed},
6948 {"foldclosedend", 1, 1, f_foldclosedend},
6949 {"foldlevel", 1, 1, f_foldlevel},
6950 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006951 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006953 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006955 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006956 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"getbufvar", 2, 2, f_getbufvar},
6958 {"getchar", 0, 1, f_getchar},
6959 {"getcharmod", 0, 0, f_getcharmod},
6960 {"getcmdline", 0, 0, f_getcmdline},
6961 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006962 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006964 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006965 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {"getfsize", 1, 1, f_getfsize},
6967 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006968 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006969 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006970 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006971 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006972 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006973 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {"getregtype", 0, 1, f_getregtype},
6975 {"getwinposx", 0, 0, f_getwinposx},
6976 {"getwinposy", 0, 0, f_getwinposy},
6977 {"getwinvar", 2, 2, f_getwinvar},
6978 {"glob", 1, 1, f_glob},
6979 {"globpath", 2, 2, f_globpath},
6980 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00006982 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6984 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6985 {"histadd", 2, 2, f_histadd},
6986 {"histdel", 1, 2, f_histdel},
6987 {"histget", 1, 2, f_histget},
6988 {"histnr", 1, 1, f_histnr},
6989 {"hlID", 1, 1, f_hlID},
6990 {"hlexists", 1, 1, f_hlexists},
6991 {"hostname", 0, 0, f_hostname},
6992 {"iconv", 3, 3, f_iconv},
6993 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006994 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006995 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006997 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {"inputrestore", 0, 0, f_inputrestore},
6999 {"inputsave", 0, 0, f_inputsave},
7000 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007001 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007003 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007005 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007008 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {"libcall", 3, 3, f_libcall},
7010 {"libcallnr", 3, 3, f_libcallnr},
7011 {"line", 1, 1, f_line},
7012 {"line2byte", 1, 1, f_line2byte},
7013 {"lispindent", 1, 1, f_lispindent},
7014 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007015 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007016 {"maparg", 1, 3, f_maparg},
7017 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007018 {"match", 2, 4, f_match},
7019 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007020 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007021 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007022 {"max", 1, 1, f_max},
7023 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007024#ifdef vim_mkdir
7025 {"mkdir", 1, 3, f_mkdir},
7026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {"mode", 0, 0, f_mode},
7028 {"nextnonblank", 1, 1, f_nextnonblank},
7029 {"nr2char", 1, 1, f_nr2char},
7030 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007031 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007032 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007034 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {"remote_expr", 2, 3, f_remote_expr},
7036 {"remote_foreground", 1, 1, f_remote_foreground},
7037 {"remote_peek", 1, 2, f_remote_peek},
7038 {"remote_read", 1, 1, f_remote_read},
7039 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007040 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007042 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007044 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007045 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007046 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007047 {"searchpair", 3, 6, f_searchpair},
7048 {"searchpairpos", 3, 6, f_searchpairpos},
7049 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"server2client", 2, 2, f_server2client},
7051 {"serverlist", 0, 0, f_serverlist},
7052 {"setbufvar", 3, 3, f_setbufvar},
7053 {"setcmdpos", 1, 1, f_setcmdpos},
7054 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007055 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007056 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007057 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"setreg", 2, 3, f_setreg},
7059 {"setwinvar", 3, 3, f_setwinvar},
7060 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007061 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007062 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007063 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007064 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007065 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007066 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#ifdef HAVE_STRFTIME
7068 {"strftime", 1, 2, f_strftime},
7069#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007070 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007071 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"strlen", 1, 1, f_strlen},
7073 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007074 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"strtrans", 1, 1, f_strtrans},
7076 {"submatch", 1, 1, f_submatch},
7077 {"substitute", 4, 4, f_substitute},
7078 {"synID", 3, 3, f_synID},
7079 {"synIDattr", 2, 3, f_synIDattr},
7080 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007081 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007082 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007083 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007084 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007085 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007086 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007088 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"tolower", 1, 1, f_tolower},
7090 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007091 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"virtcol", 1, 1, f_virtcol},
7095 {"visualmode", 0, 1, f_visualmode},
7096 {"winbufnr", 1, 1, f_winbufnr},
7097 {"wincol", 0, 0, f_wincol},
7098 {"winheight", 1, 1, f_winheight},
7099 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007100 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007102 {"winrestview", 1, 1, f_winrestview},
7103 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007105 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106};
7107
7108#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7109
7110/*
7111 * Function given to ExpandGeneric() to obtain the list of internal
7112 * or user defined function names.
7113 */
7114 char_u *
7115get_function_name(xp, idx)
7116 expand_T *xp;
7117 int idx;
7118{
7119 static int intidx = -1;
7120 char_u *name;
7121
7122 if (idx == 0)
7123 intidx = -1;
7124 if (intidx < 0)
7125 {
7126 name = get_user_func_name(xp, idx);
7127 if (name != NULL)
7128 return name;
7129 }
7130 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7131 {
7132 STRCPY(IObuff, functions[intidx].f_name);
7133 STRCAT(IObuff, "(");
7134 if (functions[intidx].f_max_argc == 0)
7135 STRCAT(IObuff, ")");
7136 return IObuff;
7137 }
7138
7139 return NULL;
7140}
7141
7142/*
7143 * Function given to ExpandGeneric() to obtain the list of internal or
7144 * user defined variable or function names.
7145 */
7146/*ARGSUSED*/
7147 char_u *
7148get_expr_name(xp, idx)
7149 expand_T *xp;
7150 int idx;
7151{
7152 static int intidx = -1;
7153 char_u *name;
7154
7155 if (idx == 0)
7156 intidx = -1;
7157 if (intidx < 0)
7158 {
7159 name = get_function_name(xp, idx);
7160 if (name != NULL)
7161 return name;
7162 }
7163 return get_user_var_name(xp, ++intidx);
7164}
7165
7166#endif /* FEAT_CMDL_COMPL */
7167
7168/*
7169 * Find internal function in table above.
7170 * Return index, or -1 if not found
7171 */
7172 static int
7173find_internal_func(name)
7174 char_u *name; /* name of the function */
7175{
7176 int first = 0;
7177 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7178 int cmp;
7179 int x;
7180
7181 /*
7182 * Find the function name in the table. Binary search.
7183 */
7184 while (first <= last)
7185 {
7186 x = first + ((unsigned)(last - first) >> 1);
7187 cmp = STRCMP(name, functions[x].f_name);
7188 if (cmp < 0)
7189 last = x - 1;
7190 else if (cmp > 0)
7191 first = x + 1;
7192 else
7193 return x;
7194 }
7195 return -1;
7196}
7197
7198/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007199 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7200 * name it contains, otherwise return "name".
7201 */
7202 static char_u *
7203deref_func_name(name, lenp)
7204 char_u *name;
7205 int *lenp;
7206{
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007208 int cc;
7209
7210 cc = name[*lenp];
7211 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007212 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007213 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007217 {
7218 *lenp = 0;
7219 return (char_u *)""; /* just in case */
7220 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 *lenp = STRLEN(v->di_tv.vval.v_string);
7222 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007223 }
7224
7225 return name;
7226}
7227
7228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 * Allocate a variable for the result of a function.
7230 * Return OK or FAIL.
7231 */
7232 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7234 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 char_u *name; /* name of the function */
7236 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 char_u **arg; /* argument, pointing to the '(' */
7239 linenr_T firstline; /* first line of range */
7240 linenr_T lastline; /* last line of range */
7241 int *doesrange; /* return: function handled range */
7242 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 char_u *argp;
7246 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 int argcount = 0; /* number of arguments found */
7249
7250 /*
7251 * Get the arguments.
7252 */
7253 argp = *arg;
7254 while (argcount < MAX_FUNC_ARGS)
7255 {
7256 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7257 if (*argp == ')' || *argp == ',' || *argp == NUL)
7258 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7260 {
7261 ret = FAIL;
7262 break;
7263 }
7264 ++argcount;
7265 if (*argp != ',')
7266 break;
7267 }
7268 if (*argp == ')')
7269 ++argp;
7270 else
7271 ret = FAIL;
7272
7273 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007274 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 {
7278 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007279 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007281 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007285 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
7287 *arg = skipwhite(argp);
7288 return ret;
7289}
7290
7291
7292/*
7293 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007294 * Return OK when the function can't be called, FAIL otherwise.
7295 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 */
7297 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007298call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 char_u *name; /* name of the function */
7301 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 linenr_T firstline; /* first line of range */
7306 linenr_T lastline; /* last line of range */
7307 int *doesrange; /* return: function handled range */
7308 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#define ERROR_UNKNOWN 0
7313#define ERROR_TOOMANY 1
7314#define ERROR_TOOFEW 2
7315#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316#define ERROR_DICT 4
7317#define ERROR_NONE 5
7318#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 int error = ERROR_NONE;
7320 int i;
7321 int llen;
7322 ufunc_T *fp;
7323 int cc;
7324#define FLEN_FIXED 40
7325 char_u fname_buf[FLEN_FIXED + 1];
7326 char_u *fname;
7327
7328 /*
7329 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7330 * Change <SNR>123_name() to K_SNR 123_name().
7331 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7332 */
7333 cc = name[len];
7334 name[len] = NUL;
7335 llen = eval_fname_script(name);
7336 if (llen > 0)
7337 {
7338 fname_buf[0] = K_SPECIAL;
7339 fname_buf[1] = KS_EXTRA;
7340 fname_buf[2] = (int)KE_SNR;
7341 i = 3;
7342 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7343 {
7344 if (current_SID <= 0)
7345 error = ERROR_SCRIPT;
7346 else
7347 {
7348 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7349 i = (int)STRLEN(fname_buf);
7350 }
7351 }
7352 if (i + STRLEN(name + llen) < FLEN_FIXED)
7353 {
7354 STRCPY(fname_buf + i, name + llen);
7355 fname = fname_buf;
7356 }
7357 else
7358 {
7359 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7360 if (fname == NULL)
7361 error = ERROR_OTHER;
7362 else
7363 {
7364 mch_memmove(fname, fname_buf, (size_t)i);
7365 STRCPY(fname + i, name + llen);
7366 }
7367 }
7368 }
7369 else
7370 fname = name;
7371
7372 *doesrange = FALSE;
7373
7374
7375 /* execute the function if no errors detected and executing */
7376 if (evaluate && error == ERROR_NONE)
7377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 error = ERROR_UNKNOWN;
7380
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007381 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
7383 /*
7384 * User defined function.
7385 */
7386 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007387
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007389 /* Trigger FuncUndefined event, may load the function. */
7390 if (fp == NULL
7391 && apply_autocmds(EVENT_FUNCUNDEFINED,
7392 fname, fname, TRUE, NULL)
7393 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007395 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 fp = find_func(fname);
7397 }
7398#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007399 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007400 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007401 {
7402 /* loaded a package, search for the function again */
7403 fp = find_func(fname);
7404 }
7405
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (fp != NULL)
7407 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007408 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007410 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007412 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007414 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 else
7417 {
7418 /*
7419 * Call the user function.
7420 * Save and restore search patterns, script variables and
7421 * redo buffer.
7422 */
7423 save_search_patterns();
7424 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007425 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007428 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7429 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7430 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007431 /* Function was unreferenced while being used, free it
7432 * now. */
7433 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 restoreRedobuff();
7435 restore_search_patterns();
7436 error = ERROR_NONE;
7437 }
7438 }
7439 }
7440 else
7441 {
7442 /*
7443 * Find the function name in the table, call its implementation.
7444 */
7445 i = find_internal_func(fname);
7446 if (i >= 0)
7447 {
7448 if (argcount < functions[i].f_min_argc)
7449 error = ERROR_TOOFEW;
7450 else if (argcount > functions[i].f_max_argc)
7451 error = ERROR_TOOMANY;
7452 else
7453 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 error = ERROR_NONE;
7457 }
7458 }
7459 }
7460 /*
7461 * The function call (or "FuncUndefined" autocommand sequence) might
7462 * have been aborted by an error, an interrupt, or an explicitly thrown
7463 * exception that has not been caught so far. This situation can be
7464 * tested for by calling aborting(). For an error in an internal
7465 * function or for the "E132" error in call_user_func(), however, the
7466 * throw point at which the "force_abort" flag (temporarily reset by
7467 * emsg()) is normally updated has not been reached yet. We need to
7468 * update that flag first to make aborting() reliable.
7469 */
7470 update_force_abort();
7471 }
7472 if (error == ERROR_NONE)
7473 ret = OK;
7474
7475 /*
7476 * Report an error unless the argument evaluation or function call has been
7477 * cancelled due to an aborting error, an interrupt, or an exception.
7478 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 if (!aborting())
7480 {
7481 switch (error)
7482 {
7483 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007484 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 break;
7486 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007487 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 break;
7489 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007490 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 name);
7492 break;
7493 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007494 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 name);
7496 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007498 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 name);
7500 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 }
7502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
7504 name[len] = cc;
7505 if (fname != name && fname != fname_buf)
7506 vim_free(fname);
7507
7508 return ret;
7509}
7510
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007511/*
7512 * Give an error message with a function name. Handle <SNR> things.
7513 */
7514 static void
7515emsg_funcname(msg, name)
7516 char *msg;
7517 char_u *name;
7518{
7519 char_u *p;
7520
7521 if (*name == K_SPECIAL)
7522 p = concat_str((char_u *)"<SNR>", name + 3);
7523 else
7524 p = name;
7525 EMSG2(_(msg), p);
7526 if (p != name)
7527 vim_free(p);
7528}
7529
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530/*********************************************
7531 * Implementation of the built-in functions
7532 */
7533
7534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007535 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 */
7537 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007538f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 typval_T *argvars;
7540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541{
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007544 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007545 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007547 if ((l = argvars[0].vval.v_list) != NULL
7548 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7549 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007551 }
7552 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007553 EMSG(_(e_listreq));
7554}
7555
7556/*
7557 * "append(lnum, string/list)" function
7558 */
7559 static void
7560f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 typval_T *argvars;
7562 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007563{
7564 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007565 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 list_T *l = NULL;
7567 listitem_T *li = NULL;
7568 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007569 long added = 0;
7570
Bram Moolenaar0d660222005-01-07 21:51:51 +00007571 lnum = get_tv_lnum(argvars);
7572 if (lnum >= 0
7573 && lnum <= curbuf->b_ml.ml_line_count
7574 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007578 l = argvars[1].vval.v_list;
7579 if (l == NULL)
7580 return;
7581 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007583 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007584 for (;;)
7585 {
7586 if (l == NULL)
7587 tv = &argvars[1]; /* append a string */
7588 else if (li == NULL)
7589 break; /* end of list */
7590 else
7591 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592 line = get_tv_string_chk(tv);
7593 if (line == NULL) /* type error */
7594 {
7595 rettv->vval.v_number = 1; /* Failed */
7596 break;
7597 }
7598 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007599 ++added;
7600 if (l == NULL)
7601 break;
7602 li = li->li_next;
7603 }
7604
7605 appended_lines_mark(lnum, added);
7606 if (curwin->w_cursor.lnum > lnum)
7607 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007609 else
7610 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611}
7612
7613/*
7614 * "argc()" function
7615 */
7616/* ARGSUSED */
7617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 typval_T *argvars;
7620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007622 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
7624
7625/*
7626 * "argidx()" function
7627 */
7628/* ARGSUSED */
7629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *argvars;
7632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635}
7636
7637/*
7638 * "argv(nr)" function
7639 */
7640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 typval_T *argvars;
7643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
7645 int idx;
7646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 rettv->vval.v_string = NULL;
7652 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
7655/*
7656 * "browse(save, title, initdir, default)" function
7657 */
7658/* ARGSUSED */
7659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *argvars;
7662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664#ifdef FEAT_BROWSE
7665 int save;
7666 char_u *title;
7667 char_u *initdir;
7668 char_u *defname;
7669 char_u buf[NUMBUFLEN];
7670 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007673 save = get_tv_number_chk(&argvars[0], &error);
7674 title = get_tv_string_chk(&argvars[1]);
7675 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7676 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007678 if (error || title == NULL || initdir == NULL || defname == NULL)
7679 rettv->vval.v_string = NULL;
7680 else
7681 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007682 do_browse(save ? BROWSE_SAVE : 0,
7683 title, defname, NULL, initdir, NULL, curbuf);
7684#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007686#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007688}
7689
7690/*
7691 * "browsedir(title, initdir)" function
7692 */
7693/* ARGSUSED */
7694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 typval_T *argvars;
7697 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007698{
7699#ifdef FEAT_BROWSE
7700 char_u *title;
7701 char_u *initdir;
7702 char_u buf[NUMBUFLEN];
7703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007704 title = get_tv_string_chk(&argvars[0]);
7705 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007707 if (title == NULL || initdir == NULL)
7708 rettv->vval.v_string = NULL;
7709 else
7710 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007711 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717
Bram Moolenaar33570922005-01-25 22:26:29 +00007718static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720/*
7721 * Find a buffer by number or exact name.
7722 */
7723 static buf_T *
7724find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007729 if (avar->v_type == VAR_NUMBER)
7730 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007731 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007733 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007734 if (buf == NULL)
7735 {
7736 /* No full path name match, try a match with a URL or a "nofile"
7737 * buffer, these don't use the full path. */
7738 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7739 if (buf->b_fname != NULL
7740 && (path_with_url(buf->b_fname)
7741#ifdef FEAT_QUICKFIX
7742 || bt_nofile(buf)
7743#endif
7744 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007746 break;
7747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
7749 return buf;
7750}
7751
7752/*
7753 * "bufexists(expr)" function
7754 */
7755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007756f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 typval_T *argvars;
7758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761}
7762
7763/*
7764 * "buflisted(expr)" function
7765 */
7766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 typval_T *argvars;
7769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 buf_T *buf;
7772
7773 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775}
7776
7777/*
7778 * "bufloaded(expr)" function
7779 */
7780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 typval_T *argvars;
7783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 buf_T *buf;
7786
7787 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790
Bram Moolenaar33570922005-01-25 22:26:29 +00007791static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007792
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793/*
7794 * Get buffer by number or pattern.
7795 */
7796 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 int save_magic;
7802 char_u *save_cpo;
7803 buf_T *buf;
7804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 if (tv->v_type == VAR_NUMBER)
7806 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007807 if (tv->v_type != VAR_STRING)
7808 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 if (name == NULL || *name == NUL)
7810 return curbuf;
7811 if (name[0] == '$' && name[1] == NUL)
7812 return lastbuf;
7813
7814 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7815 save_magic = p_magic;
7816 p_magic = TRUE;
7817 save_cpo = p_cpo;
7818 p_cpo = (char_u *)"";
7819
7820 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7821 TRUE, FALSE));
7822
7823 p_magic = save_magic;
7824 p_cpo = save_cpo;
7825
7826 /* If not found, try expanding the name, like done for bufexists(). */
7827 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 return buf;
7831}
7832
7833/*
7834 * "bufname(expr)" function
7835 */
7836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837f_bufname(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{
7841 buf_T *buf;
7842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007843 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 buf = get_buf_tv(&argvars[0]);
7846 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 --emsg_off;
7852}
7853
7854/*
7855 * "bufnr(expr)" function
7856 */
7857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 typval_T *argvars;
7860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007863 int error = FALSE;
7864 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007869 --emsg_off;
7870
7871 /* If the buffer isn't found and the second argument is not zero create a
7872 * new buffer. */
7873 if (buf == NULL
7874 && argvars[1].v_type != VAR_UNKNOWN
7875 && get_tv_number_chk(&argvars[1], &error) != 0
7876 && !error
7877 && (name = get_tv_string_chk(&argvars[0])) != NULL
7878 && !error)
7879 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7880
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885}
7886
7887/*
7888 * "bufwinnr(nr)" function
7889 */
7890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 typval_T *argvars;
7893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895#ifdef FEAT_WINDOWS
7896 win_T *wp;
7897 int winnr = 0;
7898#endif
7899 buf_T *buf;
7900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007901 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#ifdef FEAT_WINDOWS
7905 for (wp = firstwin; wp; wp = wp->w_next)
7906 {
7907 ++winnr;
7908 if (wp->w_buffer == buf)
7909 break;
7910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914#endif
7915 --emsg_off;
7916}
7917
7918/*
7919 * "byte2line(byte)" function
7920 */
7921/*ARGSUSED*/
7922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 typval_T *argvars;
7925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929#else
7930 long boff = 0;
7931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007932 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 (linenr_T)0, &boff);
7938#endif
7939}
7940
7941/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007942 * "byteidx()" function
7943 */
7944/*ARGSUSED*/
7945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 typval_T *argvars;
7948 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007949{
7950#ifdef FEAT_MBYTE
7951 char_u *t;
7952#endif
7953 char_u *str;
7954 long idx;
7955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956 str = get_tv_string_chk(&argvars[0]);
7957 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007959 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007960 return;
7961
7962#ifdef FEAT_MBYTE
7963 t = str;
7964 for ( ; idx > 0; idx--)
7965 {
7966 if (*t == NUL) /* EOL reached */
7967 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007968 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007971#else
7972 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007974#endif
7975}
7976
7977/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007978 * "call(func, arglist)" function
7979 */
7980 static void
7981f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 typval_T *argvars;
7983 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007984{
7985 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007987 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007989 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007990 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991
7992 rettv->vval.v_number = 0;
7993 if (argvars[1].v_type != VAR_LIST)
7994 {
7995 EMSG(_(e_listreq));
7996 return;
7997 }
7998 if (argvars[1].vval.v_list == NULL)
7999 return;
8000
8001 if (argvars[0].v_type == VAR_FUNC)
8002 func = argvars[0].vval.v_string;
8003 else
8004 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008005 if (*func == NUL)
8006 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008007
Bram Moolenaare9a41262005-01-15 22:18:47 +00008008 if (argvars[2].v_type != VAR_UNKNOWN)
8009 {
8010 if (argvars[2].v_type != VAR_DICT)
8011 {
8012 EMSG(_(e_dictreq));
8013 return;
8014 }
8015 selfdict = argvars[2].vval.v_dict;
8016 }
8017
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008018 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8019 item = item->li_next)
8020 {
8021 if (argc == MAX_FUNC_ARGS)
8022 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008023 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008024 break;
8025 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008026 /* Make a copy of each argument. This is needed to be able to set
8027 * v_lock to VAR_FIXED in the copy without changing the original list.
8028 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008029 copy_tv(&item->li_tv, &argv[argc++]);
8030 }
8031
8032 if (item == NULL)
8033 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008034 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8035 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036
8037 /* Free the arguments. */
8038 while (argc > 0)
8039 clear_tv(&argv[--argc]);
8040}
8041
8042/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008043 * "changenr()" function
8044 */
8045/*ARGSUSED*/
8046 static void
8047f_changenr(argvars, rettv)
8048 typval_T *argvars;
8049 typval_T *rettv;
8050{
8051 rettv->vval.v_number = curbuf->b_u_seq_cur;
8052}
8053
8054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 * "char2nr(string)" function
8056 */
8057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008058f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 typval_T *argvars;
8060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062#ifdef FEAT_MBYTE
8063 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008064 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 else
8066#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008067 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
8070/*
8071 * "cindent(lnum)" function
8072 */
8073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 typval_T *argvars;
8076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078#ifdef FEAT_CINDENT
8079 pos_T pos;
8080 linenr_T lnum;
8081
8082 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8085 {
8086 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008087 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 curwin->w_cursor = pos;
8089 }
8090 else
8091#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
8094
8095/*
8096 * "col(string)" function
8097 */
8098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 typval_T *argvars;
8101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 colnr_T col = 0;
8104 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008105 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008107 fp = var2fpos(&argvars[0], FALSE, &fnum);
8108 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {
8110 if (fp->col == MAXCOL)
8111 {
8112 /* '> can be MAXCOL, get the length of the line then */
8113 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8114 col = STRLEN(ml_get(fp->lnum)) + 1;
8115 else
8116 col = MAXCOL;
8117 }
8118 else
8119 {
8120 col = fp->col + 1;
8121#ifdef FEAT_VIRTUALEDIT
8122 /* col(".") when the cursor is on the NUL at the end of the line
8123 * because of "coladd" can be seen as an extra column. */
8124 if (virtual_active() && fp == &curwin->w_cursor)
8125 {
8126 char_u *p = ml_get_cursor();
8127
8128 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8129 curwin->w_virtcol - curwin->w_cursor.coladd))
8130 {
8131# ifdef FEAT_MBYTE
8132 int l;
8133
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008134 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 col += l;
8136# else
8137 if (*p != NUL && p[1] == NUL)
8138 ++col;
8139# endif
8140 }
8141 }
8142#endif
8143 }
8144 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146}
8147
Bram Moolenaar572cb562005-08-05 21:35:02 +00008148#if defined(FEAT_INS_EXPAND)
8149/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008150 * "complete()" function
8151 */
8152/*ARGSUSED*/
8153 static void
8154f_complete(argvars, rettv)
8155 typval_T *argvars;
8156 typval_T *rettv;
8157{
8158 int startcol;
8159
8160 if ((State & INSERT) == 0)
8161 {
8162 EMSG(_("E785: complete() can only be used in Insert mode"));
8163 return;
8164 }
8165 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8166 {
8167 EMSG(_(e_invarg));
8168 return;
8169 }
8170
8171 startcol = get_tv_number_chk(&argvars[0], NULL);
8172 if (startcol <= 0)
8173 return;
8174
8175 set_completion(startcol - 1, argvars[1].vval.v_list);
8176}
8177
8178/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008179 * "complete_add()" function
8180 */
8181/*ARGSUSED*/
8182 static void
8183f_complete_add(argvars, rettv)
8184 typval_T *argvars;
8185 typval_T *rettv;
8186{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008187 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008188}
8189
8190/*
8191 * "complete_check()" function
8192 */
8193/*ARGSUSED*/
8194 static void
8195f_complete_check(argvars, rettv)
8196 typval_T *argvars;
8197 typval_T *rettv;
8198{
8199 int saved = RedrawingDisabled;
8200
8201 RedrawingDisabled = 0;
8202 ins_compl_check_keys(0);
8203 rettv->vval.v_number = compl_interrupted;
8204 RedrawingDisabled = saved;
8205}
8206#endif
8207
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208/*
8209 * "confirm(message, buttons[, default [, type]])" function
8210 */
8211/*ARGSUSED*/
8212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008213f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008214 typval_T *argvars;
8215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8218 char_u *message;
8219 char_u *buttons = NULL;
8220 char_u buf[NUMBUFLEN];
8221 char_u buf2[NUMBUFLEN];
8222 int def = 1;
8223 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008224 char_u *typestr;
8225 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008227 message = get_tv_string_chk(&argvars[0]);
8228 if (message == NULL)
8229 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008230 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008232 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8233 if (buttons == NULL)
8234 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008237 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008238 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008240 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8241 if (typestr == NULL)
8242 error = TRUE;
8243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008245 switch (TOUPPER_ASC(*typestr))
8246 {
8247 case 'E': type = VIM_ERROR; break;
8248 case 'Q': type = VIM_QUESTION; break;
8249 case 'I': type = VIM_INFO; break;
8250 case 'W': type = VIM_WARNING; break;
8251 case 'G': type = VIM_GENERIC; break;
8252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 }
8254 }
8255 }
8256 }
8257
8258 if (buttons == NULL || *buttons == NUL)
8259 buttons = (char_u *)_("&Ok");
8260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008261 if (error)
8262 rettv->vval.v_number = 0;
8263 else
8264 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 def, NULL);
8266#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008267 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268#endif
8269}
8270
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271/*
8272 * "copy()" function
8273 */
8274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008275f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 typval_T *argvars;
8277 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008279 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008280}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281
8282/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008283 * "count()" function
8284 */
8285 static void
8286f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008287 typval_T *argvars;
8288 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008289{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008290 long n = 0;
8291 int ic = FALSE;
8292
Bram Moolenaare9a41262005-01-15 22:18:47 +00008293 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008294 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 listitem_T *li;
8296 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008297 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008298
Bram Moolenaare9a41262005-01-15 22:18:47 +00008299 if ((l = argvars[0].vval.v_list) != NULL)
8300 {
8301 li = l->lv_first;
8302 if (argvars[2].v_type != VAR_UNKNOWN)
8303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008304 int error = FALSE;
8305
8306 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008307 if (argvars[3].v_type != VAR_UNKNOWN)
8308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008309 idx = get_tv_number_chk(&argvars[3], &error);
8310 if (!error)
8311 {
8312 li = list_find(l, idx);
8313 if (li == NULL)
8314 EMSGN(_(e_listidx), idx);
8315 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008317 if (error)
8318 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008319 }
8320
8321 for ( ; li != NULL; li = li->li_next)
8322 if (tv_equal(&li->li_tv, &argvars[1], ic))
8323 ++n;
8324 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008325 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008326 else if (argvars[0].v_type == VAR_DICT)
8327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 int todo;
8329 dict_T *d;
8330 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008331
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008332 if ((d = argvars[0].vval.v_dict) != NULL)
8333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008334 int error = FALSE;
8335
Bram Moolenaare9a41262005-01-15 22:18:47 +00008336 if (argvars[2].v_type != VAR_UNKNOWN)
8337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008338 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008339 if (argvars[3].v_type != VAR_UNKNOWN)
8340 EMSG(_(e_invarg));
8341 }
8342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008343 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008345 {
8346 if (!HASHITEM_EMPTY(hi))
8347 {
8348 --todo;
8349 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8350 ++n;
8351 }
8352 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008353 }
8354 }
8355 else
8356 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008357 rettv->vval.v_number = n;
8358}
8359
8360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8362 *
8363 * Checks the existence of a cscope connection.
8364 */
8365/*ARGSUSED*/
8366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 typval_T *argvars;
8369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371#ifdef FEAT_CSCOPE
8372 int num = 0;
8373 char_u *dbpath = NULL;
8374 char_u *prepend = NULL;
8375 char_u buf[NUMBUFLEN];
8376
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008377 if (argvars[0].v_type != VAR_UNKNOWN
8378 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 num = (int)get_tv_number(&argvars[0]);
8381 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008382 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008383 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
8385
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008386 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389#endif
8390}
8391
8392/*
8393 * "cursor(lnum, col)" function
8394 *
8395 * Moves the cursor to the specified line and column
8396 */
8397/*ARGSUSED*/
8398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008399f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008400 typval_T *argvars;
8401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008404#ifdef FEAT_VIRTUALEDIT
8405 long coladd = 0;
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
Bram Moolenaara5525202006-03-02 22:52:09 +00008408 if (argvars[1].v_type == VAR_UNKNOWN)
8409 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008410 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008411
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008412 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008413 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008414 line = pos.lnum;
8415 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008416#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008417 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008418#endif
8419 }
8420 else
8421 {
8422 line = get_tv_lnum(argvars);
8423 col = get_tv_number_chk(&argvars[1], NULL);
8424#ifdef FEAT_VIRTUALEDIT
8425 if (argvars[2].v_type != VAR_UNKNOWN)
8426 coladd = get_tv_number_chk(&argvars[2], NULL);
8427#endif
8428 }
8429 if (line < 0 || col < 0
8430#ifdef FEAT_VIRTUALEDIT
8431 || coladd < 0
8432#endif
8433 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008434 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (line > 0)
8436 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 if (col > 0)
8438 curwin->w_cursor.col = col - 1;
8439#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008440 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441#endif
8442
8443 /* Make sure the cursor is in a valid position. */
8444 check_cursor();
8445#ifdef FEAT_MBYTE
8446 /* Correct cursor for multi-byte character. */
8447 if (has_mbyte)
8448 mb_adjust_cursor();
8449#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008450
8451 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452}
8453
8454/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008455 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 */
8457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 typval_T *argvars;
8460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008462 int noref = 0;
8463
8464 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008466 if (noref < 0 || noref > 1)
8467 EMSG(_(e_invarg));
8468 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008469 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470}
8471
8472/*
8473 * "delete()" function
8474 */
8475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008477 typval_T *argvars;
8478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
8480 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484}
8485
8486/*
8487 * "did_filetype()" function
8488 */
8489/*ARGSUSED*/
8490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008491f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 typval_T *argvars;
8493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
8495#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499#endif
8500}
8501
8502/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008503 * "diff_filler()" function
8504 */
8505/*ARGSUSED*/
8506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008507f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008508 typval_T *argvars;
8509 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008510{
8511#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008513#endif
8514}
8515
8516/*
8517 * "diff_hlID()" function
8518 */
8519/*ARGSUSED*/
8520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008522 typval_T *argvars;
8523 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008524{
8525#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008527 static linenr_T prev_lnum = 0;
8528 static int changedtick = 0;
8529 static int fnum = 0;
8530 static int change_start = 0;
8531 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008532 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008533 int filler_lines;
8534 int col;
8535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008536 if (lnum < 0) /* ignore type error in {lnum} arg */
8537 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008538 if (lnum != prev_lnum
8539 || changedtick != curbuf->b_changedtick
8540 || fnum != curbuf->b_fnum)
8541 {
8542 /* New line, buffer, change: need to get the values. */
8543 filler_lines = diff_check(curwin, lnum);
8544 if (filler_lines < 0)
8545 {
8546 if (filler_lines == -1)
8547 {
8548 change_start = MAXCOL;
8549 change_end = -1;
8550 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8551 hlID = HLF_ADD; /* added line */
8552 else
8553 hlID = HLF_CHD; /* changed line */
8554 }
8555 else
8556 hlID = HLF_ADD; /* added line */
8557 }
8558 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008559 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008560 prev_lnum = lnum;
8561 changedtick = curbuf->b_changedtick;
8562 fnum = curbuf->b_fnum;
8563 }
8564
8565 if (hlID == HLF_CHD || hlID == HLF_TXD)
8566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008567 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008568 if (col >= change_start && col <= change_end)
8569 hlID = HLF_TXD; /* changed text */
8570 else
8571 hlID = HLF_CHD; /* changed line */
8572 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008573 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008574#endif
8575}
8576
8577/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008578 * "empty({expr})" function
8579 */
8580 static void
8581f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *argvars;
8583 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008584{
8585 int n;
8586
8587 switch (argvars[0].v_type)
8588 {
8589 case VAR_STRING:
8590 case VAR_FUNC:
8591 n = argvars[0].vval.v_string == NULL
8592 || *argvars[0].vval.v_string == NUL;
8593 break;
8594 case VAR_NUMBER:
8595 n = argvars[0].vval.v_number == 0;
8596 break;
8597 case VAR_LIST:
8598 n = argvars[0].vval.v_list == NULL
8599 || argvars[0].vval.v_list->lv_first == NULL;
8600 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008601 case VAR_DICT:
8602 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008604 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008605 default:
8606 EMSG2(_(e_intern2), "f_empty()");
8607 n = 0;
8608 }
8609
8610 rettv->vval.v_number = n;
8611}
8612
8613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 * "escape({string}, {chars})" function
8615 */
8616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008618 typval_T *argvars;
8619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621 char_u buf[NUMBUFLEN];
8622
Bram Moolenaar758711c2005-02-02 23:11:38 +00008623 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8624 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008625 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626}
8627
8628/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008629 * "eval()" function
8630 */
8631/*ARGSUSED*/
8632 static void
8633f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 typval_T *argvars;
8635 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008636{
8637 char_u *s;
8638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008639 s = get_tv_string_chk(&argvars[0]);
8640 if (s != NULL)
8641 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008643 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8644 {
8645 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008646 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008647 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008648 else if (*s != NUL)
8649 EMSG(_(e_trailing));
8650}
8651
8652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 * "eventhandler()" function
8654 */
8655/*ARGSUSED*/
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008661 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662}
8663
8664/*
8665 * "executable()" function
8666 */
8667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 typval_T *argvars;
8670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673}
8674
8675/*
8676 * "exists()" function
8677 */
8678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008680 typval_T *argvars;
8681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 char_u *p;
8684 char_u *name;
8685 int n = FALSE;
8686 int len = 0;
8687
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008688 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if (*p == '$') /* environment variable */
8690 {
8691 /* first try "normal" environment variables (fast) */
8692 if (mch_getenv(p + 1) != NULL)
8693 n = TRUE;
8694 else
8695 {
8696 /* try expanding things like $VIM and ${HOME} */
8697 p = expand_env_save(p);
8698 if (p != NULL && *p != '$')
8699 n = TRUE;
8700 vim_free(p);
8701 }
8702 }
8703 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 else if (*p == '*') /* internal or user defined function */
8706 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709 else if (*p == ':')
8710 {
8711 n = cmd_exists(p + 1);
8712 }
8713 else if (*p == '#')
8714 {
8715#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008716 if (p[1] == '#')
8717 n = autocmd_supported(p + 2);
8718 else
8719 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720#endif
8721 }
8722 else /* internal variable */
8723 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008724 char_u *tofree;
8725 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008727 /* get_name_len() takes care of expanding curly braces */
8728 name = p;
8729 len = get_name_len(&p, &tofree, TRUE, FALSE);
8730 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008732 if (tofree != NULL)
8733 name = tofree;
8734 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8735 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008737 /* handle d.key, l[idx], f(expr) */
8738 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8739 if (n)
8740 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 }
8742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008744 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 }
8746
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748}
8749
8750/*
8751 * "expand()" function
8752 */
8753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 typval_T *argvars;
8756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757{
8758 char_u *s;
8759 int len;
8760 char_u *errormsg;
8761 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8762 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008763 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 rettv->v_type = VAR_STRING;
8766 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 if (*s == '%' || *s == '#' || *s == '<')
8768 {
8769 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 --emsg_off;
8772 }
8773 else
8774 {
8775 /* When the optional second argument is non-zero, don't remove matches
8776 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008777 if (argvars[1].v_type != VAR_UNKNOWN
8778 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 if (!error)
8781 {
8782 ExpandInit(&xpc);
8783 xpc.xp_context = EXPAND_FILES;
8784 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8785 ExpandCleanup(&xpc);
8786 }
8787 else
8788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
8790}
8791
8792/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008793 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008794 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008795 */
8796 static void
8797f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008798 typval_T *argvars;
8799 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008800{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008801 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008802 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008803 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 list_T *l1, *l2;
8805 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008806 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008808
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 l1 = argvars[0].vval.v_list;
8810 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008811 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8812 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008813 {
8814 if (argvars[2].v_type != VAR_UNKNOWN)
8815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008816 before = get_tv_number_chk(&argvars[2], &error);
8817 if (error)
8818 return; /* type error; errmsg already given */
8819
Bram Moolenaar758711c2005-02-02 23:11:38 +00008820 if (before == l1->lv_len)
8821 item = NULL;
8822 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008823 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008824 item = list_find(l1, before);
8825 if (item == NULL)
8826 {
8827 EMSGN(_(e_listidx), before);
8828 return;
8829 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008830 }
8831 }
8832 else
8833 item = NULL;
8834 list_extend(l1, l2, item);
8835
Bram Moolenaare9a41262005-01-15 22:18:47 +00008836 copy_tv(&argvars[0], rettv);
8837 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008839 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8840 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008841 dict_T *d1, *d2;
8842 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 char_u *action;
8844 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008846 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008847
8848 d1 = argvars[0].vval.v_dict;
8849 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008850 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8851 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 {
8853 /* Check the third argument. */
8854 if (argvars[2].v_type != VAR_UNKNOWN)
8855 {
8856 static char *(av[]) = {"keep", "force", "error"};
8857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008858 action = get_tv_string_chk(&argvars[2]);
8859 if (action == NULL)
8860 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008861 for (i = 0; i < 3; ++i)
8862 if (STRCMP(action, av[i]) == 0)
8863 break;
8864 if (i == 3)
8865 {
8866 EMSGN(_(e_invarg2), action);
8867 return;
8868 }
8869 }
8870 else
8871 action = (char_u *)"force";
8872
8873 /* Go over all entries in the second dict and add them to the
8874 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008875 todo = d2->dv_hashtab.ht_used;
8876 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008878 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008879 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008880 --todo;
8881 di1 = dict_find(d1, hi2->hi_key, -1);
8882 if (di1 == NULL)
8883 {
8884 di1 = dictitem_copy(HI2DI(hi2));
8885 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8886 dictitem_free(di1);
8887 }
8888 else if (*action == 'e')
8889 {
8890 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8891 break;
8892 }
8893 else if (*action == 'f')
8894 {
8895 clear_tv(&di1->di_tv);
8896 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 }
8899 }
8900
Bram Moolenaare9a41262005-01-15 22:18:47 +00008901 copy_tv(&argvars[0], rettv);
8902 }
8903 }
8904 else
8905 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008906}
8907
8908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 * "filereadable()" function
8910 */
8911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008913 typval_T *argvars;
8914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 FILE *fd;
8917 char_u *p;
8918 int n;
8919
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8922 {
8923 n = TRUE;
8924 fclose(fd);
8925 }
8926 else
8927 n = FALSE;
8928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008929 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930}
8931
8932/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008933 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 * rights to write into.
8935 */
8936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008938 typval_T *argvars;
8939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008941 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942}
8943
Bram Moolenaar33570922005-01-25 22:26:29 +00008944static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008945
8946 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008947findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T *argvars;
8949 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008950 int dir;
8951{
8952#ifdef FEAT_SEARCHPATH
8953 char_u *fname;
8954 char_u *fresult = NULL;
8955 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8956 char_u *p;
8957 char_u pathbuf[NUMBUFLEN];
8958 int count = 1;
8959 int first = TRUE;
8960
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008962
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8966 if (p == NULL)
8967 count = -1; /* error */
8968 else
8969 {
8970 if (*p != NUL)
8971 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973 if (argvars[2].v_type != VAR_UNKNOWN)
8974 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8975 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008976 }
8977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 do
8981 {
8982 vim_free(fresult);
8983 fresult = find_file_in_path_option(first ? fname : NULL,
8984 first ? (int)STRLEN(fname) : 0,
8985 0, first, path, dir, NULL);
8986 first = FALSE;
8987 } while (--count > 0 && fresult != NULL);
8988 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008991#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008993#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008995}
8996
Bram Moolenaar33570922005-01-25 22:26:29 +00008997static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8998static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008999
9000/*
9001 * Implementation of map() and filter().
9002 */
9003 static void
9004filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009007 int map;
9008{
9009 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009010 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 listitem_T *li, *nli;
9012 list_T *l = NULL;
9013 dictitem_T *di;
9014 hashtab_T *ht;
9015 hashitem_T *hi;
9016 dict_T *d = NULL;
9017 typval_T save_val;
9018 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009020 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009021 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009022 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009023
9024 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009026 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009027 if ((l = argvars[0].vval.v_list) == NULL
9028 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009029 return;
9030 }
9031 else if (argvars[0].v_type == VAR_DICT)
9032 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009033 if ((d = argvars[0].vval.v_dict) == NULL
9034 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009035 return;
9036 }
9037 else
9038 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009039 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009040 return;
9041 }
9042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 expr = get_tv_string_buf_chk(&argvars[1], buf);
9044 /* On type errors, the preceding call has already displayed an error
9045 * message. Avoid a misleading error message for an empty string that
9046 * was not passed as argument. */
9047 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 prepare_vimvar(VV_VAL, &save_val);
9050 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009051
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009052 /* We reset "did_emsg" to be able to detect whether an error
9053 * occurred during evaluation of the expression. */
9054 save_did_emsg = did_emsg;
9055 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009057 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 prepare_vimvar(VV_KEY, &save_key);
9060 vimvars[VV_KEY].vv_type = VAR_STRING;
9061
9062 ht = &d->dv_hashtab;
9063 hash_lock(ht);
9064 todo = ht->ht_used;
9065 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 if (!HASHITEM_EMPTY(hi))
9068 {
9069 --todo;
9070 di = HI2DI(hi);
9071 if (tv_check_lock(di->di_tv.v_lock, msg))
9072 break;
9073 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009074 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009075 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009076 break;
9077 if (!map && rem)
9078 dictitem_remove(d, di);
9079 clear_tv(&vimvars[VV_KEY].vv_tv);
9080 }
9081 }
9082 hash_unlock(ht);
9083
9084 restore_vimvar(VV_KEY, &save_key);
9085 }
9086 else
9087 {
9088 for (li = l->lv_first; li != NULL; li = nli)
9089 {
9090 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009091 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009092 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009093 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009094 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009095 break;
9096 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009097 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009098 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009099 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009101 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009102
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009103 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009104 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009105
9106 copy_tv(&argvars[0], rettv);
9107}
9108
9109 static int
9110filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009112 char_u *expr;
9113 int map;
9114 int *remp;
9115{
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009117 char_u *s;
9118
Bram Moolenaar33570922005-01-25 22:26:29 +00009119 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009120 s = expr;
9121 if (eval1(&s, &rettv, TRUE) == FAIL)
9122 return FAIL;
9123 if (*s != NUL) /* check for trailing chars after expr */
9124 {
9125 EMSG2(_(e_invexpr2), s);
9126 return FAIL;
9127 }
9128 if (map)
9129 {
9130 /* map(): replace the list item value */
9131 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009132 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009133 *tv = rettv;
9134 }
9135 else
9136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 int error = FALSE;
9138
Bram Moolenaare9a41262005-01-15 22:18:47 +00009139 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009141 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 /* On type error, nothing has been removed; return FAIL to stop the
9143 * loop. The error message was given by get_tv_number_chk(). */
9144 if (error)
9145 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009146 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009147 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009148 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009149}
9150
9151/*
9152 * "filter()" function
9153 */
9154 static void
9155f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009156 typval_T *argvars;
9157 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009158{
9159 filter_map(argvars, rettv, FALSE);
9160}
9161
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009162/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009163 * "finddir({fname}[, {path}[, {count}]])" function
9164 */
9165 static void
9166f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *argvars;
9168 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009169{
9170 findfilendir(argvars, rettv, TRUE);
9171}
9172
9173/*
9174 * "findfile({fname}[, {path}[, {count}]])" function
9175 */
9176 static void
9177f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009178 typval_T *argvars;
9179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009180{
9181 findfilendir(argvars, rettv, FALSE);
9182}
9183
9184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 * "fnamemodify({fname}, {mods})" function
9186 */
9187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009189 typval_T *argvars;
9190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 char_u *fname;
9193 char_u *mods;
9194 int usedlen = 0;
9195 int len;
9196 char_u *fbuf = NULL;
9197 char_u buf[NUMBUFLEN];
9198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 fname = get_tv_string_chk(&argvars[0]);
9200 mods = get_tv_string_buf_chk(&argvars[1], buf);
9201 if (fname == NULL || mods == NULL)
9202 fname = NULL;
9203 else
9204 {
9205 len = (int)STRLEN(fname);
9206 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 vim_free(fbuf);
9215}
9216
Bram Moolenaar33570922005-01-25 22:26:29 +00009217static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
9219/*
9220 * "foldclosed()" function
9221 */
9222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009223foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009224 typval_T *argvars;
9225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 int end;
9227{
9228#ifdef FEAT_FOLDING
9229 linenr_T lnum;
9230 linenr_T first, last;
9231
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9234 {
9235 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9236 {
9237 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 return;
9242 }
9243 }
9244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009245 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246}
9247
9248/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009249 * "foldclosed()" function
9250 */
9251 static void
9252f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009253 typval_T *argvars;
9254 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009255{
9256 foldclosed_both(argvars, rettv, FALSE);
9257}
9258
9259/*
9260 * "foldclosedend()" function
9261 */
9262 static void
9263f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009264 typval_T *argvars;
9265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009266{
9267 foldclosed_both(argvars, rettv, TRUE);
9268}
9269
9270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 * "foldlevel()" function
9272 */
9273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009275 typval_T *argvars;
9276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
9278#ifdef FEAT_FOLDING
9279 linenr_T lnum;
9280
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009283 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 else
9285#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009286 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287}
9288
9289/*
9290 * "foldtext()" function
9291 */
9292/*ARGSUSED*/
9293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *argvars;
9296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298#ifdef FEAT_FOLDING
9299 linenr_T lnum;
9300 char_u *s;
9301 char_u *r;
9302 int len;
9303 char *txt;
9304#endif
9305
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->v_type = VAR_STRING;
9307 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009309 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9310 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9311 <= curbuf->b_ml.ml_line_count
9312 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 {
9314 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9316 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 {
9318 if (!linewhite(lnum))
9319 break;
9320 ++lnum;
9321 }
9322
9323 /* Find interesting text in this line. */
9324 s = skipwhite(ml_get(lnum));
9325 /* skip C comment-start */
9326 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009329 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009330 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009331 {
9332 s = skipwhite(ml_get(lnum + 1));
9333 if (*s == '*')
9334 s = skipwhite(s + 1);
9335 }
9336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 txt = _("+-%s%3ld lines: ");
9338 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009339 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 + 20 /* for %3ld */
9341 + STRLEN(s))); /* concatenated */
9342 if (r != NULL)
9343 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009344 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9345 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9346 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 len = (int)STRLEN(r);
9348 STRCAT(r, s);
9349 /* remove 'foldmarker' and 'commentstring' */
9350 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 }
9353 }
9354#endif
9355}
9356
9357/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009358 * "foldtextresult(lnum)" function
9359 */
9360/*ARGSUSED*/
9361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009365{
9366#ifdef FEAT_FOLDING
9367 linenr_T lnum;
9368 char_u *text;
9369 char_u buf[51];
9370 foldinfo_T foldinfo;
9371 int fold_count;
9372#endif
9373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->v_type = VAR_STRING;
9375 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009376#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 /* treat illegal types and illegal string values for {lnum} the same */
9379 if (lnum < 0)
9380 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009381 fold_count = foldedCount(curwin, lnum, &foldinfo);
9382 if (fold_count > 0)
9383 {
9384 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9385 &foldinfo, buf);
9386 if (text == buf)
9387 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009389 }
9390#endif
9391}
9392
9393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 * "foreground()" function
9395 */
9396/*ARGSUSED*/
9397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009399 typval_T *argvars;
9400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009402 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403#ifdef FEAT_GUI
9404 if (gui.in_use)
9405 gui_mch_set_foreground();
9406#else
9407# ifdef WIN32
9408 win32_set_foreground();
9409# endif
9410#endif
9411}
9412
9413/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009414 * "function()" function
9415 */
9416/*ARGSUSED*/
9417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009421{
9422 char_u *s;
9423
Bram Moolenaara7043832005-01-21 11:56:39 +00009424 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009426 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009427 EMSG2(_(e_invarg2), s);
9428 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009429 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009430 else
9431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 rettv->vval.v_string = vim_strsave(s);
9433 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009434 }
9435}
9436
9437/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009438 * "garbagecollect()" function
9439 */
9440/*ARGSUSED*/
9441 static void
9442f_garbagecollect(argvars, rettv)
9443 typval_T *argvars;
9444 typval_T *rettv;
9445{
9446 garbage_collect();
9447}
9448
9449/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009450 * "get()" function
9451 */
9452 static void
9453f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 typval_T *argvars;
9455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009456{
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 listitem_T *li;
9458 list_T *l;
9459 dictitem_T *di;
9460 dict_T *d;
9461 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009462
Bram Moolenaare9a41262005-01-15 22:18:47 +00009463 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009464 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009465 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 int error = FALSE;
9468
9469 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9470 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009471 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009472 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009473 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009474 else if (argvars[0].v_type == VAR_DICT)
9475 {
9476 if ((d = argvars[0].vval.v_dict) != NULL)
9477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009478 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009479 if (di != NULL)
9480 tv = &di->di_tv;
9481 }
9482 }
9483 else
9484 EMSG2(_(e_listdictarg), "get()");
9485
9486 if (tv == NULL)
9487 {
9488 if (argvars[2].v_type == VAR_UNKNOWN)
9489 rettv->vval.v_number = 0;
9490 else
9491 copy_tv(&argvars[2], rettv);
9492 }
9493 else
9494 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009495}
9496
Bram Moolenaar342337a2005-07-21 21:11:17 +00009497static 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 +00009498
9499/*
9500 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009501 * Return a range (from start to end) of lines in rettv from the specified
9502 * buffer.
9503 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009504 */
9505 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009506get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009507 buf_T *buf;
9508 linenr_T start;
9509 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009510 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009511 typval_T *rettv;
9512{
9513 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009514
Bram Moolenaar342337a2005-07-21 21:11:17 +00009515 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009516 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009517 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009518 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009519 }
9520 else
9521 rettv->vval.v_number = 0;
9522
9523 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9524 return;
9525
9526 if (!retlist)
9527 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009528 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9529 p = ml_get_buf(buf, start, FALSE);
9530 else
9531 p = (char_u *)"";
9532
9533 rettv->v_type = VAR_STRING;
9534 rettv->vval.v_string = vim_strsave(p);
9535 }
9536 else
9537 {
9538 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009539 return;
9540
9541 if (start < 1)
9542 start = 1;
9543 if (end > buf->b_ml.ml_line_count)
9544 end = buf->b_ml.ml_line_count;
9545 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009546 if (list_append_string(rettv->vval.v_list,
9547 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009548 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009549 }
9550}
9551
9552/*
9553 * "getbufline()" function
9554 */
9555 static void
9556f_getbufline(argvars, rettv)
9557 typval_T *argvars;
9558 typval_T *rettv;
9559{
9560 linenr_T lnum;
9561 linenr_T end;
9562 buf_T *buf;
9563
9564 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9565 ++emsg_off;
9566 buf = get_buf_tv(&argvars[0]);
9567 --emsg_off;
9568
Bram Moolenaar661b1822005-07-28 22:36:45 +00009569 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009570 if (argvars[2].v_type == VAR_UNKNOWN)
9571 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009572 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009573 end = get_tv_lnum_buf(&argvars[2], buf);
9574
Bram Moolenaar342337a2005-07-21 21:11:17 +00009575 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009576}
9577
Bram Moolenaar0d660222005-01-07 21:51:51 +00009578/*
9579 * "getbufvar()" function
9580 */
9581 static void
9582f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009583 typval_T *argvars;
9584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009585{
9586 buf_T *buf;
9587 buf_T *save_curbuf;
9588 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009589 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009591 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9592 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009593 ++emsg_off;
9594 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009595
9596 rettv->v_type = VAR_STRING;
9597 rettv->vval.v_string = NULL;
9598
9599 if (buf != NULL && varname != NULL)
9600 {
9601 if (*varname == '&') /* buffer-local-option */
9602 {
9603 /* set curbuf to be our buf, temporarily */
9604 save_curbuf = curbuf;
9605 curbuf = buf;
9606
9607 get_option_tv(&varname, rettv, TRUE);
9608
9609 /* restore previous notion of curbuf */
9610 curbuf = save_curbuf;
9611 }
9612 else
9613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 if (*varname == NUL)
9615 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9616 * scope prefix before the NUL byte is required by
9617 * find_var_in_ht(). */
9618 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009619 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009620 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009621 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009622 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009623 }
9624 }
9625
9626 --emsg_off;
9627}
9628
9629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 * "getchar()" function
9631 */
9632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009634 typval_T *argvars;
9635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639
9640 ++no_mapping;
9641 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009642 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 /* getchar(): blocking wait. */
9644 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 /* getchar(1): only check if char avail */
9647 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009648 else if (error || vpeekc() == NUL)
9649 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 n = 0;
9651 else
9652 /* getchar(0) and char avail: return char */
9653 n = safe_vgetc();
9654 --no_mapping;
9655 --allow_keys;
9656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (IS_SPECIAL(n) || mod_mask != 0)
9659 {
9660 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9661 int i = 0;
9662
9663 /* Turn a special key into three bytes, plus modifier. */
9664 if (mod_mask != 0)
9665 {
9666 temp[i++] = K_SPECIAL;
9667 temp[i++] = KS_MODIFIER;
9668 temp[i++] = mod_mask;
9669 }
9670 if (IS_SPECIAL(n))
9671 {
9672 temp[i++] = K_SPECIAL;
9673 temp[i++] = K_SECOND(n);
9674 temp[i++] = K_THIRD(n);
9675 }
9676#ifdef FEAT_MBYTE
9677 else if (has_mbyte)
9678 i += (*mb_char2bytes)(n, temp + i);
9679#endif
9680 else
9681 temp[i++] = n;
9682 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->v_type = VAR_STRING;
9684 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 }
9686}
9687
9688/*
9689 * "getcharmod()" function
9690 */
9691/*ARGSUSED*/
9692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009694 typval_T *argvars;
9695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698}
9699
9700/*
9701 * "getcmdline()" function
9702 */
9703/*ARGSUSED*/
9704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009706 typval_T *argvars;
9707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->v_type = VAR_STRING;
9710 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711}
9712
9713/*
9714 * "getcmdpos()" function
9715 */
9716/*ARGSUSED*/
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
9725/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009726 * "getcmdtype()" function
9727 */
9728/*ARGSUSED*/
9729 static void
9730f_getcmdtype(argvars, rettv)
9731 typval_T *argvars;
9732 typval_T *rettv;
9733{
9734 rettv->v_type = VAR_STRING;
9735 rettv->vval.v_string = alloc(2);
9736 if (rettv->vval.v_string != NULL)
9737 {
9738 rettv->vval.v_string[0] = get_cmdline_type();
9739 rettv->vval.v_string[1] = NUL;
9740 }
9741}
9742
9743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 * "getcwd()" function
9745 */
9746/*ARGSUSED*/
9747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009749 typval_T *argvars;
9750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752 char_u cwd[MAXPATHL];
9753
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 else
9758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009761 if (rettv->vval.v_string != NULL)
9762 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#endif
9764 }
9765}
9766
9767/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009768 * "getfontname()" function
9769 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009770/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009773 typval_T *argvars;
9774 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009775{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->v_type = VAR_STRING;
9777 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009778#ifdef FEAT_GUI
9779 if (gui.in_use)
9780 {
9781 GuiFont font;
9782 char_u *name = NULL;
9783
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009784 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009785 {
9786 /* Get the "Normal" font. Either the name saved by
9787 * hl_set_font_name() or from the font ID. */
9788 font = gui.norm_font;
9789 name = hl_get_font_name();
9790 }
9791 else
9792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009794 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9795 return;
9796 font = gui_mch_get_font(name, FALSE);
9797 if (font == NOFONT)
9798 return; /* Invalid font name, return empty string. */
9799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009801 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009802 gui_mch_free_font(font);
9803 }
9804#endif
9805}
9806
9807/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009808 * "getfperm({fname})" function
9809 */
9810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009812 typval_T *argvars;
9813 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009814{
9815 char_u *fname;
9816 struct stat st;
9817 char_u *perm = NULL;
9818 char_u flags[] = "rwx";
9819 int i;
9820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009824 if (mch_stat((char *)fname, &st) >= 0)
9825 {
9826 perm = vim_strsave((char_u *)"---------");
9827 if (perm != NULL)
9828 {
9829 for (i = 0; i < 9; i++)
9830 {
9831 if (st.st_mode & (1 << (8 - i)))
9832 perm[i] = flags[i % 3];
9833 }
9834 }
9835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009837}
9838
9839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 * "getfsize({fname})" function
9841 */
9842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009844 typval_T *argvars;
9845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 char_u *fname;
9848 struct stat st;
9849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009852 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
9854 if (mch_stat((char *)fname, &st) >= 0)
9855 {
9856 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 }
9861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863}
9864
9865/*
9866 * "getftime({fname})" function
9867 */
9868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009870 typval_T *argvars;
9871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873 char_u *fname;
9874 struct stat st;
9875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
9878 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882}
9883
9884/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009885 * "getftype({fname})" function
9886 */
9887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009889 typval_T *argvars;
9890 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009891{
9892 char_u *fname;
9893 struct stat st;
9894 char_u *type = NULL;
9895 char *t;
9896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009898
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009900 if (mch_lstat((char *)fname, &st) >= 0)
9901 {
9902#ifdef S_ISREG
9903 if (S_ISREG(st.st_mode))
9904 t = "file";
9905 else if (S_ISDIR(st.st_mode))
9906 t = "dir";
9907# ifdef S_ISLNK
9908 else if (S_ISLNK(st.st_mode))
9909 t = "link";
9910# endif
9911# ifdef S_ISBLK
9912 else if (S_ISBLK(st.st_mode))
9913 t = "bdev";
9914# endif
9915# ifdef S_ISCHR
9916 else if (S_ISCHR(st.st_mode))
9917 t = "cdev";
9918# endif
9919# ifdef S_ISFIFO
9920 else if (S_ISFIFO(st.st_mode))
9921 t = "fifo";
9922# endif
9923# ifdef S_ISSOCK
9924 else if (S_ISSOCK(st.st_mode))
9925 t = "fifo";
9926# endif
9927 else
9928 t = "other";
9929#else
9930# ifdef S_IFMT
9931 switch (st.st_mode & S_IFMT)
9932 {
9933 case S_IFREG: t = "file"; break;
9934 case S_IFDIR: t = "dir"; break;
9935# ifdef S_IFLNK
9936 case S_IFLNK: t = "link"; break;
9937# endif
9938# ifdef S_IFBLK
9939 case S_IFBLK: t = "bdev"; break;
9940# endif
9941# ifdef S_IFCHR
9942 case S_IFCHR: t = "cdev"; break;
9943# endif
9944# ifdef S_IFIFO
9945 case S_IFIFO: t = "fifo"; break;
9946# endif
9947# ifdef S_IFSOCK
9948 case S_IFSOCK: t = "socket"; break;
9949# endif
9950 default: t = "other";
9951 }
9952# else
9953 if (mch_isdir(fname))
9954 t = "dir";
9955 else
9956 t = "file";
9957# endif
9958#endif
9959 type = vim_strsave((char_u *)t);
9960 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009962}
9963
9964/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009965 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009966 */
9967 static void
9968f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 typval_T *argvars;
9970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009971{
9972 linenr_T lnum;
9973 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009974 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009975
9976 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009977 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009978 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009979 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009980 retlist = FALSE;
9981 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009982 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009983 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009984 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009985 retlist = TRUE;
9986 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009987
Bram Moolenaar342337a2005-07-21 21:11:17 +00009988 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009989}
9990
9991/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009992 * "getpos(string)" function
9993 */
9994 static void
9995f_getpos(argvars, rettv)
9996 typval_T *argvars;
9997 typval_T *rettv;
9998{
9999 pos_T *fp;
10000 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010001 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010002
10003 if (rettv_list_alloc(rettv) == OK)
10004 {
10005 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010006 fp = var2fpos(&argvars[0], TRUE, &fnum);
10007 if (fnum != -1)
10008 list_append_number(l, (varnumber_T)fnum);
10009 else
10010 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010011 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10012 : (varnumber_T)0);
10013 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10014 : (varnumber_T)0);
10015 list_append_number(l,
10016#ifdef FEAT_VIRTUALEDIT
10017 (fp != NULL) ? (varnumber_T)fp->coladd :
10018#endif
10019 (varnumber_T)0);
10020 }
10021 else
10022 rettv->vval.v_number = FALSE;
10023}
10024
10025/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010026 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010027 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010028/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010029 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010030f_getqflist(argvars, rettv)
10031 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010032 typval_T *rettv;
10033{
10034#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010035 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010036#endif
10037
10038 rettv->vval.v_number = FALSE;
10039#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010040 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010041 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010042 wp = NULL;
10043 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10044 {
10045 wp = find_win_by_nr(&argvars[0]);
10046 if (wp == NULL)
10047 return;
10048 }
10049
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010050 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010051 }
10052#endif
10053}
10054
10055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 * "getreg()" function
10057 */
10058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063 char_u *strregname;
10064 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010065 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010068 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010070 strregname = get_tv_string_chk(&argvars[0]);
10071 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010072 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010073 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010076 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 regname = (strregname == NULL ? '"' : *strregname);
10078 if (regname == 0)
10079 regname = '"';
10080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010082 rettv->vval.v_string = error ? NULL :
10083 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
10086/*
10087 * "getregtype()" function
10088 */
10089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010091 typval_T *argvars;
10092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
10094 char_u *strregname;
10095 int regname;
10096 char_u buf[NUMBUFLEN + 2];
10097 long reglen = 0;
10098
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010099 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 {
10101 strregname = get_tv_string_chk(&argvars[0]);
10102 if (strregname == NULL) /* type error; errmsg already given */
10103 {
10104 rettv->v_type = VAR_STRING;
10105 rettv->vval.v_string = NULL;
10106 return;
10107 }
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 else
10110 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010111 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112
10113 regname = (strregname == NULL ? '"' : *strregname);
10114 if (regname == 0)
10115 regname = '"';
10116
10117 buf[0] = NUL;
10118 buf[1] = NUL;
10119 switch (get_reg_type(regname, &reglen))
10120 {
10121 case MLINE: buf[0] = 'V'; break;
10122 case MCHAR: buf[0] = 'v'; break;
10123#ifdef FEAT_VISUAL
10124 case MBLOCK:
10125 buf[0] = Ctrl_V;
10126 sprintf((char *)buf + 1, "%ld", reglen + 1);
10127 break;
10128#endif
10129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 rettv->v_type = VAR_STRING;
10131 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132}
10133
10134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 * "getwinposx()" function
10136 */
10137/*ARGSUSED*/
10138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010140 typval_T *argvars;
10141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144#ifdef FEAT_GUI
10145 if (gui.in_use)
10146 {
10147 int x, y;
10148
10149 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 }
10152#endif
10153}
10154
10155/*
10156 * "getwinposy()" function
10157 */
10158/*ARGSUSED*/
10159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010161 typval_T *argvars;
10162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165#ifdef FEAT_GUI
10166 if (gui.in_use)
10167 {
10168 int x, y;
10169
10170 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 }
10173#endif
10174}
10175
Bram Moolenaara40058a2005-07-11 22:42:07 +000010176 static win_T *
10177find_win_by_nr(vp)
10178 typval_T *vp;
10179{
10180#ifdef FEAT_WINDOWS
10181 win_T *wp;
10182#endif
10183 int nr;
10184
10185 nr = get_tv_number_chk(vp, NULL);
10186
10187#ifdef FEAT_WINDOWS
10188 if (nr < 0)
10189 return NULL;
10190 if (nr == 0)
10191 return curwin;
10192
10193 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10194 if (--nr <= 0)
10195 break;
10196 return wp;
10197#else
10198 if (nr == 0 || nr == 1)
10199 return curwin;
10200 return NULL;
10201#endif
10202}
10203
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204/*
10205 * "getwinvar()" function
10206 */
10207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010209 typval_T *argvars;
10210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211{
10212 win_T *win, *oldcurwin;
10213 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010214 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 varname = get_tv_string_chk(&argvars[1]);
10218 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 rettv->v_type = VAR_STRING;
10221 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
10223 if (win != NULL && varname != NULL)
10224 {
10225 if (*varname == '&') /* window-local-option */
10226 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010227 /* Set curwin to be our win, temporarily. Also set curbuf, so
10228 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 oldcurwin = curwin;
10230 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010231 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010233 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234
10235 /* restore previous notion of curwin */
10236 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010237 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 }
10239 else
10240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 if (*varname == NUL)
10242 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10243 * scope prefix before the NUL byte is required by
10244 * find_var_in_ht(). */
10245 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010247 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 }
10251 }
10252
10253 --emsg_off;
10254}
10255
10256/*
10257 * "glob()" function
10258 */
10259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010261 typval_T *argvars;
10262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263{
10264 expand_T xpc;
10265
10266 ExpandInit(&xpc);
10267 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 rettv->v_type = VAR_STRING;
10269 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10271 ExpandCleanup(&xpc);
10272}
10273
10274/*
10275 * "globpath()" function
10276 */
10277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010279 typval_T *argvars;
10280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 if (file == NULL)
10287 rettv->vval.v_string = NULL;
10288 else
10289 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290}
10291
10292/*
10293 * "has()" function
10294 */
10295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010297 typval_T *argvars;
10298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
10300 int i;
10301 char_u *name;
10302 int n = FALSE;
10303 static char *(has_list[]) =
10304 {
10305#ifdef AMIGA
10306 "amiga",
10307# ifdef FEAT_ARP
10308 "arp",
10309# endif
10310#endif
10311#ifdef __BEOS__
10312 "beos",
10313#endif
10314#ifdef MSDOS
10315# ifdef DJGPP
10316 "dos32",
10317# else
10318 "dos16",
10319# endif
10320#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010321#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 "mac",
10323#endif
10324#if defined(MACOS_X_UNIX)
10325 "macunix",
10326#endif
10327#ifdef OS2
10328 "os2",
10329#endif
10330#ifdef __QNX__
10331 "qnx",
10332#endif
10333#ifdef RISCOS
10334 "riscos",
10335#endif
10336#ifdef UNIX
10337 "unix",
10338#endif
10339#ifdef VMS
10340 "vms",
10341#endif
10342#ifdef WIN16
10343 "win16",
10344#endif
10345#ifdef WIN32
10346 "win32",
10347#endif
10348#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10349 "win32unix",
10350#endif
10351#ifdef WIN64
10352 "win64",
10353#endif
10354#ifdef EBCDIC
10355 "ebcdic",
10356#endif
10357#ifndef CASE_INSENSITIVE_FILENAME
10358 "fname_case",
10359#endif
10360#ifdef FEAT_ARABIC
10361 "arabic",
10362#endif
10363#ifdef FEAT_AUTOCMD
10364 "autocmd",
10365#endif
10366#ifdef FEAT_BEVAL
10367 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010368# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10369 "balloon_multiline",
10370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371#endif
10372#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10373 "builtin_terms",
10374# ifdef ALL_BUILTIN_TCAPS
10375 "all_builtin_terms",
10376# endif
10377#endif
10378#ifdef FEAT_BYTEOFF
10379 "byte_offset",
10380#endif
10381#ifdef FEAT_CINDENT
10382 "cindent",
10383#endif
10384#ifdef FEAT_CLIENTSERVER
10385 "clientserver",
10386#endif
10387#ifdef FEAT_CLIPBOARD
10388 "clipboard",
10389#endif
10390#ifdef FEAT_CMDL_COMPL
10391 "cmdline_compl",
10392#endif
10393#ifdef FEAT_CMDHIST
10394 "cmdline_hist",
10395#endif
10396#ifdef FEAT_COMMENTS
10397 "comments",
10398#endif
10399#ifdef FEAT_CRYPT
10400 "cryptv",
10401#endif
10402#ifdef FEAT_CSCOPE
10403 "cscope",
10404#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010405#ifdef CURSOR_SHAPE
10406 "cursorshape",
10407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408#ifdef DEBUG
10409 "debug",
10410#endif
10411#ifdef FEAT_CON_DIALOG
10412 "dialog_con",
10413#endif
10414#ifdef FEAT_GUI_DIALOG
10415 "dialog_gui",
10416#endif
10417#ifdef FEAT_DIFF
10418 "diff",
10419#endif
10420#ifdef FEAT_DIGRAPHS
10421 "digraphs",
10422#endif
10423#ifdef FEAT_DND
10424 "dnd",
10425#endif
10426#ifdef FEAT_EMACS_TAGS
10427 "emacs_tags",
10428#endif
10429 "eval", /* always present, of course! */
10430#ifdef FEAT_EX_EXTRA
10431 "ex_extra",
10432#endif
10433#ifdef FEAT_SEARCH_EXTRA
10434 "extra_search",
10435#endif
10436#ifdef FEAT_FKMAP
10437 "farsi",
10438#endif
10439#ifdef FEAT_SEARCHPATH
10440 "file_in_path",
10441#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010442#if defined(UNIX) && !defined(USE_SYSTEM)
10443 "filterpipe",
10444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445#ifdef FEAT_FIND_ID
10446 "find_in_path",
10447#endif
10448#ifdef FEAT_FOLDING
10449 "folding",
10450#endif
10451#ifdef FEAT_FOOTER
10452 "footer",
10453#endif
10454#if !defined(USE_SYSTEM) && defined(UNIX)
10455 "fork",
10456#endif
10457#ifdef FEAT_GETTEXT
10458 "gettext",
10459#endif
10460#ifdef FEAT_GUI
10461 "gui",
10462#endif
10463#ifdef FEAT_GUI_ATHENA
10464# ifdef FEAT_GUI_NEXTAW
10465 "gui_neXtaw",
10466# else
10467 "gui_athena",
10468# endif
10469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470#ifdef FEAT_GUI_GTK
10471 "gui_gtk",
10472# ifdef HAVE_GTK2
10473 "gui_gtk2",
10474# endif
10475#endif
10476#ifdef FEAT_GUI_MAC
10477 "gui_mac",
10478#endif
10479#ifdef FEAT_GUI_MOTIF
10480 "gui_motif",
10481#endif
10482#ifdef FEAT_GUI_PHOTON
10483 "gui_photon",
10484#endif
10485#ifdef FEAT_GUI_W16
10486 "gui_win16",
10487#endif
10488#ifdef FEAT_GUI_W32
10489 "gui_win32",
10490#endif
10491#ifdef FEAT_HANGULIN
10492 "hangul_input",
10493#endif
10494#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10495 "iconv",
10496#endif
10497#ifdef FEAT_INS_EXPAND
10498 "insert_expand",
10499#endif
10500#ifdef FEAT_JUMPLIST
10501 "jumplist",
10502#endif
10503#ifdef FEAT_KEYMAP
10504 "keymap",
10505#endif
10506#ifdef FEAT_LANGMAP
10507 "langmap",
10508#endif
10509#ifdef FEAT_LIBCALL
10510 "libcall",
10511#endif
10512#ifdef FEAT_LINEBREAK
10513 "linebreak",
10514#endif
10515#ifdef FEAT_LISP
10516 "lispindent",
10517#endif
10518#ifdef FEAT_LISTCMDS
10519 "listcmds",
10520#endif
10521#ifdef FEAT_LOCALMAP
10522 "localmap",
10523#endif
10524#ifdef FEAT_MENU
10525 "menu",
10526#endif
10527#ifdef FEAT_SESSION
10528 "mksession",
10529#endif
10530#ifdef FEAT_MODIFY_FNAME
10531 "modify_fname",
10532#endif
10533#ifdef FEAT_MOUSE
10534 "mouse",
10535#endif
10536#ifdef FEAT_MOUSESHAPE
10537 "mouseshape",
10538#endif
10539#if defined(UNIX) || defined(VMS)
10540# ifdef FEAT_MOUSE_DEC
10541 "mouse_dec",
10542# endif
10543# ifdef FEAT_MOUSE_GPM
10544 "mouse_gpm",
10545# endif
10546# ifdef FEAT_MOUSE_JSB
10547 "mouse_jsbterm",
10548# endif
10549# ifdef FEAT_MOUSE_NET
10550 "mouse_netterm",
10551# endif
10552# ifdef FEAT_MOUSE_PTERM
10553 "mouse_pterm",
10554# endif
10555# ifdef FEAT_MOUSE_XTERM
10556 "mouse_xterm",
10557# endif
10558#endif
10559#ifdef FEAT_MBYTE
10560 "multi_byte",
10561#endif
10562#ifdef FEAT_MBYTE_IME
10563 "multi_byte_ime",
10564#endif
10565#ifdef FEAT_MULTI_LANG
10566 "multi_lang",
10567#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010568#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010569#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010570 "mzscheme",
10571#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573#ifdef FEAT_OLE
10574 "ole",
10575#endif
10576#ifdef FEAT_OSFILETYPE
10577 "osfiletype",
10578#endif
10579#ifdef FEAT_PATH_EXTRA
10580 "path_extra",
10581#endif
10582#ifdef FEAT_PERL
10583#ifndef DYNAMIC_PERL
10584 "perl",
10585#endif
10586#endif
10587#ifdef FEAT_PYTHON
10588#ifndef DYNAMIC_PYTHON
10589 "python",
10590#endif
10591#endif
10592#ifdef FEAT_POSTSCRIPT
10593 "postscript",
10594#endif
10595#ifdef FEAT_PRINTER
10596 "printer",
10597#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010598#ifdef FEAT_PROFILE
10599 "profile",
10600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601#ifdef FEAT_QUICKFIX
10602 "quickfix",
10603#endif
10604#ifdef FEAT_RIGHTLEFT
10605 "rightleft",
10606#endif
10607#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10608 "ruby",
10609#endif
10610#ifdef FEAT_SCROLLBIND
10611 "scrollbind",
10612#endif
10613#ifdef FEAT_CMDL_INFO
10614 "showcmd",
10615 "cmdline_info",
10616#endif
10617#ifdef FEAT_SIGNS
10618 "signs",
10619#endif
10620#ifdef FEAT_SMARTINDENT
10621 "smartindent",
10622#endif
10623#ifdef FEAT_SNIFF
10624 "sniff",
10625#endif
10626#ifdef FEAT_STL_OPT
10627 "statusline",
10628#endif
10629#ifdef FEAT_SUN_WORKSHOP
10630 "sun_workshop",
10631#endif
10632#ifdef FEAT_NETBEANS_INTG
10633 "netbeans_intg",
10634#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010635#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010636 "spell",
10637#endif
10638#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 "syntax",
10640#endif
10641#if defined(USE_SYSTEM) || !defined(UNIX)
10642 "system",
10643#endif
10644#ifdef FEAT_TAG_BINS
10645 "tag_binary",
10646#endif
10647#ifdef FEAT_TAG_OLDSTATIC
10648 "tag_old_static",
10649#endif
10650#ifdef FEAT_TAG_ANYWHITE
10651 "tag_any_white",
10652#endif
10653#ifdef FEAT_TCL
10654# ifndef DYNAMIC_TCL
10655 "tcl",
10656# endif
10657#endif
10658#ifdef TERMINFO
10659 "terminfo",
10660#endif
10661#ifdef FEAT_TERMRESPONSE
10662 "termresponse",
10663#endif
10664#ifdef FEAT_TEXTOBJ
10665 "textobjects",
10666#endif
10667#ifdef HAVE_TGETENT
10668 "tgetent",
10669#endif
10670#ifdef FEAT_TITLE
10671 "title",
10672#endif
10673#ifdef FEAT_TOOLBAR
10674 "toolbar",
10675#endif
10676#ifdef FEAT_USR_CMDS
10677 "user-commands", /* was accidentally included in 5.4 */
10678 "user_commands",
10679#endif
10680#ifdef FEAT_VIMINFO
10681 "viminfo",
10682#endif
10683#ifdef FEAT_VERTSPLIT
10684 "vertsplit",
10685#endif
10686#ifdef FEAT_VIRTUALEDIT
10687 "virtualedit",
10688#endif
10689#ifdef FEAT_VISUAL
10690 "visual",
10691#endif
10692#ifdef FEAT_VISUALEXTRA
10693 "visualextra",
10694#endif
10695#ifdef FEAT_VREPLACE
10696 "vreplace",
10697#endif
10698#ifdef FEAT_WILDIGN
10699 "wildignore",
10700#endif
10701#ifdef FEAT_WILDMENU
10702 "wildmenu",
10703#endif
10704#ifdef FEAT_WINDOWS
10705 "windows",
10706#endif
10707#ifdef FEAT_WAK
10708 "winaltkeys",
10709#endif
10710#ifdef FEAT_WRITEBACKUP
10711 "writebackup",
10712#endif
10713#ifdef FEAT_XIM
10714 "xim",
10715#endif
10716#ifdef FEAT_XFONTSET
10717 "xfontset",
10718#endif
10719#ifdef USE_XSMP
10720 "xsmp",
10721#endif
10722#ifdef USE_XSMP_INTERACT
10723 "xsmp_interact",
10724#endif
10725#ifdef FEAT_XCLIPBOARD
10726 "xterm_clipboard",
10727#endif
10728#ifdef FEAT_XTERM_SAVE
10729 "xterm_save",
10730#endif
10731#if defined(UNIX) && defined(FEAT_X11)
10732 "X11",
10733#endif
10734 NULL
10735 };
10736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 for (i = 0; has_list[i] != NULL; ++i)
10739 if (STRICMP(name, has_list[i]) == 0)
10740 {
10741 n = TRUE;
10742 break;
10743 }
10744
10745 if (n == FALSE)
10746 {
10747 if (STRNICMP(name, "patch", 5) == 0)
10748 n = has_patch(atoi((char *)name + 5));
10749 else if (STRICMP(name, "vim_starting") == 0)
10750 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010751#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10752 else if (STRICMP(name, "balloon_multiline") == 0)
10753 n = multiline_balloon_available();
10754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755#ifdef DYNAMIC_TCL
10756 else if (STRICMP(name, "tcl") == 0)
10757 n = tcl_enabled(FALSE);
10758#endif
10759#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10760 else if (STRICMP(name, "iconv") == 0)
10761 n = iconv_enabled(FALSE);
10762#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010763#ifdef DYNAMIC_MZSCHEME
10764 else if (STRICMP(name, "mzscheme") == 0)
10765 n = mzscheme_enabled(FALSE);
10766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767#ifdef DYNAMIC_RUBY
10768 else if (STRICMP(name, "ruby") == 0)
10769 n = ruby_enabled(FALSE);
10770#endif
10771#ifdef DYNAMIC_PYTHON
10772 else if (STRICMP(name, "python") == 0)
10773 n = python_enabled(FALSE);
10774#endif
10775#ifdef DYNAMIC_PERL
10776 else if (STRICMP(name, "perl") == 0)
10777 n = perl_enabled(FALSE);
10778#endif
10779#ifdef FEAT_GUI
10780 else if (STRICMP(name, "gui_running") == 0)
10781 n = (gui.in_use || gui.starting);
10782# ifdef FEAT_GUI_W32
10783 else if (STRICMP(name, "gui_win32s") == 0)
10784 n = gui_is_win32s();
10785# endif
10786# ifdef FEAT_BROWSE
10787 else if (STRICMP(name, "browse") == 0)
10788 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10789# endif
10790#endif
10791#ifdef FEAT_SYN_HL
10792 else if (STRICMP(name, "syntax_items") == 0)
10793 n = syntax_present(curbuf);
10794#endif
10795#if defined(WIN3264)
10796 else if (STRICMP(name, "win95") == 0)
10797 n = mch_windows95();
10798#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010799#ifdef FEAT_NETBEANS_INTG
10800 else if (STRICMP(name, "netbeans_enabled") == 0)
10801 n = usingNetbeans;
10802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 }
10804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806}
10807
10808/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010809 * "has_key()" function
10810 */
10811 static void
10812f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010813 typval_T *argvars;
10814 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815{
10816 rettv->vval.v_number = 0;
10817 if (argvars[0].v_type != VAR_DICT)
10818 {
10819 EMSG(_(e_dictreq));
10820 return;
10821 }
10822 if (argvars[0].vval.v_dict == NULL)
10823 return;
10824
10825 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010826 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010827}
10828
10829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 * "hasmapto()" function
10831 */
10832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010834 typval_T *argvars;
10835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836{
10837 char_u *name;
10838 char_u *mode;
10839 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010840 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010843 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 mode = (char_u *)"nvo";
10845 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010848 if (argvars[2].v_type != VAR_UNKNOWN)
10849 abbr = get_tv_number(&argvars[2]);
10850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851
Bram Moolenaar2c932302006-03-18 21:42:09 +000010852 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856}
10857
10858/*
10859 * "histadd()" function
10860 */
10861/*ARGSUSED*/
10862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010864 typval_T *argvars;
10865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866{
10867#ifdef FEAT_CMDHIST
10868 int histype;
10869 char_u *str;
10870 char_u buf[NUMBUFLEN];
10871#endif
10872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 if (check_restricted() || check_secure())
10875 return;
10876#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10878 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (histype >= 0)
10880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 if (*str != NUL)
10883 {
10884 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 return;
10887 }
10888 }
10889#endif
10890}
10891
10892/*
10893 * "histdel()" function
10894 */
10895/*ARGSUSED*/
10896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *argvars;
10899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901#ifdef FEAT_CMDHIST
10902 int n;
10903 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10907 if (str == NULL)
10908 n = 0;
10909 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010912 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 else
10917 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919 get_tv_string_buf(&argvars[1], buf));
10920 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923#endif
10924}
10925
10926/*
10927 * "histget()" function
10928 */
10929/*ARGSUSED*/
10930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010932 typval_T *argvars;
10933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934{
10935#ifdef FEAT_CMDHIST
10936 int type;
10937 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10941 if (str == NULL)
10942 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 {
10945 type = get_histtype(str);
10946 if (argvars[1].v_type == VAR_UNKNOWN)
10947 idx = get_history_idx(type);
10948 else
10949 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10950 /* -1 on type error */
10951 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957}
10958
10959/*
10960 * "histnr()" function
10961 */
10962/*ARGSUSED*/
10963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *argvars;
10966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
10968 int i;
10969
10970#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 char_u *history = get_tv_string_chk(&argvars[0]);
10972
10973 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 if (i >= HIST_CMD && i < HIST_COUNT)
10975 i = get_history_idx(i);
10976 else
10977#endif
10978 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
10982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 * "highlightID(name)" function
10984 */
10985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 typval_T *argvars;
10988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991}
10992
10993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994 * "highlight_exists()" function
10995 */
10996 static void
10997f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010998 typval_T *argvars;
10999 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000{
11001 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11002}
11003
11004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 * "hostname()" function
11006 */
11007/*ARGSUSED*/
11008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 typval_T *argvars;
11011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012{
11013 char_u hostname[256];
11014
11015 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->v_type = VAR_STRING;
11017 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018}
11019
11020/*
11021 * iconv() function
11022 */
11023/*ARGSUSED*/
11024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *argvars;
11027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028{
11029#ifdef FEAT_MBYTE
11030 char_u buf1[NUMBUFLEN];
11031 char_u buf2[NUMBUFLEN];
11032 char_u *from, *to, *str;
11033 vimconv_T vimconv;
11034#endif
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->v_type = VAR_STRING;
11037 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038
11039#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 str = get_tv_string(&argvars[0]);
11041 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11042 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 vimconv.vc_type = CONV_NONE;
11044 convert_setup(&vimconv, from, to);
11045
11046 /* If the encodings are equal, no conversion needed. */
11047 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051
11052 convert_setup(&vimconv, NULL, NULL);
11053 vim_free(from);
11054 vim_free(to);
11055#endif
11056}
11057
11058/*
11059 * "indent()" function
11060 */
11061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 typval_T *argvars;
11064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065{
11066 linenr_T lnum;
11067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011072 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073}
11074
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011075/*
11076 * "index()" function
11077 */
11078 static void
11079f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 typval_T *argvars;
11081 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011082{
Bram Moolenaar33570922005-01-25 22:26:29 +000011083 list_T *l;
11084 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011085 long idx = 0;
11086 int ic = FALSE;
11087
11088 rettv->vval.v_number = -1;
11089 if (argvars[0].v_type != VAR_LIST)
11090 {
11091 EMSG(_(e_listreq));
11092 return;
11093 }
11094 l = argvars[0].vval.v_list;
11095 if (l != NULL)
11096 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011097 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011098 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 int error = FALSE;
11101
Bram Moolenaar758711c2005-02-02 23:11:38 +000011102 /* Start at specified item. Use the cached index that list_find()
11103 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011104 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011105 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011106 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 ic = get_tv_number_chk(&argvars[3], &error);
11108 if (error)
11109 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011110 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011111
Bram Moolenaar758711c2005-02-02 23:11:38 +000011112 for ( ; item != NULL; item = item->li_next, ++idx)
11113 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011114 {
11115 rettv->vval.v_number = idx;
11116 break;
11117 }
11118 }
11119}
11120
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121static int inputsecret_flag = 0;
11122
11123/*
11124 * "input()" function
11125 * Also handles inputsecret() when inputsecret is set.
11126 */
11127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011129 typval_T *argvars;
11130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011132 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 char_u *p = NULL;
11134 int c;
11135 char_u buf[NUMBUFLEN];
11136 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011137 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011138 int xp_type = EXPAND_NOTHING;
11139 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
11143#ifdef NO_CONSOLE_INPUT
11144 /* While starting up, there is no place to enter text. */
11145 if (no_console_input())
11146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 return;
11149 }
11150#endif
11151
11152 cmd_silent = FALSE; /* Want to see the prompt. */
11153 if (prompt != NULL)
11154 {
11155 /* Only the part of the message after the last NL is considered as
11156 * prompt for the command line */
11157 p = vim_strrchr(prompt, '\n');
11158 if (p == NULL)
11159 p = prompt;
11160 else
11161 {
11162 ++p;
11163 c = *p;
11164 *p = NUL;
11165 msg_start();
11166 msg_clr_eos();
11167 msg_puts_attr(prompt, echo_attr);
11168 msg_didout = FALSE;
11169 msg_starthere();
11170 *p = c;
11171 }
11172 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 if (argvars[1].v_type != VAR_UNKNOWN)
11175 {
11176 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11177 if (defstr != NULL)
11178 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179
Bram Moolenaar4463f292005-09-25 22:20:24 +000011180 if (argvars[2].v_type != VAR_UNKNOWN)
11181 {
11182 char_u *xp_name;
11183 int xp_namelen;
11184 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011185
Bram Moolenaar4463f292005-09-25 22:20:24 +000011186 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011187
Bram Moolenaar4463f292005-09-25 22:20:24 +000011188 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11189 if (xp_name == NULL)
11190 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011191
Bram Moolenaar4463f292005-09-25 22:20:24 +000011192 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011193
Bram Moolenaar4463f292005-09-25 22:20:24 +000011194 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11195 &xp_arg) == FAIL)
11196 return;
11197 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011198 }
11199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 if (defstr != NULL)
11201 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011202 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11203 xp_type, xp_arg);
11204
11205 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 /* since the user typed this, no need to wait for return */
11208 need_wait_return = FALSE;
11209 msg_didout = FALSE;
11210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 cmd_silent = cmd_silent_save;
11212}
11213
11214/*
11215 * "inputdialog()" function
11216 */
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 typval_T *argvars;
11220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222#if defined(FEAT_GUI_TEXTDIALOG)
11223 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11224 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11225 {
11226 char_u *message;
11227 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 message = get_tv_string_chk(&argvars[0]);
11231 if (argvars[1].v_type != VAR_UNKNOWN
11232 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011233 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 else
11235 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011236 if (message != NULL && defstr != NULL
11237 && do_dialog(VIM_QUESTION, NULL, message,
11238 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 else
11241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 if (message != NULL && defstr != NULL
11243 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011244 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245 rettv->vval.v_string = vim_strsave(
11246 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 }
11252 else
11253#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255}
11256
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011257/*
11258 * "inputlist()" function
11259 */
11260 static void
11261f_inputlist(argvars, rettv)
11262 typval_T *argvars;
11263 typval_T *rettv;
11264{
11265 listitem_T *li;
11266 int selected;
11267 int mouse_used;
11268
11269 rettv->vval.v_number = 0;
11270#ifdef NO_CONSOLE_INPUT
11271 /* While starting up, there is no place to enter text. */
11272 if (no_console_input())
11273 return;
11274#endif
11275 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11276 {
11277 EMSG2(_(e_listarg), "inputlist()");
11278 return;
11279 }
11280
11281 msg_start();
11282 lines_left = Rows; /* avoid more prompt */
11283 msg_scroll = TRUE;
11284 msg_clr_eos();
11285
11286 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11287 {
11288 msg_puts(get_tv_string(&li->li_tv));
11289 msg_putchar('\n');
11290 }
11291
11292 /* Ask for choice. */
11293 selected = prompt_for_number(&mouse_used);
11294 if (mouse_used)
11295 selected -= lines_left;
11296
11297 rettv->vval.v_number = selected;
11298}
11299
11300
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11302
11303/*
11304 * "inputrestore()" function
11305 */
11306/*ARGSUSED*/
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
11312 if (ga_userinput.ga_len > 0)
11313 {
11314 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11316 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011317 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 }
11319 else if (p_verbose > 1)
11320 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011321 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 }
11324}
11325
11326/*
11327 * "inputsave()" function
11328 */
11329/*ARGSUSED*/
11330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *argvars;
11333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335 /* Add an entry to the stack of typehead storage. */
11336 if (ga_grow(&ga_userinput, 1) == OK)
11337 {
11338 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11339 + ga_userinput.ga_len);
11340 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 }
11343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345}
11346
11347/*
11348 * "inputsecret()" function
11349 */
11350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011352 typval_T *argvars;
11353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
11355 ++cmdline_star;
11356 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 --cmdline_star;
11359 --inputsecret_flag;
11360}
11361
11362/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011363 * "insert()" function
11364 */
11365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011367 typval_T *argvars;
11368 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011369{
11370 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011371 listitem_T *item;
11372 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011374
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011375 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011376 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011377 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011378 else if ((l = argvars[0].vval.v_list) != NULL
11379 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011380 {
11381 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 before = get_tv_number_chk(&argvars[2], &error);
11383 if (error)
11384 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011385
Bram Moolenaar758711c2005-02-02 23:11:38 +000011386 if (before == l->lv_len)
11387 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011388 else
11389 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011390 item = list_find(l, before);
11391 if (item == NULL)
11392 {
11393 EMSGN(_(e_listidx), before);
11394 l = NULL;
11395 }
11396 }
11397 if (l != NULL)
11398 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011399 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011400 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 }
11402 }
11403}
11404
11405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 * "isdirectory()" function
11407 */
11408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011410 typval_T *argvars;
11411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414}
11415
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011416/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011417 * "islocked()" function
11418 */
11419 static void
11420f_islocked(argvars, rettv)
11421 typval_T *argvars;
11422 typval_T *rettv;
11423{
11424 lval_T lv;
11425 char_u *end;
11426 dictitem_T *di;
11427
11428 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011429 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11430 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011431 if (end != NULL && lv.ll_name != NULL)
11432 {
11433 if (*end != NUL)
11434 EMSG(_(e_trailing));
11435 else
11436 {
11437 if (lv.ll_tv == NULL)
11438 {
11439 if (check_changedtick(lv.ll_name))
11440 rettv->vval.v_number = 1; /* always locked */
11441 else
11442 {
11443 di = find_var(lv.ll_name, NULL);
11444 if (di != NULL)
11445 {
11446 /* Consider a variable locked when:
11447 * 1. the variable itself is locked
11448 * 2. the value of the variable is locked.
11449 * 3. the List or Dict value is locked.
11450 */
11451 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11452 || tv_islocked(&di->di_tv));
11453 }
11454 }
11455 }
11456 else if (lv.ll_range)
11457 EMSG(_("E745: Range not allowed"));
11458 else if (lv.ll_newkey != NULL)
11459 EMSG2(_(e_dictkey), lv.ll_newkey);
11460 else if (lv.ll_list != NULL)
11461 /* List item. */
11462 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11463 else
11464 /* Dictionary item. */
11465 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11466 }
11467 }
11468
11469 clear_lval(&lv);
11470}
11471
Bram Moolenaar33570922005-01-25 22:26:29 +000011472static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011473
11474/*
11475 * Turn a dict into a list:
11476 * "what" == 0: list of keys
11477 * "what" == 1: list of values
11478 * "what" == 2: list of items
11479 */
11480 static void
11481dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 typval_T *argvars;
11483 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011484 int what;
11485{
Bram Moolenaar33570922005-01-25 22:26:29 +000011486 list_T *l2;
11487 dictitem_T *di;
11488 hashitem_T *hi;
11489 listitem_T *li;
11490 listitem_T *li2;
11491 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011492 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011493
11494 rettv->vval.v_number = 0;
11495 if (argvars[0].v_type != VAR_DICT)
11496 {
11497 EMSG(_(e_dictreq));
11498 return;
11499 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011500 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011501 return;
11502
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011503 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011504 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011505
Bram Moolenaar33570922005-01-25 22:26:29 +000011506 todo = d->dv_hashtab.ht_used;
11507 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011509 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011511 --todo;
11512 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011513
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011514 li = listitem_alloc();
11515 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011516 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011517 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011518
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011519 if (what == 0)
11520 {
11521 /* keys() */
11522 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011523 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011524 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11525 }
11526 else if (what == 1)
11527 {
11528 /* values() */
11529 copy_tv(&di->di_tv, &li->li_tv);
11530 }
11531 else
11532 {
11533 /* items() */
11534 l2 = list_alloc();
11535 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011536 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011537 li->li_tv.vval.v_list = l2;
11538 if (l2 == NULL)
11539 break;
11540 ++l2->lv_refcount;
11541
11542 li2 = listitem_alloc();
11543 if (li2 == NULL)
11544 break;
11545 list_append(l2, li2);
11546 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011547 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011548 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11549
11550 li2 = listitem_alloc();
11551 if (li2 == NULL)
11552 break;
11553 list_append(l2, li2);
11554 copy_tv(&di->di_tv, &li2->li_tv);
11555 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011556 }
11557 }
11558}
11559
11560/*
11561 * "items(dict)" function
11562 */
11563 static void
11564f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011565 typval_T *argvars;
11566 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011567{
11568 dict_list(argvars, rettv, 2);
11569}
11570
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011572 * "join()" function
11573 */
11574 static void
11575f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011576 typval_T *argvars;
11577 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011578{
11579 garray_T ga;
11580 char_u *sep;
11581
11582 rettv->vval.v_number = 0;
11583 if (argvars[0].v_type != VAR_LIST)
11584 {
11585 EMSG(_(e_listreq));
11586 return;
11587 }
11588 if (argvars[0].vval.v_list == NULL)
11589 return;
11590 if (argvars[1].v_type == VAR_UNKNOWN)
11591 sep = (char_u *)" ";
11592 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011593 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011594
11595 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011596
11597 if (sep != NULL)
11598 {
11599 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011600 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011601 ga_append(&ga, NUL);
11602 rettv->vval.v_string = (char_u *)ga.ga_data;
11603 }
11604 else
11605 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011606}
11607
11608/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011609 * "keys()" function
11610 */
11611 static void
11612f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011613 typval_T *argvars;
11614 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011615{
11616 dict_list(argvars, rettv, 0);
11617}
11618
11619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 * "last_buffer_nr()" function.
11621 */
11622/*ARGSUSED*/
11623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011625 typval_T *argvars;
11626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627{
11628 int n = 0;
11629 buf_T *buf;
11630
11631 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11632 if (n < buf->b_fnum)
11633 n = buf->b_fnum;
11634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011636}
11637
11638/*
11639 * "len()" function
11640 */
11641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011643 typval_T *argvars;
11644 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011645{
11646 switch (argvars[0].v_type)
11647 {
11648 case VAR_STRING:
11649 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650 rettv->vval.v_number = (varnumber_T)STRLEN(
11651 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011652 break;
11653 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011655 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011656 case VAR_DICT:
11657 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11658 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011659 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011660 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011661 break;
11662 }
11663}
11664
Bram Moolenaar33570922005-01-25 22:26:29 +000011665static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011666
11667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011669 typval_T *argvars;
11670 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011671 int type;
11672{
11673#ifdef FEAT_LIBCALL
11674 char_u *string_in;
11675 char_u **string_result;
11676 int nr_result;
11677#endif
11678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011680 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011684
11685 if (check_restricted() || check_secure())
11686 return;
11687
11688#ifdef FEAT_LIBCALL
11689 /* The first two args must be strings, otherwise its meaningless */
11690 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11691 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011692 string_in = NULL;
11693 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011694 string_in = argvars[2].vval.v_string;
11695 if (type == VAR_NUMBER)
11696 string_result = NULL;
11697 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011698 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011699 if (mch_libcall(argvars[0].vval.v_string,
11700 argvars[1].vval.v_string,
11701 string_in,
11702 argvars[2].vval.v_number,
11703 string_result,
11704 &nr_result) == OK
11705 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011707 }
11708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709}
11710
11711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011712 * "libcall()" function
11713 */
11714 static void
11715f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011716 typval_T *argvars;
11717 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011718{
11719 libcall_common(argvars, rettv, VAR_STRING);
11720}
11721
11722/*
11723 * "libcallnr()" function
11724 */
11725 static void
11726f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011727 typval_T *argvars;
11728 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729{
11730 libcall_common(argvars, rettv, VAR_NUMBER);
11731}
11732
11733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 * "line(string)" function
11735 */
11736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011738 typval_T *argvars;
11739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740{
11741 linenr_T lnum = 0;
11742 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011743 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011745 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 if (fp != NULL)
11747 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749}
11750
11751/*
11752 * "line2byte(lnum)" function
11753 */
11754/*ARGSUSED*/
11755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756f_line2byte(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#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762#else
11763 linenr_T lnum;
11764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11770 if (rettv->vval.v_number >= 0)
11771 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772#endif
11773}
11774
11775/*
11776 * "lispindent(lnum)" function
11777 */
11778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011780 typval_T *argvars;
11781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782{
11783#ifdef FEAT_LISP
11784 pos_T pos;
11785 linenr_T lnum;
11786
11787 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11790 {
11791 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011792 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 curwin->w_cursor = pos;
11794 }
11795 else
11796#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798}
11799
11800/*
11801 * "localtime()" function
11802 */
11803/*ARGSUSED*/
11804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 typval_T *argvars;
11807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810}
11811
Bram Moolenaar33570922005-01-25 22:26:29 +000011812static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813
11814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011815get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 int exact;
11819{
11820 char_u *keys;
11821 char_u *which;
11822 char_u buf[NUMBUFLEN];
11823 char_u *keys_buf = NULL;
11824 char_u *rhs;
11825 int mode;
11826 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011827 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828
11829 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
11831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 if (*keys == NUL)
11835 return;
11836
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011837 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011839 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011840 if (argvars[2].v_type != VAR_UNKNOWN)
11841 abbr = get_tv_number(&argvars[2]);
11842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 else
11844 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 if (which == NULL)
11846 return;
11847
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848 mode = get_map_mode(&which, 0);
11849
11850 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011851 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 vim_free(keys_buf);
11853 if (rhs != NULL)
11854 {
11855 ga_init(&ga);
11856 ga.ga_itemsize = 1;
11857 ga.ga_growsize = 40;
11858
11859 while (*rhs != NUL)
11860 ga_concat(&ga, str2special(&rhs, FALSE));
11861
11862 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011863 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 }
11865}
11866
11867/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011868 * "map()" function
11869 */
11870 static void
11871f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011872 typval_T *argvars;
11873 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011874{
11875 filter_map(argvars, rettv, TRUE);
11876}
11877
11878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011879 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 */
11881 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011882f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011883 typval_T *argvars;
11884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011886 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887}
11888
11889/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011890 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 */
11892 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011893f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 typval_T *argvars;
11895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898}
11899
Bram Moolenaar33570922005-01-25 22:26:29 +000011900static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901
11902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 typval_T *argvars;
11905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 int type;
11907{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011908 char_u *str = NULL;
11909 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 char_u *pat;
11911 regmatch_T regmatch;
11912 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011913 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 char_u *save_cpo;
11915 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011916 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011917 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011918 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 list_T *l = NULL;
11920 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011921 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011922 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923
11924 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11925 save_cpo = p_cpo;
11926 p_cpo = (char_u *)"";
11927
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011928 rettv->vval.v_number = -1;
11929 if (type == 3)
11930 {
11931 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011932 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011933 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011934 }
11935 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937 rettv->v_type = VAR_STRING;
11938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011941 if (argvars[0].v_type == VAR_LIST)
11942 {
11943 if ((l = argvars[0].vval.v_list) == NULL)
11944 goto theend;
11945 li = l->lv_first;
11946 }
11947 else
11948 expr = str = get_tv_string(&argvars[0]);
11949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011950 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11951 if (pat == NULL)
11952 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011953
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011954 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956 int error = FALSE;
11957
11958 start = get_tv_number_chk(&argvars[2], &error);
11959 if (error)
11960 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011961 if (l != NULL)
11962 {
11963 li = list_find(l, start);
11964 if (li == NULL)
11965 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011966 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011967 }
11968 else
11969 {
11970 if (start < 0)
11971 start = 0;
11972 if (start > (long)STRLEN(str))
11973 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011974 /* When "count" argument is there ignore matches before "start",
11975 * otherwise skip part of the string. Differs when pattern is "^"
11976 * or "\<". */
11977 if (argvars[3].v_type != VAR_UNKNOWN)
11978 startcol = start;
11979 else
11980 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011981 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011982
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011983 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 nth = get_tv_number_chk(&argvars[3], &error);
11985 if (error)
11986 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 }
11988
11989 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11990 if (regmatch.regprog != NULL)
11991 {
11992 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011993
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011994 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011995 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011996 if (l != NULL)
11997 {
11998 if (li == NULL)
11999 {
12000 match = FALSE;
12001 break;
12002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012003 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012004 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012005 if (str == NULL)
12006 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012007 }
12008
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012009 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012010
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012011 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012012 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012013 if (l == NULL && !match)
12014 break;
12015
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012016 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012017 if (l != NULL)
12018 {
12019 li = li->li_next;
12020 ++idx;
12021 }
12022 else
12023 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012024#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012025 startcol = regmatch.startp[0]
12026 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012027#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012028 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012029#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012030 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012031 }
12032
12033 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012035 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012036 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012037 int i;
12038
12039 /* return list with matched string and submatches */
12040 for (i = 0; i < NSUBEXP; ++i)
12041 {
12042 if (regmatch.endp[i] == NULL)
12043 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012044 if (list_append_string(rettv->vval.v_list,
12045 regmatch.startp[i],
12046 (int)(regmatch.endp[i] - regmatch.startp[i]))
12047 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012048 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012049 }
12050 }
12051 else if (type == 2)
12052 {
12053 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012054 if (l != NULL)
12055 copy_tv(&li->li_tv, rettv);
12056 else
12057 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012059 }
12060 else if (l != NULL)
12061 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 else
12063 {
12064 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 (varnumber_T)(regmatch.startp[0] - str);
12067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012068 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012070 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 }
12072 }
12073 vim_free(regmatch.regprog);
12074 }
12075
12076theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012077 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 p_cpo = save_cpo;
12079}
12080
12081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082 * "match()" function
12083 */
12084 static void
12085f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012086 typval_T *argvars;
12087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012088{
12089 find_some_match(argvars, rettv, 1);
12090}
12091
12092/*
12093 * "matchend()" function
12094 */
12095 static void
12096f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012097 typval_T *argvars;
12098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099{
12100 find_some_match(argvars, rettv, 0);
12101}
12102
12103/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012104 * "matchlist()" function
12105 */
12106 static void
12107f_matchlist(argvars, rettv)
12108 typval_T *argvars;
12109 typval_T *rettv;
12110{
12111 find_some_match(argvars, rettv, 3);
12112}
12113
12114/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012115 * "matchstr()" function
12116 */
12117 static void
12118f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012119 typval_T *argvars;
12120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012121{
12122 find_some_match(argvars, rettv, 2);
12123}
12124
Bram Moolenaar33570922005-01-25 22:26:29 +000012125static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012126
12127 static void
12128max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012129 typval_T *argvars;
12130 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012131 int domax;
12132{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012133 long n = 0;
12134 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012135 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012136
12137 if (argvars[0].v_type == VAR_LIST)
12138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012139 list_T *l;
12140 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012141
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012142 l = argvars[0].vval.v_list;
12143 if (l != NULL)
12144 {
12145 li = l->lv_first;
12146 if (li != NULL)
12147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012148 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012149 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012150 {
12151 li = li->li_next;
12152 if (li == NULL)
12153 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012154 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012155 if (domax ? i > n : i < n)
12156 n = i;
12157 }
12158 }
12159 }
12160 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012161 else if (argvars[0].v_type == VAR_DICT)
12162 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012163 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012164 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012165 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012166 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012167
12168 d = argvars[0].vval.v_dict;
12169 if (d != NULL)
12170 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012171 todo = d->dv_hashtab.ht_used;
12172 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012174 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012175 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012176 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012177 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012178 if (first)
12179 {
12180 n = i;
12181 first = FALSE;
12182 }
12183 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012184 n = i;
12185 }
12186 }
12187 }
12188 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012189 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012190 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012191 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012192}
12193
12194/*
12195 * "max()" function
12196 */
12197 static void
12198f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012199 typval_T *argvars;
12200 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012201{
12202 max_min(argvars, rettv, TRUE);
12203}
12204
12205/*
12206 * "min()" function
12207 */
12208 static void
12209f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012210 typval_T *argvars;
12211 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012212{
12213 max_min(argvars, rettv, FALSE);
12214}
12215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012216static int mkdir_recurse __ARGS((char_u *dir, int prot));
12217
12218/*
12219 * Create the directory in which "dir" is located, and higher levels when
12220 * needed.
12221 */
12222 static int
12223mkdir_recurse(dir, prot)
12224 char_u *dir;
12225 int prot;
12226{
12227 char_u *p;
12228 char_u *updir;
12229 int r = FAIL;
12230
12231 /* Get end of directory name in "dir".
12232 * We're done when it's "/" or "c:/". */
12233 p = gettail_sep(dir);
12234 if (p <= get_past_head(dir))
12235 return OK;
12236
12237 /* If the directory exists we're done. Otherwise: create it.*/
12238 updir = vim_strnsave(dir, (int)(p - dir));
12239 if (updir == NULL)
12240 return FAIL;
12241 if (mch_isdir(updir))
12242 r = OK;
12243 else if (mkdir_recurse(updir, prot) == OK)
12244 r = vim_mkdir_emsg(updir, prot);
12245 vim_free(updir);
12246 return r;
12247}
12248
12249#ifdef vim_mkdir
12250/*
12251 * "mkdir()" function
12252 */
12253 static void
12254f_mkdir(argvars, rettv)
12255 typval_T *argvars;
12256 typval_T *rettv;
12257{
12258 char_u *dir;
12259 char_u buf[NUMBUFLEN];
12260 int prot = 0755;
12261
12262 rettv->vval.v_number = FAIL;
12263 if (check_restricted() || check_secure())
12264 return;
12265
12266 dir = get_tv_string_buf(&argvars[0], buf);
12267 if (argvars[1].v_type != VAR_UNKNOWN)
12268 {
12269 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012270 prot = get_tv_number_chk(&argvars[2], NULL);
12271 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012272 mkdir_recurse(dir, prot);
12273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012274 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012275}
12276#endif
12277
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 * "mode()" function
12280 */
12281/*ARGSUSED*/
12282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012284 typval_T *argvars;
12285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286{
12287 char_u buf[2];
12288
12289#ifdef FEAT_VISUAL
12290 if (VIsual_active)
12291 {
12292 if (VIsual_select)
12293 buf[0] = VIsual_mode + 's' - 'v';
12294 else
12295 buf[0] = VIsual_mode;
12296 }
12297 else
12298#endif
12299 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12300 buf[0] = 'r';
12301 else if (State & INSERT)
12302 {
12303 if (State & REPLACE_FLAG)
12304 buf[0] = 'R';
12305 else
12306 buf[0] = 'i';
12307 }
12308 else if (State & CMDLINE)
12309 buf[0] = 'c';
12310 else
12311 buf[0] = 'n';
12312
12313 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 rettv->vval.v_string = vim_strsave(buf);
12315 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316}
12317
12318/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319 * "nextnonblank()" function
12320 */
12321 static void
12322f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012323 typval_T *argvars;
12324 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325{
12326 linenr_T lnum;
12327
12328 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012330 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012331 {
12332 lnum = 0;
12333 break;
12334 }
12335 if (*skipwhite(ml_get(lnum)) != NUL)
12336 break;
12337 }
12338 rettv->vval.v_number = lnum;
12339}
12340
12341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 * "nr2char()" function
12343 */
12344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *argvars;
12347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348{
12349 char_u buf[NUMBUFLEN];
12350
12351#ifdef FEAT_MBYTE
12352 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 else
12355#endif
12356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 buf[1] = NUL;
12359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->v_type = VAR_STRING;
12361 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012362}
12363
12364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012365 * "prevnonblank()" function
12366 */
12367 static void
12368f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012369 typval_T *argvars;
12370 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371{
12372 linenr_T lnum;
12373
12374 lnum = get_tv_lnum(argvars);
12375 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12376 lnum = 0;
12377 else
12378 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12379 --lnum;
12380 rettv->vval.v_number = lnum;
12381}
12382
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012383#ifdef HAVE_STDARG_H
12384/* This dummy va_list is here because:
12385 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12386 * - locally in the function results in a "used before set" warning
12387 * - using va_start() to initialize it gives "function with fixed args" error */
12388static va_list ap;
12389#endif
12390
Bram Moolenaar8c711452005-01-14 21:53:12 +000012391/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012392 * "printf()" function
12393 */
12394 static void
12395f_printf(argvars, rettv)
12396 typval_T *argvars;
12397 typval_T *rettv;
12398{
12399 rettv->v_type = VAR_STRING;
12400 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012401#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012402 {
12403 char_u buf[NUMBUFLEN];
12404 int len;
12405 char_u *s;
12406 int saved_did_emsg = did_emsg;
12407 char *fmt;
12408
12409 /* Get the required length, allocate the buffer and do it for real. */
12410 did_emsg = FALSE;
12411 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012412 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012413 if (!did_emsg)
12414 {
12415 s = alloc(len + 1);
12416 if (s != NULL)
12417 {
12418 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012419 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012420 }
12421 }
12422 did_emsg |= saved_did_emsg;
12423 }
12424#endif
12425}
12426
12427/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012428 * "pumvisible()" function
12429 */
12430/*ARGSUSED*/
12431 static void
12432f_pumvisible(argvars, rettv)
12433 typval_T *argvars;
12434 typval_T *rettv;
12435{
12436 rettv->vval.v_number = 0;
12437#ifdef FEAT_INS_EXPAND
12438 if (pum_visible())
12439 rettv->vval.v_number = 1;
12440#endif
12441}
12442
12443/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012444 * "range()" function
12445 */
12446 static void
12447f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012448 typval_T *argvars;
12449 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012450{
12451 long start;
12452 long end;
12453 long stride = 1;
12454 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012455 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012457 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012458 if (argvars[1].v_type == VAR_UNKNOWN)
12459 {
12460 end = start - 1;
12461 start = 0;
12462 }
12463 else
12464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012468 }
12469
12470 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471 if (error)
12472 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012473 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012474 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012475 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012476 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012477 else
12478 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012479 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012480 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012481 if (list_append_number(rettv->vval.v_list,
12482 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012483 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012484 }
12485}
12486
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012487/*
12488 * "readfile()" function
12489 */
12490 static void
12491f_readfile(argvars, rettv)
12492 typval_T *argvars;
12493 typval_T *rettv;
12494{
12495 int binary = FALSE;
12496 char_u *fname;
12497 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012498 listitem_T *li;
12499#define FREAD_SIZE 200 /* optimized for text lines */
12500 char_u buf[FREAD_SIZE];
12501 int readlen; /* size of last fread() */
12502 int buflen; /* nr of valid chars in buf[] */
12503 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12504 int tolist; /* first byte in buf[] still to be put in list */
12505 int chop; /* how many CR to chop off */
12506 char_u *prev = NULL; /* previously read bytes, if any */
12507 int prevlen = 0; /* length of "prev" if not NULL */
12508 char_u *s;
12509 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012510 long maxline = MAXLNUM;
12511 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012512
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012513 if (argvars[1].v_type != VAR_UNKNOWN)
12514 {
12515 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12516 binary = TRUE;
12517 if (argvars[2].v_type != VAR_UNKNOWN)
12518 maxline = get_tv_number(&argvars[2]);
12519 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012520
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012521 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012522 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012523
12524 /* Always open the file in binary mode, library functions have a mind of
12525 * their own about CR-LF conversion. */
12526 fname = get_tv_string(&argvars[0]);
12527 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12528 {
12529 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12530 return;
12531 }
12532
12533 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012534 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012535 {
12536 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12537 buflen = filtd + readlen;
12538 tolist = 0;
12539 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12540 {
12541 if (buf[filtd] == '\n' || readlen <= 0)
12542 {
12543 /* Only when in binary mode add an empty list item when the
12544 * last line ends in a '\n'. */
12545 if (!binary && readlen == 0 && filtd == 0)
12546 break;
12547
12548 /* Found end-of-line or end-of-file: add a text line to the
12549 * list. */
12550 chop = 0;
12551 if (!binary)
12552 while (filtd - chop - 1 >= tolist
12553 && buf[filtd - chop - 1] == '\r')
12554 ++chop;
12555 len = filtd - tolist - chop;
12556 if (prev == NULL)
12557 s = vim_strnsave(buf + tolist, len);
12558 else
12559 {
12560 s = alloc((unsigned)(prevlen + len + 1));
12561 if (s != NULL)
12562 {
12563 mch_memmove(s, prev, prevlen);
12564 vim_free(prev);
12565 prev = NULL;
12566 mch_memmove(s + prevlen, buf + tolist, len);
12567 s[prevlen + len] = NUL;
12568 }
12569 }
12570 tolist = filtd + 1;
12571
12572 li = listitem_alloc();
12573 if (li == NULL)
12574 {
12575 vim_free(s);
12576 break;
12577 }
12578 li->li_tv.v_type = VAR_STRING;
12579 li->li_tv.v_lock = 0;
12580 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012581 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012582
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012583 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012584 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012585 if (readlen <= 0)
12586 break;
12587 }
12588 else if (buf[filtd] == NUL)
12589 buf[filtd] = '\n';
12590 }
12591 if (readlen <= 0)
12592 break;
12593
12594 if (tolist == 0)
12595 {
12596 /* "buf" is full, need to move text to an allocated buffer */
12597 if (prev == NULL)
12598 {
12599 prev = vim_strnsave(buf, buflen);
12600 prevlen = buflen;
12601 }
12602 else
12603 {
12604 s = alloc((unsigned)(prevlen + buflen));
12605 if (s != NULL)
12606 {
12607 mch_memmove(s, prev, prevlen);
12608 mch_memmove(s + prevlen, buf, buflen);
12609 vim_free(prev);
12610 prev = s;
12611 prevlen += buflen;
12612 }
12613 }
12614 filtd = 0;
12615 }
12616 else
12617 {
12618 mch_memmove(buf, buf + tolist, buflen - tolist);
12619 filtd -= tolist;
12620 }
12621 }
12622
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012623 /*
12624 * For a negative line count use only the lines at the end of the file,
12625 * free the rest.
12626 */
12627 if (maxline < 0)
12628 while (cnt > -maxline)
12629 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012630 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012631 --cnt;
12632 }
12633
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012634 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012635 fclose(fd);
12636}
12637
12638
Bram Moolenaar0d660222005-01-07 21:51:51 +000012639#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12640static void make_connection __ARGS((void));
12641static int check_connection __ARGS((void));
12642
12643 static void
12644make_connection()
12645{
12646 if (X_DISPLAY == NULL
12647# ifdef FEAT_GUI
12648 && !gui.in_use
12649# endif
12650 )
12651 {
12652 x_force_connect = TRUE;
12653 setup_term_clip();
12654 x_force_connect = FALSE;
12655 }
12656}
12657
12658 static int
12659check_connection()
12660{
12661 make_connection();
12662 if (X_DISPLAY == NULL)
12663 {
12664 EMSG(_("E240: No connection to Vim server"));
12665 return FAIL;
12666 }
12667 return OK;
12668}
12669#endif
12670
12671#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012672static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012673
12674 static void
12675remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012678 int expr;
12679{
12680 char_u *server_name;
12681 char_u *keys;
12682 char_u *r = NULL;
12683 char_u buf[NUMBUFLEN];
12684# ifdef WIN32
12685 HWND w;
12686# else
12687 Window w;
12688# endif
12689
12690 if (check_restricted() || check_secure())
12691 return;
12692
12693# ifdef FEAT_X11
12694 if (check_connection() == FAIL)
12695 return;
12696# endif
12697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012698 server_name = get_tv_string_chk(&argvars[0]);
12699 if (server_name == NULL)
12700 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012701 keys = get_tv_string_buf(&argvars[1], buf);
12702# ifdef WIN32
12703 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12704# else
12705 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12706 < 0)
12707# endif
12708 {
12709 if (r != NULL)
12710 EMSG(r); /* sending worked but evaluation failed */
12711 else
12712 EMSG2(_("E241: Unable to send to %s"), server_name);
12713 return;
12714 }
12715
12716 rettv->vval.v_string = r;
12717
12718 if (argvars[2].v_type != VAR_UNKNOWN)
12719 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012720 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012721 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012723
12724 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 v.di_tv.v_type = VAR_STRING;
12726 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 idvar = get_tv_string_chk(&argvars[2]);
12728 if (idvar != NULL)
12729 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012730 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012731 }
12732}
12733#endif
12734
12735/*
12736 * "remote_expr()" function
12737 */
12738/*ARGSUSED*/
12739 static void
12740f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 typval_T *argvars;
12742 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012743{
12744 rettv->v_type = VAR_STRING;
12745 rettv->vval.v_string = NULL;
12746#ifdef FEAT_CLIENTSERVER
12747 remote_common(argvars, rettv, TRUE);
12748#endif
12749}
12750
12751/*
12752 * "remote_foreground()" function
12753 */
12754/*ARGSUSED*/
12755 static void
12756f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012757 typval_T *argvars;
12758 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012759{
12760 rettv->vval.v_number = 0;
12761#ifdef FEAT_CLIENTSERVER
12762# ifdef WIN32
12763 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012764 {
12765 char_u *server_name = get_tv_string_chk(&argvars[0]);
12766
12767 if (server_name != NULL)
12768 serverForeground(server_name);
12769 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012770# else
12771 /* Send a foreground() expression to the server. */
12772 argvars[1].v_type = VAR_STRING;
12773 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12774 argvars[2].v_type = VAR_UNKNOWN;
12775 remote_common(argvars, rettv, TRUE);
12776 vim_free(argvars[1].vval.v_string);
12777# endif
12778#endif
12779}
12780
12781/*ARGSUSED*/
12782 static void
12783f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012784 typval_T *argvars;
12785 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012786{
12787#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012789 char_u *s = NULL;
12790# ifdef WIN32
12791 int n = 0;
12792# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012794
12795 if (check_restricted() || check_secure())
12796 {
12797 rettv->vval.v_number = -1;
12798 return;
12799 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012800 serverid = get_tv_string_chk(&argvars[0]);
12801 if (serverid == NULL)
12802 {
12803 rettv->vval.v_number = -1;
12804 return; /* type error; errmsg already given */
12805 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012806# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012808 if (n == 0)
12809 rettv->vval.v_number = -1;
12810 else
12811 {
12812 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12813 rettv->vval.v_number = (s != NULL);
12814 }
12815# else
12816 rettv->vval.v_number = 0;
12817 if (check_connection() == FAIL)
12818 return;
12819
12820 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012821 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012822# endif
12823
12824 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 char_u *retvar;
12827
Bram Moolenaar33570922005-01-25 22:26:29 +000012828 v.di_tv.v_type = VAR_STRING;
12829 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 retvar = get_tv_string_chk(&argvars[1]);
12831 if (retvar != NULL)
12832 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012833 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012834 }
12835#else
12836 rettv->vval.v_number = -1;
12837#endif
12838}
12839
12840/*ARGSUSED*/
12841 static void
12842f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012843 typval_T *argvars;
12844 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012845{
12846 char_u *r = NULL;
12847
12848#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012849 char_u *serverid = get_tv_string_chk(&argvars[0]);
12850
12851 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012852 {
12853# ifdef WIN32
12854 /* The server's HWND is encoded in the 'id' parameter */
12855 int n = 0;
12856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012857 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012858 if (n != 0)
12859 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12860 if (r == NULL)
12861# else
12862 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012863 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012864# endif
12865 EMSG(_("E277: Unable to read a server reply"));
12866 }
12867#endif
12868 rettv->v_type = VAR_STRING;
12869 rettv->vval.v_string = r;
12870}
12871
12872/*
12873 * "remote_send()" function
12874 */
12875/*ARGSUSED*/
12876 static void
12877f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 typval_T *argvars;
12879 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012880{
12881 rettv->v_type = VAR_STRING;
12882 rettv->vval.v_string = NULL;
12883#ifdef FEAT_CLIENTSERVER
12884 remote_common(argvars, rettv, FALSE);
12885#endif
12886}
12887
12888/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012889 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012890 */
12891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012893 typval_T *argvars;
12894 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012895{
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 list_T *l;
12897 listitem_T *item, *item2;
12898 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012900 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012901 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012902 dict_T *d;
12903 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012904
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012905 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012906 if (argvars[0].v_type == VAR_DICT)
12907 {
12908 if (argvars[2].v_type != VAR_UNKNOWN)
12909 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012910 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012911 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012913 key = get_tv_string_chk(&argvars[1]);
12914 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012916 di = dict_find(d, key, -1);
12917 if (di == NULL)
12918 EMSG2(_(e_dictkey), key);
12919 else
12920 {
12921 *rettv = di->di_tv;
12922 init_tv(&di->di_tv);
12923 dictitem_remove(d, di);
12924 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012926 }
12927 }
12928 else if (argvars[0].v_type != VAR_LIST)
12929 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012930 else if ((l = argvars[0].vval.v_list) != NULL
12931 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012933 int error = FALSE;
12934
12935 idx = get_tv_number_chk(&argvars[1], &error);
12936 if (error)
12937 ; /* type error: do nothing, errmsg already given */
12938 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012939 EMSGN(_(e_listidx), idx);
12940 else
12941 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012942 if (argvars[2].v_type == VAR_UNKNOWN)
12943 {
12944 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012945 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012946 *rettv = item->li_tv;
12947 vim_free(item);
12948 }
12949 else
12950 {
12951 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012952 end = get_tv_number_chk(&argvars[2], &error);
12953 if (error)
12954 ; /* type error: do nothing */
12955 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012956 EMSGN(_(e_listidx), end);
12957 else
12958 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012959 int cnt = 0;
12960
12961 for (li = item; li != NULL; li = li->li_next)
12962 {
12963 ++cnt;
12964 if (li == item2)
12965 break;
12966 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012967 if (li == NULL) /* didn't find "item2" after "item" */
12968 EMSG(_(e_invrange));
12969 else
12970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012971 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012972 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012973 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012974 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012975 l->lv_first = item;
12976 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012977 item->li_prev = NULL;
12978 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012979 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012980 }
12981 }
12982 }
12983 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012984 }
12985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986}
12987
12988/*
12989 * "rename({from}, {to})" function
12990 */
12991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012993 typval_T *argvars;
12994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995{
12996 char_u buf[NUMBUFLEN];
12997
12998 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13002 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003}
13004
13005/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013006 * "repeat()" function
13007 */
13008/*ARGSUSED*/
13009 static void
13010f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013011 typval_T *argvars;
13012 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013013{
13014 char_u *p;
13015 int n;
13016 int slen;
13017 int len;
13018 char_u *r;
13019 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013020
13021 n = get_tv_number(&argvars[1]);
13022 if (argvars[0].v_type == VAR_LIST)
13023 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013024 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013025 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013026 if (list_extend(rettv->vval.v_list,
13027 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013028 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013029 }
13030 else
13031 {
13032 p = get_tv_string(&argvars[0]);
13033 rettv->v_type = VAR_STRING;
13034 rettv->vval.v_string = NULL;
13035
13036 slen = (int)STRLEN(p);
13037 len = slen * n;
13038 if (len <= 0)
13039 return;
13040
13041 r = alloc(len + 1);
13042 if (r != NULL)
13043 {
13044 for (i = 0; i < n; i++)
13045 mch_memmove(r + i * slen, p, (size_t)slen);
13046 r[len] = NUL;
13047 }
13048
13049 rettv->vval.v_string = r;
13050 }
13051}
13052
13053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 * "resolve()" function
13055 */
13056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013058 typval_T *argvars;
13059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060{
13061 char_u *p;
13062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064#ifdef FEAT_SHORTCUT
13065 {
13066 char_u *v = NULL;
13067
13068 v = mch_resolve_shortcut(p);
13069 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 }
13074#else
13075# ifdef HAVE_READLINK
13076 {
13077 char_u buf[MAXPATHL + 1];
13078 char_u *cpy;
13079 int len;
13080 char_u *remain = NULL;
13081 char_u *q;
13082 int is_relative_to_current = FALSE;
13083 int has_trailing_pathsep = FALSE;
13084 int limit = 100;
13085
13086 p = vim_strsave(p);
13087
13088 if (p[0] == '.' && (vim_ispathsep(p[1])
13089 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13090 is_relative_to_current = TRUE;
13091
13092 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013093 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 has_trailing_pathsep = TRUE;
13095
13096 q = getnextcomp(p);
13097 if (*q != NUL)
13098 {
13099 /* Separate the first path component in "p", and keep the
13100 * remainder (beginning with the path separator). */
13101 remain = vim_strsave(q - 1);
13102 q[-1] = NUL;
13103 }
13104
13105 for (;;)
13106 {
13107 for (;;)
13108 {
13109 len = readlink((char *)p, (char *)buf, MAXPATHL);
13110 if (len <= 0)
13111 break;
13112 buf[len] = NUL;
13113
13114 if (limit-- == 0)
13115 {
13116 vim_free(p);
13117 vim_free(remain);
13118 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013119 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 goto fail;
13121 }
13122
13123 /* Ensure that the result will have a trailing path separator
13124 * if the argument has one. */
13125 if (remain == NULL && has_trailing_pathsep)
13126 add_pathsep(buf);
13127
13128 /* Separate the first path component in the link value and
13129 * concatenate the remainders. */
13130 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13131 if (*q != NUL)
13132 {
13133 if (remain == NULL)
13134 remain = vim_strsave(q - 1);
13135 else
13136 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013137 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 if (cpy != NULL)
13139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 vim_free(remain);
13141 remain = cpy;
13142 }
13143 }
13144 q[-1] = NUL;
13145 }
13146
13147 q = gettail(p);
13148 if (q > p && *q == NUL)
13149 {
13150 /* Ignore trailing path separator. */
13151 q[-1] = NUL;
13152 q = gettail(p);
13153 }
13154 if (q > p && !mch_isFullName(buf))
13155 {
13156 /* symlink is relative to directory of argument */
13157 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13158 if (cpy != NULL)
13159 {
13160 STRCPY(cpy, p);
13161 STRCPY(gettail(cpy), buf);
13162 vim_free(p);
13163 p = cpy;
13164 }
13165 }
13166 else
13167 {
13168 vim_free(p);
13169 p = vim_strsave(buf);
13170 }
13171 }
13172
13173 if (remain == NULL)
13174 break;
13175
13176 /* Append the first path component of "remain" to "p". */
13177 q = getnextcomp(remain + 1);
13178 len = q - remain - (*q != NUL);
13179 cpy = vim_strnsave(p, STRLEN(p) + len);
13180 if (cpy != NULL)
13181 {
13182 STRNCAT(cpy, remain, len);
13183 vim_free(p);
13184 p = cpy;
13185 }
13186 /* Shorten "remain". */
13187 if (*q != NUL)
13188 STRCPY(remain, q - 1);
13189 else
13190 {
13191 vim_free(remain);
13192 remain = NULL;
13193 }
13194 }
13195
13196 /* If the result is a relative path name, make it explicitly relative to
13197 * the current directory if and only if the argument had this form. */
13198 if (!vim_ispathsep(*p))
13199 {
13200 if (is_relative_to_current
13201 && *p != NUL
13202 && !(p[0] == '.'
13203 && (p[1] == NUL
13204 || vim_ispathsep(p[1])
13205 || (p[1] == '.'
13206 && (p[2] == NUL
13207 || vim_ispathsep(p[2]))))))
13208 {
13209 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013210 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 if (cpy != NULL)
13212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213 vim_free(p);
13214 p = cpy;
13215 }
13216 }
13217 else if (!is_relative_to_current)
13218 {
13219 /* Strip leading "./". */
13220 q = p;
13221 while (q[0] == '.' && vim_ispathsep(q[1]))
13222 q += 2;
13223 if (q > p)
13224 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13225 }
13226 }
13227
13228 /* Ensure that the result will have no trailing path separator
13229 * if the argument had none. But keep "/" or "//". */
13230 if (!has_trailing_pathsep)
13231 {
13232 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013233 if (after_pathsep(p, q))
13234 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 }
13236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 }
13239# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241# endif
13242#endif
13243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013244 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245
13246#ifdef HAVE_READLINK
13247fail:
13248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250}
13251
13252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013253 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 */
13255 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013256f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013257 typval_T *argvars;
13258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259{
Bram Moolenaar33570922005-01-25 22:26:29 +000013260 list_T *l;
13261 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262
Bram Moolenaar0d660222005-01-07 21:51:51 +000013263 rettv->vval.v_number = 0;
13264 if (argvars[0].v_type != VAR_LIST)
13265 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013266 else if ((l = argvars[0].vval.v_list) != NULL
13267 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013268 {
13269 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013270 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013271 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013272 while (li != NULL)
13273 {
13274 ni = li->li_prev;
13275 list_append(l, li);
13276 li = ni;
13277 }
13278 rettv->vval.v_list = l;
13279 rettv->v_type = VAR_LIST;
13280 ++l->lv_refcount;
13281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282}
13283
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013284#define SP_NOMOVE 0x01 /* don't move cursor */
13285#define SP_REPEAT 0x02 /* repeat to find outer pair */
13286#define SP_RETCOUNT 0x04 /* return matchcount */
13287#define SP_SETPCMARK 0x08 /* set previous context mark */
13288#define SP_START 0x10 /* accept match at start position */
13289#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13290#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013291
Bram Moolenaar33570922005-01-25 22:26:29 +000013292static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013293
13294/*
13295 * Get flags for a search function.
13296 * Possibly sets "p_ws".
13297 * Returns BACKWARD, FORWARD or zero (for an error).
13298 */
13299 static int
13300get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013301 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013302 int *flagsp;
13303{
13304 int dir = FORWARD;
13305 char_u *flags;
13306 char_u nbuf[NUMBUFLEN];
13307 int mask;
13308
13309 if (varp->v_type != VAR_UNKNOWN)
13310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 flags = get_tv_string_buf_chk(varp, nbuf);
13312 if (flags == NULL)
13313 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013314 while (*flags != NUL)
13315 {
13316 switch (*flags)
13317 {
13318 case 'b': dir = BACKWARD; break;
13319 case 'w': p_ws = TRUE; break;
13320 case 'W': p_ws = FALSE; break;
13321 default: mask = 0;
13322 if (flagsp != NULL)
13323 switch (*flags)
13324 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013325 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013326 case 'e': mask = SP_END; break;
13327 case 'm': mask = SP_RETCOUNT; break;
13328 case 'n': mask = SP_NOMOVE; break;
13329 case 'p': mask = SP_SUBPAT; break;
13330 case 'r': mask = SP_REPEAT; break;
13331 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013332 }
13333 if (mask == 0)
13334 {
13335 EMSG2(_(e_invarg2), flags);
13336 dir = 0;
13337 }
13338 else
13339 *flagsp |= mask;
13340 }
13341 if (dir == 0)
13342 break;
13343 ++flags;
13344 }
13345 }
13346 return dir;
13347}
13348
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013350 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013352 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013353search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013355 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013356 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013358 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 char_u *pat;
13360 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013361 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 int save_p_ws = p_ws;
13363 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013364 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013365 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013366 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013367 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013369 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013370 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013371 if (dir == 0)
13372 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013373 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013374 if (flags & SP_START)
13375 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013376 if (flags & SP_END)
13377 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013378
13379 /* Optional extra argument: line number to stop searching. */
13380 if (argvars[1].v_type != VAR_UNKNOWN
13381 && argvars[2].v_type != VAR_UNKNOWN)
13382 {
13383 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13384 if (lnum_stop < 0)
13385 goto theend;
13386 }
13387
Bram Moolenaar231334e2005-07-25 20:46:57 +000013388 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013389 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013390 * Check to make sure only those flags are set.
13391 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13392 * flags cannot be set. Check for that condition also.
13393 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013394 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013395 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013398 goto theend;
13399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013401 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013402 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13403 options, RE_SEARCH, (linenr_T)lnum_stop);
13404 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013406 if (flags & SP_SUBPAT)
13407 retval = subpatnum;
13408 else
13409 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013410 if (flags & SP_SETPCMARK)
13411 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013413 if (match_pos != NULL)
13414 {
13415 /* Store the match cursor position */
13416 match_pos->lnum = pos.lnum;
13417 match_pos->col = pos.col + 1;
13418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 /* "/$" will put the cursor after the end of the line, may need to
13420 * correct that here */
13421 check_cursor();
13422 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013423
13424 /* If 'n' flag is used: restore cursor position. */
13425 if (flags & SP_NOMOVE)
13426 curwin->w_cursor = save_cursor;
13427theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013429
13430 return retval;
13431}
13432
13433/*
13434 * "search()" function
13435 */
13436 static void
13437f_search(argvars, rettv)
13438 typval_T *argvars;
13439 typval_T *rettv;
13440{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013441 int flags = 0;
13442
13443 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444}
13445
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013447 * "searchdecl()" function
13448 */
13449 static void
13450f_searchdecl(argvars, rettv)
13451 typval_T *argvars;
13452 typval_T *rettv;
13453{
13454 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013455 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013456 int error = FALSE;
13457 char_u *name;
13458
13459 rettv->vval.v_number = 1; /* default: FAIL */
13460
13461 name = get_tv_string_chk(&argvars[0]);
13462 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013463 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013464 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013465 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13466 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13467 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013468 if (!error && name != NULL)
13469 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013470 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013471}
13472
13473/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013474 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013476 static int
13477searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013478 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013479 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480{
13481 char_u *spat, *mpat, *epat;
13482 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 int dir;
13485 int flags = 0;
13486 char_u nbuf1[NUMBUFLEN];
13487 char_u nbuf2[NUMBUFLEN];
13488 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013489 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013490 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 spat = get_tv_string_chk(&argvars[0]);
13494 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13495 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13496 if (spat == NULL || mpat == NULL || epat == NULL)
13497 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 /* Handle the optional fourth argument: flags */
13500 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013501 if (dir == 0)
13502 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013503
13504 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013505 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13506 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013507 if ((flags & (SP_END | SP_SUBPAT)) != 0
13508 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013509 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013510 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013511 goto theend;
13512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013514 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515 if (argvars[3].v_type == VAR_UNKNOWN
13516 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 skip = (char_u *)"";
13518 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013520 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013521 if (argvars[5].v_type != VAR_UNKNOWN)
13522 {
13523 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13524 if (lnum_stop < 0)
13525 goto theend;
13526 }
13527 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 if (skip == NULL)
13529 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013531 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13532 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013533
13534theend:
13535 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013536
13537 return retval;
13538}
13539
13540/*
13541 * "searchpair()" function
13542 */
13543 static void
13544f_searchpair(argvars, rettv)
13545 typval_T *argvars;
13546 typval_T *rettv;
13547{
13548 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13549}
13550
13551/*
13552 * "searchpairpos()" function
13553 */
13554 static void
13555f_searchpairpos(argvars, rettv)
13556 typval_T *argvars;
13557 typval_T *rettv;
13558{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013559 pos_T match_pos;
13560 int lnum = 0;
13561 int col = 0;
13562
13563 rettv->vval.v_number = 0;
13564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013565 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013566 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013567
13568 if (searchpair_cmn(argvars, &match_pos) > 0)
13569 {
13570 lnum = match_pos.lnum;
13571 col = match_pos.col;
13572 }
13573
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013574 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13575 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013576}
13577
13578/*
13579 * Search for a start/middle/end thing.
13580 * Used by searchpair(), see its documentation for the details.
13581 * Returns 0 or -1 for no match,
13582 */
13583 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013584do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013585 char_u *spat; /* start pattern */
13586 char_u *mpat; /* middle pattern */
13587 char_u *epat; /* end pattern */
13588 int dir; /* BACKWARD or FORWARD */
13589 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013590 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013591 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013592 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013593{
13594 char_u *save_cpo;
13595 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13596 long retval = 0;
13597 pos_T pos;
13598 pos_T firstpos;
13599 pos_T foundpos;
13600 pos_T save_cursor;
13601 pos_T save_pos;
13602 int n;
13603 int r;
13604 int nest = 1;
13605 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013606 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013607
13608 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13609 save_cpo = p_cpo;
13610 p_cpo = (char_u *)"";
13611
13612 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13613 * start/middle/end (pat3, for the top pair). */
13614 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13615 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13616 if (pat2 == NULL || pat3 == NULL)
13617 goto theend;
13618 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13619 if (*mpat == NUL)
13620 STRCPY(pat3, pat2);
13621 else
13622 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13623 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013624 if (flags & SP_START)
13625 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013626
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 save_cursor = curwin->w_cursor;
13628 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013629 clearpos(&firstpos);
13630 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 pat = pat3;
13632 for (;;)
13633 {
13634 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013635 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13637 /* didn't find it or found the first match again: FAIL */
13638 break;
13639
13640 if (firstpos.lnum == 0)
13641 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013642 if (equalpos(pos, foundpos))
13643 {
13644 /* Found the same position again. Can happen with a pattern that
13645 * has "\zs" at the end and searching backwards. Advance one
13646 * character and try again. */
13647 if (dir == BACKWARD)
13648 decl(&pos);
13649 else
13650 incl(&pos);
13651 }
13652 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653
13654 /* If the skip pattern matches, ignore this match. */
13655 if (*skip != NUL)
13656 {
13657 save_pos = curwin->w_cursor;
13658 curwin->w_cursor = pos;
13659 r = eval_to_bool(skip, &err, NULL, FALSE);
13660 curwin->w_cursor = save_pos;
13661 if (err)
13662 {
13663 /* Evaluating {skip} caused an error, break here. */
13664 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013665 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 break;
13667 }
13668 if (r)
13669 continue;
13670 }
13671
13672 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13673 {
13674 /* Found end when searching backwards or start when searching
13675 * forward: nested pair. */
13676 ++nest;
13677 pat = pat2; /* nested, don't search for middle */
13678 }
13679 else
13680 {
13681 /* Found end when searching forward or start when searching
13682 * backward: end of (nested) pair; or found middle in outer pair. */
13683 if (--nest == 1)
13684 pat = pat3; /* outer level, search for middle */
13685 }
13686
13687 if (nest == 0)
13688 {
13689 /* Found the match: return matchcount or line number. */
13690 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013691 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013693 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013694 if (flags & SP_SETPCMARK)
13695 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 curwin->w_cursor = pos;
13697 if (!(flags & SP_REPEAT))
13698 break;
13699 nest = 1; /* search for next unmatched */
13700 }
13701 }
13702
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013703 if (match_pos != NULL)
13704 {
13705 /* Store the match cursor position */
13706 match_pos->lnum = curwin->w_cursor.lnum;
13707 match_pos->col = curwin->w_cursor.col + 1;
13708 }
13709
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013711 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 curwin->w_cursor = save_cursor;
13713
13714theend:
13715 vim_free(pat2);
13716 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013718
13719 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720}
13721
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013722/*
13723 * "searchpos()" function
13724 */
13725 static void
13726f_searchpos(argvars, rettv)
13727 typval_T *argvars;
13728 typval_T *rettv;
13729{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013730 pos_T match_pos;
13731 int lnum = 0;
13732 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013733 int n;
13734 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013735
13736 rettv->vval.v_number = 0;
13737
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013738 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013739 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013740
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013741 n = search_cmn(argvars, &match_pos, &flags);
13742 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013743 {
13744 lnum = match_pos.lnum;
13745 col = match_pos.col;
13746 }
13747
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013748 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13749 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013750 if (flags & SP_SUBPAT)
13751 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013752}
13753
13754
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755/*ARGSUSED*/
13756 static void
13757f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 typval_T *argvars;
13759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761#ifdef FEAT_CLIENTSERVER
13762 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013763 char_u *server = get_tv_string_chk(&argvars[0]);
13764 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765
Bram Moolenaar0d660222005-01-07 21:51:51 +000013766 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 if (server == NULL || reply == NULL)
13768 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769 if (check_restricted() || check_secure())
13770 return;
13771# ifdef FEAT_X11
13772 if (check_connection() == FAIL)
13773 return;
13774# endif
13775
13776 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013778 EMSG(_("E258: Unable to send to client"));
13779 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013781 rettv->vval.v_number = 0;
13782#else
13783 rettv->vval.v_number = -1;
13784#endif
13785}
13786
13787/*ARGSUSED*/
13788 static void
13789f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 typval_T *argvars;
13791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013792{
13793 char_u *r = NULL;
13794
13795#ifdef FEAT_CLIENTSERVER
13796# ifdef WIN32
13797 r = serverGetVimNames();
13798# else
13799 make_connection();
13800 if (X_DISPLAY != NULL)
13801 r = serverGetVimNames(X_DISPLAY);
13802# endif
13803#endif
13804 rettv->v_type = VAR_STRING;
13805 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806}
13807
13808/*
13809 * "setbufvar()" function
13810 */
13811/*ARGSUSED*/
13812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 typval_T *argvars;
13815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816{
13817 buf_T *buf;
13818#ifdef FEAT_AUTOCMD
13819 aco_save_T aco;
13820#else
13821 buf_T *save_curbuf;
13822#endif
13823 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013824 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 char_u nbuf[NUMBUFLEN];
13826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013827 rettv->vval.v_number = 0;
13828
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 if (check_restricted() || check_secure())
13830 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13832 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 varp = &argvars[2];
13835
13836 if (buf != NULL && varname != NULL && varp != NULL)
13837 {
13838 /* set curbuf to be our buf, temporarily */
13839#ifdef FEAT_AUTOCMD
13840 aucmd_prepbuf(&aco, buf);
13841#else
13842 save_curbuf = curbuf;
13843 curbuf = buf;
13844#endif
13845
13846 if (*varname == '&')
13847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 long numval;
13849 char_u *strval;
13850 int error = FALSE;
13851
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013853 numval = get_tv_number_chk(varp, &error);
13854 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013855 if (!error && strval != NULL)
13856 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857 }
13858 else
13859 {
13860 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13861 if (bufvarname != NULL)
13862 {
13863 STRCPY(bufvarname, "b:");
13864 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013865 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 vim_free(bufvarname);
13867 }
13868 }
13869
13870 /* reset notion of buffer */
13871#ifdef FEAT_AUTOCMD
13872 aucmd_restbuf(&aco);
13873#else
13874 curbuf = save_curbuf;
13875#endif
13876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877}
13878
13879/*
13880 * "setcmdpos()" function
13881 */
13882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013883f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013884 typval_T *argvars;
13885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 int pos = (int)get_tv_number(&argvars[0]) - 1;
13888
13889 if (pos >= 0)
13890 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891}
13892
13893/*
13894 * "setline()" function
13895 */
13896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013897f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013898 typval_T *argvars;
13899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900{
13901 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013902 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013903 list_T *l = NULL;
13904 listitem_T *li = NULL;
13905 long added = 0;
13906 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013908 lnum = get_tv_lnum(&argvars[0]);
13909 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013911 l = argvars[1].vval.v_list;
13912 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013914 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013916
13917 rettv->vval.v_number = 0; /* OK */
13918 for (;;)
13919 {
13920 if (l != NULL)
13921 {
13922 /* list argument, get next string */
13923 if (li == NULL)
13924 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013926 li = li->li_next;
13927 }
13928
13929 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013930 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013931 break;
13932 if (lnum <= curbuf->b_ml.ml_line_count)
13933 {
13934 /* existing line, replace it */
13935 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13936 {
13937 changed_bytes(lnum, 0);
13938 check_cursor_col();
13939 rettv->vval.v_number = 0; /* OK */
13940 }
13941 }
13942 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13943 {
13944 /* lnum is one past the last line, append the line */
13945 ++added;
13946 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13947 rettv->vval.v_number = 0; /* OK */
13948 }
13949
13950 if (l == NULL) /* only one string argument */
13951 break;
13952 ++lnum;
13953 }
13954
13955 if (added > 0)
13956 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957}
13958
13959/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013960 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013961 */
13962/*ARGSUSED*/
13963 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013964set_qf_ll_list(wp, list_arg, action_arg, rettv)
13965 win_T *wp;
13966 typval_T *list_arg;
13967 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013968 typval_T *rettv;
13969{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013970#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013971 char_u *act;
13972 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013973#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013974
Bram Moolenaar2641f772005-03-25 21:58:17 +000013975 rettv->vval.v_number = -1;
13976
13977#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013978 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013979 EMSG(_(e_listreq));
13980 else
13981 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013982 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013983
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013984 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013985 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013986 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987 if (act == NULL)
13988 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013989 if (*act == 'a' || *act == 'r')
13990 action = *act;
13991 }
13992
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013993 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013994 rettv->vval.v_number = 0;
13995 }
13996#endif
13997}
13998
13999/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014000 * "setloclist()" function
14001 */
14002/*ARGSUSED*/
14003 static void
14004f_setloclist(argvars, rettv)
14005 typval_T *argvars;
14006 typval_T *rettv;
14007{
14008 win_T *win;
14009
14010 rettv->vval.v_number = -1;
14011
14012 win = find_win_by_nr(&argvars[0]);
14013 if (win != NULL)
14014 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14015}
14016
14017/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014018 * "setpos()" function
14019 */
14020/*ARGSUSED*/
14021 static void
14022f_setpos(argvars, rettv)
14023 typval_T *argvars;
14024 typval_T *rettv;
14025{
14026 pos_T pos;
14027 int fnum;
14028 char_u *name;
14029
14030 name = get_tv_string_chk(argvars);
14031 if (name != NULL)
14032 {
14033 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14034 {
14035 --pos.col;
14036 if (name[0] == '.') /* cursor */
14037 {
14038 if (fnum == curbuf->b_fnum)
14039 {
14040 curwin->w_cursor = pos;
14041 check_cursor();
14042 }
14043 else
14044 EMSG(_(e_invarg));
14045 }
14046 else if (name[0] == '\'') /* mark */
14047 (void)setmark_pos(name[1], &pos, fnum);
14048 else
14049 EMSG(_(e_invarg));
14050 }
14051 }
14052}
14053
14054/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014055 * "setqflist()" function
14056 */
14057/*ARGSUSED*/
14058 static void
14059f_setqflist(argvars, rettv)
14060 typval_T *argvars;
14061 typval_T *rettv;
14062{
14063 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14064}
14065
14066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 * "setreg()" function
14068 */
14069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014071 typval_T *argvars;
14072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073{
14074 int regname;
14075 char_u *strregname;
14076 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 int append;
14079 char_u yank_type;
14080 long block_len;
14081
14082 block_len = -1;
14083 yank_type = MAUTO;
14084 append = FALSE;
14085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 if (strregname == NULL)
14090 return; /* type error; errmsg already given */
14091 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 if (regname == 0 || regname == '@')
14093 regname = '"';
14094 else if (regname == '=')
14095 return;
14096
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014097 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099 stropt = get_tv_string_chk(&argvars[2]);
14100 if (stropt == NULL)
14101 return; /* type error */
14102 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 switch (*stropt)
14104 {
14105 case 'a': case 'A': /* append */
14106 append = TRUE;
14107 break;
14108 case 'v': case 'c': /* character-wise selection */
14109 yank_type = MCHAR;
14110 break;
14111 case 'V': case 'l': /* line-wise selection */
14112 yank_type = MLINE;
14113 break;
14114#ifdef FEAT_VISUAL
14115 case 'b': case Ctrl_V: /* block-wise selection */
14116 yank_type = MBLOCK;
14117 if (VIM_ISDIGIT(stropt[1]))
14118 {
14119 ++stropt;
14120 block_len = getdigits(&stropt) - 1;
14121 --stropt;
14122 }
14123 break;
14124#endif
14125 }
14126 }
14127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 strval = get_tv_string_chk(&argvars[1]);
14129 if (strval != NULL)
14130 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133}
14134
14135
14136/*
14137 * "setwinvar(expr)" function
14138 */
14139/*ARGSUSED*/
14140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014141f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014142 typval_T *argvars;
14143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144{
14145 win_T *win;
14146#ifdef FEAT_WINDOWS
14147 win_T *save_curwin;
14148#endif
14149 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014150 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 char_u nbuf[NUMBUFLEN];
14152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 rettv->vval.v_number = 0;
14154
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 if (check_restricted() || check_secure())
14156 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 varp = &argvars[2];
14160
14161 if (win != NULL && varname != NULL && varp != NULL)
14162 {
14163#ifdef FEAT_WINDOWS
14164 /* set curwin to be our win, temporarily */
14165 save_curwin = curwin;
14166 curwin = win;
14167 curbuf = curwin->w_buffer;
14168#endif
14169
14170 if (*varname == '&')
14171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 long numval;
14173 char_u *strval;
14174 int error = FALSE;
14175
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 numval = get_tv_number_chk(varp, &error);
14178 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 if (!error && strval != NULL)
14180 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 }
14182 else
14183 {
14184 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14185 if (winvarname != NULL)
14186 {
14187 STRCPY(winvarname, "w:");
14188 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014189 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 vim_free(winvarname);
14191 }
14192 }
14193
14194#ifdef FEAT_WINDOWS
14195 /* Restore current window, if it's still valid (autocomands can make
14196 * it invalid). */
14197 if (win_valid(save_curwin))
14198 {
14199 curwin = save_curwin;
14200 curbuf = curwin->w_buffer;
14201 }
14202#endif
14203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204}
14205
14206/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014207 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 */
14209 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014210f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014211 typval_T *argvars;
14212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014214 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215
Bram Moolenaar0d660222005-01-07 21:51:51 +000014216 p = get_tv_string(&argvars[0]);
14217 rettv->vval.v_string = vim_strsave(p);
14218 simplify_filename(rettv->vval.v_string); /* simplify in place */
14219 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220}
14221
Bram Moolenaar0d660222005-01-07 21:51:51 +000014222static int
14223#ifdef __BORLANDC__
14224 _RTLENTRYF
14225#endif
14226 item_compare __ARGS((const void *s1, const void *s2));
14227static int
14228#ifdef __BORLANDC__
14229 _RTLENTRYF
14230#endif
14231 item_compare2 __ARGS((const void *s1, const void *s2));
14232
14233static int item_compare_ic;
14234static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014236#define ITEM_COMPARE_FAIL 999
14237
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014239 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014241 static int
14242#ifdef __BORLANDC__
14243_RTLENTRYF
14244#endif
14245item_compare(s1, s2)
14246 const void *s1;
14247 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014249 char_u *p1, *p2;
14250 char_u *tofree1, *tofree2;
14251 int res;
14252 char_u numbuf1[NUMBUFLEN];
14253 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014255 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14256 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014257 if (item_compare_ic)
14258 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014260 res = STRCMP(p1, p2);
14261 vim_free(tofree1);
14262 vim_free(tofree2);
14263 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264}
14265
14266 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267#ifdef __BORLANDC__
14268_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014270item_compare2(s1, s2)
14271 const void *s1;
14272 const void *s2;
14273{
14274 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014275 typval_T rettv;
14276 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014277 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014279 /* shortcut after failure in previous call; compare all items equal */
14280 if (item_compare_func_err)
14281 return 0;
14282
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014283 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14284 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014285 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14286 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014287
14288 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14289 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014290 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014291 clear_tv(&argv[0]);
14292 clear_tv(&argv[1]);
14293
14294 if (res == FAIL)
14295 res = ITEM_COMPARE_FAIL;
14296 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 /* return value has wrong type */
14298 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14299 if (item_compare_func_err)
14300 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014301 clear_tv(&rettv);
14302 return res;
14303}
14304
14305/*
14306 * "sort({list})" function
14307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014309f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 typval_T *argvars;
14311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312{
Bram Moolenaar33570922005-01-25 22:26:29 +000014313 list_T *l;
14314 listitem_T *li;
14315 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316 long len;
14317 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318
Bram Moolenaar0d660222005-01-07 21:51:51 +000014319 rettv->vval.v_number = 0;
14320 if (argvars[0].v_type != VAR_LIST)
14321 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 else
14323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014325 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014326 return;
14327 rettv->vval.v_list = l;
14328 rettv->v_type = VAR_LIST;
14329 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330
Bram Moolenaar0d660222005-01-07 21:51:51 +000014331 len = list_len(l);
14332 if (len <= 1)
14333 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334
Bram Moolenaar0d660222005-01-07 21:51:51 +000014335 item_compare_ic = FALSE;
14336 item_compare_func = NULL;
14337 if (argvars[1].v_type != VAR_UNKNOWN)
14338 {
14339 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014341 else
14342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 int error = FALSE;
14344
14345 i = get_tv_number_chk(&argvars[1], &error);
14346 if (error)
14347 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014348 if (i == 1)
14349 item_compare_ic = TRUE;
14350 else
14351 item_compare_func = get_tv_string(&argvars[1]);
14352 }
14353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357 if (ptrs == NULL)
14358 return;
14359 i = 0;
14360 for (li = l->lv_first; li != NULL; li = li->li_next)
14361 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014364 /* test the compare function */
14365 if (item_compare_func != NULL
14366 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14367 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014368 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014370 {
14371 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014372 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014373 item_compare_func == NULL ? item_compare : item_compare2);
14374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 if (!item_compare_func_err)
14376 {
14377 /* Clear the List and append the items in the sorted order. */
14378 l->lv_first = l->lv_last = NULL;
14379 l->lv_len = 0;
14380 for (i = 0; i < len; ++i)
14381 list_append(l, ptrs[i]);
14382 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383 }
14384
14385 vim_free(ptrs);
14386 }
14387}
14388
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014389/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014390 * "soundfold({word})" function
14391 */
14392 static void
14393f_soundfold(argvars, rettv)
14394 typval_T *argvars;
14395 typval_T *rettv;
14396{
14397 char_u *s;
14398
14399 rettv->v_type = VAR_STRING;
14400 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014401#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014402 rettv->vval.v_string = eval_soundfold(s);
14403#else
14404 rettv->vval.v_string = vim_strsave(s);
14405#endif
14406}
14407
14408/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014409 * "spellbadword()" function
14410 */
14411/* ARGSUSED */
14412 static void
14413f_spellbadword(argvars, rettv)
14414 typval_T *argvars;
14415 typval_T *rettv;
14416{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014417 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014418 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014419 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014421 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014422 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014423
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014424#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014425 if (argvars[0].v_type == VAR_UNKNOWN)
14426 {
14427 /* Find the start and length of the badly spelled word. */
14428 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14429 if (len != 0)
14430 word = ml_get_cursor();
14431 }
14432 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14433 {
14434 char_u *str = get_tv_string_chk(&argvars[0]);
14435 int capcol = -1;
14436
14437 if (str != NULL)
14438 {
14439 /* Check the argument for spelling. */
14440 while (*str != NUL)
14441 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014442 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014443 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014444 {
14445 word = str;
14446 break;
14447 }
14448 str += len;
14449 }
14450 }
14451 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014452#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014454 list_append_string(rettv->vval.v_list, word, len);
14455 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014456 attr == HLF_SPB ? "bad" :
14457 attr == HLF_SPR ? "rare" :
14458 attr == HLF_SPL ? "local" :
14459 attr == HLF_SPC ? "caps" :
14460 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014461}
14462
14463/*
14464 * "spellsuggest()" function
14465 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014466/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014467 static void
14468f_spellsuggest(argvars, rettv)
14469 typval_T *argvars;
14470 typval_T *rettv;
14471{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014472#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014473 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014474 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014475 int maxcount;
14476 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014477 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014478 listitem_T *li;
14479 int need_capital = FALSE;
14480#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014482 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014483 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014484
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014485#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014486 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14487 {
14488 str = get_tv_string(&argvars[0]);
14489 if (argvars[1].v_type != VAR_UNKNOWN)
14490 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014491 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014492 if (maxcount <= 0)
14493 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014494 if (argvars[2].v_type != VAR_UNKNOWN)
14495 {
14496 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14497 if (typeerr)
14498 return;
14499 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014500 }
14501 else
14502 maxcount = 25;
14503
Bram Moolenaar4770d092006-01-12 23:22:24 +000014504 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014505
14506 for (i = 0; i < ga.ga_len; ++i)
14507 {
14508 str = ((char_u **)ga.ga_data)[i];
14509
14510 li = listitem_alloc();
14511 if (li == NULL)
14512 vim_free(str);
14513 else
14514 {
14515 li->li_tv.v_type = VAR_STRING;
14516 li->li_tv.v_lock = 0;
14517 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014518 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014519 }
14520 }
14521 ga_clear(&ga);
14522 }
14523#endif
14524}
14525
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014527f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014528 typval_T *argvars;
14529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014530{
14531 char_u *str;
14532 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014533 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014534 regmatch_T regmatch;
14535 char_u patbuf[NUMBUFLEN];
14536 char_u *save_cpo;
14537 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014539 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541
14542 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14543 save_cpo = p_cpo;
14544 p_cpo = (char_u *)"";
14545
14546 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014547 if (argvars[1].v_type != VAR_UNKNOWN)
14548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14550 if (pat == NULL)
14551 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014552 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014553 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014554 }
14555 if (pat == NULL || *pat == NUL)
14556 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014558 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 if (typeerr)
14561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14564 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014567 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014569 if (*str == NUL)
14570 match = FALSE; /* empty item at the end */
14571 else
14572 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573 if (match)
14574 end = regmatch.startp[0];
14575 else
14576 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014577 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14578 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014580 if (list_append_string(rettv->vval.v_list, str,
14581 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583 }
14584 if (!match)
14585 break;
14586 /* Advance to just after the match. */
14587 if (regmatch.endp[0] > str)
14588 col = 0;
14589 else
14590 {
14591 /* Don't get stuck at the same match. */
14592#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014593 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014594#else
14595 col = 1;
14596#endif
14597 }
14598 str = regmatch.endp[0];
14599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605}
14606
Bram Moolenaar2c932302006-03-18 21:42:09 +000014607/*
14608 * "str2nr()" function
14609 */
14610 static void
14611f_str2nr(argvars, rettv)
14612 typval_T *argvars;
14613 typval_T *rettv;
14614{
14615 int base = 10;
14616 char_u *p;
14617 long n;
14618
14619 if (argvars[1].v_type != VAR_UNKNOWN)
14620 {
14621 base = get_tv_number(&argvars[1]);
14622 if (base != 8 && base != 10 && base != 16)
14623 {
14624 EMSG(_(e_invarg));
14625 return;
14626 }
14627 }
14628
14629 p = skipwhite(get_tv_string(&argvars[0]));
14630 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14631 rettv->vval.v_number = n;
14632}
14633
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634#ifdef HAVE_STRFTIME
14635/*
14636 * "strftime({format}[, {time}])" function
14637 */
14638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014640 typval_T *argvars;
14641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642{
14643 char_u result_buf[256];
14644 struct tm *curtime;
14645 time_t seconds;
14646 char_u *p;
14647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014648 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 seconds = time(NULL);
14653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014654 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 curtime = localtime(&seconds);
14656 /* MSVC returns NULL for an invalid value of seconds. */
14657 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014658 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 else
14660 {
14661# ifdef FEAT_MBYTE
14662 vimconv_T conv;
14663 char_u *enc;
14664
14665 conv.vc_type = CONV_NONE;
14666 enc = enc_locale();
14667 convert_setup(&conv, p_enc, enc);
14668 if (conv.vc_type != CONV_NONE)
14669 p = string_convert(&conv, p, NULL);
14670# endif
14671 if (p != NULL)
14672 (void)strftime((char *)result_buf, sizeof(result_buf),
14673 (char *)p, curtime);
14674 else
14675 result_buf[0] = NUL;
14676
14677# ifdef FEAT_MBYTE
14678 if (conv.vc_type != CONV_NONE)
14679 vim_free(p);
14680 convert_setup(&conv, enc, p_enc);
14681 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 else
14684# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686
14687# ifdef FEAT_MBYTE
14688 /* Release conversion descriptors */
14689 convert_setup(&conv, NULL, NULL);
14690 vim_free(enc);
14691# endif
14692 }
14693}
14694#endif
14695
14696/*
14697 * "stridx()" function
14698 */
14699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014700f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014701 typval_T *argvars;
14702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703{
14704 char_u buf[NUMBUFLEN];
14705 char_u *needle;
14706 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014707 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014711 needle = get_tv_string_chk(&argvars[1]);
14712 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014713 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014714 if (needle == NULL || haystack == NULL)
14715 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716
Bram Moolenaar33570922005-01-25 22:26:29 +000014717 if (argvars[2].v_type != VAR_UNKNOWN)
14718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014719 int error = FALSE;
14720
14721 start_idx = get_tv_number_chk(&argvars[2], &error);
14722 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014723 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014724 if (start_idx >= 0)
14725 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 }
14727
14728 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14729 if (pos != NULL)
14730 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731}
14732
14733/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014734 * "string()" function
14735 */
14736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014737f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 typval_T *argvars;
14739 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014740{
14741 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014742 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014745 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014746 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014747 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748}
14749
14750/*
14751 * "strlen()" function
14752 */
14753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014755 typval_T *argvars;
14756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 rettv->vval.v_number = (varnumber_T)(STRLEN(
14759 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760}
14761
14762/*
14763 * "strpart()" function
14764 */
14765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014767 typval_T *argvars;
14768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769{
14770 char_u *p;
14771 int n;
14772 int len;
14773 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014776 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 slen = (int)STRLEN(p);
14778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014779 n = get_tv_number_chk(&argvars[1], &error);
14780 if (error)
14781 len = 0;
14782 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784 else
14785 len = slen - n; /* default len: all bytes that are available. */
14786
14787 /*
14788 * Only return the overlap between the specified part and the actual
14789 * string.
14790 */
14791 if (n < 0)
14792 {
14793 len += n;
14794 n = 0;
14795 }
14796 else if (n > slen)
14797 n = slen;
14798 if (len < 0)
14799 len = 0;
14800 else if (n + len > slen)
14801 len = slen - n;
14802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014803 rettv->v_type = VAR_STRING;
14804 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805}
14806
14807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014808 * "strridx()" function
14809 */
14810 static void
14811f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014812 typval_T *argvars;
14813 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014814{
14815 char_u buf[NUMBUFLEN];
14816 char_u *needle;
14817 char_u *haystack;
14818 char_u *rest;
14819 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014820 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 needle = get_tv_string_chk(&argvars[1]);
14823 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014824 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825
14826 rettv->vval.v_number = -1;
14827 if (needle == NULL || haystack == NULL)
14828 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014829 if (argvars[2].v_type != VAR_UNKNOWN)
14830 {
14831 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014832 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014833 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014834 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014835 }
14836 else
14837 end_idx = haystack_len;
14838
Bram Moolenaar0d660222005-01-07 21:51:51 +000014839 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014840 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014842 lastmatch = haystack + end_idx;
14843 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014844 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014845 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014846 for (rest = haystack; *rest != '\0'; ++rest)
14847 {
14848 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014849 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014850 break;
14851 lastmatch = rest;
14852 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014853 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014854
14855 if (lastmatch == NULL)
14856 rettv->vval.v_number = -1;
14857 else
14858 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14859}
14860
14861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 * "strtrans()" function
14863 */
14864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 typval_T *argvars;
14867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014869 rettv->v_type = VAR_STRING;
14870 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871}
14872
14873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874 * "submatch()" function
14875 */
14876 static void
14877f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014878 typval_T *argvars;
14879 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880{
14881 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014882 rettv->vval.v_string =
14883 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014884}
14885
14886/*
14887 * "substitute()" function
14888 */
14889 static void
14890f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014891 typval_T *argvars;
14892 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014893{
14894 char_u patbuf[NUMBUFLEN];
14895 char_u subbuf[NUMBUFLEN];
14896 char_u flagsbuf[NUMBUFLEN];
14897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014898 char_u *str = get_tv_string_chk(&argvars[0]);
14899 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14900 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14901 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14902
Bram Moolenaar0d660222005-01-07 21:51:51 +000014903 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014904 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14905 rettv->vval.v_string = NULL;
14906 else
14907 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014908}
14909
14910/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014911 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 */
14913/*ARGSUSED*/
14914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014916 typval_T *argvars;
14917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918{
14919 int id = 0;
14920#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014921 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 long col;
14923 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014924 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014926 lnum = get_tv_lnum(argvars); /* -1 on type error */
14927 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14928 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014930 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014931 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014932 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933#endif
14934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936}
14937
14938/*
14939 * "synIDattr(id, what [, mode])" function
14940 */
14941/*ARGSUSED*/
14942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014943f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014944 typval_T *argvars;
14945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946{
14947 char_u *p = NULL;
14948#ifdef FEAT_SYN_HL
14949 int id;
14950 char_u *what;
14951 char_u *mode;
14952 char_u modebuf[NUMBUFLEN];
14953 int modec;
14954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 id = get_tv_number(&argvars[0]);
14956 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014957 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 modec = TOLOWER_ASC(mode[0]);
14961 if (modec != 't' && modec != 'c'
14962#ifdef FEAT_GUI
14963 && modec != 'g'
14964#endif
14965 )
14966 modec = 0; /* replace invalid with current */
14967 }
14968 else
14969 {
14970#ifdef FEAT_GUI
14971 if (gui.in_use)
14972 modec = 'g';
14973 else
14974#endif
14975 if (t_colors > 1)
14976 modec = 'c';
14977 else
14978 modec = 't';
14979 }
14980
14981
14982 switch (TOLOWER_ASC(what[0]))
14983 {
14984 case 'b':
14985 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14986 p = highlight_color(id, what, modec);
14987 else /* bold */
14988 p = highlight_has_attr(id, HL_BOLD, modec);
14989 break;
14990
14991 case 'f': /* fg[#] */
14992 p = highlight_color(id, what, modec);
14993 break;
14994
14995 case 'i':
14996 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14997 p = highlight_has_attr(id, HL_INVERSE, modec);
14998 else /* italic */
14999 p = highlight_has_attr(id, HL_ITALIC, modec);
15000 break;
15001
15002 case 'n': /* name */
15003 p = get_highlight_name(NULL, id - 1);
15004 break;
15005
15006 case 'r': /* reverse */
15007 p = highlight_has_attr(id, HL_INVERSE, modec);
15008 break;
15009
15010 case 's': /* standout */
15011 p = highlight_has_attr(id, HL_STANDOUT, modec);
15012 break;
15013
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015014 case 'u':
15015 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15016 /* underline */
15017 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15018 else
15019 /* undercurl */
15020 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 break;
15022 }
15023
15024 if (p != NULL)
15025 p = vim_strsave(p);
15026#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027 rettv->v_type = VAR_STRING;
15028 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029}
15030
15031/*
15032 * "synIDtrans(id)" function
15033 */
15034/*ARGSUSED*/
15035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015036f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 typval_T *argvars;
15038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039{
15040 int id;
15041
15042#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015043 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044
15045 if (id > 0)
15046 id = syn_get_final_id(id);
15047 else
15048#endif
15049 id = 0;
15050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052}
15053
15054/*
15055 * "system()" function
15056 */
15057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015058f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015059 typval_T *argvars;
15060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015062 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015064 char_u *infile = NULL;
15065 char_u buf[NUMBUFLEN];
15066 int err = FALSE;
15067 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015069 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015070 {
15071 /*
15072 * Write the string to a temp file, to be used for input of the shell
15073 * command.
15074 */
15075 if ((infile = vim_tempname('i')) == NULL)
15076 {
15077 EMSG(_(e_notmp));
15078 return;
15079 }
15080
15081 fd = mch_fopen((char *)infile, WRITEBIN);
15082 if (fd == NULL)
15083 {
15084 EMSG2(_(e_notopen), infile);
15085 goto done;
15086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015087 p = get_tv_string_buf_chk(&argvars[1], buf);
15088 if (p == NULL)
15089 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015090 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15091 err = TRUE;
15092 if (fclose(fd) != 0)
15093 err = TRUE;
15094 if (err)
15095 {
15096 EMSG(_("E677: Error writing temp file"));
15097 goto done;
15098 }
15099 }
15100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015101 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103#ifdef USE_CR
15104 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015105 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 {
15107 char_u *s;
15108
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015109 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 {
15111 if (*s == CAR)
15112 *s = NL;
15113 }
15114 }
15115#else
15116# ifdef USE_CRNL
15117 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015118 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 {
15120 char_u *s, *d;
15121
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015122 d = res;
15123 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124 {
15125 if (s[0] == CAR && s[1] == NL)
15126 ++s;
15127 *d++ = *s;
15128 }
15129 *d = NUL;
15130 }
15131# endif
15132#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015133
15134done:
15135 if (infile != NULL)
15136 {
15137 mch_remove(infile);
15138 vim_free(infile);
15139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 rettv->v_type = VAR_STRING;
15141 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142}
15143
15144/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015145 * "tabpagebuflist()" function
15146 */
15147/* ARGSUSED */
15148 static void
15149f_tabpagebuflist(argvars, rettv)
15150 typval_T *argvars;
15151 typval_T *rettv;
15152{
15153#ifndef FEAT_WINDOWS
15154 rettv->vval.v_number = 0;
15155#else
15156 tabpage_T *tp;
15157 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015158
15159 if (argvars[0].v_type == VAR_UNKNOWN)
15160 wp = firstwin;
15161 else
15162 {
15163 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15164 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015165 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015166 }
15167 if (wp == NULL)
15168 rettv->vval.v_number = 0;
15169 else
15170 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015171 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015172 rettv->vval.v_number = 0;
15173 else
15174 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015175 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015176 if (list_append_number(rettv->vval.v_list,
15177 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015178 break;
15179 }
15180 }
15181#endif
15182}
15183
15184
15185/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015186 * "tabpagenr()" function
15187 */
15188/* ARGSUSED */
15189 static void
15190f_tabpagenr(argvars, rettv)
15191 typval_T *argvars;
15192 typval_T *rettv;
15193{
15194 int nr = 1;
15195#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015196 char_u *arg;
15197
15198 if (argvars[0].v_type != VAR_UNKNOWN)
15199 {
15200 arg = get_tv_string_chk(&argvars[0]);
15201 nr = 0;
15202 if (arg != NULL)
15203 {
15204 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015205 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015206 else
15207 EMSG2(_(e_invexpr2), arg);
15208 }
15209 }
15210 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015211 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015212#endif
15213 rettv->vval.v_number = nr;
15214}
15215
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015216
15217#ifdef FEAT_WINDOWS
15218static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15219
15220/*
15221 * Common code for tabpagewinnr() and winnr().
15222 */
15223 static int
15224get_winnr(tp, argvar)
15225 tabpage_T *tp;
15226 typval_T *argvar;
15227{
15228 win_T *twin;
15229 int nr = 1;
15230 win_T *wp;
15231 char_u *arg;
15232
15233 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15234 if (argvar->v_type != VAR_UNKNOWN)
15235 {
15236 arg = get_tv_string_chk(argvar);
15237 if (arg == NULL)
15238 nr = 0; /* type error; errmsg already given */
15239 else if (STRCMP(arg, "$") == 0)
15240 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15241 else if (STRCMP(arg, "#") == 0)
15242 {
15243 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15244 if (twin == NULL)
15245 nr = 0;
15246 }
15247 else
15248 {
15249 EMSG2(_(e_invexpr2), arg);
15250 nr = 0;
15251 }
15252 }
15253
15254 if (nr > 0)
15255 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15256 wp != twin; wp = wp->w_next)
15257 ++nr;
15258 return nr;
15259}
15260#endif
15261
15262/*
15263 * "tabpagewinnr()" function
15264 */
15265/* ARGSUSED */
15266 static void
15267f_tabpagewinnr(argvars, rettv)
15268 typval_T *argvars;
15269 typval_T *rettv;
15270{
15271 int nr = 1;
15272#ifdef FEAT_WINDOWS
15273 tabpage_T *tp;
15274
15275 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15276 if (tp == NULL)
15277 nr = 0;
15278 else
15279 nr = get_winnr(tp, &argvars[1]);
15280#endif
15281 rettv->vval.v_number = nr;
15282}
15283
15284
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015285/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015286 * "tagfiles()" function
15287 */
15288/*ARGSUSED*/
15289 static void
15290f_tagfiles(argvars, rettv)
15291 typval_T *argvars;
15292 typval_T *rettv;
15293{
15294 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015295 tagname_T tn;
15296 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015297
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015298 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015299 {
15300 rettv->vval.v_number = 0;
15301 return;
15302 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015304 for (first = TRUE; ; first = FALSE)
15305 if (get_tagfname(&tn, first, fname) == FAIL
15306 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015307 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015308 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015309}
15310
15311/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015312 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015313 */
15314 static void
15315f_taglist(argvars, rettv)
15316 typval_T *argvars;
15317 typval_T *rettv;
15318{
15319 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015320
15321 tag_pattern = get_tv_string(&argvars[0]);
15322
15323 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 if (*tag_pattern == NUL)
15325 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015326
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015327 if (rettv_list_alloc(rettv) == OK)
15328 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015329}
15330
15331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 * "tempname()" function
15333 */
15334/*ARGSUSED*/
15335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015336f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015337 typval_T *argvars;
15338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339{
15340 static int x = 'A';
15341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342 rettv->v_type = VAR_STRING;
15343 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344
15345 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15346 * names. Skip 'I' and 'O', they are used for shell redirection. */
15347 do
15348 {
15349 if (x == 'Z')
15350 x = '0';
15351 else if (x == '9')
15352 x = 'A';
15353 else
15354 {
15355#ifdef EBCDIC
15356 if (x == 'I')
15357 x = 'J';
15358 else if (x == 'R')
15359 x = 'S';
15360 else
15361#endif
15362 ++x;
15363 }
15364 } while (x == 'I' || x == 'O');
15365}
15366
15367/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015368 * "test(list)" function: Just checking the walls...
15369 */
15370/*ARGSUSED*/
15371 static void
15372f_test(argvars, rettv)
15373 typval_T *argvars;
15374 typval_T *rettv;
15375{
15376 /* Used for unit testing. Change the code below to your liking. */
15377#if 0
15378 listitem_T *li;
15379 list_T *l;
15380 char_u *bad, *good;
15381
15382 if (argvars[0].v_type != VAR_LIST)
15383 return;
15384 l = argvars[0].vval.v_list;
15385 if (l == NULL)
15386 return;
15387 li = l->lv_first;
15388 if (li == NULL)
15389 return;
15390 bad = get_tv_string(&li->li_tv);
15391 li = li->li_next;
15392 if (li == NULL)
15393 return;
15394 good = get_tv_string(&li->li_tv);
15395 rettv->vval.v_number = test_edit_score(bad, good);
15396#endif
15397}
15398
15399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 * "tolower(string)" function
15401 */
15402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015404 typval_T *argvars;
15405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406{
15407 char_u *p;
15408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 p = vim_strsave(get_tv_string(&argvars[0]));
15410 rettv->v_type = VAR_STRING;
15411 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412
15413 if (p != NULL)
15414 while (*p != NUL)
15415 {
15416#ifdef FEAT_MBYTE
15417 int l;
15418
15419 if (enc_utf8)
15420 {
15421 int c, lc;
15422
15423 c = utf_ptr2char(p);
15424 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015425 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 /* TODO: reallocate string when byte count changes. */
15427 if (utf_char2len(lc) == l)
15428 utf_char2bytes(lc, p);
15429 p += l;
15430 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015431 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 p += l; /* skip multi-byte character */
15433 else
15434#endif
15435 {
15436 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15437 ++p;
15438 }
15439 }
15440}
15441
15442/*
15443 * "toupper(string)" function
15444 */
15445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015446f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015447 typval_T *argvars;
15448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015451 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452}
15453
15454/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015455 * "tr(string, fromstr, tostr)" function
15456 */
15457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 typval_T *argvars;
15460 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015461{
15462 char_u *instr;
15463 char_u *fromstr;
15464 char_u *tostr;
15465 char_u *p;
15466#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015467 int inlen;
15468 int fromlen;
15469 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015470 int idx;
15471 char_u *cpstr;
15472 int cplen;
15473 int first = TRUE;
15474#endif
15475 char_u buf[NUMBUFLEN];
15476 char_u buf2[NUMBUFLEN];
15477 garray_T ga;
15478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015480 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15481 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015482
15483 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484 rettv->v_type = VAR_STRING;
15485 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015486 if (fromstr == NULL || tostr == NULL)
15487 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015488 ga_init2(&ga, (int)sizeof(char), 80);
15489
15490#ifdef FEAT_MBYTE
15491 if (!has_mbyte)
15492#endif
15493 /* not multi-byte: fromstr and tostr must be the same length */
15494 if (STRLEN(fromstr) != STRLEN(tostr))
15495 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015496#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015497error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015498#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015499 EMSG2(_(e_invarg2), fromstr);
15500 ga_clear(&ga);
15501 return;
15502 }
15503
15504 /* fromstr and tostr have to contain the same number of chars */
15505 while (*instr != NUL)
15506 {
15507#ifdef FEAT_MBYTE
15508 if (has_mbyte)
15509 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015510 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015511 cpstr = instr;
15512 cplen = inlen;
15513 idx = 0;
15514 for (p = fromstr; *p != NUL; p += fromlen)
15515 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015516 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015517 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15518 {
15519 for (p = tostr; *p != NUL; p += tolen)
15520 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015521 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015522 if (idx-- == 0)
15523 {
15524 cplen = tolen;
15525 cpstr = p;
15526 break;
15527 }
15528 }
15529 if (*p == NUL) /* tostr is shorter than fromstr */
15530 goto error;
15531 break;
15532 }
15533 ++idx;
15534 }
15535
15536 if (first && cpstr == instr)
15537 {
15538 /* Check that fromstr and tostr have the same number of
15539 * (multi-byte) characters. Done only once when a character
15540 * of instr doesn't appear in fromstr. */
15541 first = FALSE;
15542 for (p = tostr; *p != NUL; p += tolen)
15543 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015544 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015545 --idx;
15546 }
15547 if (idx != 0)
15548 goto error;
15549 }
15550
15551 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015552 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015553 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015554
15555 instr += inlen;
15556 }
15557 else
15558#endif
15559 {
15560 /* When not using multi-byte chars we can do it faster. */
15561 p = vim_strchr(fromstr, *instr);
15562 if (p != NULL)
15563 ga_append(&ga, tostr[p - fromstr]);
15564 else
15565 ga_append(&ga, *instr);
15566 ++instr;
15567 }
15568 }
15569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015571}
15572
15573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 * "type(expr)" function
15575 */
15576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015577f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015578 typval_T *argvars;
15579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015581 int n;
15582
15583 switch (argvars[0].v_type)
15584 {
15585 case VAR_NUMBER: n = 0; break;
15586 case VAR_STRING: n = 1; break;
15587 case VAR_FUNC: n = 2; break;
15588 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015589 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015590 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15591 }
15592 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593}
15594
15595/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015596 * "values(dict)" function
15597 */
15598 static void
15599f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015600 typval_T *argvars;
15601 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015602{
15603 dict_list(argvars, rettv, 1);
15604}
15605
15606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 * "virtcol(string)" function
15608 */
15609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015611 typval_T *argvars;
15612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
15614 colnr_T vcol = 0;
15615 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015616 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015618 fp = var2fpos(&argvars[0], FALSE, &fnum);
15619 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15620 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 {
15622 getvvcol(curwin, fp, NULL, NULL, &vcol);
15623 ++vcol;
15624 }
15625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627}
15628
15629/*
15630 * "visualmode()" function
15631 */
15632/*ARGSUSED*/
15633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015634f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015635 typval_T *argvars;
15636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637{
15638#ifdef FEAT_VISUAL
15639 char_u str[2];
15640
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015641 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 str[0] = curbuf->b_visual_mode_eval;
15643 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015644 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645
15646 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015647 if ((argvars[0].v_type == VAR_NUMBER
15648 && argvars[0].vval.v_number != 0)
15649 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015650 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 curbuf->b_visual_mode_eval = NUL;
15652#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015653 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654#endif
15655}
15656
15657/*
15658 * "winbufnr(nr)" function
15659 */
15660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015662 typval_T *argvars;
15663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664{
15665 win_T *wp;
15666
15667 wp = find_win_by_nr(&argvars[0]);
15668 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015669 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015671 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672}
15673
15674/*
15675 * "wincol()" function
15676 */
15677/*ARGSUSED*/
15678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 typval_T *argvars;
15681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682{
15683 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015684 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685}
15686
15687/*
15688 * "winheight(nr)" function
15689 */
15690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015692 typval_T *argvars;
15693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694{
15695 win_T *wp;
15696
15697 wp = find_win_by_nr(&argvars[0]);
15698 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015699 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702}
15703
15704/*
15705 * "winline()" function
15706 */
15707/*ARGSUSED*/
15708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015709f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015710 typval_T *argvars;
15711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712{
15713 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015714 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715}
15716
15717/*
15718 * "winnr()" function
15719 */
15720/* ARGSUSED */
15721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015722f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015723 typval_T *argvars;
15724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725{
15726 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015727
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015729 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015731 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732}
15733
15734/*
15735 * "winrestcmd()" function
15736 */
15737/* ARGSUSED */
15738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015739f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015740 typval_T *argvars;
15741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742{
15743#ifdef FEAT_WINDOWS
15744 win_T *wp;
15745 int winnr = 1;
15746 garray_T ga;
15747 char_u buf[50];
15748
15749 ga_init2(&ga, (int)sizeof(char), 70);
15750 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15751 {
15752 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15753 ga_concat(&ga, buf);
15754# ifdef FEAT_VERTSPLIT
15755 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15756 ga_concat(&ga, buf);
15757# endif
15758 ++winnr;
15759 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015760 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767}
15768
15769/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015770 * "winrestview()" function
15771 */
15772/* ARGSUSED */
15773 static void
15774f_winrestview(argvars, rettv)
15775 typval_T *argvars;
15776 typval_T *rettv;
15777{
15778 dict_T *dict;
15779
15780 if (argvars[0].v_type != VAR_DICT
15781 || (dict = argvars[0].vval.v_dict) == NULL)
15782 EMSG(_(e_invarg));
15783 else
15784 {
15785 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15786 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15787#ifdef FEAT_VIRTUALEDIT
15788 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15789#endif
15790 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015791 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015792
15793 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15794#ifdef FEAT_DIFF
15795 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15796#endif
15797 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15798 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15799
15800 check_cursor();
15801 changed_cline_bef_curs();
15802 invalidate_botline();
15803 redraw_later(VALID);
15804
15805 if (curwin->w_topline == 0)
15806 curwin->w_topline = 1;
15807 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15808 curwin->w_topline = curbuf->b_ml.ml_line_count;
15809#ifdef FEAT_DIFF
15810 check_topfill(curwin, TRUE);
15811#endif
15812 }
15813}
15814
15815/*
15816 * "winsaveview()" function
15817 */
15818/* ARGSUSED */
15819 static void
15820f_winsaveview(argvars, rettv)
15821 typval_T *argvars;
15822 typval_T *rettv;
15823{
15824 dict_T *dict;
15825
15826 dict = dict_alloc();
15827 if (dict == NULL)
15828 return;
15829 rettv->v_type = VAR_DICT;
15830 rettv->vval.v_dict = dict;
15831 ++dict->dv_refcount;
15832
15833 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15834 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15835#ifdef FEAT_VIRTUALEDIT
15836 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15837#endif
15838 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15839
15840 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15841#ifdef FEAT_DIFF
15842 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15843#endif
15844 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15845 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15846}
15847
15848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 * "winwidth(nr)" function
15850 */
15851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015852f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 typval_T *argvars;
15854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855{
15856 win_T *wp;
15857
15858 wp = find_win_by_nr(&argvars[0]);
15859 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015860 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861 else
15862#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015863 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015865 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866#endif
15867}
15868
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015870 * "writefile()" function
15871 */
15872 static void
15873f_writefile(argvars, rettv)
15874 typval_T *argvars;
15875 typval_T *rettv;
15876{
15877 int binary = FALSE;
15878 char_u *fname;
15879 FILE *fd;
15880 listitem_T *li;
15881 char_u *s;
15882 int ret = 0;
15883 int c;
15884
15885 if (argvars[0].v_type != VAR_LIST)
15886 {
15887 EMSG2(_(e_listarg), "writefile()");
15888 return;
15889 }
15890 if (argvars[0].vval.v_list == NULL)
15891 return;
15892
15893 if (argvars[2].v_type != VAR_UNKNOWN
15894 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15895 binary = TRUE;
15896
15897 /* Always open the file in binary mode, library functions have a mind of
15898 * their own about CR-LF conversion. */
15899 fname = get_tv_string(&argvars[1]);
15900 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15901 {
15902 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15903 ret = -1;
15904 }
15905 else
15906 {
15907 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15908 li = li->li_next)
15909 {
15910 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15911 {
15912 if (*s == '\n')
15913 c = putc(NUL, fd);
15914 else
15915 c = putc(*s, fd);
15916 if (c == EOF)
15917 {
15918 ret = -1;
15919 break;
15920 }
15921 }
15922 if (!binary || li->li_next != NULL)
15923 if (putc('\n', fd) == EOF)
15924 {
15925 ret = -1;
15926 break;
15927 }
15928 if (ret < 0)
15929 {
15930 EMSG(_(e_write));
15931 break;
15932 }
15933 }
15934 fclose(fd);
15935 }
15936
15937 rettv->vval.v_number = ret;
15938}
15939
15940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015941 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015942 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 */
15944 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015945var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015946 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015948 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015950 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015952 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953
Bram Moolenaara5525202006-03-02 22:52:09 +000015954 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015955 if (varp->v_type == VAR_LIST)
15956 {
15957 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015958 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015959 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015960
15961 l = varp->vval.v_list;
15962 if (l == NULL)
15963 return NULL;
15964
15965 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015966 pos.lnum = list_find_nr(l, 0L, &error);
15967 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015968 return NULL; /* invalid line number */
15969
15970 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015971 pos.col = list_find_nr(l, 1L, &error);
15972 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015973 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015974 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015975 /* Accept a position up to the NUL after the line. */
15976 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015977 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015978 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015979
Bram Moolenaara5525202006-03-02 22:52:09 +000015980#ifdef FEAT_VIRTUALEDIT
15981 /* Get the virtual offset. Defaults to zero. */
15982 pos.coladd = list_find_nr(l, 2L, &error);
15983 if (error)
15984 pos.coladd = 0;
15985#endif
15986
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015987 return &pos;
15988 }
15989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 name = get_tv_string_chk(varp);
15991 if (name == NULL)
15992 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 if (name[0] == '.') /* cursor */
15994 return &curwin->w_cursor;
15995 if (name[0] == '\'') /* mark */
15996 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015997 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15999 return NULL;
16000 return pp;
16001 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016002
16003#ifdef FEAT_VIRTUALEDIT
16004 pos.coladd = 0;
16005#endif
16006
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016007 if (name[0] == 'w' && lnum)
16008 {
16009 pos.col = 0;
16010 if (name[1] == '0') /* "w0": first visible line */
16011 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016012 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016013 pos.lnum = curwin->w_topline;
16014 return &pos;
16015 }
16016 else if (name[1] == '$') /* "w$": last visible line */
16017 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016018 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016019 pos.lnum = curwin->w_botline - 1;
16020 return &pos;
16021 }
16022 }
16023 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 {
16025 if (lnum)
16026 {
16027 pos.lnum = curbuf->b_ml.ml_line_count;
16028 pos.col = 0;
16029 }
16030 else
16031 {
16032 pos.lnum = curwin->w_cursor.lnum;
16033 pos.col = (colnr_T)STRLEN(ml_get_curline());
16034 }
16035 return &pos;
16036 }
16037 return NULL;
16038}
16039
16040/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016041 * Convert list in "arg" into a position and optional file number.
16042 * When "fnump" is NULL there is no file number, only 3 items.
16043 * Note that the column is passed on as-is, the caller may want to decrement
16044 * it to use 1 for the first column.
16045 * Return FAIL when conversion is not possible, doesn't check the position for
16046 * validity.
16047 */
16048 static int
16049list2fpos(arg, posp, fnump)
16050 typval_T *arg;
16051 pos_T *posp;
16052 int *fnump;
16053{
16054 list_T *l = arg->vval.v_list;
16055 long i = 0;
16056 long n;
16057
16058 /* List must be: [fnum, lnum, col, coladd] */
16059 if (arg->v_type != VAR_LIST || l == NULL
16060 || l->lv_len != (fnump == NULL ? 3 : 4))
16061 return FAIL;
16062
16063 if (fnump != NULL)
16064 {
16065 n = list_find_nr(l, i++, NULL); /* fnum */
16066 if (n < 0)
16067 return FAIL;
16068 if (n == 0)
16069 n = curbuf->b_fnum; /* current buffer */
16070 *fnump = n;
16071 }
16072
16073 n = list_find_nr(l, i++, NULL); /* lnum */
16074 if (n < 0)
16075 return FAIL;
16076 posp->lnum = n;
16077
16078 n = list_find_nr(l, i++, NULL); /* col */
16079 if (n < 0)
16080 return FAIL;
16081 posp->col = n;
16082
16083#ifdef FEAT_VIRTUALEDIT
16084 n = list_find_nr(l, i, NULL);
16085 if (n < 0)
16086 return FAIL;
16087 posp->coladd = n;
16088#endif
16089
16090 return OK;
16091}
16092
16093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094 * Get the length of an environment variable name.
16095 * Advance "arg" to the first character after the name.
16096 * Return 0 for error.
16097 */
16098 static int
16099get_env_len(arg)
16100 char_u **arg;
16101{
16102 char_u *p;
16103 int len;
16104
16105 for (p = *arg; vim_isIDc(*p); ++p)
16106 ;
16107 if (p == *arg) /* no name found */
16108 return 0;
16109
16110 len = (int)(p - *arg);
16111 *arg = p;
16112 return len;
16113}
16114
16115/*
16116 * Get the length of the name of a function or internal variable.
16117 * "arg" is advanced to the first non-white character after the name.
16118 * Return 0 if something is wrong.
16119 */
16120 static int
16121get_id_len(arg)
16122 char_u **arg;
16123{
16124 char_u *p;
16125 int len;
16126
16127 /* Find the end of the name. */
16128 for (p = *arg; eval_isnamec(*p); ++p)
16129 ;
16130 if (p == *arg) /* no name found */
16131 return 0;
16132
16133 len = (int)(p - *arg);
16134 *arg = skipwhite(p);
16135
16136 return len;
16137}
16138
16139/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016140 * Get the length of the name of a variable or function.
16141 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016143 * Return -1 if curly braces expansion failed.
16144 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 * If the name contains 'magic' {}'s, expand them and return the
16146 * expanded name in an allocated string via 'alias' - caller must free.
16147 */
16148 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016149get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 char_u **arg;
16151 char_u **alias;
16152 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016153 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154{
16155 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 char_u *p;
16157 char_u *expr_start;
16158 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
16160 *alias = NULL; /* default to no alias */
16161
16162 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16163 && (*arg)[2] == (int)KE_SNR)
16164 {
16165 /* hard coded <SNR>, already translated */
16166 *arg += 3;
16167 return get_id_len(arg) + 3;
16168 }
16169 len = eval_fname_script(*arg);
16170 if (len > 0)
16171 {
16172 /* literal "<SID>", "s:" or "<SNR>" */
16173 *arg += len;
16174 }
16175
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016177 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016179 p = find_name_end(*arg, &expr_start, &expr_end,
16180 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181 if (expr_start != NULL)
16182 {
16183 char_u *temp_string;
16184
16185 if (!evaluate)
16186 {
16187 len += (int)(p - *arg);
16188 *arg = skipwhite(p);
16189 return len;
16190 }
16191
16192 /*
16193 * Include any <SID> etc in the expanded string:
16194 * Thus the -len here.
16195 */
16196 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16197 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016198 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 *alias = temp_string;
16200 *arg = skipwhite(p);
16201 return (int)STRLEN(temp_string);
16202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203
16204 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016205 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 EMSG2(_(e_invexpr2), *arg);
16207
16208 return len;
16209}
16210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016211/*
16212 * Find the end of a variable or function name, taking care of magic braces.
16213 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16214 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016215 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016216 * Return a pointer to just after the name. Equal to "arg" if there is no
16217 * valid name.
16218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016220find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 char_u *arg;
16222 char_u **expr_start;
16223 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016224 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 int mb_nest = 0;
16227 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 char_u *p;
16229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016230 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016232 *expr_start = NULL;
16233 *expr_end = NULL;
16234 }
16235
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016236 /* Quick check for valid starting character. */
16237 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16238 return arg;
16239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016240 for (p = arg; *p != NUL
16241 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016242 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016243 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016244 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016245 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016247 if (*p == '\'')
16248 {
16249 /* skip over 'string' to avoid counting [ and ] inside it. */
16250 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16251 ;
16252 if (*p == NUL)
16253 break;
16254 }
16255 else if (*p == '"')
16256 {
16257 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16258 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16259 if (*p == '\\' && p[1] != NUL)
16260 ++p;
16261 if (*p == NUL)
16262 break;
16263 }
16264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016265 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016267 if (*p == '[')
16268 ++br_nest;
16269 else if (*p == ']')
16270 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016275 if (*p == '{')
16276 {
16277 mb_nest++;
16278 if (expr_start != NULL && *expr_start == NULL)
16279 *expr_start = p;
16280 }
16281 else if (*p == '}')
16282 {
16283 mb_nest--;
16284 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16285 *expr_end = p;
16286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 }
16289
16290 return p;
16291}
16292
16293/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016294 * Expands out the 'magic' {}'s in a variable/function name.
16295 * Note that this can call itself recursively, to deal with
16296 * constructs like foo{bar}{baz}{bam}
16297 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16298 * "in_start" ^
16299 * "expr_start" ^
16300 * "expr_end" ^
16301 * "in_end" ^
16302 *
16303 * Returns a new allocated string, which the caller must free.
16304 * Returns NULL for failure.
16305 */
16306 static char_u *
16307make_expanded_name(in_start, expr_start, expr_end, in_end)
16308 char_u *in_start;
16309 char_u *expr_start;
16310 char_u *expr_end;
16311 char_u *in_end;
16312{
16313 char_u c1;
16314 char_u *retval = NULL;
16315 char_u *temp_result;
16316 char_u *nextcmd = NULL;
16317
16318 if (expr_end == NULL || in_end == NULL)
16319 return NULL;
16320 *expr_start = NUL;
16321 *expr_end = NUL;
16322 c1 = *in_end;
16323 *in_end = NUL;
16324
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016325 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016326 if (temp_result != NULL && nextcmd == NULL)
16327 {
16328 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16329 + (in_end - expr_end) + 1));
16330 if (retval != NULL)
16331 {
16332 STRCPY(retval, in_start);
16333 STRCAT(retval, temp_result);
16334 STRCAT(retval, expr_end + 1);
16335 }
16336 }
16337 vim_free(temp_result);
16338
16339 *in_end = c1; /* put char back for error messages */
16340 *expr_start = '{';
16341 *expr_end = '}';
16342
16343 if (retval != NULL)
16344 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016345 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016346 if (expr_start != NULL)
16347 {
16348 /* Further expansion! */
16349 temp_result = make_expanded_name(retval, expr_start,
16350 expr_end, temp_result);
16351 vim_free(retval);
16352 retval = temp_result;
16353 }
16354 }
16355
16356 return retval;
16357}
16358
16359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016361 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 */
16363 static int
16364eval_isnamec(c)
16365 int c;
16366{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016367 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16368}
16369
16370/*
16371 * Return TRUE if character "c" can be used as the first character in a
16372 * variable or function name (excluding '{' and '}').
16373 */
16374 static int
16375eval_isnamec1(c)
16376 int c;
16377{
16378 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379}
16380
16381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 * Set number v: variable to "val".
16383 */
16384 void
16385set_vim_var_nr(idx, val)
16386 int idx;
16387 long val;
16388{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016389 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390}
16391
16392/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016393 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 */
16395 long
16396get_vim_var_nr(idx)
16397 int idx;
16398{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016399 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400}
16401
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016402#if defined(FEAT_AUTOCMD) || defined(PROTO)
16403/*
16404 * Get string v: variable value. Uses a static buffer, can only be used once.
16405 */
16406 char_u *
16407get_vim_var_str(idx)
16408 int idx;
16409{
16410 return get_tv_string(&vimvars[idx].vv_tv);
16411}
16412#endif
16413
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414/*
16415 * Set v:count, v:count1 and v:prevcount.
16416 */
16417 void
16418set_vcount(count, count1)
16419 long count;
16420 long count1;
16421{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016422 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16423 vimvars[VV_COUNT].vv_nr = count;
16424 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425}
16426
16427/*
16428 * Set string v: variable to a copy of "val".
16429 */
16430 void
16431set_vim_var_string(idx, val, len)
16432 int idx;
16433 char_u *val;
16434 int len; /* length of "val" to use or -1 (whole string) */
16435{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016436 /* Need to do this (at least) once, since we can't initialize a union.
16437 * Will always be invoked when "v:progname" is set. */
16438 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16439
Bram Moolenaare9a41262005-01-15 22:18:47 +000016440 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016442 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016444 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016446 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447}
16448
16449/*
16450 * Set v:register if needed.
16451 */
16452 void
16453set_reg_var(c)
16454 int c;
16455{
16456 char_u regname;
16457
16458 if (c == 0 || c == ' ')
16459 regname = '"';
16460 else
16461 regname = c;
16462 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016463 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 set_vim_var_string(VV_REG, &regname, 1);
16465}
16466
16467/*
16468 * Get or set v:exception. If "oldval" == NULL, return the current value.
16469 * Otherwise, restore the value to "oldval" and return NULL.
16470 * Must always be called in pairs to save and restore v:exception! Does not
16471 * take care of memory allocations.
16472 */
16473 char_u *
16474v_exception(oldval)
16475 char_u *oldval;
16476{
16477 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016478 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479
Bram Moolenaare9a41262005-01-15 22:18:47 +000016480 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481 return NULL;
16482}
16483
16484/*
16485 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16486 * Otherwise, restore the value to "oldval" and return NULL.
16487 * Must always be called in pairs to save and restore v:throwpoint! Does not
16488 * take care of memory allocations.
16489 */
16490 char_u *
16491v_throwpoint(oldval)
16492 char_u *oldval;
16493{
16494 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016495 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496
Bram Moolenaare9a41262005-01-15 22:18:47 +000016497 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 return NULL;
16499}
16500
16501#if defined(FEAT_AUTOCMD) || defined(PROTO)
16502/*
16503 * Set v:cmdarg.
16504 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16505 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16506 * Must always be called in pairs!
16507 */
16508 char_u *
16509set_cmdarg(eap, oldarg)
16510 exarg_T *eap;
16511 char_u *oldarg;
16512{
16513 char_u *oldval;
16514 char_u *newval;
16515 unsigned len;
16516
Bram Moolenaare9a41262005-01-15 22:18:47 +000016517 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016518 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016520 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016521 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 }
16524
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016525 if (eap->force_bin == FORCE_BIN)
16526 len = 6;
16527 else if (eap->force_bin == FORCE_NOBIN)
16528 len = 8;
16529 else
16530 len = 0;
16531 if (eap->force_ff != 0)
16532 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16533# ifdef FEAT_MBYTE
16534 if (eap->force_enc != 0)
16535 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016536 if (eap->bad_char != 0)
16537 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016538# endif
16539
16540 newval = alloc(len + 1);
16541 if (newval == NULL)
16542 return NULL;
16543
16544 if (eap->force_bin == FORCE_BIN)
16545 sprintf((char *)newval, " ++bin");
16546 else if (eap->force_bin == FORCE_NOBIN)
16547 sprintf((char *)newval, " ++nobin");
16548 else
16549 *newval = NUL;
16550 if (eap->force_ff != 0)
16551 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16552 eap->cmd + eap->force_ff);
16553# ifdef FEAT_MBYTE
16554 if (eap->force_enc != 0)
16555 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16556 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016557 if (eap->bad_char != 0)
16558 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16559 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016560# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016561 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016562 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563}
16564#endif
16565
16566/*
16567 * Get the value of internal variable "name".
16568 * Return OK or FAIL.
16569 */
16570 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016571get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 char_u *name;
16573 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016574 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016575 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576{
16577 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016578 typval_T *tv = NULL;
16579 typval_T atv;
16580 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016581 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582
16583 /* truncate the name, so that we can use strcmp() */
16584 cc = name[len];
16585 name[len] = NUL;
16586
16587 /*
16588 * Check for "b:changedtick".
16589 */
16590 if (STRCMP(name, "b:changedtick") == 0)
16591 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016592 atv.v_type = VAR_NUMBER;
16593 atv.vval.v_number = curbuf->b_changedtick;
16594 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 }
16596
16597 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 * Check for user-defined variables.
16599 */
16600 else
16601 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016602 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016604 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 }
16606
Bram Moolenaare9a41262005-01-15 22:18:47 +000016607 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016609 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016611 ret = FAIL;
16612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016614 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615
16616 name[len] = cc;
16617
16618 return ret;
16619}
16620
16621/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016622 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16623 * Also handle function call with Funcref variable: func(expr)
16624 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16625 */
16626 static int
16627handle_subscript(arg, rettv, evaluate, verbose)
16628 char_u **arg;
16629 typval_T *rettv;
16630 int evaluate; /* do more than finding the end */
16631 int verbose; /* give error messages */
16632{
16633 int ret = OK;
16634 dict_T *selfdict = NULL;
16635 char_u *s;
16636 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016637 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016638
16639 while (ret == OK
16640 && (**arg == '['
16641 || (**arg == '.' && rettv->v_type == VAR_DICT)
16642 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16643 && !vim_iswhite(*(*arg - 1)))
16644 {
16645 if (**arg == '(')
16646 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016647 /* need to copy the funcref so that we can clear rettv */
16648 functv = *rettv;
16649 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016650
16651 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016652 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016653 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016654 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16655 &len, evaluate, selfdict);
16656
16657 /* Clear the funcref afterwards, so that deleting it while
16658 * evaluating the arguments is possible (see test55). */
16659 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016660
16661 /* Stop the expression evaluation when immediately aborting on
16662 * error, or when an interrupt occurred or an exception was thrown
16663 * but not caught. */
16664 if (aborting())
16665 {
16666 if (ret == OK)
16667 clear_tv(rettv);
16668 ret = FAIL;
16669 }
16670 dict_unref(selfdict);
16671 selfdict = NULL;
16672 }
16673 else /* **arg == '[' || **arg == '.' */
16674 {
16675 dict_unref(selfdict);
16676 if (rettv->v_type == VAR_DICT)
16677 {
16678 selfdict = rettv->vval.v_dict;
16679 if (selfdict != NULL)
16680 ++selfdict->dv_refcount;
16681 }
16682 else
16683 selfdict = NULL;
16684 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16685 {
16686 clear_tv(rettv);
16687 ret = FAIL;
16688 }
16689 }
16690 }
16691 dict_unref(selfdict);
16692 return ret;
16693}
16694
16695/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016696 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16697 * value).
16698 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016699 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016701{
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016703}
16704
16705/*
16706 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707 * The string "s" must have been allocated, it is consumed.
16708 * Return NULL for out of memory, the variable otherwise.
16709 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016710 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016711alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 char_u *s;
16713{
Bram Moolenaar33570922005-01-25 22:26:29 +000016714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 rettv = alloc_tv();
16717 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 rettv->v_type = VAR_STRING;
16720 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 }
16722 else
16723 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016724 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725}
16726
16727/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016728 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016730 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016731free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016732 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733{
16734 if (varp != NULL)
16735 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016736 switch (varp->v_type)
16737 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016738 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016739 func_unref(varp->vval.v_string);
16740 /*FALLTHROUGH*/
16741 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016742 vim_free(varp->vval.v_string);
16743 break;
16744 case VAR_LIST:
16745 list_unref(varp->vval.v_list);
16746 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016747 case VAR_DICT:
16748 dict_unref(varp->vval.v_dict);
16749 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016750 case VAR_NUMBER:
16751 case VAR_UNKNOWN:
16752 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016753 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016754 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016755 break;
16756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016757 vim_free(varp);
16758 }
16759}
16760
16761/*
16762 * Free the memory for a variable value and set the value to NULL or 0.
16763 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016764 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016766 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767{
16768 if (varp != NULL)
16769 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016770 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016772 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016773 func_unref(varp->vval.v_string);
16774 /*FALLTHROUGH*/
16775 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016776 vim_free(varp->vval.v_string);
16777 varp->vval.v_string = NULL;
16778 break;
16779 case VAR_LIST:
16780 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016781 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016782 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016783 case VAR_DICT:
16784 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016785 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016786 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016788 varp->vval.v_number = 0;
16789 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016790 case VAR_UNKNOWN:
16791 break;
16792 default:
16793 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016795 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 }
16797}
16798
16799/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016800 * Set the value of a variable to NULL without freeing items.
16801 */
16802 static void
16803init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016804 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016805{
16806 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016807 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016808}
16809
16810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 * Get the number value of a variable.
16812 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016813 * For incompatible types, return 0.
16814 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16815 * caller of incompatible types: it sets *denote to TRUE if "denote"
16816 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 */
16818 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016819get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016820 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016822 int error = FALSE;
16823
16824 return get_tv_number_chk(varp, &error); /* return 0L on error */
16825}
16826
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016827 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016828get_tv_number_chk(varp, denote)
16829 typval_T *varp;
16830 int *denote;
16831{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016832 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016834 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016836 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016837 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016838 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016839 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016840 break;
16841 case VAR_STRING:
16842 if (varp->vval.v_string != NULL)
16843 vim_str2nr(varp->vval.v_string, NULL, NULL,
16844 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016845 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016846 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016847 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016848 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016849 case VAR_DICT:
16850 EMSG(_("E728: Using a Dictionary as a number"));
16851 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016852 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016853 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016856 if (denote == NULL) /* useful for values that must be unsigned */
16857 n = -1;
16858 else
16859 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016860 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861}
16862
16863/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016864 * Get the lnum from the first argument.
16865 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867 */
16868 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016869get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016870 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871{
Bram Moolenaar33570922005-01-25 22:26:29 +000016872 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 linenr_T lnum;
16874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016875 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 if (lnum == 0) /* no valid number, try using line() */
16877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016878 rettv.v_type = VAR_NUMBER;
16879 f_line(argvars, &rettv);
16880 lnum = rettv.vval.v_number;
16881 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 }
16883 return lnum;
16884}
16885
16886/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016887 * Get the lnum from the first argument.
16888 * Also accepts "$", then "buf" is used.
16889 * Returns 0 on error.
16890 */
16891 static linenr_T
16892get_tv_lnum_buf(argvars, buf)
16893 typval_T *argvars;
16894 buf_T *buf;
16895{
16896 if (argvars[0].v_type == VAR_STRING
16897 && argvars[0].vval.v_string != NULL
16898 && argvars[0].vval.v_string[0] == '$'
16899 && buf != NULL)
16900 return buf->b_ml.ml_line_count;
16901 return get_tv_number_chk(&argvars[0], NULL);
16902}
16903
16904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 * Get the string value of a variable.
16906 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016907 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16908 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 * If the String variable has never been set, return an empty string.
16910 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016911 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16912 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 */
16914 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016915get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917{
16918 static char_u mybuf[NUMBUFLEN];
16919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016920 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921}
16922
16923 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016924get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016925 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016926 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016928 char_u *res = get_tv_string_buf_chk(varp, buf);
16929
16930 return res != NULL ? res : (char_u *)"";
16931}
16932
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016933 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934get_tv_string_chk(varp)
16935 typval_T *varp;
16936{
16937 static char_u mybuf[NUMBUFLEN];
16938
16939 return get_tv_string_buf_chk(varp, mybuf);
16940}
16941
16942 static char_u *
16943get_tv_string_buf_chk(varp, buf)
16944 typval_T *varp;
16945 char_u *buf;
16946{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016947 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016949 case VAR_NUMBER:
16950 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16951 return buf;
16952 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016953 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016954 break;
16955 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016956 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016957 break;
16958 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016959 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016960 break;
16961 case VAR_STRING:
16962 if (varp->vval.v_string != NULL)
16963 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016964 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016965 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016966 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016967 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016969 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970}
16971
16972/*
16973 * Find variable "name" in the list of variables.
16974 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016975 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016976 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016980find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016985 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986
Bram Moolenaara7043832005-01-21 11:56:39 +000016987 ht = find_var_ht(name, &varname);
16988 if (htp != NULL)
16989 *htp = ht;
16990 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016992 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993}
16994
16995/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016996 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016997 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016999 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017000find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017002 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017003 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017004{
Bram Moolenaar33570922005-01-25 22:26:29 +000017005 hashitem_T *hi;
17006
17007 if (*varname == NUL)
17008 {
17009 /* Must be something like "s:", otherwise "ht" would be NULL. */
17010 switch (varname[-2])
17011 {
17012 case 's': return &SCRIPT_SV(current_SID).sv_var;
17013 case 'g': return &globvars_var;
17014 case 'v': return &vimvars_var;
17015 case 'b': return &curbuf->b_bufvar;
17016 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017017 case 'l': return current_funccal == NULL
17018 ? NULL : &current_funccal->l_vars_var;
17019 case 'a': return current_funccal == NULL
17020 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 }
17022 return NULL;
17023 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017024
17025 hi = hash_find(ht, varname);
17026 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017027 {
17028 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017029 * worked find the variable again. Don't auto-load a script if it was
17030 * loaded already, otherwise it would be loaded every time when
17031 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017032 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017033 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017034 hi = hash_find(ht, varname);
17035 if (HASHITEM_EMPTY(hi))
17036 return NULL;
17037 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017038 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017039}
17040
17041/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017042 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017043 * Set "varname" to the start of name without ':'.
17044 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017045 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017046find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 char_u *name;
17048 char_u **varname;
17049{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017050 hashitem_T *hi;
17051
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052 if (name[1] != ':')
17053 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017054 /* The name must not start with a colon or #. */
17055 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 return NULL;
17057 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017058
17059 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017060 hi = hash_find(&compat_hashtab, name);
17061 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017062 return &compat_hashtab;
17063
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 return &globvarht; /* global variable */
17066 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 }
17068 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017069 if (*name == 'g') /* global variable */
17070 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017071 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17072 */
17073 if (vim_strchr(name + 2, ':') != NULL
17074 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017075 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017077 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017079 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017080 if (*name == 'v') /* v: variable */
17081 return &vimvarht;
17082 if (*name == 'a' && current_funccal != NULL) /* function argument */
17083 return &current_funccal->l_avars.dv_hashtab;
17084 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17085 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 if (*name == 's' /* script variable */
17087 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17088 return &SCRIPT_VARS(current_SID);
17089 return NULL;
17090}
17091
17092/*
17093 * Get the string value of a (global/local) variable.
17094 * Returns NULL when it doesn't exist.
17095 */
17096 char_u *
17097get_var_value(name)
17098 char_u *name;
17099{
Bram Moolenaar33570922005-01-25 22:26:29 +000017100 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101
Bram Moolenaara7043832005-01-21 11:56:39 +000017102 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 if (v == NULL)
17104 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106}
17107
17108/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017109 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 * sourcing this script and when executing functions defined in the script.
17111 */
17112 void
17113new_script_vars(id)
17114 scid_T id;
17115{
Bram Moolenaara7043832005-01-21 11:56:39 +000017116 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 hashtab_T *ht;
17118 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017119
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17121 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017122 /* Re-allocating ga_data means that an ht_array pointing to
17123 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017124 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017125 for (i = 1; i <= ga_scripts.ga_len; ++i)
17126 {
17127 ht = &SCRIPT_VARS(i);
17128 if (ht->ht_mask == HT_INIT_SIZE - 1)
17129 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017130 sv = &SCRIPT_SV(i);
17131 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017132 }
17133
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 while (ga_scripts.ga_len < id)
17135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017136 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17137 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 }
17140 }
17141}
17142
17143/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17145 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 */
17147 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017148init_var_dict(dict, dict_var)
17149 dict_T *dict;
17150 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151{
Bram Moolenaar33570922005-01-25 22:26:29 +000017152 hash_init(&dict->dv_hashtab);
17153 dict->dv_refcount = 99999;
17154 dict_var->di_tv.vval.v_dict = dict;
17155 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017156 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17158 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159}
17160
17161/*
17162 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017163 * Frees all allocated variables and the value they contain.
17164 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 */
17166 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017167vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017168 hashtab_T *ht;
17169{
17170 vars_clear_ext(ht, TRUE);
17171}
17172
17173/*
17174 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17175 */
17176 static void
17177vars_clear_ext(ht, free_val)
17178 hashtab_T *ht;
17179 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180{
Bram Moolenaara7043832005-01-21 11:56:39 +000017181 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017182 hashitem_T *hi;
17183 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184
Bram Moolenaar33570922005-01-25 22:26:29 +000017185 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017186 todo = ht->ht_used;
17187 for (hi = ht->ht_array; todo > 0; ++hi)
17188 {
17189 if (!HASHITEM_EMPTY(hi))
17190 {
17191 --todo;
17192
Bram Moolenaar33570922005-01-25 22:26:29 +000017193 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017194 * ht_array might change then. hash_clear() takes care of it
17195 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017196 v = HI2DI(hi);
17197 if (free_val)
17198 clear_tv(&v->di_tv);
17199 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17200 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017201 }
17202 }
17203 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017204 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205}
17206
Bram Moolenaara7043832005-01-21 11:56:39 +000017207/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017208 * Delete a variable from hashtab "ht" at item "hi".
17209 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017212delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017213 hashtab_T *ht;
17214 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215{
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017217
17218 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017219 clear_tv(&di->di_tv);
17220 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221}
17222
17223/*
17224 * List the value of one internal variable.
17225 */
17226 static void
17227list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 char_u *prefix;
17230{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017231 char_u *tofree;
17232 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017233 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017234
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017235 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017236 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017237 s == NULL ? (char_u *)"" : s);
17238 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239}
17240
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 static void
17242list_one_var_a(prefix, name, type, string)
17243 char_u *prefix;
17244 char_u *name;
17245 int type;
17246 char_u *string;
17247{
17248 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17249 if (name != NULL) /* "a:" vars don't have a name stored */
17250 msg_puts(name);
17251 msg_putchar(' ');
17252 msg_advance(22);
17253 if (type == VAR_NUMBER)
17254 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017255 else if (type == VAR_FUNC)
17256 msg_putchar('*');
17257 else if (type == VAR_LIST)
17258 {
17259 msg_putchar('[');
17260 if (*string == '[')
17261 ++string;
17262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017263 else if (type == VAR_DICT)
17264 {
17265 msg_putchar('{');
17266 if (*string == '{')
17267 ++string;
17268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 else
17270 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017271
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017273
17274 if (type == VAR_FUNC)
17275 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276}
17277
17278/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 * If the variable already exists, the value is updated.
17281 * Otherwise the variable is created.
17282 */
17283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017284set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017287 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288{
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017291 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017292 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017295 {
17296 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17297 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17298 ? name[2] : name[0]))
17299 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017300 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017301 return;
17302 }
17303 if (function_exists(name))
17304 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017305 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017306 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017307 return;
17308 }
17309 }
17310
Bram Moolenaara7043832005-01-21 11:56:39 +000017311 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017312 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017313 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017314 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017315 return;
17316 }
17317
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017318 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017321 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017322 if (var_check_ro(v->di_flags, name)
17323 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017324 return;
17325 if (v->di_tv.v_type != tv->v_type
17326 && !((v->di_tv.v_type == VAR_STRING
17327 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017328 && (tv->v_type == VAR_STRING
17329 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017330 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017331 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017332 return;
17333 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017334
17335 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017336 * Handle setting internal v: variables separately: we don't change
17337 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017338 */
17339 if (ht == &vimvarht)
17340 {
17341 if (v->di_tv.v_type == VAR_STRING)
17342 {
17343 vim_free(v->di_tv.vval.v_string);
17344 if (copy || tv->v_type != VAR_STRING)
17345 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17346 else
17347 {
17348 /* Take over the string to avoid an extra alloc/free. */
17349 v->di_tv.vval.v_string = tv->vval.v_string;
17350 tv->vval.v_string = NULL;
17351 }
17352 }
17353 else if (v->di_tv.v_type != VAR_NUMBER)
17354 EMSG2(_(e_intern2), "set_var()");
17355 else
17356 v->di_tv.vval.v_number = get_tv_number(tv);
17357 return;
17358 }
17359
17360 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 }
17362 else /* add a new variable */
17363 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017364 /* Make sure the variable name is valid. */
17365 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017366 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17367 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017368 {
17369 EMSG2(_(e_illvar), varname);
17370 return;
17371 }
17372
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017373 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17374 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017375 if (v == NULL)
17376 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017380 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 return;
17382 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017383 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017386 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017387 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017388 else
17389 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017391 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017392 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394}
17395
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017396/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17398 * Also give an error message.
17399 */
17400 static int
17401var_check_ro(flags, name)
17402 int flags;
17403 char_u *name;
17404{
17405 if (flags & DI_FLAGS_RO)
17406 {
17407 EMSG2(_(e_readonlyvar), name);
17408 return TRUE;
17409 }
17410 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17411 {
17412 EMSG2(_(e_readonlysbx), name);
17413 return TRUE;
17414 }
17415 return FALSE;
17416}
17417
17418/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017419 * Return TRUE if typeval "tv" is set to be locked (immutable).
17420 * Also give an error message, using "name".
17421 */
17422 static int
17423tv_check_lock(lock, name)
17424 int lock;
17425 char_u *name;
17426{
17427 if (lock & VAR_LOCKED)
17428 {
17429 EMSG2(_("E741: Value is locked: %s"),
17430 name == NULL ? (char_u *)_("Unknown") : name);
17431 return TRUE;
17432 }
17433 if (lock & VAR_FIXED)
17434 {
17435 EMSG2(_("E742: Cannot change value of %s"),
17436 name == NULL ? (char_u *)_("Unknown") : name);
17437 return TRUE;
17438 }
17439 return FALSE;
17440}
17441
17442/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017443 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017444 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017445 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017448copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017449 typval_T *from;
17450 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017452 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017453 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017454 switch (from->v_type)
17455 {
17456 case VAR_NUMBER:
17457 to->vval.v_number = from->vval.v_number;
17458 break;
17459 case VAR_STRING:
17460 case VAR_FUNC:
17461 if (from->vval.v_string == NULL)
17462 to->vval.v_string = NULL;
17463 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017465 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017466 if (from->v_type == VAR_FUNC)
17467 func_ref(to->vval.v_string);
17468 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017469 break;
17470 case VAR_LIST:
17471 if (from->vval.v_list == NULL)
17472 to->vval.v_list = NULL;
17473 else
17474 {
17475 to->vval.v_list = from->vval.v_list;
17476 ++to->vval.v_list->lv_refcount;
17477 }
17478 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017479 case VAR_DICT:
17480 if (from->vval.v_dict == NULL)
17481 to->vval.v_dict = NULL;
17482 else
17483 {
17484 to->vval.v_dict = from->vval.v_dict;
17485 ++to->vval.v_dict->dv_refcount;
17486 }
17487 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017488 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017489 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017490 break;
17491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492}
17493
17494/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017495 * Make a copy of an item.
17496 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017497 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17498 * reference to an already copied list/dict can be used.
17499 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017500 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017501 static int
17502item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017503 typval_T *from;
17504 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017505 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017506 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017507{
17508 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017509 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017510
Bram Moolenaar33570922005-01-25 22:26:29 +000017511 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017512 {
17513 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017514 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017515 }
17516 ++recurse;
17517
17518 switch (from->v_type)
17519 {
17520 case VAR_NUMBER:
17521 case VAR_STRING:
17522 case VAR_FUNC:
17523 copy_tv(from, to);
17524 break;
17525 case VAR_LIST:
17526 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017527 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017528 if (from->vval.v_list == NULL)
17529 to->vval.v_list = NULL;
17530 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17531 {
17532 /* use the copy made earlier */
17533 to->vval.v_list = from->vval.v_list->lv_copylist;
17534 ++to->vval.v_list->lv_refcount;
17535 }
17536 else
17537 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17538 if (to->vval.v_list == NULL)
17539 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017540 break;
17541 case VAR_DICT:
17542 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017543 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017544 if (from->vval.v_dict == NULL)
17545 to->vval.v_dict = NULL;
17546 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17547 {
17548 /* use the copy made earlier */
17549 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17550 ++to->vval.v_dict->dv_refcount;
17551 }
17552 else
17553 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17554 if (to->vval.v_dict == NULL)
17555 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017556 break;
17557 default:
17558 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017559 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017560 }
17561 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017562 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017563}
17564
17565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 * ":echo expr1 ..." print each argument separated with a space, add a
17567 * newline at the end.
17568 * ":echon expr1 ..." print each argument plain.
17569 */
17570 void
17571ex_echo(eap)
17572 exarg_T *eap;
17573{
17574 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017575 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017576 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 char_u *p;
17578 int needclr = TRUE;
17579 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017580 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581
17582 if (eap->skip)
17583 ++emsg_skip;
17584 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17585 {
17586 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017587 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588 {
17589 /*
17590 * Report the invalid expression unless the expression evaluation
17591 * has been cancelled due to an aborting error, an interrupt, or an
17592 * exception.
17593 */
17594 if (!aborting())
17595 EMSG2(_(e_invexpr2), p);
17596 break;
17597 }
17598 if (!eap->skip)
17599 {
17600 if (atstart)
17601 {
17602 atstart = FALSE;
17603 /* Call msg_start() after eval1(), evaluating the expression
17604 * may cause a message to appear. */
17605 if (eap->cmdidx == CMD_echo)
17606 msg_start();
17607 }
17608 else if (eap->cmdidx == CMD_echo)
17609 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017610 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017611 if (p != NULL)
17612 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017614 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017616 if (*p != TAB && needclr)
17617 {
17618 /* remove any text still there from the command */
17619 msg_clr_eos();
17620 needclr = FALSE;
17621 }
17622 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 }
17624 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017625 {
17626#ifdef FEAT_MBYTE
17627 if (has_mbyte)
17628 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017629 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017630
17631 (void)msg_outtrans_len_attr(p, i, echo_attr);
17632 p += i - 1;
17633 }
17634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017636 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017639 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017641 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 arg = skipwhite(arg);
17643 }
17644 eap->nextcmd = check_nextcmd(arg);
17645
17646 if (eap->skip)
17647 --emsg_skip;
17648 else
17649 {
17650 /* remove text that may still be there from the command */
17651 if (needclr)
17652 msg_clr_eos();
17653 if (eap->cmdidx == CMD_echo)
17654 msg_end();
17655 }
17656}
17657
17658/*
17659 * ":echohl {name}".
17660 */
17661 void
17662ex_echohl(eap)
17663 exarg_T *eap;
17664{
17665 int id;
17666
17667 id = syn_name2id(eap->arg);
17668 if (id == 0)
17669 echo_attr = 0;
17670 else
17671 echo_attr = syn_id2attr(id);
17672}
17673
17674/*
17675 * ":execute expr1 ..." execute the result of an expression.
17676 * ":echomsg expr1 ..." Print a message
17677 * ":echoerr expr1 ..." Print an error
17678 * Each gets spaces around each argument and a newline at the end for
17679 * echo commands
17680 */
17681 void
17682ex_execute(eap)
17683 exarg_T *eap;
17684{
17685 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017686 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 int ret = OK;
17688 char_u *p;
17689 garray_T ga;
17690 int len;
17691 int save_did_emsg;
17692
17693 ga_init2(&ga, 1, 80);
17694
17695 if (eap->skip)
17696 ++emsg_skip;
17697 while (*arg != NUL && *arg != '|' && *arg != '\n')
17698 {
17699 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 {
17702 /*
17703 * Report the invalid expression unless the expression evaluation
17704 * has been cancelled due to an aborting error, an interrupt, or an
17705 * exception.
17706 */
17707 if (!aborting())
17708 EMSG2(_(e_invexpr2), p);
17709 ret = FAIL;
17710 break;
17711 }
17712
17713 if (!eap->skip)
17714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017715 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 len = (int)STRLEN(p);
17717 if (ga_grow(&ga, len + 2) == FAIL)
17718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017719 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 ret = FAIL;
17721 break;
17722 }
17723 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 ga.ga_len += len;
17727 }
17728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017729 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 arg = skipwhite(arg);
17731 }
17732
17733 if (ret != FAIL && ga.ga_data != NULL)
17734 {
17735 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017738 out_flush();
17739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 else if (eap->cmdidx == CMD_echoerr)
17741 {
17742 /* We don't want to abort following commands, restore did_emsg. */
17743 save_did_emsg = did_emsg;
17744 EMSG((char_u *)ga.ga_data);
17745 if (!force_abort)
17746 did_emsg = save_did_emsg;
17747 }
17748 else if (eap->cmdidx == CMD_execute)
17749 do_cmdline((char_u *)ga.ga_data,
17750 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17751 }
17752
17753 ga_clear(&ga);
17754
17755 if (eap->skip)
17756 --emsg_skip;
17757
17758 eap->nextcmd = check_nextcmd(arg);
17759}
17760
17761/*
17762 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17763 * "arg" points to the "&" or '+' when called, to "option" when returning.
17764 * Returns NULL when no option name found. Otherwise pointer to the char
17765 * after the option name.
17766 */
17767 static char_u *
17768find_option_end(arg, opt_flags)
17769 char_u **arg;
17770 int *opt_flags;
17771{
17772 char_u *p = *arg;
17773
17774 ++p;
17775 if (*p == 'g' && p[1] == ':')
17776 {
17777 *opt_flags = OPT_GLOBAL;
17778 p += 2;
17779 }
17780 else if (*p == 'l' && p[1] == ':')
17781 {
17782 *opt_flags = OPT_LOCAL;
17783 p += 2;
17784 }
17785 else
17786 *opt_flags = 0;
17787
17788 if (!ASCII_ISALPHA(*p))
17789 return NULL;
17790 *arg = p;
17791
17792 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17793 p += 4; /* termcap option */
17794 else
17795 while (ASCII_ISALPHA(*p))
17796 ++p;
17797 return p;
17798}
17799
17800/*
17801 * ":function"
17802 */
17803 void
17804ex_function(eap)
17805 exarg_T *eap;
17806{
17807 char_u *theline;
17808 int j;
17809 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 char_u *name = NULL;
17812 char_u *p;
17813 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017814 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 garray_T newargs;
17816 garray_T newlines;
17817 int varargs = FALSE;
17818 int mustend = FALSE;
17819 int flags = 0;
17820 ufunc_T *fp;
17821 int indent;
17822 int nesting;
17823 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017824 dictitem_T *v;
17825 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017826 static int func_nr = 0; /* number for nameless function */
17827 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017828 hashtab_T *ht;
17829 int todo;
17830 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017831 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832
17833 /*
17834 * ":function" without argument: list functions.
17835 */
17836 if (ends_excmd(*eap->arg))
17837 {
17838 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017839 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017840 todo = func_hashtab.ht_used;
17841 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017842 {
17843 if (!HASHITEM_EMPTY(hi))
17844 {
17845 --todo;
17846 fp = HI2UF(hi);
17847 if (!isdigit(*fp->uf_name))
17848 list_func_head(fp, FALSE);
17849 }
17850 }
17851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 eap->nextcmd = check_nextcmd(eap->arg);
17853 return;
17854 }
17855
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017856 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017857 * ":function /pat": list functions matching pattern.
17858 */
17859 if (*eap->arg == '/')
17860 {
17861 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17862 if (!eap->skip)
17863 {
17864 regmatch_T regmatch;
17865
17866 c = *p;
17867 *p = NUL;
17868 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17869 *p = c;
17870 if (regmatch.regprog != NULL)
17871 {
17872 regmatch.rm_ic = p_ic;
17873
17874 todo = func_hashtab.ht_used;
17875 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17876 {
17877 if (!HASHITEM_EMPTY(hi))
17878 {
17879 --todo;
17880 fp = HI2UF(hi);
17881 if (!isdigit(*fp->uf_name)
17882 && vim_regexec(&regmatch, fp->uf_name, 0))
17883 list_func_head(fp, FALSE);
17884 }
17885 }
17886 }
17887 }
17888 if (*p == '/')
17889 ++p;
17890 eap->nextcmd = check_nextcmd(p);
17891 return;
17892 }
17893
17894 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017895 * Get the function name. There are these situations:
17896 * func normal function name
17897 * "name" == func, "fudi.fd_dict" == NULL
17898 * dict.func new dictionary entry
17899 * "name" == NULL, "fudi.fd_dict" set,
17900 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17901 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017902 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017903 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17904 * dict.func existing dict entry that's not a Funcref
17905 * "name" == NULL, "fudi.fd_dict" set,
17906 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017909 name = trans_function_name(&p, eap->skip, 0, &fudi);
17910 paren = (vim_strchr(p, '(') != NULL);
17911 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 {
17913 /*
17914 * Return on an invalid expression in braces, unless the expression
17915 * evaluation has been cancelled due to an aborting error, an
17916 * interrupt, or an exception.
17917 */
17918 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017919 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017920 if (!eap->skip && fudi.fd_newkey != NULL)
17921 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017922 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 else
17926 eap->skip = TRUE;
17927 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000017928
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 /* An error in a function call during evaluation of an expression in magic
17930 * braces should not cause the function not to be defined. */
17931 saved_did_emsg = did_emsg;
17932 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933
17934 /*
17935 * ":function func" with only function name: list function.
17936 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017937 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 {
17939 if (!ends_excmd(*skipwhite(p)))
17940 {
17941 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017942 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 }
17944 eap->nextcmd = check_nextcmd(p);
17945 if (eap->nextcmd != NULL)
17946 *p = NUL;
17947 if (!eap->skip && !got_int)
17948 {
17949 fp = find_func(name);
17950 if (fp != NULL)
17951 {
17952 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017953 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017955 if (FUNCLINE(fp, j) == NULL)
17956 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957 msg_putchar('\n');
17958 msg_outnum((long)(j + 1));
17959 if (j < 9)
17960 msg_putchar(' ');
17961 if (j < 99)
17962 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017963 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 out_flush(); /* show a line at a time */
17965 ui_breakcheck();
17966 }
17967 if (!got_int)
17968 {
17969 msg_putchar('\n');
17970 msg_puts((char_u *)" endfunction");
17971 }
17972 }
17973 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017974 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017976 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 }
17978
17979 /*
17980 * ":function name(arg1, arg2)" Define function.
17981 */
17982 p = skipwhite(p);
17983 if (*p != '(')
17984 {
17985 if (!eap->skip)
17986 {
17987 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017988 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 }
17990 /* attempt to continue by skipping some text */
17991 if (vim_strchr(p, '(') != NULL)
17992 p = vim_strchr(p, '(');
17993 }
17994 p = skipwhite(p + 1);
17995
17996 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17997 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17998
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017999 if (!eap->skip)
18000 {
18001 /* Check the name of the function. */
18002 if (name != NULL)
18003 arg = name;
18004 else
18005 arg = fudi.fd_newkey;
18006 if (arg != NULL)
18007 {
18008 if (*arg == K_SPECIAL)
18009 j = 3;
18010 else
18011 j = 0;
18012 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18013 : eval_isnamec(arg[j])))
18014 ++j;
18015 if (arg[j] != NUL)
18016 emsg_funcname(_(e_invarg2), arg);
18017 }
18018 }
18019
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 /*
18021 * Isolate the arguments: "arg1, arg2, ...)"
18022 */
18023 while (*p != ')')
18024 {
18025 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18026 {
18027 varargs = TRUE;
18028 p += 3;
18029 mustend = TRUE;
18030 }
18031 else
18032 {
18033 arg = p;
18034 while (ASCII_ISALNUM(*p) || *p == '_')
18035 ++p;
18036 if (arg == p || isdigit(*arg)
18037 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18038 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18039 {
18040 if (!eap->skip)
18041 EMSG2(_("E125: Illegal argument: %s"), arg);
18042 break;
18043 }
18044 if (ga_grow(&newargs, 1) == FAIL)
18045 goto erret;
18046 c = *p;
18047 *p = NUL;
18048 arg = vim_strsave(arg);
18049 if (arg == NULL)
18050 goto erret;
18051 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18052 *p = c;
18053 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 if (*p == ',')
18055 ++p;
18056 else
18057 mustend = TRUE;
18058 }
18059 p = skipwhite(p);
18060 if (mustend && *p != ')')
18061 {
18062 if (!eap->skip)
18063 EMSG2(_(e_invarg2), eap->arg);
18064 break;
18065 }
18066 }
18067 ++p; /* skip the ')' */
18068
Bram Moolenaare9a41262005-01-15 22:18:47 +000018069 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 for (;;)
18071 {
18072 p = skipwhite(p);
18073 if (STRNCMP(p, "range", 5) == 0)
18074 {
18075 flags |= FC_RANGE;
18076 p += 5;
18077 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018078 else if (STRNCMP(p, "dict", 4) == 0)
18079 {
18080 flags |= FC_DICT;
18081 p += 4;
18082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 else if (STRNCMP(p, "abort", 5) == 0)
18084 {
18085 flags |= FC_ABORT;
18086 p += 5;
18087 }
18088 else
18089 break;
18090 }
18091
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018092 /* When there is a line break use what follows for the function body.
18093 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18094 if (*p == '\n')
18095 line_arg = p + 1;
18096 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 EMSG(_(e_trailing));
18098
18099 /*
18100 * Read the body of the function, until ":endfunction" is found.
18101 */
18102 if (KeyTyped)
18103 {
18104 /* Check if the function already exists, don't let the user type the
18105 * whole function before telling him it doesn't work! For a script we
18106 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018107 if (!eap->skip && !eap->forceit)
18108 {
18109 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18110 EMSG(_(e_funcdict));
18111 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018112 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018115 if (!eap->skip && did_emsg)
18116 goto erret;
18117
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 msg_putchar('\n'); /* don't overwrite the function name */
18119 cmdline_row = msg_row;
18120 }
18121
18122 indent = 2;
18123 nesting = 0;
18124 for (;;)
18125 {
18126 msg_scroll = TRUE;
18127 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018128 sourcing_lnum_off = sourcing_lnum;
18129
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018130 if (line_arg != NULL)
18131 {
18132 /* Use eap->arg, split up in parts by line breaks. */
18133 theline = line_arg;
18134 p = vim_strchr(theline, '\n');
18135 if (p == NULL)
18136 line_arg += STRLEN(line_arg);
18137 else
18138 {
18139 *p = NUL;
18140 line_arg = p + 1;
18141 }
18142 }
18143 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 theline = getcmdline(':', 0L, indent);
18145 else
18146 theline = eap->getline(':', eap->cookie, indent);
18147 if (KeyTyped)
18148 lines_left = Rows - 1;
18149 if (theline == NULL)
18150 {
18151 EMSG(_("E126: Missing :endfunction"));
18152 goto erret;
18153 }
18154
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018155 /* Detect line continuation: sourcing_lnum increased more than one. */
18156 if (sourcing_lnum > sourcing_lnum_off + 1)
18157 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18158 else
18159 sourcing_lnum_off = 0;
18160
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 if (skip_until != NULL)
18162 {
18163 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18164 * don't check for ":endfunc". */
18165 if (STRCMP(theline, skip_until) == 0)
18166 {
18167 vim_free(skip_until);
18168 skip_until = NULL;
18169 }
18170 }
18171 else
18172 {
18173 /* skip ':' and blanks*/
18174 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18175 ;
18176
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018177 /* Check for "endfunction". */
18178 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018180 if (line_arg == NULL)
18181 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 break;
18183 }
18184
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018185 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 * at "end". */
18187 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18188 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018189 else if (STRNCMP(p, "if", 2) == 0
18190 || STRNCMP(p, "wh", 2) == 0
18191 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 || STRNCMP(p, "try", 3) == 0)
18193 indent += 2;
18194
18195 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018196 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018198 if (*p == '!')
18199 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 p += eval_fname_script(p);
18201 if (ASCII_ISALPHA(*p))
18202 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018203 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 if (*skipwhite(p) == '(')
18205 {
18206 ++nesting;
18207 indent += 2;
18208 }
18209 }
18210 }
18211
18212 /* Check for ":append" or ":insert". */
18213 p = skip_range(p, NULL);
18214 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18215 || (p[0] == 'i'
18216 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18217 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18218 skip_until = vim_strsave((char_u *)".");
18219
18220 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18221 arg = skipwhite(skiptowhite(p));
18222 if (arg[0] == '<' && arg[1] =='<'
18223 && ((p[0] == 'p' && p[1] == 'y'
18224 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18225 || (p[0] == 'p' && p[1] == 'e'
18226 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18227 || (p[0] == 't' && p[1] == 'c'
18228 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18229 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18230 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018231 || (p[0] == 'm' && p[1] == 'z'
18232 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 ))
18234 {
18235 /* ":python <<" continues until a dot, like ":append" */
18236 p = skipwhite(arg + 2);
18237 if (*p == NUL)
18238 skip_until = vim_strsave((char_u *)".");
18239 else
18240 skip_until = vim_strsave(p);
18241 }
18242 }
18243
18244 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018245 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018246 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018247 if (line_arg == NULL)
18248 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018250 }
18251
18252 /* Copy the line to newly allocated memory. get_one_sourceline()
18253 * allocates 250 bytes per line, this saves 80% on average. The cost
18254 * is an extra alloc/free. */
18255 p = vim_strsave(theline);
18256 if (p != NULL)
18257 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018258 if (line_arg == NULL)
18259 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018260 theline = p;
18261 }
18262
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018263 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18264
18265 /* Add NULL lines for continuation lines, so that the line count is
18266 * equal to the index in the growarray. */
18267 while (sourcing_lnum_off-- > 0)
18268 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018269
18270 /* Check for end of eap->arg. */
18271 if (line_arg != NULL && *line_arg == NUL)
18272 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 }
18274
18275 /* Don't define the function when skipping commands or when an error was
18276 * detected. */
18277 if (eap->skip || did_emsg)
18278 goto erret;
18279
18280 /*
18281 * If there are no errors, add the function
18282 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018283 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018284 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018285 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018286 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018287 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018288 emsg_funcname("E707: Function name conflicts with variable: %s",
18289 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018290 goto erret;
18291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018292
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018293 fp = find_func(name);
18294 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018296 if (!eap->forceit)
18297 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018298 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018299 goto erret;
18300 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018301 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018302 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018303 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018304 name);
18305 goto erret;
18306 }
18307 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018308 ga_clear_strings(&(fp->uf_args));
18309 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018310 vim_free(name);
18311 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 }
18314 else
18315 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018316 char numbuf[20];
18317
18318 fp = NULL;
18319 if (fudi.fd_newkey == NULL && !eap->forceit)
18320 {
18321 EMSG(_(e_funcdict));
18322 goto erret;
18323 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018324 if (fudi.fd_di == NULL)
18325 {
18326 /* Can't add a function to a locked dictionary */
18327 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18328 goto erret;
18329 }
18330 /* Can't change an existing function if it is locked */
18331 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18332 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018333
18334 /* Give the function a sequential number. Can only be used with a
18335 * Funcref! */
18336 vim_free(name);
18337 sprintf(numbuf, "%d", ++func_nr);
18338 name = vim_strsave((char_u *)numbuf);
18339 if (name == NULL)
18340 goto erret;
18341 }
18342
18343 if (fp == NULL)
18344 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018345 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018346 {
18347 int slen, plen;
18348 char_u *scriptname;
18349
18350 /* Check that the autoload name matches the script name. */
18351 j = FAIL;
18352 if (sourcing_name != NULL)
18353 {
18354 scriptname = autoload_name(name);
18355 if (scriptname != NULL)
18356 {
18357 p = vim_strchr(scriptname, '/');
18358 plen = STRLEN(p);
18359 slen = STRLEN(sourcing_name);
18360 if (slen > plen && fnamecmp(p,
18361 sourcing_name + slen - plen) == 0)
18362 j = OK;
18363 vim_free(scriptname);
18364 }
18365 }
18366 if (j == FAIL)
18367 {
18368 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18369 goto erret;
18370 }
18371 }
18372
18373 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 if (fp == NULL)
18375 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018376
18377 if (fudi.fd_dict != NULL)
18378 {
18379 if (fudi.fd_di == NULL)
18380 {
18381 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018382 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018383 if (fudi.fd_di == NULL)
18384 {
18385 vim_free(fp);
18386 goto erret;
18387 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018388 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18389 {
18390 vim_free(fudi.fd_di);
18391 goto erret;
18392 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018393 }
18394 else
18395 /* overwrite existing dict entry */
18396 clear_tv(&fudi.fd_di->di_tv);
18397 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018398 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018399 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018400 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018401 }
18402
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018404 STRCPY(fp->uf_name, name);
18405 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018407 fp->uf_args = newargs;
18408 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018409#ifdef FEAT_PROFILE
18410 fp->uf_tml_count = NULL;
18411 fp->uf_tml_total = NULL;
18412 fp->uf_tml_self = NULL;
18413 fp->uf_profiling = FALSE;
18414 if (prof_def_func())
18415 func_do_profile(fp);
18416#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018417 fp->uf_varargs = varargs;
18418 fp->uf_flags = flags;
18419 fp->uf_calls = 0;
18420 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018421 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422
18423erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 ga_clear_strings(&newargs);
18425 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018426ret_free:
18427 vim_free(skip_until);
18428 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431}
18432
18433/*
18434 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018435 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018437 * flags:
18438 * TFN_INT: internal function name OK
18439 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 * Advances "pp" to just after the function name (if no error).
18441 */
18442 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018443trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444 char_u **pp;
18445 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018446 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018447 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018449 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 char_u *start;
18451 char_u *end;
18452 int lead;
18453 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018455 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018456
18457 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018458 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018460
18461 /* Check for hard coded <SNR>: already translated function ID (from a user
18462 * command). */
18463 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18464 && (*pp)[2] == (int)KE_SNR)
18465 {
18466 *pp += 3;
18467 len = get_id_len(pp) + 3;
18468 return vim_strnsave(start, len);
18469 }
18470
18471 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18472 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018474 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018476
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018477 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18478 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018479 if (end == start)
18480 {
18481 if (!skip)
18482 EMSG(_("E129: Function name required"));
18483 goto theend;
18484 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018485 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018486 {
18487 /*
18488 * Report an invalid expression in braces, unless the expression
18489 * evaluation has been cancelled due to an aborting error, an
18490 * interrupt, or an exception.
18491 */
18492 if (!aborting())
18493 {
18494 if (end != NULL)
18495 EMSG2(_(e_invarg2), start);
18496 }
18497 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018498 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018499 goto theend;
18500 }
18501
18502 if (lv.ll_tv != NULL)
18503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018504 if (fdp != NULL)
18505 {
18506 fdp->fd_dict = lv.ll_dict;
18507 fdp->fd_newkey = lv.ll_newkey;
18508 lv.ll_newkey = NULL;
18509 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018510 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018511 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18512 {
18513 name = vim_strsave(lv.ll_tv->vval.v_string);
18514 *pp = end;
18515 }
18516 else
18517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018518 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18519 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018520 EMSG(_(e_funcref));
18521 else
18522 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018523 name = NULL;
18524 }
18525 goto theend;
18526 }
18527
18528 if (lv.ll_name == NULL)
18529 {
18530 /* Error found, but continue after the function name. */
18531 *pp = end;
18532 goto theend;
18533 }
18534
18535 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018537 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018538 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18539 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18540 {
18541 /* When there was "s:" already or the name expanded to get a
18542 * leading "s:" then remove it. */
18543 lv.ll_name += 2;
18544 len -= 2;
18545 lead = 2;
18546 }
18547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018548 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018549 {
18550 if (lead == 2) /* skip over "s:" */
18551 lv.ll_name += 2;
18552 len = (int)(end - lv.ll_name);
18553 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018554
18555 /*
18556 * Copy the function name to allocated memory.
18557 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18558 * Accept <SNR>123_name() outside a script.
18559 */
18560 if (skip)
18561 lead = 0; /* do nothing */
18562 else if (lead > 0)
18563 {
18564 lead = 3;
18565 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18566 {
18567 if (current_SID <= 0)
18568 {
18569 EMSG(_(e_usingsid));
18570 goto theend;
18571 }
18572 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18573 lead += (int)STRLEN(sid_buf);
18574 }
18575 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018576 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018577 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018578 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018579 goto theend;
18580 }
18581 name = alloc((unsigned)(len + lead + 1));
18582 if (name != NULL)
18583 {
18584 if (lead > 0)
18585 {
18586 name[0] = K_SPECIAL;
18587 name[1] = KS_EXTRA;
18588 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018589 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018590 STRCPY(name + 3, sid_buf);
18591 }
18592 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18593 name[len + lead] = NUL;
18594 }
18595 *pp = end;
18596
18597theend:
18598 clear_lval(&lv);
18599 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600}
18601
18602/*
18603 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18604 * Return 2 if "p" starts with "s:".
18605 * Return 0 otherwise.
18606 */
18607 static int
18608eval_fname_script(p)
18609 char_u *p;
18610{
18611 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18612 || STRNICMP(p + 1, "SNR>", 4) == 0))
18613 return 5;
18614 if (p[0] == 's' && p[1] == ':')
18615 return 2;
18616 return 0;
18617}
18618
18619/*
18620 * Return TRUE if "p" starts with "<SID>" or "s:".
18621 * Only works if eval_fname_script() returned non-zero for "p"!
18622 */
18623 static int
18624eval_fname_sid(p)
18625 char_u *p;
18626{
18627 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18628}
18629
18630/*
18631 * List the head of the function: "name(arg1, arg2)".
18632 */
18633 static void
18634list_func_head(fp, indent)
18635 ufunc_T *fp;
18636 int indent;
18637{
18638 int j;
18639
18640 msg_start();
18641 if (indent)
18642 MSG_PUTS(" ");
18643 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018644 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 {
18646 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018647 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 }
18649 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018650 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018652 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 {
18654 if (j)
18655 MSG_PUTS(", ");
18656 msg_puts(FUNCARG(fp, j));
18657 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018658 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 {
18660 if (j)
18661 MSG_PUTS(", ");
18662 MSG_PUTS("...");
18663 }
18664 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018665 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018666 if (p_verbose > 0)
18667 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668}
18669
18670/*
18671 * Find a function by name, return pointer to it in ufuncs.
18672 * Return NULL for unknown function.
18673 */
18674 static ufunc_T *
18675find_func(name)
18676 char_u *name;
18677{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018678 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018680 hi = hash_find(&func_hashtab, name);
18681 if (!HASHITEM_EMPTY(hi))
18682 return HI2UF(hi);
18683 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684}
18685
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018686#if defined(EXITFREE) || defined(PROTO)
18687 void
18688free_all_functions()
18689{
18690 hashitem_T *hi;
18691
18692 /* Need to start all over every time, because func_free() may change the
18693 * hash table. */
18694 while (func_hashtab.ht_used > 0)
18695 for (hi = func_hashtab.ht_array; ; ++hi)
18696 if (!HASHITEM_EMPTY(hi))
18697 {
18698 func_free(HI2UF(hi));
18699 break;
18700 }
18701}
18702#endif
18703
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018704/*
18705 * Return TRUE if a function "name" exists.
18706 */
18707 static int
18708function_exists(name)
18709 char_u *name;
18710{
18711 char_u *p = name;
18712 int n = FALSE;
18713
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018714 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018715 if (p != NULL)
18716 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018717 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018718 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018719 else
18720 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018721 vim_free(p);
18722 }
18723 return n;
18724}
18725
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018726/*
18727 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018728 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018729 */
18730 static int
18731builtin_function(name)
18732 char_u *name;
18733{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018734 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18735 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018736}
18737
Bram Moolenaar05159a02005-02-26 23:04:13 +000018738#if defined(FEAT_PROFILE) || defined(PROTO)
18739/*
18740 * Start profiling function "fp".
18741 */
18742 static void
18743func_do_profile(fp)
18744 ufunc_T *fp;
18745{
18746 fp->uf_tm_count = 0;
18747 profile_zero(&fp->uf_tm_self);
18748 profile_zero(&fp->uf_tm_total);
18749 if (fp->uf_tml_count == NULL)
18750 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18751 (sizeof(int) * fp->uf_lines.ga_len));
18752 if (fp->uf_tml_total == NULL)
18753 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18754 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18755 if (fp->uf_tml_self == NULL)
18756 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18757 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18758 fp->uf_tml_idx = -1;
18759 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18760 || fp->uf_tml_self == NULL)
18761 return; /* out of memory */
18762
18763 fp->uf_profiling = TRUE;
18764}
18765
18766/*
18767 * Dump the profiling results for all functions in file "fd".
18768 */
18769 void
18770func_dump_profile(fd)
18771 FILE *fd;
18772{
18773 hashitem_T *hi;
18774 int todo;
18775 ufunc_T *fp;
18776 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018777 ufunc_T **sorttab;
18778 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018779
18780 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018781 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18782
Bram Moolenaar05159a02005-02-26 23:04:13 +000018783 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18784 {
18785 if (!HASHITEM_EMPTY(hi))
18786 {
18787 --todo;
18788 fp = HI2UF(hi);
18789 if (fp->uf_profiling)
18790 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018791 if (sorttab != NULL)
18792 sorttab[st_len++] = fp;
18793
Bram Moolenaar05159a02005-02-26 23:04:13 +000018794 if (fp->uf_name[0] == K_SPECIAL)
18795 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18796 else
18797 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18798 if (fp->uf_tm_count == 1)
18799 fprintf(fd, "Called 1 time\n");
18800 else
18801 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18802 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18803 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18804 fprintf(fd, "\n");
18805 fprintf(fd, "count total (s) self (s)\n");
18806
18807 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18808 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018809 if (FUNCLINE(fp, i) == NULL)
18810 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018811 prof_func_line(fd, fp->uf_tml_count[i],
18812 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018813 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18814 }
18815 fprintf(fd, "\n");
18816 }
18817 }
18818 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018819
18820 if (sorttab != NULL && st_len > 0)
18821 {
18822 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18823 prof_total_cmp);
18824 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18825 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18826 prof_self_cmp);
18827 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18828 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018829}
Bram Moolenaar73830342005-02-28 22:48:19 +000018830
18831 static void
18832prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18833 FILE *fd;
18834 ufunc_T **sorttab;
18835 int st_len;
18836 char *title;
18837 int prefer_self; /* when equal print only self time */
18838{
18839 int i;
18840 ufunc_T *fp;
18841
18842 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18843 fprintf(fd, "count total (s) self (s) function\n");
18844 for (i = 0; i < 20 && i < st_len; ++i)
18845 {
18846 fp = sorttab[i];
18847 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18848 prefer_self);
18849 if (fp->uf_name[0] == K_SPECIAL)
18850 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18851 else
18852 fprintf(fd, " %s()\n", fp->uf_name);
18853 }
18854 fprintf(fd, "\n");
18855}
18856
18857/*
18858 * Print the count and times for one function or function line.
18859 */
18860 static void
18861prof_func_line(fd, count, total, self, prefer_self)
18862 FILE *fd;
18863 int count;
18864 proftime_T *total;
18865 proftime_T *self;
18866 int prefer_self; /* when equal print only self time */
18867{
18868 if (count > 0)
18869 {
18870 fprintf(fd, "%5d ", count);
18871 if (prefer_self && profile_equal(total, self))
18872 fprintf(fd, " ");
18873 else
18874 fprintf(fd, "%s ", profile_msg(total));
18875 if (!prefer_self && profile_equal(total, self))
18876 fprintf(fd, " ");
18877 else
18878 fprintf(fd, "%s ", profile_msg(self));
18879 }
18880 else
18881 fprintf(fd, " ");
18882}
18883
18884/*
18885 * Compare function for total time sorting.
18886 */
18887 static int
18888#ifdef __BORLANDC__
18889_RTLENTRYF
18890#endif
18891prof_total_cmp(s1, s2)
18892 const void *s1;
18893 const void *s2;
18894{
18895 ufunc_T *p1, *p2;
18896
18897 p1 = *(ufunc_T **)s1;
18898 p2 = *(ufunc_T **)s2;
18899 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18900}
18901
18902/*
18903 * Compare function for self time sorting.
18904 */
18905 static int
18906#ifdef __BORLANDC__
18907_RTLENTRYF
18908#endif
18909prof_self_cmp(s1, s2)
18910 const void *s1;
18911 const void *s2;
18912{
18913 ufunc_T *p1, *p2;
18914
18915 p1 = *(ufunc_T **)s1;
18916 p2 = *(ufunc_T **)s2;
18917 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18918}
18919
Bram Moolenaar05159a02005-02-26 23:04:13 +000018920#endif
18921
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018922/* The names of packages that once were loaded is remembered. */
18923static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18924
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018925/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018926 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018927 * Return TRUE if a package was loaded.
18928 */
18929 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018930script_autoload(name, reload)
18931 char_u *name;
18932 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018933{
18934 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018935 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018936 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018937 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018938
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018939 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018940 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018941 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018942 return FALSE;
18943
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018944 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018945
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018946 /* Find the name in the list of previously loaded package names. Skip
18947 * "autoload/", it's always the same. */
18948 for (i = 0; i < ga_loaded.ga_len; ++i)
18949 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18950 break;
18951 if (!reload && i < ga_loaded.ga_len)
18952 ret = FALSE; /* was loaded already */
18953 else
18954 {
18955 /* Remember the name if it wasn't loaded already. */
18956 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18957 {
18958 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18959 tofree = NULL;
18960 }
18961
18962 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018963 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018964 ret = TRUE;
18965 }
18966
18967 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018968 return ret;
18969}
18970
18971/*
18972 * Return the autoload script name for a function or variable name.
18973 * Returns NULL when out of memory.
18974 */
18975 static char_u *
18976autoload_name(name)
18977 char_u *name;
18978{
18979 char_u *p;
18980 char_u *scriptname;
18981
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018982 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018983 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18984 if (scriptname == NULL)
18985 return FALSE;
18986 STRCPY(scriptname, "autoload/");
18987 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018988 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018989 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018990 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018991 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018992 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018993}
18994
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18996
18997/*
18998 * Function given to ExpandGeneric() to obtain the list of user defined
18999 * function names.
19000 */
19001 char_u *
19002get_user_func_name(xp, idx)
19003 expand_T *xp;
19004 int idx;
19005{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019006 static long_u done;
19007 static hashitem_T *hi;
19008 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009
19010 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019012 done = 0;
19013 hi = func_hashtab.ht_array;
19014 }
19015 if (done < func_hashtab.ht_used)
19016 {
19017 if (done++ > 0)
19018 ++hi;
19019 while (HASHITEM_EMPTY(hi))
19020 ++hi;
19021 fp = HI2UF(hi);
19022
19023 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19024 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025
19026 cat_func_name(IObuff, fp);
19027 if (xp->xp_context != EXPAND_USER_FUNC)
19028 {
19029 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019030 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 STRCAT(IObuff, ")");
19032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 return IObuff;
19034 }
19035 return NULL;
19036}
19037
19038#endif /* FEAT_CMDL_COMPL */
19039
19040/*
19041 * Copy the function name of "fp" to buffer "buf".
19042 * "buf" must be able to hold the function name plus three bytes.
19043 * Takes care of script-local function names.
19044 */
19045 static void
19046cat_func_name(buf, fp)
19047 char_u *buf;
19048 ufunc_T *fp;
19049{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019050 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 {
19052 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019053 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 }
19055 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019056 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057}
19058
19059/*
19060 * ":delfunction {name}"
19061 */
19062 void
19063ex_delfunction(eap)
19064 exarg_T *eap;
19065{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019066 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 char_u *p;
19068 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019069 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070
19071 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019072 name = trans_function_name(&p, eap->skip, 0, &fudi);
19073 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019075 {
19076 if (fudi.fd_dict != NULL && !eap->skip)
19077 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 if (!ends_excmd(*skipwhite(p)))
19081 {
19082 vim_free(name);
19083 EMSG(_(e_trailing));
19084 return;
19085 }
19086 eap->nextcmd = check_nextcmd(p);
19087 if (eap->nextcmd != NULL)
19088 *p = NUL;
19089
19090 if (!eap->skip)
19091 fp = find_func(name);
19092 vim_free(name);
19093
19094 if (!eap->skip)
19095 {
19096 if (fp == NULL)
19097 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019098 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 return;
19100 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019101 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 {
19103 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19104 return;
19105 }
19106
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019107 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019109 /* Delete the dict item that refers to the function, it will
19110 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019111 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019113 else
19114 func_free(fp);
19115 }
19116}
19117
19118/*
19119 * Free a function and remove it from the list of functions.
19120 */
19121 static void
19122func_free(fp)
19123 ufunc_T *fp;
19124{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019125 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019126
19127 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019128 ga_clear_strings(&(fp->uf_args));
19129 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019130#ifdef FEAT_PROFILE
19131 vim_free(fp->uf_tml_count);
19132 vim_free(fp->uf_tml_total);
19133 vim_free(fp->uf_tml_self);
19134#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019135
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019136 /* remove the function from the function hashtable */
19137 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19138 if (HASHITEM_EMPTY(hi))
19139 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019140 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019141 hash_remove(&func_hashtab, hi);
19142
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019143 vim_free(fp);
19144}
19145
19146/*
19147 * Unreference a Function: decrement the reference count and free it when it
19148 * becomes zero. Only for numbered functions.
19149 */
19150 static void
19151func_unref(name)
19152 char_u *name;
19153{
19154 ufunc_T *fp;
19155
19156 if (name != NULL && isdigit(*name))
19157 {
19158 fp = find_func(name);
19159 if (fp == NULL)
19160 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019161 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019162 {
19163 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019164 * when "uf_calls" becomes zero. */
19165 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019166 func_free(fp);
19167 }
19168 }
19169}
19170
19171/*
19172 * Count a reference to a Function.
19173 */
19174 static void
19175func_ref(name)
19176 char_u *name;
19177{
19178 ufunc_T *fp;
19179
19180 if (name != NULL && isdigit(*name))
19181 {
19182 fp = find_func(name);
19183 if (fp == NULL)
19184 EMSG2(_(e_intern2), "func_ref()");
19185 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019186 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 }
19188}
19189
19190/*
19191 * Call a user function.
19192 */
19193 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019194call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 ufunc_T *fp; /* pointer to function */
19196 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019197 typval_T *argvars; /* arguments */
19198 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 linenr_T firstline; /* first line of range */
19200 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019201 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202{
Bram Moolenaar33570922005-01-25 22:26:29 +000019203 char_u *save_sourcing_name;
19204 linenr_T save_sourcing_lnum;
19205 scid_T save_current_SID;
19206 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019207 int save_did_emsg;
19208 static int depth = 0;
19209 dictitem_T *v;
19210 int fixvar_idx = 0; /* index in fixvar[] */
19211 int i;
19212 int ai;
19213 char_u numbuf[NUMBUFLEN];
19214 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019215#ifdef FEAT_PROFILE
19216 proftime_T wait_start;
19217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218
19219 /* If depth of calling is getting too high, don't execute the function */
19220 if (depth >= p_mfd)
19221 {
19222 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019223 rettv->v_type = VAR_NUMBER;
19224 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 return;
19226 }
19227 ++depth;
19228
19229 line_breakcheck(); /* check for CTRL-C hit */
19230
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019231 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019234 fc.rettv = rettv;
19235 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236 fc.linenr = 0;
19237 fc.returned = FALSE;
19238 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019240 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 fc.dbg_tick = debug_tick;
19242
Bram Moolenaar33570922005-01-25 22:26:29 +000019243 /*
19244 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19245 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19246 * each argument variable and saves a lot of time.
19247 */
19248 /*
19249 * Init l: variables.
19250 */
19251 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019252 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019253 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 /* Set l:self to "selfdict". */
19255 v = &fc.fixvar[fixvar_idx++].var;
19256 STRCPY(v->di_key, "self");
19257 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19258 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19259 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019260 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 v->di_tv.vval.v_dict = selfdict;
19262 ++selfdict->dv_refcount;
19263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019264
Bram Moolenaar33570922005-01-25 22:26:29 +000019265 /*
19266 * Init a: variables.
19267 * Set a:0 to "argcount".
19268 * Set a:000 to a list with room for the "..." arguments.
19269 */
19270 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19271 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019272 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019273 v = &fc.fixvar[fixvar_idx++].var;
19274 STRCPY(v->di_key, "000");
19275 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19276 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19277 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019278 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019279 v->di_tv.vval.v_list = &fc.l_varlist;
19280 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19281 fc.l_varlist.lv_refcount = 99999;
19282
19283 /*
19284 * Set a:firstline to "firstline" and a:lastline to "lastline".
19285 * Set a:name to named arguments.
19286 * Set a:N to the "..." arguments.
19287 */
19288 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19289 (varnumber_T)firstline);
19290 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19291 (varnumber_T)lastline);
19292 for (i = 0; i < argcount; ++i)
19293 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019294 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019295 if (ai < 0)
19296 /* named argument a:name */
19297 name = FUNCARG(fp, i);
19298 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019299 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019300 /* "..." argument a:1, a:2, etc. */
19301 sprintf((char *)numbuf, "%d", ai + 1);
19302 name = numbuf;
19303 }
19304 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19305 {
19306 v = &fc.fixvar[fixvar_idx++].var;
19307 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19308 }
19309 else
19310 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019311 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19312 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019313 if (v == NULL)
19314 break;
19315 v->di_flags = DI_FLAGS_RO;
19316 }
19317 STRCPY(v->di_key, name);
19318 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19319
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019320 /* Note: the values are copied directly to avoid alloc/free.
19321 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019322 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019323 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019324
19325 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19326 {
19327 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19328 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019329 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019330 }
19331 }
19332
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333 /* Don't redraw while executing the function. */
19334 ++RedrawingDisabled;
19335 save_sourcing_name = sourcing_name;
19336 save_sourcing_lnum = sourcing_lnum;
19337 sourcing_lnum = 1;
19338 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019339 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340 if (sourcing_name != NULL)
19341 {
19342 if (save_sourcing_name != NULL
19343 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19344 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19345 else
19346 STRCPY(sourcing_name, "function ");
19347 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19348
19349 if (p_verbose >= 12)
19350 {
19351 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019352 verbose_enter_scroll();
19353
Bram Moolenaar555b2802005-05-19 21:08:39 +000019354 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 if (p_verbose >= 14)
19356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019358 char_u numbuf[NUMBUFLEN];
19359 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360
19361 msg_puts((char_u *)"(");
19362 for (i = 0; i < argcount; ++i)
19363 {
19364 if (i > 0)
19365 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019366 if (argvars[i].v_type == VAR_NUMBER)
19367 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 else
19369 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019370 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019371 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019373 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 }
19375 }
19376 msg_puts((char_u *)")");
19377 }
19378 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019379
19380 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 --no_wait_return;
19382 }
19383 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019384#ifdef FEAT_PROFILE
19385 if (do_profiling)
19386 {
19387 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19388 func_do_profile(fp);
19389 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019390 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019391 {
19392 ++fp->uf_tm_count;
19393 profile_start(&fp->uf_tm_start);
19394 profile_zero(&fp->uf_tm_children);
19395 }
19396 script_prof_save(&wait_start);
19397 }
19398#endif
19399
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019401 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 save_did_emsg = did_emsg;
19403 did_emsg = FALSE;
19404
19405 /* call do_cmdline() to execute the lines */
19406 do_cmdline(NULL, get_func_line, (void *)&fc,
19407 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19408
19409 --RedrawingDisabled;
19410
19411 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019412 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414 clear_tv(rettv);
19415 rettv->v_type = VAR_NUMBER;
19416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 }
19418
Bram Moolenaar05159a02005-02-26 23:04:13 +000019419#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019420 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019421 {
19422 profile_end(&fp->uf_tm_start);
19423 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19424 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019425 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019426 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019427 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019428 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19429 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019430 }
19431 }
19432#endif
19433
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 /* when being verbose, mention the return value */
19435 if (p_verbose >= 12)
19436 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019438 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019441 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019442 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019443 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19444 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019447 char_u buf[MSG_BUF_LEN];
19448 char_u numbuf[NUMBUFLEN];
19449 char_u *tofree;
19450
Bram Moolenaar555b2802005-05-19 21:08:39 +000019451 /* The value may be very long. Skip the middle part, so that we
19452 * have some idea how it starts and ends. smsg() would always
19453 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019454 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019455 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019456 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019457 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 }
19459 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019460
19461 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 --no_wait_return;
19463 }
19464
19465 vim_free(sourcing_name);
19466 sourcing_name = save_sourcing_name;
19467 sourcing_lnum = save_sourcing_lnum;
19468 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019469#ifdef FEAT_PROFILE
19470 if (do_profiling)
19471 script_prof_restore(&wait_start);
19472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473
19474 if (p_verbose >= 12 && sourcing_name != NULL)
19475 {
19476 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019477 verbose_enter_scroll();
19478
Bram Moolenaar555b2802005-05-19 21:08:39 +000019479 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019481
19482 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 --no_wait_return;
19484 }
19485
19486 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019487 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488
Bram Moolenaar33570922005-01-25 22:26:29 +000019489 /* The a: variables typevals were not alloced, only free the allocated
19490 * variables. */
19491 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19492
19493 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 --depth;
19495}
19496
19497/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 * Add a number variable "name" to dict "dp" with value "nr".
19499 */
19500 static void
19501add_nr_var(dp, v, name, nr)
19502 dict_T *dp;
19503 dictitem_T *v;
19504 char *name;
19505 varnumber_T nr;
19506{
19507 STRCPY(v->di_key, name);
19508 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19509 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19510 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019511 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 v->di_tv.vval.v_number = nr;
19513}
19514
19515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 * ":return [expr]"
19517 */
19518 void
19519ex_return(eap)
19520 exarg_T *eap;
19521{
19522 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019523 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 int returning = FALSE;
19525
19526 if (current_funccal == NULL)
19527 {
19528 EMSG(_("E133: :return not inside a function"));
19529 return;
19530 }
19531
19532 if (eap->skip)
19533 ++emsg_skip;
19534
19535 eap->nextcmd = NULL;
19536 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 {
19539 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019540 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 }
19544 /* It's safer to return also on error. */
19545 else if (!eap->skip)
19546 {
19547 /*
19548 * Return unless the expression evaluation has been cancelled due to an
19549 * aborting error, an interrupt, or an exception.
19550 */
19551 if (!aborting())
19552 returning = do_return(eap, FALSE, TRUE, NULL);
19553 }
19554
19555 /* When skipping or the return gets pending, advance to the next command
19556 * in this line (!returning). Otherwise, ignore the rest of the line.
19557 * Following lines will be ignored by get_func_line(). */
19558 if (returning)
19559 eap->nextcmd = NULL;
19560 else if (eap->nextcmd == NULL) /* no argument */
19561 eap->nextcmd = check_nextcmd(arg);
19562
19563 if (eap->skip)
19564 --emsg_skip;
19565}
19566
19567/*
19568 * Return from a function. Possibly makes the return pending. Also called
19569 * for a pending return at the ":endtry" or after returning from an extra
19570 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019571 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019572 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 * FALSE when the return gets pending.
19574 */
19575 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 exarg_T *eap;
19578 int reanimate;
19579 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581{
19582 int idx;
19583 struct condstack *cstack = eap->cstack;
19584
19585 if (reanimate)
19586 /* Undo the return. */
19587 current_funccal->returned = FALSE;
19588
19589 /*
19590 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19591 * not in its finally clause (which then is to be executed next) is found.
19592 * In this case, make the ":return" pending for execution at the ":endtry".
19593 * Otherwise, return normally.
19594 */
19595 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19596 if (idx >= 0)
19597 {
19598 cstack->cs_pending[idx] = CSTP_RETURN;
19599
19600 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019601 /* A pending return again gets pending. "rettv" points to an
19602 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019604 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 else
19606 {
19607 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019608 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019610 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 {
19614 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019615 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617 else
19618 EMSG(_(e_outofmem));
19619 }
19620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019621 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622
19623 if (reanimate)
19624 {
19625 /* The pending return value could be overwritten by a ":return"
19626 * without argument in a finally clause; reset the default
19627 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 current_funccal->rettv->v_type = VAR_NUMBER;
19629 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 }
19631 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019632 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 }
19634 else
19635 {
19636 current_funccal->returned = TRUE;
19637
19638 /* If the return is carried out now, store the return value. For
19639 * a return immediately after reanimation, the value is already
19640 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019641 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 }
19648 }
19649
19650 return idx < 0;
19651}
19652
19653/*
19654 * Free the variable with a pending return value.
19655 */
19656 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019657discard_pending_return(rettv)
19658 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659{
Bram Moolenaar33570922005-01-25 22:26:29 +000019660 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661}
19662
19663/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019664 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 * is an allocated string. Used by report_pending() for verbose messages.
19666 */
19667 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668get_return_cmd(rettv)
19669 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019671 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019673 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019675 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019676 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019677 if (s == NULL)
19678 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019679
19680 STRCPY(IObuff, ":return ");
19681 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19682 if (STRLEN(s) + 8 >= IOSIZE)
19683 STRCPY(IObuff + IOSIZE - 4, "...");
19684 vim_free(tofree);
19685 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686}
19687
19688/*
19689 * Get next function line.
19690 * Called by do_cmdline() to get the next line.
19691 * Returns allocated string, or NULL for end of function.
19692 */
19693/* ARGSUSED */
19694 char_u *
19695get_func_line(c, cookie, indent)
19696 int c; /* not used */
19697 void *cookie;
19698 int indent; /* not used */
19699{
Bram Moolenaar33570922005-01-25 22:26:29 +000019700 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019701 ufunc_T *fp = fcp->func;
19702 char_u *retval;
19703 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704
19705 /* If breakpoints have been added/deleted need to check for it. */
19706 if (fcp->dbg_tick != debug_tick)
19707 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019708 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 sourcing_lnum);
19710 fcp->dbg_tick = debug_tick;
19711 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019712#ifdef FEAT_PROFILE
19713 if (do_profiling)
19714 func_line_end(cookie);
19715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716
Bram Moolenaar05159a02005-02-26 23:04:13 +000019717 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019718 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19719 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 retval = NULL;
19721 else
19722 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019723 /* Skip NULL lines (continuation lines). */
19724 while (fcp->linenr < gap->ga_len
19725 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19726 ++fcp->linenr;
19727 if (fcp->linenr >= gap->ga_len)
19728 retval = NULL;
19729 else
19730 {
19731 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19732 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019733#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019734 if (do_profiling)
19735 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019736#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 }
19739
19740 /* Did we encounter a breakpoint? */
19741 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19742 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019743 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019745 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 sourcing_lnum);
19747 fcp->dbg_tick = debug_tick;
19748 }
19749
19750 return retval;
19751}
19752
Bram Moolenaar05159a02005-02-26 23:04:13 +000019753#if defined(FEAT_PROFILE) || defined(PROTO)
19754/*
19755 * Called when starting to read a function line.
19756 * "sourcing_lnum" must be correct!
19757 * When skipping lines it may not actually be executed, but we won't find out
19758 * until later and we need to store the time now.
19759 */
19760 void
19761func_line_start(cookie)
19762 void *cookie;
19763{
19764 funccall_T *fcp = (funccall_T *)cookie;
19765 ufunc_T *fp = fcp->func;
19766
19767 if (fp->uf_profiling && sourcing_lnum >= 1
19768 && sourcing_lnum <= fp->uf_lines.ga_len)
19769 {
19770 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019771 /* Skip continuation lines. */
19772 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19773 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019774 fp->uf_tml_execed = FALSE;
19775 profile_start(&fp->uf_tml_start);
19776 profile_zero(&fp->uf_tml_children);
19777 profile_get_wait(&fp->uf_tml_wait);
19778 }
19779}
19780
19781/*
19782 * Called when actually executing a function line.
19783 */
19784 void
19785func_line_exec(cookie)
19786 void *cookie;
19787{
19788 funccall_T *fcp = (funccall_T *)cookie;
19789 ufunc_T *fp = fcp->func;
19790
19791 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19792 fp->uf_tml_execed = TRUE;
19793}
19794
19795/*
19796 * Called when done with a function line.
19797 */
19798 void
19799func_line_end(cookie)
19800 void *cookie;
19801{
19802 funccall_T *fcp = (funccall_T *)cookie;
19803 ufunc_T *fp = fcp->func;
19804
19805 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19806 {
19807 if (fp->uf_tml_execed)
19808 {
19809 ++fp->uf_tml_count[fp->uf_tml_idx];
19810 profile_end(&fp->uf_tml_start);
19811 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019812 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019813 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19814 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019815 }
19816 fp->uf_tml_idx = -1;
19817 }
19818}
19819#endif
19820
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821/*
19822 * Return TRUE if the currently active function should be ended, because a
19823 * return was encountered or an error occured. Used inside a ":while".
19824 */
19825 int
19826func_has_ended(cookie)
19827 void *cookie;
19828{
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830
19831 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19832 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019833 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 || fcp->returned);
19835}
19836
19837/*
19838 * return TRUE if cookie indicates a function which "abort"s on errors.
19839 */
19840 int
19841func_has_abort(cookie)
19842 void *cookie;
19843{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019844 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845}
19846
19847#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19848typedef enum
19849{
19850 VAR_FLAVOUR_DEFAULT,
19851 VAR_FLAVOUR_SESSION,
19852 VAR_FLAVOUR_VIMINFO
19853} var_flavour_T;
19854
19855static var_flavour_T var_flavour __ARGS((char_u *varname));
19856
19857 static var_flavour_T
19858var_flavour(varname)
19859 char_u *varname;
19860{
19861 char_u *p = varname;
19862
19863 if (ASCII_ISUPPER(*p))
19864 {
19865 while (*(++p))
19866 if (ASCII_ISLOWER(*p))
19867 return VAR_FLAVOUR_SESSION;
19868 return VAR_FLAVOUR_VIMINFO;
19869 }
19870 else
19871 return VAR_FLAVOUR_DEFAULT;
19872}
19873#endif
19874
19875#if defined(FEAT_VIMINFO) || defined(PROTO)
19876/*
19877 * Restore global vars that start with a capital from the viminfo file
19878 */
19879 int
19880read_viminfo_varlist(virp, writing)
19881 vir_T *virp;
19882 int writing;
19883{
19884 char_u *tab;
19885 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887
19888 if (!writing && (find_viminfo_parameter('!') != NULL))
19889 {
19890 tab = vim_strchr(virp->vir_line + 1, '\t');
19891 if (tab != NULL)
19892 {
19893 *tab++ = '\0'; /* isolate the variable name */
19894 if (*tab == 'S') /* string var */
19895 is_string = TRUE;
19896
19897 tab = vim_strchr(tab, '\t');
19898 if (tab != NULL)
19899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900 if (is_string)
19901 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019902 tv.v_type = VAR_STRING;
19903 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 }
19906 else
19907 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019908 tv.v_type = VAR_NUMBER;
19909 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019911 set_var(virp->vir_line + 1, &tv, FALSE);
19912 if (is_string)
19913 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 }
19915 }
19916 }
19917
19918 return viminfo_readline(virp);
19919}
19920
19921/*
19922 * Write global vars that start with a capital to the viminfo file
19923 */
19924 void
19925write_viminfo_varlist(fp)
19926 FILE *fp;
19927{
Bram Moolenaar33570922005-01-25 22:26:29 +000019928 hashitem_T *hi;
19929 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019930 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019931 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019932 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019934 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935
19936 if (find_viminfo_parameter('!') == NULL)
19937 return;
19938
19939 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019940
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 todo = globvarht.ht_used;
19942 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019944 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019946 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019947 this_var = HI2DI(hi);
19948 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019950 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019951 {
19952 case VAR_STRING: s = "STR"; break;
19953 case VAR_NUMBER: s = "NUM"; break;
19954 default: continue;
19955 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019957 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019958 if (p != NULL)
19959 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019960 vim_free(tofree);
19961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962 }
19963 }
19964}
19965#endif
19966
19967#if defined(FEAT_SESSION) || defined(PROTO)
19968 int
19969store_session_globals(fd)
19970 FILE *fd;
19971{
Bram Moolenaar33570922005-01-25 22:26:29 +000019972 hashitem_T *hi;
19973 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019974 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 char_u *p, *t;
19976
Bram Moolenaar33570922005-01-25 22:26:29 +000019977 todo = globvarht.ht_used;
19978 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019980 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019982 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 this_var = HI2DI(hi);
19984 if ((this_var->di_tv.v_type == VAR_NUMBER
19985 || this_var->di_tv.v_type == VAR_STRING)
19986 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019987 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019988 /* Escape special characters with a backslash. Turn a LF and
19989 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019991 (char_u *)"\\\"\n\r");
19992 if (p == NULL) /* out of memory */
19993 break;
19994 for (t = p; *t != NUL; ++t)
19995 if (*t == '\n')
19996 *t = 'n';
19997 else if (*t == '\r')
19998 *t = 'r';
19999 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020000 this_var->di_key,
20001 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20002 : ' ',
20003 p,
20004 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20005 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020006 || put_eol(fd) == FAIL)
20007 {
20008 vim_free(p);
20009 return FAIL;
20010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 vim_free(p);
20012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 }
20014 }
20015 return OK;
20016}
20017#endif
20018
Bram Moolenaar661b1822005-07-28 22:36:45 +000020019/*
20020 * Display script name where an item was last set.
20021 * Should only be invoked when 'verbose' is non-zero.
20022 */
20023 void
20024last_set_msg(scriptID)
20025 scid_T scriptID;
20026{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020027 char_u *p;
20028
Bram Moolenaar661b1822005-07-28 22:36:45 +000020029 if (scriptID != 0)
20030 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020031 p = home_replace_save(NULL, get_scriptname(scriptID));
20032 if (p != NULL)
20033 {
20034 verbose_enter();
20035 MSG_PUTS(_("\n\tLast set from "));
20036 MSG_PUTS(p);
20037 vim_free(p);
20038 verbose_leave();
20039 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020040 }
20041}
20042
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043#endif /* FEAT_EVAL */
20044
20045#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20046
20047
20048#ifdef WIN3264
20049/*
20050 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20051 */
20052static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20053static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20054static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20055
20056/*
20057 * Get the short pathname of a file.
20058 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20059 */
20060 static int
20061get_short_pathname(fnamep, bufp, fnamelen)
20062 char_u **fnamep;
20063 char_u **bufp;
20064 int *fnamelen;
20065{
20066 int l,len;
20067 char_u *newbuf;
20068
20069 len = *fnamelen;
20070
20071 l = GetShortPathName(*fnamep, *fnamep, len);
20072 if (l > len - 1)
20073 {
20074 /* If that doesn't work (not enough space), then save the string
20075 * and try again with a new buffer big enough
20076 */
20077 newbuf = vim_strnsave(*fnamep, l);
20078 if (newbuf == NULL)
20079 return 0;
20080
20081 vim_free(*bufp);
20082 *fnamep = *bufp = newbuf;
20083
20084 l = GetShortPathName(*fnamep,*fnamep,l+1);
20085
20086 /* Really should always succeed, as the buffer is big enough */
20087 }
20088
20089 *fnamelen = l;
20090 return 1;
20091}
20092
20093/*
20094 * Create a short path name. Returns the length of the buffer it needs.
20095 * Doesn't copy over the end of the buffer passed in.
20096 */
20097 static int
20098shortpath_for_invalid_fname(fname, bufp, fnamelen)
20099 char_u **fname;
20100 char_u **bufp;
20101 int *fnamelen;
20102{
20103 char_u *s, *p, *pbuf2, *pbuf3;
20104 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020105 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106
20107 /* Make a copy */
20108 len2 = *fnamelen;
20109 pbuf2 = vim_strnsave(*fname, len2);
20110 pbuf3 = NULL;
20111
20112 s = pbuf2 + len2 - 1; /* Find the end */
20113 slen = 1;
20114 plen = len2;
20115
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020116 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 {
20118 --s;
20119 ++slen;
20120 --plen;
20121 }
20122
20123 do
20124 {
20125 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020126 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 {
20128 --s;
20129 ++slen;
20130 --plen;
20131 }
20132 if (s <= pbuf2)
20133 break;
20134
20135 /* Remeber the character that is about to be blatted */
20136 ch = *s;
20137 *s = 0; /* get_short_pathname requires a null-terminated string */
20138
20139 /* Try it in situ */
20140 p = pbuf2;
20141 if (!get_short_pathname(&p, &pbuf3, &plen))
20142 {
20143 vim_free(pbuf2);
20144 return -1;
20145 }
20146 *s = ch; /* Preserve the string */
20147 } while (plen == 0);
20148
20149 if (plen > 0)
20150 {
20151 /* Remeber the length of the new string. */
20152 *fnamelen = len = plen + slen;
20153 vim_free(*bufp);
20154 if (len > len2)
20155 {
20156 /* If there's not enough space in the currently allocated string,
20157 * then copy it to a buffer big enough.
20158 */
20159 *fname= *bufp = vim_strnsave(p, len);
20160 if (*fname == NULL)
20161 return -1;
20162 }
20163 else
20164 {
20165 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20166 *fname = *bufp = pbuf2;
20167 if (p != pbuf2)
20168 strncpy(*fname, p, plen);
20169 pbuf2 = NULL;
20170 }
20171 /* Concat the next bit */
20172 strncpy(*fname + plen, s, slen);
20173 (*fname)[len] = '\0';
20174 }
20175 vim_free(pbuf3);
20176 vim_free(pbuf2);
20177 return 0;
20178}
20179
20180/*
20181 * Get a pathname for a partial path.
20182 */
20183 static int
20184shortpath_for_partial(fnamep, bufp, fnamelen)
20185 char_u **fnamep;
20186 char_u **bufp;
20187 int *fnamelen;
20188{
20189 int sepcount, len, tflen;
20190 char_u *p;
20191 char_u *pbuf, *tfname;
20192 int hasTilde;
20193
20194 /* Count up the path seperators from the RHS.. so we know which part
20195 * of the path to return.
20196 */
20197 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020198 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 if (vim_ispathsep(*p))
20200 ++sepcount;
20201
20202 /* Need full path first (use expand_env() to remove a "~/") */
20203 hasTilde = (**fnamep == '~');
20204 if (hasTilde)
20205 pbuf = tfname = expand_env_save(*fnamep);
20206 else
20207 pbuf = tfname = FullName_save(*fnamep, FALSE);
20208
20209 len = tflen = STRLEN(tfname);
20210
20211 if (!get_short_pathname(&tfname, &pbuf, &len))
20212 return -1;
20213
20214 if (len == 0)
20215 {
20216 /* Don't have a valid filename, so shorten the rest of the
20217 * path if we can. This CAN give us invalid 8.3 filenames, but
20218 * there's not a lot of point in guessing what it might be.
20219 */
20220 len = tflen;
20221 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20222 return -1;
20223 }
20224
20225 /* Count the paths backward to find the beginning of the desired string. */
20226 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020227 {
20228#ifdef FEAT_MBYTE
20229 if (has_mbyte)
20230 p -= mb_head_off(tfname, p);
20231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232 if (vim_ispathsep(*p))
20233 {
20234 if (sepcount == 0 || (hasTilde && sepcount == 1))
20235 break;
20236 else
20237 sepcount --;
20238 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 if (hasTilde)
20241 {
20242 --p;
20243 if (p >= tfname)
20244 *p = '~';
20245 else
20246 return -1;
20247 }
20248 else
20249 ++p;
20250
20251 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20252 vim_free(*bufp);
20253 *fnamelen = (int)STRLEN(p);
20254 *bufp = pbuf;
20255 *fnamep = p;
20256
20257 return 0;
20258}
20259#endif /* WIN3264 */
20260
20261/*
20262 * Adjust a filename, according to a string of modifiers.
20263 * *fnamep must be NUL terminated when called. When returning, the length is
20264 * determined by *fnamelen.
20265 * Returns valid flags.
20266 * When there is an error, *fnamep is set to NULL.
20267 */
20268 int
20269modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20270 char_u *src; /* string with modifiers */
20271 int *usedlen; /* characters after src that are used */
20272 char_u **fnamep; /* file name so far */
20273 char_u **bufp; /* buffer for allocated file name or NULL */
20274 int *fnamelen; /* length of fnamep */
20275{
20276 int valid = 0;
20277 char_u *tail;
20278 char_u *s, *p, *pbuf;
20279 char_u dirname[MAXPATHL];
20280 int c;
20281 int has_fullname = 0;
20282#ifdef WIN3264
20283 int has_shortname = 0;
20284#endif
20285
20286repeat:
20287 /* ":p" - full path/file_name */
20288 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20289 {
20290 has_fullname = 1;
20291
20292 valid |= VALID_PATH;
20293 *usedlen += 2;
20294
20295 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20296 if ((*fnamep)[0] == '~'
20297#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20298 && ((*fnamep)[1] == '/'
20299# ifdef BACKSLASH_IN_FILENAME
20300 || (*fnamep)[1] == '\\'
20301# endif
20302 || (*fnamep)[1] == NUL)
20303
20304#endif
20305 )
20306 {
20307 *fnamep = expand_env_save(*fnamep);
20308 vim_free(*bufp); /* free any allocated file name */
20309 *bufp = *fnamep;
20310 if (*fnamep == NULL)
20311 return -1;
20312 }
20313
20314 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020315 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 {
20317 if (vim_ispathsep(*p)
20318 && p[1] == '.'
20319 && (p[2] == NUL
20320 || vim_ispathsep(p[2])
20321 || (p[2] == '.'
20322 && (p[3] == NUL || vim_ispathsep(p[3])))))
20323 break;
20324 }
20325
20326 /* FullName_save() is slow, don't use it when not needed. */
20327 if (*p != NUL || !vim_isAbsName(*fnamep))
20328 {
20329 *fnamep = FullName_save(*fnamep, *p != NUL);
20330 vim_free(*bufp); /* free any allocated file name */
20331 *bufp = *fnamep;
20332 if (*fnamep == NULL)
20333 return -1;
20334 }
20335
20336 /* Append a path separator to a directory. */
20337 if (mch_isdir(*fnamep))
20338 {
20339 /* Make room for one or two extra characters. */
20340 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20341 vim_free(*bufp); /* free any allocated file name */
20342 *bufp = *fnamep;
20343 if (*fnamep == NULL)
20344 return -1;
20345 add_pathsep(*fnamep);
20346 }
20347 }
20348
20349 /* ":." - path relative to the current directory */
20350 /* ":~" - path relative to the home directory */
20351 /* ":8" - shortname path - postponed till after */
20352 while (src[*usedlen] == ':'
20353 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20354 {
20355 *usedlen += 2;
20356 if (c == '8')
20357 {
20358#ifdef WIN3264
20359 has_shortname = 1; /* Postpone this. */
20360#endif
20361 continue;
20362 }
20363 pbuf = NULL;
20364 /* Need full path first (use expand_env() to remove a "~/") */
20365 if (!has_fullname)
20366 {
20367 if (c == '.' && **fnamep == '~')
20368 p = pbuf = expand_env_save(*fnamep);
20369 else
20370 p = pbuf = FullName_save(*fnamep, FALSE);
20371 }
20372 else
20373 p = *fnamep;
20374
20375 has_fullname = 0;
20376
20377 if (p != NULL)
20378 {
20379 if (c == '.')
20380 {
20381 mch_dirname(dirname, MAXPATHL);
20382 s = shorten_fname(p, dirname);
20383 if (s != NULL)
20384 {
20385 *fnamep = s;
20386 if (pbuf != NULL)
20387 {
20388 vim_free(*bufp); /* free any allocated file name */
20389 *bufp = pbuf;
20390 pbuf = NULL;
20391 }
20392 }
20393 }
20394 else
20395 {
20396 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20397 /* Only replace it when it starts with '~' */
20398 if (*dirname == '~')
20399 {
20400 s = vim_strsave(dirname);
20401 if (s != NULL)
20402 {
20403 *fnamep = s;
20404 vim_free(*bufp);
20405 *bufp = s;
20406 }
20407 }
20408 }
20409 vim_free(pbuf);
20410 }
20411 }
20412
20413 tail = gettail(*fnamep);
20414 *fnamelen = (int)STRLEN(*fnamep);
20415
20416 /* ":h" - head, remove "/file_name", can be repeated */
20417 /* Don't remove the first "/" or "c:\" */
20418 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20419 {
20420 valid |= VALID_HEAD;
20421 *usedlen += 2;
20422 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020423 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 --tail;
20425 *fnamelen = (int)(tail - *fnamep);
20426#ifdef VMS
20427 if (*fnamelen > 0)
20428 *fnamelen += 1; /* the path separator is part of the path */
20429#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020430 while (tail > s && !after_pathsep(s, tail))
20431 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 }
20433
20434 /* ":8" - shortname */
20435 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20436 {
20437 *usedlen += 2;
20438#ifdef WIN3264
20439 has_shortname = 1;
20440#endif
20441 }
20442
20443#ifdef WIN3264
20444 /* Check shortname after we have done 'heads' and before we do 'tails'
20445 */
20446 if (has_shortname)
20447 {
20448 pbuf = NULL;
20449 /* Copy the string if it is shortened by :h */
20450 if (*fnamelen < (int)STRLEN(*fnamep))
20451 {
20452 p = vim_strnsave(*fnamep, *fnamelen);
20453 if (p == 0)
20454 return -1;
20455 vim_free(*bufp);
20456 *bufp = *fnamep = p;
20457 }
20458
20459 /* Split into two implementations - makes it easier. First is where
20460 * there isn't a full name already, second is where there is.
20461 */
20462 if (!has_fullname && !vim_isAbsName(*fnamep))
20463 {
20464 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20465 return -1;
20466 }
20467 else
20468 {
20469 int l;
20470
20471 /* Simple case, already have the full-name
20472 * Nearly always shorter, so try first time. */
20473 l = *fnamelen;
20474 if (!get_short_pathname(fnamep, bufp, &l))
20475 return -1;
20476
20477 if (l == 0)
20478 {
20479 /* Couldn't find the filename.. search the paths.
20480 */
20481 l = *fnamelen;
20482 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20483 return -1;
20484 }
20485 *fnamelen = l;
20486 }
20487 }
20488#endif /* WIN3264 */
20489
20490 /* ":t" - tail, just the basename */
20491 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20492 {
20493 *usedlen += 2;
20494 *fnamelen -= (int)(tail - *fnamep);
20495 *fnamep = tail;
20496 }
20497
20498 /* ":e" - extension, can be repeated */
20499 /* ":r" - root, without extension, can be repeated */
20500 while (src[*usedlen] == ':'
20501 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20502 {
20503 /* find a '.' in the tail:
20504 * - for second :e: before the current fname
20505 * - otherwise: The last '.'
20506 */
20507 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20508 s = *fnamep - 2;
20509 else
20510 s = *fnamep + *fnamelen - 1;
20511 for ( ; s > tail; --s)
20512 if (s[0] == '.')
20513 break;
20514 if (src[*usedlen + 1] == 'e') /* :e */
20515 {
20516 if (s > tail)
20517 {
20518 *fnamelen += (int)(*fnamep - (s + 1));
20519 *fnamep = s + 1;
20520#ifdef VMS
20521 /* cut version from the extension */
20522 s = *fnamep + *fnamelen - 1;
20523 for ( ; s > *fnamep; --s)
20524 if (s[0] == ';')
20525 break;
20526 if (s > *fnamep)
20527 *fnamelen = s - *fnamep;
20528#endif
20529 }
20530 else if (*fnamep <= tail)
20531 *fnamelen = 0;
20532 }
20533 else /* :r */
20534 {
20535 if (s > tail) /* remove one extension */
20536 *fnamelen = (int)(s - *fnamep);
20537 }
20538 *usedlen += 2;
20539 }
20540
20541 /* ":s?pat?foo?" - substitute */
20542 /* ":gs?pat?foo?" - global substitute */
20543 if (src[*usedlen] == ':'
20544 && (src[*usedlen + 1] == 's'
20545 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20546 {
20547 char_u *str;
20548 char_u *pat;
20549 char_u *sub;
20550 int sep;
20551 char_u *flags;
20552 int didit = FALSE;
20553
20554 flags = (char_u *)"";
20555 s = src + *usedlen + 2;
20556 if (src[*usedlen + 1] == 'g')
20557 {
20558 flags = (char_u *)"g";
20559 ++s;
20560 }
20561
20562 sep = *s++;
20563 if (sep)
20564 {
20565 /* find end of pattern */
20566 p = vim_strchr(s, sep);
20567 if (p != NULL)
20568 {
20569 pat = vim_strnsave(s, (int)(p - s));
20570 if (pat != NULL)
20571 {
20572 s = p + 1;
20573 /* find end of substitution */
20574 p = vim_strchr(s, sep);
20575 if (p != NULL)
20576 {
20577 sub = vim_strnsave(s, (int)(p - s));
20578 str = vim_strnsave(*fnamep, *fnamelen);
20579 if (sub != NULL && str != NULL)
20580 {
20581 *usedlen = (int)(p + 1 - src);
20582 s = do_string_sub(str, pat, sub, flags);
20583 if (s != NULL)
20584 {
20585 *fnamep = s;
20586 *fnamelen = (int)STRLEN(s);
20587 vim_free(*bufp);
20588 *bufp = s;
20589 didit = TRUE;
20590 }
20591 }
20592 vim_free(sub);
20593 vim_free(str);
20594 }
20595 vim_free(pat);
20596 }
20597 }
20598 /* after using ":s", repeat all the modifiers */
20599 if (didit)
20600 goto repeat;
20601 }
20602 }
20603
20604 return valid;
20605}
20606
20607/*
20608 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20609 * "flags" can be "g" to do a global substitute.
20610 * Returns an allocated string, NULL for error.
20611 */
20612 char_u *
20613do_string_sub(str, pat, sub, flags)
20614 char_u *str;
20615 char_u *pat;
20616 char_u *sub;
20617 char_u *flags;
20618{
20619 int sublen;
20620 regmatch_T regmatch;
20621 int i;
20622 int do_all;
20623 char_u *tail;
20624 garray_T ga;
20625 char_u *ret;
20626 char_u *save_cpo;
20627
20628 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20629 save_cpo = p_cpo;
20630 p_cpo = (char_u *)"";
20631
20632 ga_init2(&ga, 1, 200);
20633
20634 do_all = (flags[0] == 'g');
20635
20636 regmatch.rm_ic = p_ic;
20637 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20638 if (regmatch.regprog != NULL)
20639 {
20640 tail = str;
20641 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20642 {
20643 /*
20644 * Get some space for a temporary buffer to do the substitution
20645 * into. It will contain:
20646 * - The text up to where the match is.
20647 * - The substituted text.
20648 * - The text after the match.
20649 */
20650 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20651 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20652 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20653 {
20654 ga_clear(&ga);
20655 break;
20656 }
20657
20658 /* copy the text up to where the match is */
20659 i = (int)(regmatch.startp[0] - tail);
20660 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20661 /* add the substituted text */
20662 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20663 + ga.ga_len + i, TRUE, TRUE, FALSE);
20664 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665 /* avoid getting stuck on a match with an empty string */
20666 if (tail == regmatch.endp[0])
20667 {
20668 if (*tail == NUL)
20669 break;
20670 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20671 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 }
20673 else
20674 {
20675 tail = regmatch.endp[0];
20676 if (*tail == NUL)
20677 break;
20678 }
20679 if (!do_all)
20680 break;
20681 }
20682
20683 if (ga.ga_data != NULL)
20684 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20685
20686 vim_free(regmatch.regprog);
20687 }
20688
20689 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20690 ga_clear(&ga);
20691 p_cpo = save_cpo;
20692
20693 return ret;
20694}
20695
20696#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */