blob: 2d85e6c2aef78c586d413906285ebf0bae23a784 [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 Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000345};
346
347/* shorthand */
348#define vv_type vv_di.di_tv.v_type
349#define vv_nr vv_di.di_tv.vval.v_number
350#define vv_str vv_di.di_tv.vval.v_string
351#define vv_tv vv_di.di_tv
352
353/*
354 * The v: variables are stored in dictionary "vimvardict".
355 * "vimvars_var" is the variable that is used for the "l:" scope.
356 */
357static dict_T vimvardict;
358static dictitem_T vimvars_var;
359#define vimvarht vimvardict.dv_hashtab
360
Bram Moolenaara40058a2005-07-11 22:42:07 +0000361static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
362static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
363#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
364static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
365#endif
366static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
367static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
368static char_u *skip_var_one __ARGS((char_u *arg));
369static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
370static void list_glob_vars __ARGS((void));
371static void list_buf_vars __ARGS((void));
372static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000373#ifdef FEAT_WINDOWS
374static void list_tab_vars __ARGS((void));
375#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000377static void list_script_vars __ARGS((void));
378static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
380static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
381static int check_changedtick __ARGS((char_u *arg));
382static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
383static void clear_lval __ARGS((lval_T *lp));
384static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
385static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
386static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
387static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
388static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
389static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
390static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
391static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
392static void item_lock __ARGS((typval_T *tv, int deep, int lock));
393static int tv_islocked __ARGS((typval_T *tv));
394
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
396static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000403
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000404static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000405static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000409static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static listitem_T *listitem_alloc __ARGS((void));
411static void listitem_free __ARGS((listitem_T *item));
412static void listitem_remove __ARGS((list_T *l, listitem_T *item));
413static long list_len __ARGS((list_T *l));
414static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
415static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
416static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000418static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static void list_append __ARGS((list_T *l, listitem_T *item));
421static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000422static int list_append_string __ARGS((list_T *l, char_u *str, int len));
423static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
425static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
426static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000427static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000429static char_u *list2string __ARGS((typval_T *tv, int copyID));
430static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000431static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
432static void set_ref_in_list __ARGS((list_T *l, int copyID));
433static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static void dict_unref __ARGS((dict_T *d));
435static void dict_free __ARGS((dict_T *d));
436static dictitem_T *dictitem_alloc __ARGS((char_u *key));
437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
439static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000440static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int dict_add __ARGS((dict_T *d, dictitem_T *item));
442static long dict_len __ARGS((dict_T *d));
443static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
447static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static char_u *string_quote __ARGS((char_u *str, int function));
449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
451static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
452static 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));
453static 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 +0000454static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455
456static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000472static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000476#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000477static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000478static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
480#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
486static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000499static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000513static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000515static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000521static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000529static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000530static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000531static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000533static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000554static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000560static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000576static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000578static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000579static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000582#ifdef vim_mkdir
583static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
584#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000588static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000590static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000591static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000593static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000594static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000607static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000609static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000616static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000617static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000618static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000620static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000624static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000625static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000628static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629#ifdef HAVE_STRFTIME
630static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
632static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000644static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000645static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000646static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000647static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000648static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000650static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000664static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000667static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000669static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
670static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static int get_env_len __ARGS((char_u **arg));
672static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000673static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000674static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
675#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
676#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
677 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000678static 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 +0000679static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000680static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000681static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
682static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static typval_T *alloc_tv __ARGS((void));
684static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void init_tv __ARGS((typval_T *varp));
686static long get_tv_number __ARGS((typval_T *varp));
687static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000688static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static char_u *get_tv_string __ARGS((typval_T *varp));
690static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000691static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000693static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
695static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
696static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
697static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
698static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
699static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
700static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000701static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000703static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
705static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
706static int eval_fname_script __ARGS((char_u *p));
707static int eval_fname_sid __ARGS((char_u *p));
708static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static ufunc_T *find_func __ARGS((char_u *name));
710static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000711static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000712#ifdef FEAT_PROFILE
713static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000714static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
715static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
716static int
717# ifdef __BORLANDC__
718 _RTLENTRYF
719# endif
720 prof_total_cmp __ARGS((const void *s1, const void *s2));
721static int
722# ifdef __BORLANDC__
723 _RTLENTRYF
724# endif
725 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000726#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000727static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000728static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000729static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void func_free __ARGS((ufunc_T *fp));
731static void func_unref __ARGS((char_u *name));
732static void func_ref __ARGS((char_u *name));
733static 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));
734static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000735static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
736static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000737static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000738static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000739static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000740
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000741/* Character used as separated in autoload function/variable names. */
742#define AUTOLOAD_CHAR '#'
743
Bram Moolenaar33570922005-01-25 22:26:29 +0000744/*
745 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000746 */
747 void
748eval_init()
749{
Bram Moolenaar33570922005-01-25 22:26:29 +0000750 int i;
751 struct vimvar *p;
752
753 init_var_dict(&globvardict, &globvars_var);
754 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000755 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000756 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000757
758 for (i = 0; i < VV_LEN; ++i)
759 {
760 p = &vimvars[i];
761 STRCPY(p->vv_di.di_key, p->vv_name);
762 if (p->vv_flags & VV_RO)
763 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
764 else if (p->vv_flags & VV_RO_SBX)
765 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
766 else
767 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000768
769 /* add to v: scope dict, unless the value is not always available */
770 if (p->vv_type != VAR_UNKNOWN)
771 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000772 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000773 /* add to compat scope dict */
774 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000775 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000776}
777
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000778#if defined(EXITFREE) || defined(PROTO)
779 void
780eval_clear()
781{
782 int i;
783 struct vimvar *p;
784
785 for (i = 0; i < VV_LEN; ++i)
786 {
787 p = &vimvars[i];
788 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000789 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000790 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000791 p->vv_di.di_tv.vval.v_string = NULL;
792 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000793 }
794 hash_clear(&vimvarht);
795 hash_clear(&compat_hashtab);
796
797 /* script-local variables */
798 for (i = 1; i <= ga_scripts.ga_len; ++i)
799 vars_clear(&SCRIPT_VARS(i));
800 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000801 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000802
803 /* global variables */
804 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000805
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000806 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000807 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000808 hash_clear(&func_hashtab);
809
810 /* unreferenced lists and dicts */
811 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000812}
813#endif
814
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 * Return the name of the executed function.
817 */
818 char_u *
819func_name(cookie)
820 void *cookie;
821{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000822 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823}
824
825/*
826 * Return the address holding the next breakpoint line for a funccall cookie.
827 */
828 linenr_T *
829func_breakpoint(cookie)
830 void *cookie;
831{
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
835/*
836 * Return the address holding the debug tick for a funccall cookie.
837 */
838 int *
839func_dbg_tick(cookie)
840 void *cookie;
841{
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
845/*
846 * Return the nesting level for a funccall cookie.
847 */
848 int
849func_level(cookie)
850 void *cookie;
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854
855/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000856funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858/*
859 * Return TRUE when a function was ended by a ":return" command.
860 */
861 int
862current_func_returned()
863{
864 return current_funccal->returned;
865}
866
867
868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 * Set an internal variable to a string value. Creates the variable if it does
870 * not already exist.
871 */
872 void
873set_internal_string_var(name, value)
874 char_u *name;
875 char_u *value;
876{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000877 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
880 val = vim_strsave(value);
881 if (val != NULL)
882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000883 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000884 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000886 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 }
890}
891
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000892static lval_T *redir_lval = NULL;
893static char_u *redir_endp = NULL;
894static char_u *redir_varname = NULL;
895
896/*
897 * Start recording command output to a variable
898 * Returns OK if successfully completed the setup. FAIL otherwise.
899 */
900 int
901var_redir_start(name, append)
902 char_u *name;
903 int append; /* append to an existing variable */
904{
905 int save_emsg;
906 int err;
907 typval_T tv;
908
909 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000910 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000911 {
912 EMSG(_(e_invarg));
913 return FAIL;
914 }
915
916 redir_varname = vim_strsave(name);
917 if (redir_varname == NULL)
918 return FAIL;
919
920 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
921 if (redir_lval == NULL)
922 {
923 var_redir_stop();
924 return FAIL;
925 }
926
927 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000928 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
929 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000930 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
931 {
932 if (redir_endp != NULL && *redir_endp != NUL)
933 /* Trailing characters are present after the variable name */
934 EMSG(_(e_trailing));
935 else
936 EMSG(_(e_invarg));
937 var_redir_stop();
938 return FAIL;
939 }
940
941 /* check if we can write to the variable: set it to or append an empty
942 * string */
943 save_emsg = did_emsg;
944 did_emsg = FALSE;
945 tv.v_type = VAR_STRING;
946 tv.vval.v_string = (char_u *)"";
947 if (append)
948 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
949 else
950 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
951 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000952 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000953 if (err)
954 {
955 var_redir_stop();
956 return FAIL;
957 }
958 if (redir_lval->ll_newkey != NULL)
959 {
960 /* Dictionary item was created, don't do it again. */
961 vim_free(redir_lval->ll_newkey);
962 redir_lval->ll_newkey = NULL;
963 }
964
965 return OK;
966}
967
968/*
969 * Append "value[len]" to the variable set by var_redir_start().
970 */
971 void
972var_redir_str(value, len)
973 char_u *value;
974 int len;
975{
976 char_u *val;
977 typval_T tv;
978 int save_emsg;
979 int err;
980
981 if (redir_lval == NULL)
982 return;
983
984 if (len == -1)
985 /* Append the entire string */
986 val = vim_strsave(value);
987 else
988 /* Append only the specified number of characters */
989 val = vim_strnsave(value, len);
990 if (val == NULL)
991 return;
992
993 tv.v_type = VAR_STRING;
994 tv.vval.v_string = val;
995
996 save_emsg = did_emsg;
997 did_emsg = FALSE;
998 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
999 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001000 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001 if (err)
1002 var_redir_stop();
1003
1004 vim_free(tv.vval.v_string);
1005}
1006
1007/*
1008 * Stop redirecting command output to a variable.
1009 */
1010 void
1011var_redir_stop()
1012{
1013 if (redir_lval != NULL)
1014 {
1015 clear_lval(redir_lval);
1016 vim_free(redir_lval);
1017 redir_lval = NULL;
1018 }
1019 vim_free(redir_varname);
1020 redir_varname = NULL;
1021}
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023# if defined(FEAT_MBYTE) || defined(PROTO)
1024 int
1025eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1026 char_u *enc_from;
1027 char_u *enc_to;
1028 char_u *fname_from;
1029 char_u *fname_to;
1030{
1031 int err = FALSE;
1032
1033 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1034 set_vim_var_string(VV_CC_TO, enc_to, -1);
1035 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1036 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1037 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1038 err = TRUE;
1039 set_vim_var_string(VV_CC_FROM, NULL, -1);
1040 set_vim_var_string(VV_CC_TO, NULL, -1);
1041 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1042 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1043
1044 if (err)
1045 return FAIL;
1046 return OK;
1047}
1048# endif
1049
1050# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1051 int
1052eval_printexpr(fname, args)
1053 char_u *fname;
1054 char_u *args;
1055{
1056 int err = FALSE;
1057
1058 set_vim_var_string(VV_FNAME_IN, fname, -1);
1059 set_vim_var_string(VV_CMDARG, args, -1);
1060 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1061 err = TRUE;
1062 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1063 set_vim_var_string(VV_CMDARG, NULL, -1);
1064
1065 if (err)
1066 {
1067 mch_remove(fname);
1068 return FAIL;
1069 }
1070 return OK;
1071}
1072# endif
1073
1074# if defined(FEAT_DIFF) || defined(PROTO)
1075 void
1076eval_diff(origfile, newfile, outfile)
1077 char_u *origfile;
1078 char_u *newfile;
1079 char_u *outfile;
1080{
1081 int err = FALSE;
1082
1083 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1084 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1085 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1086 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1087 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1088 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1089 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1090}
1091
1092 void
1093eval_patch(origfile, difffile, outfile)
1094 char_u *origfile;
1095 char_u *difffile;
1096 char_u *outfile;
1097{
1098 int err;
1099
1100 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1101 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1102 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1103 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1104 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1105 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1106 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1107}
1108# endif
1109
1110/*
1111 * Top level evaluation function, returning a boolean.
1112 * Sets "error" to TRUE if there was an error.
1113 * Return TRUE or FALSE.
1114 */
1115 int
1116eval_to_bool(arg, error, nextcmd, skip)
1117 char_u *arg;
1118 int *error;
1119 char_u **nextcmd;
1120 int skip; /* only parse, don't execute */
1121{
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int retval = FALSE;
1124
1125 if (skip)
1126 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 else
1130 {
1131 *error = FALSE;
1132 if (!skip)
1133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001134 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 }
1138 if (skip)
1139 --emsg_skip;
1140
1141 return retval;
1142}
1143
1144/*
1145 * Top level evaluation function, returning a string. If "skip" is TRUE,
1146 * only parsing to "nextcmd" is done, without reporting errors. Return
1147 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1148 */
1149 char_u *
1150eval_to_string_skip(arg, nextcmd, skip)
1151 char_u *arg;
1152 char_u **nextcmd;
1153 int skip; /* only parse, don't execute */
1154{
Bram Moolenaar33570922005-01-25 22:26:29 +00001155 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 char_u *retval;
1157
1158 if (skip)
1159 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 retval = NULL;
1162 else
1163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 retval = vim_strsave(get_tv_string(&tv));
1165 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 if (skip)
1168 --emsg_skip;
1169
1170 return retval;
1171}
1172
1173/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001174 * Skip over an expression at "*pp".
1175 * Return FAIL for an error, OK otherwise.
1176 */
1177 int
1178skip_expr(pp)
1179 char_u **pp;
1180{
Bram Moolenaar33570922005-01-25 22:26:29 +00001181 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001182
1183 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001184 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001185}
1186
1187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 * Top level evaluation function, returning a string.
1189 * Return pointer to allocated memory, or NULL for failure.
1190 */
1191 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001192eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 char_u *arg;
1194 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001195 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196{
Bram Moolenaar33570922005-01-25 22:26:29 +00001197 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001199 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 retval = NULL;
1203 else
1204 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001205 if (dolist && tv.v_type == VAR_LIST)
1206 {
1207 ga_init2(&ga, (int)sizeof(char), 80);
1208 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1209 ga_append(&ga, NUL);
1210 retval = (char_u *)ga.ga_data;
1211 }
1212 else
1213 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001214 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216
1217 return retval;
1218}
1219
1220/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001221 * Call eval_to_string() without using current local variables and using
1222 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
1224 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001225eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 char_u *arg;
1227 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001228 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
1230 char_u *retval;
1231 void *save_funccalp;
1232
1233 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001234 if (use_sandbox)
1235 ++sandbox;
1236 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001237 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001238 if (use_sandbox)
1239 --sandbox;
1240 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 restore_funccal(save_funccalp);
1242 return retval;
1243}
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245/*
1246 * Top level evaluation function, returning a number.
1247 * Evaluates "expr" silently.
1248 * Returns -1 for an error.
1249 */
1250 int
1251eval_to_number(expr)
1252 char_u *expr;
1253{
Bram Moolenaar33570922005-01-25 22:26:29 +00001254 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001256 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258 ++emsg_off;
1259
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001260 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 retval = -1;
1262 else
1263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001264 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267 --emsg_off;
1268
1269 return retval;
1270}
1271
Bram Moolenaara40058a2005-07-11 22:42:07 +00001272/*
1273 * Prepare v: variable "idx" to be used.
1274 * Save the current typeval in "save_tv".
1275 * When not used yet add the variable to the v: hashtable.
1276 */
1277 static void
1278prepare_vimvar(idx, save_tv)
1279 int idx;
1280 typval_T *save_tv;
1281{
1282 *save_tv = vimvars[idx].vv_tv;
1283 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1284 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1285}
1286
1287/*
1288 * Restore v: variable "idx" to typeval "save_tv".
1289 * When no longer defined, remove the variable from the v: hashtable.
1290 */
1291 static void
1292restore_vimvar(idx, save_tv)
1293 int idx;
1294 typval_T *save_tv;
1295{
1296 hashitem_T *hi;
1297
1298 clear_tv(&vimvars[idx].vv_tv);
1299 vimvars[idx].vv_tv = *save_tv;
1300 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1301 {
1302 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1303 if (HASHITEM_EMPTY(hi))
1304 EMSG2(_(e_intern2), "restore_vimvar()");
1305 else
1306 hash_remove(&vimvarht, hi);
1307 }
1308}
1309
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001310#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001311/*
1312 * Evaluate an expression to a list with suggestions.
1313 * For the "expr:" part of 'spellsuggest'.
1314 */
1315 list_T *
1316eval_spell_expr(badword, expr)
1317 char_u *badword;
1318 char_u *expr;
1319{
1320 typval_T save_val;
1321 typval_T rettv;
1322 list_T *list = NULL;
1323 char_u *p = skipwhite(expr);
1324
1325 /* Set "v:val" to the bad word. */
1326 prepare_vimvar(VV_VAL, &save_val);
1327 vimvars[VV_VAL].vv_type = VAR_STRING;
1328 vimvars[VV_VAL].vv_str = badword;
1329 if (p_verbose == 0)
1330 ++emsg_off;
1331
1332 if (eval1(&p, &rettv, TRUE) == OK)
1333 {
1334 if (rettv.v_type != VAR_LIST)
1335 clear_tv(&rettv);
1336 else
1337 list = rettv.vval.v_list;
1338 }
1339
1340 if (p_verbose == 0)
1341 --emsg_off;
1342 vimvars[VV_VAL].vv_str = NULL;
1343 restore_vimvar(VV_VAL, &save_val);
1344
1345 return list;
1346}
1347
1348/*
1349 * "list" is supposed to contain two items: a word and a number. Return the
1350 * word in "pp" and the number as the return value.
1351 * Return -1 if anything isn't right.
1352 * Used to get the good word and score from the eval_spell_expr() result.
1353 */
1354 int
1355get_spellword(list, pp)
1356 list_T *list;
1357 char_u **pp;
1358{
1359 listitem_T *li;
1360
1361 li = list->lv_first;
1362 if (li == NULL)
1363 return -1;
1364 *pp = get_tv_string(&li->li_tv);
1365
1366 li = li->li_next;
1367 if (li == NULL)
1368 return -1;
1369 return get_tv_number(&li->li_tv);
1370}
1371#endif
1372
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001373/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001374 * Top level evaluation function.
1375 * Returns an allocated typval_T with the result.
1376 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001377 */
1378 typval_T *
1379eval_expr(arg, nextcmd)
1380 char_u *arg;
1381 char_u **nextcmd;
1382{
1383 typval_T *tv;
1384
1385 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001386 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001387 {
1388 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001389 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001390 }
1391
1392 return tv;
1393}
1394
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1397/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001398 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001400 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001402 static int
1403call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 char_u *func;
1405 int argc;
1406 char_u **argv;
1407 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
Bram Moolenaar33570922005-01-25 22:26:29 +00001410 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 long n;
1412 int len;
1413 int i;
1414 int doesrange;
1415 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001416 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001418 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001420 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422 for (i = 0; i < argc; i++)
1423 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001424 /* Pass a NULL or empty argument as an empty string */
1425 if (argv[i] == NULL || *argv[i] == NUL)
1426 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001427 argvars[i].v_type = VAR_STRING;
1428 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001429 continue;
1430 }
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 /* Recognize a number argument, the others must be strings. */
1433 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1434 if (len != 0 && len == (int)STRLEN(argv[i]))
1435 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001436 argvars[i].v_type = VAR_NUMBER;
1437 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 else
1440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001441 argvars[i].v_type = VAR_STRING;
1442 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444 }
1445
1446 if (safe)
1447 {
1448 save_funccalp = save_funccal();
1449 ++sandbox;
1450 }
1451
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001452 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1453 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001455 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (safe)
1457 {
1458 --sandbox;
1459 restore_funccal(save_funccalp);
1460 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 vim_free(argvars);
1462
1463 if (ret == FAIL)
1464 clear_tv(rettv);
1465
1466 return ret;
1467}
1468
1469/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001470 * Call vimL function "func" and return the result as a string.
1471 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472 * Uses argv[argc] for the function arguments.
1473 */
1474 void *
1475call_func_retstr(func, argc, argv, safe)
1476 char_u *func;
1477 int argc;
1478 char_u **argv;
1479 int safe; /* use the sandbox */
1480{
1481 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001482 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001483
1484 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1485 return NULL;
1486
1487 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001488 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 return retval;
1490}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001491
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001492#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001493/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001494 * Call vimL function "func" and return the result as a number.
1495 * Returns -1 when calling the function fails.
1496 * Uses argv[argc] for the function arguments.
1497 */
1498 long
1499call_func_retnr(func, argc, argv, safe)
1500 char_u *func;
1501 int argc;
1502 char_u **argv;
1503 int safe; /* use the sandbox */
1504{
1505 typval_T rettv;
1506 long retval;
1507
1508 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1509 return -1;
1510
1511 retval = get_tv_number_chk(&rettv, NULL);
1512 clear_tv(&rettv);
1513 return retval;
1514}
1515#endif
1516
1517/*
1518 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001519 * Uses argv[argc] for the function arguments.
1520 */
1521 void *
1522call_func_retlist(func, argc, argv, safe)
1523 char_u *func;
1524 int argc;
1525 char_u **argv;
1526 int safe; /* use the sandbox */
1527{
1528 typval_T rettv;
1529
1530 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1531 return NULL;
1532
1533 if (rettv.v_type != VAR_LIST)
1534 {
1535 clear_tv(&rettv);
1536 return NULL;
1537 }
1538
1539 return rettv.vval.v_list;
1540}
1541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
1543
1544/*
1545 * Save the current function call pointer, and set it to NULL.
1546 * Used when executing autocommands and for ":source".
1547 */
1548 void *
1549save_funccal()
1550{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001551 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 current_funccal = NULL;
1554 return (void *)fc;
1555}
1556
1557 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001558restore_funccal(vfc)
1559 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001561 funccall_T *fc = (funccall_T *)vfc;
1562
1563 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564}
1565
Bram Moolenaar05159a02005-02-26 23:04:13 +00001566#if defined(FEAT_PROFILE) || defined(PROTO)
1567/*
1568 * Prepare profiling for entering a child or something else that is not
1569 * counted for the script/function itself.
1570 * Should always be called in pair with prof_child_exit().
1571 */
1572 void
1573prof_child_enter(tm)
1574 proftime_T *tm; /* place to store waittime */
1575{
1576 funccall_T *fc = current_funccal;
1577
1578 if (fc != NULL && fc->func->uf_profiling)
1579 profile_start(&fc->prof_child);
1580 script_prof_save(tm);
1581}
1582
1583/*
1584 * Take care of time spent in a child.
1585 * Should always be called after prof_child_enter().
1586 */
1587 void
1588prof_child_exit(tm)
1589 proftime_T *tm; /* where waittime was stored */
1590{
1591 funccall_T *fc = current_funccal;
1592
1593 if (fc != NULL && fc->func->uf_profiling)
1594 {
1595 profile_end(&fc->prof_child);
1596 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1597 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1598 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1599 }
1600 script_prof_restore(tm);
1601}
1602#endif
1603
1604
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605#ifdef FEAT_FOLDING
1606/*
1607 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1608 * it in "*cp". Doesn't give error messages.
1609 */
1610 int
1611eval_foldexpr(arg, cp)
1612 char_u *arg;
1613 int *cp;
1614{
Bram Moolenaar33570922005-01-25 22:26:29 +00001615 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 int retval;
1617 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001618 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1619 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001622 if (use_sandbox)
1623 ++sandbox;
1624 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 retval = 0;
1628 else
1629 {
1630 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 if (tv.v_type == VAR_NUMBER)
1632 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001633 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 retval = 0;
1635 else
1636 {
1637 /* If the result is a string, check if there is a non-digit before
1638 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001639 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (!VIM_ISDIGIT(*s) && *s != '-')
1641 *cp = *s++;
1642 retval = atol((char *)s);
1643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
1646 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001647 if (use_sandbox)
1648 --sandbox;
1649 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
1651 return retval;
1652}
1653#endif
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 * ":let" list all variable values
1657 * ":let var1 var2" list variable values
1658 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 * ":let var += expr" assignment command.
1660 * ":let var -= expr" assignment command.
1661 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
1664 void
1665ex_let(eap)
1666 exarg_T *eap;
1667{
1668 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001670 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001672 int var_count = 0;
1673 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001675 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaardb552d602006-03-23 22:59:57 +00001677 argend = skip_var_list(arg, &var_count, &semicolon);
1678 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001680 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1681 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001682 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001685 /*
1686 * ":let" without "=": list variables
1687 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 if (*arg == '[')
1689 EMSG(_(e_invarg));
1690 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001691 /* ":let var1 var2" */
1692 arg = list_arg_vars(eap, arg);
1693 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001695 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001696 list_glob_vars();
1697 list_buf_vars();
1698 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001699#ifdef FEAT_WINDOWS
1700 list_tab_vars();
1701#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001702 list_script_vars();
1703 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001704 list_vim_vars();
1705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 eap->nextcmd = check_nextcmd(arg);
1707 }
1708 else
1709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001710 op[0] = '=';
1711 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001712 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 {
1714 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1715 op[0] = expr[-1]; /* +=, -= or .= */
1716 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (eap->skip)
1720 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (eap->skip)
1723 {
1724 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001725 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 --emsg_skip;
1727 }
1728 else if (i != FAIL)
1729 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001731 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
1734 }
1735}
1736
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737/*
1738 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1739 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 * When "nextchars" is not NULL it points to a string with characters that
1741 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1742 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 * Returns OK or FAIL;
1744 */
1745 static int
1746ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1747 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001748 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 int copy; /* copy values from "tv", don't move */
1750 int semicolon; /* from skip_var_list() */
1751 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753{
1754 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001755 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001757 listitem_T *item;
1758 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759
1760 if (*arg != '[')
1761 {
1762 /*
1763 * ":let var = expr" or ":for var in list"
1764 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 return FAIL;
1767 return OK;
1768 }
1769
1770 /*
1771 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1772 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001773 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 {
1775 EMSG(_(e_listreq));
1776 return FAIL;
1777 }
1778
1779 i = list_len(l);
1780 if (semicolon == 0 && var_count < i)
1781 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001782 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 return FAIL;
1784 }
1785 if (var_count - semicolon > i)
1786 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001787 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 return FAIL;
1789 }
1790
1791 item = l->lv_first;
1792 while (*arg != ']')
1793 {
1794 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001795 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 item = item->li_next;
1797 if (arg == NULL)
1798 return FAIL;
1799
1800 arg = skipwhite(arg);
1801 if (*arg == ';')
1802 {
1803 /* Put the rest of the list (may be empty) in the var after ';'.
1804 * Create a new list for this. */
1805 l = list_alloc();
1806 if (l == NULL)
1807 return FAIL;
1808 while (item != NULL)
1809 {
1810 list_append_tv(l, &item->li_tv);
1811 item = item->li_next;
1812 }
1813
1814 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001815 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 ltv.vval.v_list = l;
1817 l->lv_refcount = 1;
1818
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001819 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1820 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 clear_tv(&ltv);
1822 if (arg == NULL)
1823 return FAIL;
1824 break;
1825 }
1826 else if (*arg != ',' && *arg != ']')
1827 {
1828 EMSG2(_(e_intern2), "ex_let_vars()");
1829 return FAIL;
1830 }
1831 }
1832
1833 return OK;
1834}
1835
1836/*
1837 * Skip over assignable variable "var" or list of variables "[var, var]".
1838 * Used for ":let varvar = expr" and ":for varvar in expr".
1839 * For "[var, var]" increment "*var_count" for each variable.
1840 * for "[var, var; var]" set "semicolon".
1841 * Return NULL for an error.
1842 */
1843 static char_u *
1844skip_var_list(arg, var_count, semicolon)
1845 char_u *arg;
1846 int *var_count;
1847 int *semicolon;
1848{
1849 char_u *p, *s;
1850
1851 if (*arg == '[')
1852 {
1853 /* "[var, var]": find the matching ']'. */
1854 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001855 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 {
1857 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1858 s = skip_var_one(p);
1859 if (s == p)
1860 {
1861 EMSG2(_(e_invarg2), p);
1862 return NULL;
1863 }
1864 ++*var_count;
1865
1866 p = skipwhite(s);
1867 if (*p == ']')
1868 break;
1869 else if (*p == ';')
1870 {
1871 if (*semicolon == 1)
1872 {
1873 EMSG(_("Double ; in list of variables"));
1874 return NULL;
1875 }
1876 *semicolon = 1;
1877 }
1878 else if (*p != ',')
1879 {
1880 EMSG2(_(e_invarg2), p);
1881 return NULL;
1882 }
1883 }
1884 return p + 1;
1885 }
1886 else
1887 return skip_var_one(arg);
1888}
1889
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001890/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001891 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1892 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001893 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 static char_u *
1895skip_var_one(arg)
1896 char_u *arg;
1897{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001898 if (*arg == '@' && arg[1] != NUL)
1899 return arg + 2;
1900 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1901 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902}
1903
Bram Moolenaara7043832005-01-21 11:56:39 +00001904/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 * List variables for hashtab "ht" with prefix "prefix".
1906 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001907 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001909list_hashtable_vars(ht, prefix, empty)
1910 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001913{
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 hashitem_T *hi;
1915 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001916 int todo;
1917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001918 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1920 {
1921 if (!HASHITEM_EMPTY(hi))
1922 {
1923 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 di = HI2DI(hi);
1925 if (empty || di->di_tv.v_type != VAR_STRING
1926 || di->di_tv.vval.v_string != NULL)
1927 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 }
1929 }
1930}
1931
1932/*
1933 * List global variables.
1934 */
1935 static void
1936list_glob_vars()
1937{
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001939}
1940
1941/*
1942 * List buffer variables.
1943 */
1944 static void
1945list_buf_vars()
1946{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001947 char_u numbuf[NUMBUFLEN];
1948
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001950
1951 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1952 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001953}
1954
1955/*
1956 * List window variables.
1957 */
1958 static void
1959list_win_vars()
1960{
Bram Moolenaar33570922005-01-25 22:26:29 +00001961 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001962}
1963
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001964#ifdef FEAT_WINDOWS
1965/*
1966 * List tab page variables.
1967 */
1968 static void
1969list_tab_vars()
1970{
1971 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1972}
1973#endif
1974
Bram Moolenaara7043832005-01-21 11:56:39 +00001975/*
1976 * List Vim variables.
1977 */
1978 static void
1979list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001980{
Bram Moolenaar33570922005-01-25 22:26:29 +00001981 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982}
1983
1984/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001985 * List script-local variables, if there is a script.
1986 */
1987 static void
1988list_script_vars()
1989{
1990 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1991 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1992}
1993
1994/*
1995 * List function variables, if there is a function.
1996 */
1997 static void
1998list_func_vars()
1999{
2000 if (current_funccal != NULL)
2001 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
2002 (char_u *)"l:", FALSE);
2003}
2004
2005/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 * List variables in "arg".
2007 */
2008 static char_u *
2009list_arg_vars(eap, arg)
2010 exarg_T *eap;
2011 char_u *arg;
2012{
2013 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002014 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002016 char_u *name_start;
2017 char_u *arg_subsc;
2018 char_u *tofree;
2019 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020
2021 while (!ends_excmd(*arg) && !got_int)
2022 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002023 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002025 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002026 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2027 {
2028 emsg_severe = TRUE;
2029 EMSG(_(e_trailing));
2030 break;
2031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002035 /* get_name_len() takes care of expanding curly braces */
2036 name_start = name = arg;
2037 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2038 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002040 /* This is mainly to keep test 49 working: when expanding
2041 * curly braces fails overrule the exception error message. */
2042 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002044 emsg_severe = TRUE;
2045 EMSG2(_(e_invarg2), arg);
2046 break;
2047 }
2048 error = TRUE;
2049 }
2050 else
2051 {
2052 if (tofree != NULL)
2053 name = tofree;
2054 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002055 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 else
2057 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058 /* handle d.key, l[idx], f(expr) */
2059 arg_subsc = arg;
2060 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002061 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002062 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068 case 'g': list_glob_vars(); break;
2069 case 'b': list_buf_vars(); break;
2070 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002071#ifdef FEAT_WINDOWS
2072 case 't': list_tab_vars(); break;
2073#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002074 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002075 case 's': list_script_vars(); break;
2076 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002077 default:
2078 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 }
2081 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082 {
2083 char_u numbuf[NUMBUFLEN];
2084 char_u *tf;
2085 int c;
2086 char_u *s;
2087
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002088 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002089 c = *arg;
2090 *arg = NUL;
2091 list_one_var_a((char_u *)"",
2092 arg == arg_subsc ? name : name_start,
2093 tv.v_type, s == NULL ? (char_u *)"" : s);
2094 *arg = c;
2095 vim_free(tf);
2096 }
2097 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 }
2100 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002101
2102 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002104
2105 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 }
2107
2108 return arg;
2109}
2110
2111/*
2112 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2113 * Returns a pointer to the char just after the var name.
2114 * Returns NULL if there is an error.
2115 */
2116 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002117ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002119 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002120 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002122 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123{
2124 int c1;
2125 char_u *name;
2126 char_u *p;
2127 char_u *arg_end = NULL;
2128 int len;
2129 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002130 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131
2132 /*
2133 * ":let $VAR = expr": Set environment variable.
2134 */
2135 if (*arg == '$')
2136 {
2137 /* Find the end of the name. */
2138 ++arg;
2139 name = arg;
2140 len = get_env_len(&arg);
2141 if (len == 0)
2142 EMSG2(_(e_invarg2), name - 1);
2143 else
2144 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002145 if (op != NULL && (*op == '+' || *op == '-'))
2146 EMSG2(_(e_letwrong), op);
2147 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 EMSG(_(e_letunexp));
2150 else
2151 {
2152 c1 = name[len];
2153 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002154 p = get_tv_string_chk(tv);
2155 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002156 {
2157 int mustfree = FALSE;
2158 char_u *s = vim_getenv(name, &mustfree);
2159
2160 if (s != NULL)
2161 {
2162 p = tofree = concat_str(s, p);
2163 if (mustfree)
2164 vim_free(s);
2165 }
2166 }
2167 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002169 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002170 if (STRICMP(name, "HOME") == 0)
2171 init_homedir();
2172 else if (didset_vim && STRICMP(name, "VIM") == 0)
2173 didset_vim = FALSE;
2174 else if (didset_vimruntime
2175 && STRICMP(name, "VIMRUNTIME") == 0)
2176 didset_vimruntime = FALSE;
2177 arg_end = arg;
2178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002180 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 }
2182 }
2183 }
2184
2185 /*
2186 * ":let &option = expr": Set option value.
2187 * ":let &l:option = expr": Set local option value.
2188 * ":let &g:option = expr": Set global option value.
2189 */
2190 else if (*arg == '&')
2191 {
2192 /* Find the end of the name. */
2193 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002194 if (p == NULL || (endchars != NULL
2195 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 EMSG(_(e_letunexp));
2197 else
2198 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199 long n;
2200 int opt_type;
2201 long numval;
2202 char_u *stringval = NULL;
2203 char_u *s;
2204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 c1 = *p;
2206 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207
2208 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 s = get_tv_string_chk(tv); /* != NULL if number or string */
2210 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002211 {
2212 opt_type = get_option_value(arg, &numval,
2213 &stringval, opt_flags);
2214 if ((opt_type == 1 && *op == '.')
2215 || (opt_type == 0 && *op != '.'))
2216 EMSG2(_(e_letwrong), op);
2217 else
2218 {
2219 if (opt_type == 1) /* number */
2220 {
2221 if (*op == '+')
2222 n = numval + n;
2223 else
2224 n = numval - n;
2225 }
2226 else if (opt_type == 0 && stringval != NULL) /* string */
2227 {
2228 s = concat_str(stringval, s);
2229 vim_free(stringval);
2230 stringval = s;
2231 }
2232 }
2233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002234 if (s != NULL)
2235 {
2236 set_option_value(arg, n, s, opt_flags);
2237 arg_end = p;
2238 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002240 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 }
2242 }
2243
2244 /*
2245 * ":let @r = expr": Set register contents.
2246 */
2247 else if (*arg == '@')
2248 {
2249 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002250 if (op != NULL && (*op == '+' || *op == '-'))
2251 EMSG2(_(e_letwrong), op);
2252 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002253 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 EMSG(_(e_letunexp));
2255 else
2256 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002257 char_u *tofree = NULL;
2258 char_u *s;
2259
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002260 p = get_tv_string_chk(tv);
2261 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002262 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002263 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002264 if (s != NULL)
2265 {
2266 p = tofree = concat_str(s, p);
2267 vim_free(s);
2268 }
2269 }
2270 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002272 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002273 arg_end = arg + 1;
2274 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002275 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 }
2277 }
2278
2279 /*
2280 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002281 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002283 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002285 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002287 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2291 EMSG(_(e_letunexp));
2292 else
2293 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002294 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 arg_end = p;
2296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 }
2300
2301 else
2302 EMSG2(_(e_invarg2), arg);
2303
2304 return arg_end;
2305}
2306
2307/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002308 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2309 */
2310 static int
2311check_changedtick(arg)
2312 char_u *arg;
2313{
2314 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2315 {
2316 EMSG2(_(e_readonlyvar), arg);
2317 return TRUE;
2318 }
2319 return FALSE;
2320}
2321
2322/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 * Get an lval: variable, Dict item or List item that can be assigned a value
2324 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2325 * "name.key", "name.key[expr]" etc.
2326 * Indexing only works if "name" is an existing List or Dictionary.
2327 * "name" points to the start of the name.
2328 * If "rettv" is not NULL it points to the value to be assigned.
2329 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2330 * wrong; must end in space or cmd separator.
2331 *
2332 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 */
2336 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002337get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002339 typval_T *rettv;
2340 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 int unlet;
2342 int skip;
2343 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002344 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 char_u *expr_start, *expr_end;
2348 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002349 dictitem_T *v;
2350 typval_T var1;
2351 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002353 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002356 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002359 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360
2361 if (skip)
2362 {
2363 /* When skipping just find the end of the name. */
2364 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002365 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 }
2367
2368 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002369 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 if (expr_start != NULL)
2371 {
2372 /* Don't expand the name when we already know there is an error. */
2373 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2374 && *p != '[' && *p != '.')
2375 {
2376 EMSG(_(e_trailing));
2377 return NULL;
2378 }
2379
2380 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2381 if (lp->ll_exp_name == NULL)
2382 {
2383 /* Report an invalid expression in braces, unless the
2384 * expression evaluation has been cancelled due to an
2385 * aborting error, an interrupt, or an exception. */
2386 if (!aborting() && !quiet)
2387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002388 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 EMSG2(_(e_invarg2), name);
2390 return NULL;
2391 }
2392 }
2393 lp->ll_name = lp->ll_exp_name;
2394 }
2395 else
2396 lp->ll_name = name;
2397
2398 /* Without [idx] or .key we are done. */
2399 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2400 return p;
2401
2402 cc = *p;
2403 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002404 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 if (v == NULL && !quiet)
2406 EMSG2(_(e_undefvar), lp->ll_name);
2407 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 if (v == NULL)
2409 return NULL;
2410
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 /*
2412 * Loop until no more [idx] or .key is following.
2413 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002414 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2418 && !(lp->ll_tv->v_type == VAR_DICT
2419 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 if (!quiet)
2422 EMSG(_("E689: Can only index a List or Dictionary"));
2423 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 if (!quiet)
2428 EMSG(_("E708: [:] must come last"));
2429 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002430 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002431
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 len = -1;
2433 if (*p == '.')
2434 {
2435 key = p + 1;
2436 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2437 ;
2438 if (len == 0)
2439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (!quiet)
2441 EMSG(_(e_emptykey));
2442 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 }
2444 p = key + len;
2445 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002446 else
2447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002449 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 if (*p == ':')
2451 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002452 else
2453 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 empty1 = FALSE;
2455 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 if (get_tv_string_chk(&var1) == NULL)
2458 {
2459 /* not a number or string */
2460 clear_tv(&var1);
2461 return NULL;
2462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 }
2464
2465 /* Optionally get the second index [ :expr]. */
2466 if (*p == ':')
2467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002472 if (!empty1)
2473 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002475 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (rettv != NULL && (rettv->v_type != VAR_LIST
2477 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 if (!quiet)
2480 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 if (!empty1)
2482 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 }
2485 p = skipwhite(p + 1);
2486 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 else
2489 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002491 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2492 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 if (!empty1)
2494 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002497 if (get_tv_string_chk(&var2) == NULL)
2498 {
2499 /* not a number or string */
2500 if (!empty1)
2501 clear_tv(&var1);
2502 clear_tv(&var2);
2503 return NULL;
2504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002510
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002512 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (!quiet)
2514 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 if (!empty1)
2516 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 }
2521
2522 /* Skip to past ']'. */
2523 ++p;
2524 }
2525
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 {
2528 if (len == -1)
2529 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002531 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 if (*key == NUL)
2533 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 if (!quiet)
2535 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 }
2539 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002540 lp->ll_list = NULL;
2541 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002542 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002545 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 if (len == -1)
2551 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 }
2554 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 if (len == -1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 p = NULL;
2562 break;
2563 }
2564 if (len == -1)
2565 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 }
2568 else
2569 {
2570 /*
2571 * Get the number and item for the only or first index of the List.
2572 */
2573 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 else
2576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002577 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 clear_tv(&var1);
2579 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002580 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 lp->ll_list = lp->ll_tv->vval.v_list;
2582 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2583 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002585 if (lp->ll_n1 < 0)
2586 {
2587 lp->ll_n1 = 0;
2588 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2589 }
2590 }
2591 if (lp->ll_li == NULL)
2592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 }
2597
2598 /*
2599 * May need to find the item or absolute index for the second
2600 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 * When no index given: "lp->ll_empty2" is TRUE.
2602 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002606 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002607 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 }
2615
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2617 if (lp->ll_n1 < 0)
2618 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2619 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002621 }
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002625 }
2626
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 return p;
2628}
2629
2630/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 */
2633 static void
2634clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636{
2637 vim_free(lp->ll_exp_name);
2638 vim_free(lp->ll_newkey);
2639}
2640
2641/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002642 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002644 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 */
2646 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002647set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002652 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653{
2654 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002655 listitem_T *ri;
2656 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657
2658 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 cc = *endp;
2663 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002664 if (op != NULL && *op != '=')
2665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002666 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002668 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002669 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002670 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002671 {
2672 if (tv_op(&tv, rettv, op) == OK)
2673 set_var(lp->ll_name, &tv, FALSE);
2674 clear_tv(&tv);
2675 }
2676 }
2677 else
2678 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002682 else if (tv_check_lock(lp->ll_newkey == NULL
2683 ? lp->ll_tv->v_lock
2684 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2685 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 else if (lp->ll_range)
2687 {
2688 /*
2689 * Assign the List values to the list items.
2690 */
2691 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002693 if (op != NULL && *op != '=')
2694 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2695 else
2696 {
2697 clear_tv(&lp->ll_li->li_tv);
2698 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 ri = ri->li_next;
2701 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2702 break;
2703 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002706 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 ri = NULL;
2709 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_li = lp->ll_li->li_next;
2713 ++lp->ll_n1;
2714 }
2715 if (ri != NULL)
2716 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717 else if (lp->ll_empty2
2718 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 : lp->ll_n1 != lp->ll_n2)
2720 EMSG(_("E711: List value has not enough items"));
2721 }
2722 else
2723 {
2724 /*
2725 * Assign to a List or Dictionary item.
2726 */
2727 if (lp->ll_newkey != NULL)
2728 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002729 if (op != NULL && *op != '=')
2730 {
2731 EMSG2(_(e_letwrong), op);
2732 return;
2733 }
2734
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002736 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (di == NULL)
2738 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2740 {
2741 vim_free(di);
2742 return;
2743 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 else if (op != NULL && *op != '=')
2747 {
2748 tv_op(lp->ll_tv, rettv, op);
2749 return;
2750 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 /*
2755 * Assign the value to the variable or list item.
2756 */
2757 if (copy)
2758 copy_tv(rettv, lp->ll_tv);
2759 else
2760 {
2761 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002762 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002764 }
2765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002766}
2767
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002769 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2770 * Returns OK or FAIL.
2771 */
2772 static int
2773tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002774 typval_T *tv1;
2775 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 char_u *op;
2777{
2778 long n;
2779 char_u numbuf[NUMBUFLEN];
2780 char_u *s;
2781
2782 /* Can't do anything with a Funcref or a Dict on the right. */
2783 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2784 {
2785 switch (tv1->v_type)
2786 {
2787 case VAR_DICT:
2788 case VAR_FUNC:
2789 break;
2790
2791 case VAR_LIST:
2792 if (*op != '+' || tv2->v_type != VAR_LIST)
2793 break;
2794 /* List += List */
2795 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2796 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2797 return OK;
2798
2799 case VAR_NUMBER:
2800 case VAR_STRING:
2801 if (tv2->v_type == VAR_LIST)
2802 break;
2803 if (*op == '+' || *op == '-')
2804 {
2805 /* nr += nr or nr -= nr*/
2806 n = get_tv_number(tv1);
2807 if (*op == '+')
2808 n += get_tv_number(tv2);
2809 else
2810 n -= get_tv_number(tv2);
2811 clear_tv(tv1);
2812 tv1->v_type = VAR_NUMBER;
2813 tv1->vval.v_number = n;
2814 }
2815 else
2816 {
2817 /* str .= str */
2818 s = get_tv_string(tv1);
2819 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2820 clear_tv(tv1);
2821 tv1->v_type = VAR_STRING;
2822 tv1->vval.v_string = s;
2823 }
2824 return OK;
2825 }
2826 }
2827
2828 EMSG2(_(e_letwrong), op);
2829 return FAIL;
2830}
2831
2832/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 * Add a watcher to a list.
2834 */
2835 static void
2836list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002837 list_T *l;
2838 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839{
2840 lw->lw_next = l->lv_watch;
2841 l->lv_watch = lw;
2842}
2843
2844/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002845 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846 * No warning when it isn't found...
2847 */
2848 static void
2849list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 list_T *l;
2851 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852{
Bram Moolenaar33570922005-01-25 22:26:29 +00002853 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854
2855 lwp = &l->lv_watch;
2856 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2857 {
2858 if (lw == lwrem)
2859 {
2860 *lwp = lw->lw_next;
2861 break;
2862 }
2863 lwp = &lw->lw_next;
2864 }
2865}
2866
2867/*
2868 * Just before removing an item from a list: advance watchers to the next
2869 * item.
2870 */
2871 static void
2872list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 list_T *l;
2874 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875{
Bram Moolenaar33570922005-01-25 22:26:29 +00002876 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877
2878 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2879 if (lw->lw_item == item)
2880 lw->lw_item = item->li_next;
2881}
2882
2883/*
2884 * Evaluate the expression used in a ":for var in expr" command.
2885 * "arg" points to "var".
2886 * Set "*errp" to TRUE for an error, FALSE otherwise;
2887 * Return a pointer that holds the info. Null when there is an error.
2888 */
2889 void *
2890eval_for_line(arg, errp, nextcmdp, skip)
2891 char_u *arg;
2892 int *errp;
2893 char_u **nextcmdp;
2894 int skip;
2895{
Bram Moolenaar33570922005-01-25 22:26:29 +00002896 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 typval_T tv;
2899 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900
2901 *errp = TRUE; /* default: there is an error */
2902
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 if (fi == NULL)
2905 return NULL;
2906
2907 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2908 if (expr == NULL)
2909 return fi;
2910
2911 expr = skipwhite(expr);
2912 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2913 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002914 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002915 return fi;
2916 }
2917
2918 if (skip)
2919 ++emsg_skip;
2920 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2921 {
2922 *errp = FALSE;
2923 if (!skip)
2924 {
2925 l = tv.vval.v_list;
2926 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002927 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002929 clear_tv(&tv);
2930 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 else
2932 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002933 /* No need to increment the refcount, it's already set for the
2934 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 fi->fi_list = l;
2936 list_add_watch(l, &fi->fi_lw);
2937 fi->fi_lw.lw_item = l->lv_first;
2938 }
2939 }
2940 }
2941 if (skip)
2942 --emsg_skip;
2943
2944 return fi;
2945}
2946
2947/*
2948 * Use the first item in a ":for" list. Advance to the next.
2949 * Assign the values to the variable (list). "arg" points to the first one.
2950 * Return TRUE when a valid item was found, FALSE when at end of list or
2951 * something wrong.
2952 */
2953 int
2954next_for_item(fi_void, arg)
2955 void *fi_void;
2956 char_u *arg;
2957{
Bram Moolenaar33570922005-01-25 22:26:29 +00002958 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002961
2962 item = fi->fi_lw.lw_item;
2963 if (item == NULL)
2964 result = FALSE;
2965 else
2966 {
2967 fi->fi_lw.lw_item = item->li_next;
2968 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2969 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2970 }
2971 return result;
2972}
2973
2974/*
2975 * Free the structure used to store info used by ":for".
2976 */
2977 void
2978free_for_info(fi_void)
2979 void *fi_void;
2980{
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002983 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002984 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002985 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002986 list_unref(fi->fi_list);
2987 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002988 vim_free(fi);
2989}
2990
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2992
2993 void
2994set_context_for_expression(xp, arg, cmdidx)
2995 expand_T *xp;
2996 char_u *arg;
2997 cmdidx_T cmdidx;
2998{
2999 int got_eq = FALSE;
3000 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 if (cmdidx == CMD_let)
3004 {
3005 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003006 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003007 {
3008 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003009 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003010 {
3011 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003012 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003013 if (vim_iswhite(*p))
3014 break;
3015 }
3016 return;
3017 }
3018 }
3019 else
3020 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3021 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 while ((xp->xp_pattern = vim_strpbrk(arg,
3023 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3024 {
3025 c = *xp->xp_pattern;
3026 if (c == '&')
3027 {
3028 c = xp->xp_pattern[1];
3029 if (c == '&')
3030 {
3031 ++xp->xp_pattern;
3032 xp->xp_context = cmdidx != CMD_let || got_eq
3033 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3034 }
3035 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003038 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3039 xp->xp_pattern += 2;
3040
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 else if (c == '$')
3044 {
3045 /* environment variable */
3046 xp->xp_context = EXPAND_ENV_VARS;
3047 }
3048 else if (c == '=')
3049 {
3050 got_eq = TRUE;
3051 xp->xp_context = EXPAND_EXPRESSION;
3052 }
3053 else if (c == '<'
3054 && xp->xp_context == EXPAND_FUNCTIONS
3055 && vim_strchr(xp->xp_pattern, '(') == NULL)
3056 {
3057 /* Function name can start with "<SNR>" */
3058 break;
3059 }
3060 else if (cmdidx != CMD_let || got_eq)
3061 {
3062 if (c == '"') /* string */
3063 {
3064 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3065 if (c == '\\' && xp->xp_pattern[1] != NUL)
3066 ++xp->xp_pattern;
3067 xp->xp_context = EXPAND_NOTHING;
3068 }
3069 else if (c == '\'') /* literal string */
3070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003071 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3073 /* skip */ ;
3074 xp->xp_context = EXPAND_NOTHING;
3075 }
3076 else if (c == '|')
3077 {
3078 if (xp->xp_pattern[1] == '|')
3079 {
3080 ++xp->xp_pattern;
3081 xp->xp_context = EXPAND_EXPRESSION;
3082 }
3083 else
3084 xp->xp_context = EXPAND_COMMANDS;
3085 }
3086 else
3087 xp->xp_context = EXPAND_EXPRESSION;
3088 }
3089 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090 /* Doesn't look like something valid, expand as an expression
3091 * anyway. */
3092 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 arg = xp->xp_pattern;
3094 if (*arg != NUL)
3095 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3096 /* skip */ ;
3097 }
3098 xp->xp_pattern = arg;
3099}
3100
3101#endif /* FEAT_CMDL_COMPL */
3102
3103/*
3104 * ":1,25call func(arg1, arg2)" function call.
3105 */
3106 void
3107ex_call(eap)
3108 exarg_T *eap;
3109{
3110 char_u *arg = eap->arg;
3111 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 linenr_T lnum;
3117 int doesrange;
3118 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003121 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3122 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 if (tofree == NULL)
3124 return;
3125
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003126 /* Increase refcount on dictionary, it could get deleted when evaluating
3127 * the arguments. */
3128 if (fudi.fd_dict != NULL)
3129 ++fudi.fd_dict->dv_refcount;
3130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003132 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
Bram Moolenaar532c7802005-01-27 14:44:31 +00003135 /* Skip white space to allow ":call func ()". Not good, but required for
3136 * backward compatibility. */
3137 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003138 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 if (*startarg != '(')
3141 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003142 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 goto end;
3144 }
3145
3146 /*
3147 * When skipping, evaluate the function once, to find the end of the
3148 * arguments.
3149 * When the function takes a range, this is discovered after the first
3150 * call, and the loop is broken.
3151 */
3152 if (eap->skip)
3153 {
3154 ++emsg_skip;
3155 lnum = eap->line2; /* do it once, also with an invalid range */
3156 }
3157 else
3158 lnum = eap->line1;
3159 for ( ; lnum <= eap->line2; ++lnum)
3160 {
3161 if (!eap->skip && eap->addr_count > 0)
3162 {
3163 curwin->w_cursor.lnum = lnum;
3164 curwin->w_cursor.col = 0;
3165 }
3166 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003167 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003168 eap->line1, eap->line2, &doesrange,
3169 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
3171 failed = TRUE;
3172 break;
3173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003174 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (doesrange || eap->skip)
3176 break;
3177 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003178 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003179 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003180 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (aborting())
3182 break;
3183 }
3184 if (eap->skip)
3185 --emsg_skip;
3186
3187 if (!failed)
3188 {
3189 /* Check for trailing illegal characters and a following command. */
3190 if (!ends_excmd(*arg))
3191 {
3192 emsg_severe = TRUE;
3193 EMSG(_(e_trailing));
3194 }
3195 else
3196 eap->nextcmd = check_nextcmd(arg);
3197 }
3198
3199end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003200 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003201 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202}
3203
3204/*
3205 * ":unlet[!] var1 ... " command.
3206 */
3207 void
3208ex_unlet(eap)
3209 exarg_T *eap;
3210{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003211 ex_unletlock(eap, eap->arg, 0);
3212}
3213
3214/*
3215 * ":lockvar" and ":unlockvar" commands
3216 */
3217 void
3218ex_lockvar(eap)
3219 exarg_T *eap;
3220{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003222 int deep = 2;
3223
3224 if (eap->forceit)
3225 deep = -1;
3226 else if (vim_isdigit(*arg))
3227 {
3228 deep = getdigits(&arg);
3229 arg = skipwhite(arg);
3230 }
3231
3232 ex_unletlock(eap, arg, deep);
3233}
3234
3235/*
3236 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3237 */
3238 static void
3239ex_unletlock(eap, argstart, deep)
3240 exarg_T *eap;
3241 char_u *argstart;
3242 int deep;
3243{
3244 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003247 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 do
3250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003252 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3253 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003254 if (lv.ll_name == NULL)
3255 error = TRUE; /* error but continue parsing */
3256 if (name_end == NULL || (!vim_iswhite(*name_end)
3257 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003259 if (name_end != NULL)
3260 {
3261 emsg_severe = TRUE;
3262 EMSG(_(e_trailing));
3263 }
3264 if (!(eap->skip || error))
3265 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 break;
3267 }
3268
3269 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270 {
3271 if (eap->cmdidx == CMD_unlet)
3272 {
3273 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3274 error = TRUE;
3275 }
3276 else
3277 {
3278 if (do_lock_var(&lv, name_end, deep,
3279 eap->cmdidx == CMD_lockvar) == FAIL)
3280 error = TRUE;
3281 }
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003284 if (!eap->skip)
3285 clear_lval(&lv);
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 arg = skipwhite(name_end);
3288 } while (!ends_excmd(*arg));
3289
3290 eap->nextcmd = check_nextcmd(arg);
3291}
3292
Bram Moolenaar8c711452005-01-14 21:53:12 +00003293 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003294do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003296 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003297 int forceit;
3298{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003299 int ret = OK;
3300 int cc;
3301
3302 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003303 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003304 cc = *name_end;
3305 *name_end = NUL;
3306
3307 /* Normal name or expanded name. */
3308 if (check_changedtick(lp->ll_name))
3309 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003310 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003312 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003313 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003314 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3315 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003316 else if (lp->ll_range)
3317 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003319
3320 /* Delete a range of List items. */
3321 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3322 {
3323 li = lp->ll_li->li_next;
3324 listitem_remove(lp->ll_list, lp->ll_li);
3325 lp->ll_li = li;
3326 ++lp->ll_n1;
3327 }
3328 }
3329 else
3330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003331 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003332 /* unlet a List item. */
3333 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003334 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003335 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003336 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003337 }
3338
3339 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003340}
3341
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342/*
3343 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003344 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
3346 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003347do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003349 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 hashtab_T *ht;
3352 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003353 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaar33570922005-01-25 22:26:29 +00003355 ht = find_var_ht(name, &varname);
3356 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 hi = hash_find(ht, varname);
3359 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003360 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003361 if (var_check_ro(HI2DI(hi)->di_flags, name))
3362 return FAIL;
3363 delete_var(ht, hi);
3364 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003367 if (forceit)
3368 return OK;
3369 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 return FAIL;
3371}
3372
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003373/*
3374 * Lock or unlock variable indicated by "lp".
3375 * "deep" is the levels to go (-1 for unlimited);
3376 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3377 */
3378 static int
3379do_lock_var(lp, name_end, deep, lock)
3380 lval_T *lp;
3381 char_u *name_end;
3382 int deep;
3383 int lock;
3384{
3385 int ret = OK;
3386 int cc;
3387 dictitem_T *di;
3388
3389 if (deep == 0) /* nothing to do */
3390 return OK;
3391
3392 if (lp->ll_tv == NULL)
3393 {
3394 cc = *name_end;
3395 *name_end = NUL;
3396
3397 /* Normal name or expanded name. */
3398 if (check_changedtick(lp->ll_name))
3399 ret = FAIL;
3400 else
3401 {
3402 di = find_var(lp->ll_name, NULL);
3403 if (di == NULL)
3404 ret = FAIL;
3405 else
3406 {
3407 if (lock)
3408 di->di_flags |= DI_FLAGS_LOCK;
3409 else
3410 di->di_flags &= ~DI_FLAGS_LOCK;
3411 item_lock(&di->di_tv, deep, lock);
3412 }
3413 }
3414 *name_end = cc;
3415 }
3416 else if (lp->ll_range)
3417 {
3418 listitem_T *li = lp->ll_li;
3419
3420 /* (un)lock a range of List items. */
3421 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3422 {
3423 item_lock(&li->li_tv, deep, lock);
3424 li = li->li_next;
3425 ++lp->ll_n1;
3426 }
3427 }
3428 else if (lp->ll_list != NULL)
3429 /* (un)lock a List item. */
3430 item_lock(&lp->ll_li->li_tv, deep, lock);
3431 else
3432 /* un(lock) a Dictionary item. */
3433 item_lock(&lp->ll_di->di_tv, deep, lock);
3434
3435 return ret;
3436}
3437
3438/*
3439 * Lock or unlock an item. "deep" is nr of levels to go.
3440 */
3441 static void
3442item_lock(tv, deep, lock)
3443 typval_T *tv;
3444 int deep;
3445 int lock;
3446{
3447 static int recurse = 0;
3448 list_T *l;
3449 listitem_T *li;
3450 dict_T *d;
3451 hashitem_T *hi;
3452 int todo;
3453
3454 if (recurse >= DICT_MAXNEST)
3455 {
3456 EMSG(_("E743: variable nested too deep for (un)lock"));
3457 return;
3458 }
3459 if (deep == 0)
3460 return;
3461 ++recurse;
3462
3463 /* lock/unlock the item itself */
3464 if (lock)
3465 tv->v_lock |= VAR_LOCKED;
3466 else
3467 tv->v_lock &= ~VAR_LOCKED;
3468
3469 switch (tv->v_type)
3470 {
3471 case VAR_LIST:
3472 if ((l = tv->vval.v_list) != NULL)
3473 {
3474 if (lock)
3475 l->lv_lock |= VAR_LOCKED;
3476 else
3477 l->lv_lock &= ~VAR_LOCKED;
3478 if (deep < 0 || deep > 1)
3479 /* recursive: lock/unlock the items the List contains */
3480 for (li = l->lv_first; li != NULL; li = li->li_next)
3481 item_lock(&li->li_tv, deep - 1, lock);
3482 }
3483 break;
3484 case VAR_DICT:
3485 if ((d = tv->vval.v_dict) != NULL)
3486 {
3487 if (lock)
3488 d->dv_lock |= VAR_LOCKED;
3489 else
3490 d->dv_lock &= ~VAR_LOCKED;
3491 if (deep < 0 || deep > 1)
3492 {
3493 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003494 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003495 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3496 {
3497 if (!HASHITEM_EMPTY(hi))
3498 {
3499 --todo;
3500 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3501 }
3502 }
3503 }
3504 }
3505 }
3506 --recurse;
3507}
3508
Bram Moolenaara40058a2005-07-11 22:42:07 +00003509/*
3510 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3511 * it refers to a List or Dictionary that is locked.
3512 */
3513 static int
3514tv_islocked(tv)
3515 typval_T *tv;
3516{
3517 return (tv->v_lock & VAR_LOCKED)
3518 || (tv->v_type == VAR_LIST
3519 && tv->vval.v_list != NULL
3520 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3521 || (tv->v_type == VAR_DICT
3522 && tv->vval.v_dict != NULL
3523 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3524}
3525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3527/*
3528 * Delete all "menutrans_" variables.
3529 */
3530 void
3531del_menutrans_vars()
3532{
Bram Moolenaar33570922005-01-25 22:26:29 +00003533 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 {
3540 if (!HASHITEM_EMPTY(hi))
3541 {
3542 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3544 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 }
3546 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003547 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
3549#endif
3550
3551#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3552
3553/*
3554 * Local string buffer for the next two functions to store a variable name
3555 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3556 * get_user_var_name().
3557 */
3558
3559static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3560
3561static char_u *varnamebuf = NULL;
3562static int varnamebuflen = 0;
3563
3564/*
3565 * Function to concatenate a prefix and a variable name.
3566 */
3567 static char_u *
3568cat_prefix_varname(prefix, name)
3569 int prefix;
3570 char_u *name;
3571{
3572 int len;
3573
3574 len = (int)STRLEN(name) + 3;
3575 if (len > varnamebuflen)
3576 {
3577 vim_free(varnamebuf);
3578 len += 10; /* some additional space */
3579 varnamebuf = alloc(len);
3580 if (varnamebuf == NULL)
3581 {
3582 varnamebuflen = 0;
3583 return NULL;
3584 }
3585 varnamebuflen = len;
3586 }
3587 *varnamebuf = prefix;
3588 varnamebuf[1] = ':';
3589 STRCPY(varnamebuf + 2, name);
3590 return varnamebuf;
3591}
3592
3593/*
3594 * Function given to ExpandGeneric() to obtain the list of user defined
3595 * (global/buffer/window/built-in) variable names.
3596 */
3597/*ARGSUSED*/
3598 char_u *
3599get_user_var_name(xp, idx)
3600 expand_T *xp;
3601 int idx;
3602{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003603 static long_u gdone;
3604 static long_u bdone;
3605 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003606#ifdef FEAT_WINDOWS
3607 static long_u tdone;
3608#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003609 static int vidx;
3610 static hashitem_T *hi;
3611 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003614 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003615 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003616#ifdef FEAT_WINDOWS
3617 tdone = 0;
3618#endif
3619 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003620
3621 /* Global variables */
3622 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003624 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003626 else
3627 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003628 while (HASHITEM_EMPTY(hi))
3629 ++hi;
3630 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3631 return cat_prefix_varname('g', hi->hi_key);
3632 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003634
3635 /* b: variables */
3636 ht = &curbuf->b_vars.dv_hashtab;
3637 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003639 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003640 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003641 else
3642 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003643 while (HASHITEM_EMPTY(hi))
3644 ++hi;
3645 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003647 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003649 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return (char_u *)"b:changedtick";
3651 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003652
3653 /* w: variables */
3654 ht = &curwin->w_vars.dv_hashtab;
3655 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003657 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003659 else
3660 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003661 while (HASHITEM_EMPTY(hi))
3662 ++hi;
3663 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003665
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003666#ifdef FEAT_WINDOWS
3667 /* t: variables */
3668 ht = &curtab->tp_vars.dv_hashtab;
3669 if (tdone < ht->ht_used)
3670 {
3671 if (tdone++ == 0)
3672 hi = ht->ht_array;
3673 else
3674 ++hi;
3675 while (HASHITEM_EMPTY(hi))
3676 ++hi;
3677 return cat_prefix_varname('t', hi->hi_key);
3678 }
3679#endif
3680
Bram Moolenaar33570922005-01-25 22:26:29 +00003681 /* v: variables */
3682 if (vidx < VV_LEN)
3683 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 vim_free(varnamebuf);
3686 varnamebuf = NULL;
3687 varnamebuflen = 0;
3688 return NULL;
3689}
3690
3691#endif /* FEAT_CMDL_COMPL */
3692
3693/*
3694 * types for expressions.
3695 */
3696typedef enum
3697{
3698 TYPE_UNKNOWN = 0
3699 , TYPE_EQUAL /* == */
3700 , TYPE_NEQUAL /* != */
3701 , TYPE_GREATER /* > */
3702 , TYPE_GEQUAL /* >= */
3703 , TYPE_SMALLER /* < */
3704 , TYPE_SEQUAL /* <= */
3705 , TYPE_MATCH /* =~ */
3706 , TYPE_NOMATCH /* !~ */
3707} exptype_T;
3708
3709/*
3710 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3713 */
3714
3715/*
3716 * Handle zero level expression.
3717 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003718 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003719 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 * Return OK or FAIL.
3721 */
3722 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003723eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 char_u **nextcmd;
3727 int evaluate;
3728{
3729 int ret;
3730 char_u *p;
3731
3732 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (ret == FAIL || !ends_excmd(*p))
3735 {
3736 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003737 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 /*
3739 * Report the invalid expression unless the expression evaluation has
3740 * been cancelled due to an aborting error, an interrupt, or an
3741 * exception.
3742 */
3743 if (!aborting())
3744 EMSG2(_(e_invexpr2), arg);
3745 ret = FAIL;
3746 }
3747 if (nextcmd != NULL)
3748 *nextcmd = check_nextcmd(p);
3749
3750 return ret;
3751}
3752
3753/*
3754 * Handle top level expression:
3755 * expr1 ? expr0 : expr0
3756 *
3757 * "arg" must point to the first non-white of the expression.
3758 * "arg" is advanced to the next non-white after the recognized expression.
3759 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003760 * Note: "rettv.v_lock" is not set.
3761 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 * Return OK or FAIL.
3763 */
3764 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003765eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 int evaluate;
3769{
3770 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * Get the first variable.
3775 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003776 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 return FAIL;
3778
3779 if ((*arg)[0] == '?')
3780 {
3781 result = FALSE;
3782 if (evaluate)
3783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784 int error = FALSE;
3785
3786 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003788 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 if (error)
3790 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792
3793 /*
3794 * Get the second variable.
3795 */
3796 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return FAIL;
3799
3800 /*
3801 * Check for the ":".
3802 */
3803 if ((*arg)[0] != ':')
3804 {
3805 EMSG(_("E109: Missing ':' after '?'"));
3806 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return FAIL;
3809 }
3810
3811 /*
3812 * Get the third variable.
3813 */
3814 *arg = skipwhite(*arg + 1);
3815 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3816 {
3817 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003818 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 return FAIL;
3820 }
3821 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003822 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 }
3824
3825 return OK;
3826}
3827
3828/*
3829 * Handle first level expression:
3830 * expr2 || expr2 || expr2 logical OR
3831 *
3832 * "arg" must point to the first non-white of the expression.
3833 * "arg" is advanced to the next non-white after the recognized expression.
3834 *
3835 * Return OK or FAIL.
3836 */
3837 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003838eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 int evaluate;
3842{
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 long result;
3845 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003846 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 /*
3849 * Get the first variable.
3850 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return FAIL;
3853
3854 /*
3855 * Repeat until there is no following "||".
3856 */
3857 first = TRUE;
3858 result = FALSE;
3859 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3860 {
3861 if (evaluate && first)
3862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003865 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 if (error)
3867 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 first = FALSE;
3869 }
3870
3871 /*
3872 * Get the second variable.
3873 */
3874 *arg = skipwhite(*arg + 2);
3875 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3876 return FAIL;
3877
3878 /*
3879 * Compute the result.
3880 */
3881 if (evaluate && !result)
3882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003885 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 if (error)
3887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889 if (evaluate)
3890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 rettv->v_type = VAR_NUMBER;
3892 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 }
3895
3896 return OK;
3897}
3898
3899/*
3900 * Handle second level expression:
3901 * expr3 && expr3 && expr3 logical AND
3902 *
3903 * "arg" must point to the first non-white of the expression.
3904 * "arg" is advanced to the next non-white after the recognized expression.
3905 *
3906 * Return OK or FAIL.
3907 */
3908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003909eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 int evaluate;
3913{
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 long result;
3916 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003917 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919 /*
3920 * Get the first variable.
3921 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 return FAIL;
3924
3925 /*
3926 * Repeat until there is no following "&&".
3927 */
3928 first = TRUE;
3929 result = TRUE;
3930 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3931 {
3932 if (evaluate && first)
3933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003934 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003936 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003937 if (error)
3938 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 first = FALSE;
3940 }
3941
3942 /*
3943 * Get the second variable.
3944 */
3945 *arg = skipwhite(*arg + 2);
3946 if (eval4(arg, &var2, evaluate && result) == FAIL)
3947 return FAIL;
3948
3949 /*
3950 * Compute the result.
3951 */
3952 if (evaluate && result)
3953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003954 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003957 if (error)
3958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 if (evaluate)
3961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003962 rettv->v_type = VAR_NUMBER;
3963 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965 }
3966
3967 return OK;
3968}
3969
3970/*
3971 * Handle third level expression:
3972 * var1 == var2
3973 * var1 =~ var2
3974 * var1 != var2
3975 * var1 !~ var2
3976 * var1 > var2
3977 * var1 >= var2
3978 * var1 < var2
3979 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003980 * var1 is var2
3981 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 *
3983 * "arg" must point to the first non-white of the expression.
3984 * "arg" is advanced to the next non-white after the recognized expression.
3985 *
3986 * Return OK or FAIL.
3987 */
3988 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003989eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 int evaluate;
3993{
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 char_u *p;
3996 int i;
3997 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003998 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 int len = 2;
4000 long n1, n2;
4001 char_u *s1, *s2;
4002 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4003 regmatch_T regmatch;
4004 int ic;
4005 char_u *save_cpo;
4006
4007 /*
4008 * Get the first variable.
4009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 return FAIL;
4012
4013 p = *arg;
4014 switch (p[0])
4015 {
4016 case '=': if (p[1] == '=')
4017 type = TYPE_EQUAL;
4018 else if (p[1] == '~')
4019 type = TYPE_MATCH;
4020 break;
4021 case '!': if (p[1] == '=')
4022 type = TYPE_NEQUAL;
4023 else if (p[1] == '~')
4024 type = TYPE_NOMATCH;
4025 break;
4026 case '>': if (p[1] != '=')
4027 {
4028 type = TYPE_GREATER;
4029 len = 1;
4030 }
4031 else
4032 type = TYPE_GEQUAL;
4033 break;
4034 case '<': if (p[1] != '=')
4035 {
4036 type = TYPE_SMALLER;
4037 len = 1;
4038 }
4039 else
4040 type = TYPE_SEQUAL;
4041 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004042 case 'i': if (p[1] == 's')
4043 {
4044 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4045 len = 5;
4046 if (!vim_isIDc(p[len]))
4047 {
4048 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4049 type_is = TRUE;
4050 }
4051 }
4052 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054
4055 /*
4056 * If there is a comparitive operator, use it.
4057 */
4058 if (type != TYPE_UNKNOWN)
4059 {
4060 /* extra question mark appended: ignore case */
4061 if (p[len] == '?')
4062 {
4063 ic = TRUE;
4064 ++len;
4065 }
4066 /* extra '#' appended: match case */
4067 else if (p[len] == '#')
4068 {
4069 ic = FALSE;
4070 ++len;
4071 }
4072 /* nothing appened: use 'ignorecase' */
4073 else
4074 ic = p_ic;
4075
4076 /*
4077 * Get the second variable.
4078 */
4079 *arg = skipwhite(p + len);
4080 if (eval5(arg, &var2, evaluate) == FAIL)
4081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004082 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 return FAIL;
4084 }
4085
4086 if (evaluate)
4087 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004088 if (type_is && rettv->v_type != var2.v_type)
4089 {
4090 /* For "is" a different type always means FALSE, for "notis"
4091 * it means TRUE. */
4092 n1 = (type == TYPE_NEQUAL);
4093 }
4094 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4095 {
4096 if (type_is)
4097 {
4098 n1 = (rettv->v_type == var2.v_type
4099 && rettv->vval.v_list == var2.vval.v_list);
4100 if (type == TYPE_NEQUAL)
4101 n1 = !n1;
4102 }
4103 else if (rettv->v_type != var2.v_type
4104 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4105 {
4106 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004107 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004108 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004109 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004110 clear_tv(rettv);
4111 clear_tv(&var2);
4112 return FAIL;
4113 }
4114 else
4115 {
4116 /* Compare two Lists for being equal or unequal. */
4117 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4118 if (type == TYPE_NEQUAL)
4119 n1 = !n1;
4120 }
4121 }
4122
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004123 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4124 {
4125 if (type_is)
4126 {
4127 n1 = (rettv->v_type == var2.v_type
4128 && rettv->vval.v_dict == var2.vval.v_dict);
4129 if (type == TYPE_NEQUAL)
4130 n1 = !n1;
4131 }
4132 else if (rettv->v_type != var2.v_type
4133 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4134 {
4135 if (rettv->v_type != var2.v_type)
4136 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4137 else
4138 EMSG(_("E736: Invalid operation for Dictionary"));
4139 clear_tv(rettv);
4140 clear_tv(&var2);
4141 return FAIL;
4142 }
4143 else
4144 {
4145 /* Compare two Dictionaries for being equal or unequal. */
4146 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4147 if (type == TYPE_NEQUAL)
4148 n1 = !n1;
4149 }
4150 }
4151
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004152 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4153 {
4154 if (rettv->v_type != var2.v_type
4155 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4156 {
4157 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004158 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004159 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004160 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004161 clear_tv(rettv);
4162 clear_tv(&var2);
4163 return FAIL;
4164 }
4165 else
4166 {
4167 /* Compare two Funcrefs for being equal or unequal. */
4168 if (rettv->vval.v_string == NULL
4169 || var2.vval.v_string == NULL)
4170 n1 = FALSE;
4171 else
4172 n1 = STRCMP(rettv->vval.v_string,
4173 var2.vval.v_string) == 0;
4174 if (type == TYPE_NEQUAL)
4175 n1 = !n1;
4176 }
4177 }
4178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 /*
4180 * If one of the two variables is a number, compare as a number.
4181 * When using "=~" or "!~", always compare as string.
4182 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004183 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 n1 = get_tv_number(rettv);
4187 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 switch (type)
4189 {
4190 case TYPE_EQUAL: n1 = (n1 == n2); break;
4191 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4192 case TYPE_GREATER: n1 = (n1 > n2); break;
4193 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4194 case TYPE_SMALLER: n1 = (n1 < n2); break;
4195 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4196 case TYPE_UNKNOWN:
4197 case TYPE_MATCH:
4198 case TYPE_NOMATCH: break; /* avoid gcc warning */
4199 }
4200 }
4201 else
4202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 s1 = get_tv_string_buf(rettv, buf1);
4204 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4206 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4207 else
4208 i = 0;
4209 n1 = FALSE;
4210 switch (type)
4211 {
4212 case TYPE_EQUAL: n1 = (i == 0); break;
4213 case TYPE_NEQUAL: n1 = (i != 0); break;
4214 case TYPE_GREATER: n1 = (i > 0); break;
4215 case TYPE_GEQUAL: n1 = (i >= 0); break;
4216 case TYPE_SMALLER: n1 = (i < 0); break;
4217 case TYPE_SEQUAL: n1 = (i <= 0); break;
4218
4219 case TYPE_MATCH:
4220 case TYPE_NOMATCH:
4221 /* avoid 'l' flag in 'cpoptions' */
4222 save_cpo = p_cpo;
4223 p_cpo = (char_u *)"";
4224 regmatch.regprog = vim_regcomp(s2,
4225 RE_MAGIC + RE_STRING);
4226 regmatch.rm_ic = ic;
4227 if (regmatch.regprog != NULL)
4228 {
4229 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4230 vim_free(regmatch.regprog);
4231 if (type == TYPE_NOMATCH)
4232 n1 = !n1;
4233 }
4234 p_cpo = save_cpo;
4235 break;
4236
4237 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4238 }
4239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
4241 clear_tv(&var2);
4242 rettv->v_type = VAR_NUMBER;
4243 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245 }
4246
4247 return OK;
4248}
4249
4250/*
4251 * Handle fourth level expression:
4252 * + number addition
4253 * - number subtraction
4254 * . string concatenation
4255 *
4256 * "arg" must point to the first non-white of the expression.
4257 * "arg" is advanced to the next non-white after the recognized expression.
4258 *
4259 * Return OK or FAIL.
4260 */
4261 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 int evaluate;
4266{
Bram Moolenaar33570922005-01-25 22:26:29 +00004267 typval_T var2;
4268 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 int op;
4270 long n1, n2;
4271 char_u *s1, *s2;
4272 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4273 char_u *p;
4274
4275 /*
4276 * Get the first variable.
4277 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return FAIL;
4280
4281 /*
4282 * Repeat computing, until no '+', '-' or '.' is following.
4283 */
4284 for (;;)
4285 {
4286 op = **arg;
4287 if (op != '+' && op != '-' && op != '.')
4288 break;
4289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (op != '+' || rettv->v_type != VAR_LIST)
4291 {
4292 /* For "list + ...", an illegal use of the first operand as
4293 * a number cannot be determined before evaluating the 2nd
4294 * operand: if this is also a list, all is ok.
4295 * For "something . ...", "something - ..." or "non-list + ...",
4296 * we know that the first operand needs to be a string or number
4297 * without evaluating the 2nd operand. So check before to avoid
4298 * side effects after an error. */
4299 if (evaluate && get_tv_string_chk(rettv) == NULL)
4300 {
4301 clear_tv(rettv);
4302 return FAIL;
4303 }
4304 }
4305
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 /*
4307 * Get the second variable.
4308 */
4309 *arg = skipwhite(*arg + 1);
4310 if (eval6(arg, &var2, evaluate) == FAIL)
4311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314 }
4315
4316 if (evaluate)
4317 {
4318 /*
4319 * Compute the result.
4320 */
4321 if (op == '.')
4322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4324 s2 = get_tv_string_buf_chk(&var2, buf2);
4325 if (s2 == NULL) /* type error ? */
4326 {
4327 clear_tv(rettv);
4328 clear_tv(&var2);
4329 return FAIL;
4330 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004331 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 clear_tv(rettv);
4333 rettv->v_type = VAR_STRING;
4334 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004336 else if (op == '+' && rettv->v_type == VAR_LIST
4337 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004338 {
4339 /* concatenate Lists */
4340 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4341 &var3) == FAIL)
4342 {
4343 clear_tv(rettv);
4344 clear_tv(&var2);
4345 return FAIL;
4346 }
4347 clear_tv(rettv);
4348 *rettv = var3;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 else
4351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004352 int error = FALSE;
4353
4354 n1 = get_tv_number_chk(rettv, &error);
4355 if (error)
4356 {
4357 /* This can only happen for "list + non-list".
4358 * For "non-list + ..." or "something - ...", we returned
4359 * before evaluating the 2nd operand. */
4360 clear_tv(rettv);
4361 return FAIL;
4362 }
4363 n2 = get_tv_number_chk(&var2, &error);
4364 if (error)
4365 {
4366 clear_tv(rettv);
4367 clear_tv(&var2);
4368 return FAIL;
4369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (op == '+')
4372 n1 = n1 + n2;
4373 else
4374 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 rettv->v_type = VAR_NUMBER;
4376 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 }
4381 return OK;
4382}
4383
4384/*
4385 * Handle fifth level expression:
4386 * * number multiplication
4387 * / number division
4388 * % number modulo
4389 *
4390 * "arg" must point to the first non-white of the expression.
4391 * "arg" is advanced to the next non-white after the recognized expression.
4392 *
4393 * Return OK or FAIL.
4394 */
4395 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int evaluate;
4400{
Bram Moolenaar33570922005-01-25 22:26:29 +00004401 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int op;
4403 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 /*
4407 * Get the first variable.
4408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 return FAIL;
4411
4412 /*
4413 * Repeat computing, until no '*', '/' or '%' is following.
4414 */
4415 for (;;)
4416 {
4417 op = **arg;
4418 if (op != '*' && op != '/' && op != '%')
4419 break;
4420
4421 if (evaluate)
4422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 if (error)
4426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 else
4429 n1 = 0;
4430
4431 /*
4432 * Get the second variable.
4433 */
4434 *arg = skipwhite(*arg + 1);
4435 if (eval7(arg, &var2, evaluate) == FAIL)
4436 return FAIL;
4437
4438 if (evaluate)
4439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004440 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004441 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 if (error)
4443 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 /*
4446 * Compute the result.
4447 */
4448 if (op == '*')
4449 n1 = n1 * n2;
4450 else if (op == '/')
4451 {
4452 if (n2 == 0) /* give an error message? */
4453 n1 = 0x7fffffffL;
4454 else
4455 n1 = n1 / n2;
4456 }
4457 else
4458 {
4459 if (n2 == 0) /* give an error message? */
4460 n1 = 0;
4461 else
4462 n1 = n1 % n2;
4463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 rettv->v_type = VAR_NUMBER;
4465 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467 }
4468
4469 return OK;
4470}
4471
4472/*
4473 * Handle sixth level expression:
4474 * number number constant
4475 * "string" string contstant
4476 * 'string' literal string contstant
4477 * &option-name option value
4478 * @r register contents
4479 * identifier variable value
4480 * function() function call
4481 * $VAR environment variable
4482 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004483 * [expr, expr] List
4484 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 *
4486 * Also handle:
4487 * ! in front logical NOT
4488 * - in front unary minus
4489 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 * trailing [] subscript in String or List
4491 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 *
4493 * "arg" must point to the first non-white of the expression.
4494 * "arg" is advanced to the next non-white after the recognized expression.
4495 *
4496 * Return OK or FAIL.
4497 */
4498 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 int evaluate;
4503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 long n;
4505 int len;
4506 char_u *s;
4507 int val;
4508 char_u *start_leader, *end_leader;
4509 int ret = OK;
4510 char_u *alias;
4511
4512 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004513 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004516 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518 /*
4519 * Skip '!' and '-' characters. They are handled later.
4520 */
4521 start_leader = *arg;
4522 while (**arg == '!' || **arg == '-' || **arg == '+')
4523 *arg = skipwhite(*arg + 1);
4524 end_leader = *arg;
4525
4526 switch (**arg)
4527 {
4528 /*
4529 * Number constant.
4530 */
4531 case '0':
4532 case '1':
4533 case '2':
4534 case '3':
4535 case '4':
4536 case '5':
4537 case '6':
4538 case '7':
4539 case '8':
4540 case '9':
4541 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4542 *arg += len;
4543 if (evaluate)
4544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004545 rettv->v_type = VAR_NUMBER;
4546 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 break;
4549
4550 /*
4551 * String constant: "string".
4552 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004553 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 break;
4555
4556 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004559 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 break;
4561
4562 /*
4563 * List: [expr, expr]
4564 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567
4568 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004569 * Dictionary: {key: val, key: val}
4570 */
4571 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4572 break;
4573
4574 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004575 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004577 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 break;
4579
4580 /*
4581 * Environment variable: $VAR.
4582 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 break;
4585
4586 /*
4587 * Register contents: @r.
4588 */
4589 case '@': ++*arg;
4590 if (evaluate)
4591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004593 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 if (**arg != NUL)
4596 ++*arg;
4597 break;
4598
4599 /*
4600 * nested expression: (expression).
4601 */
4602 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (**arg == ')')
4605 ++*arg;
4606 else if (ret == OK)
4607 {
4608 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 ret = FAIL;
4611 }
4612 break;
4613
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 break;
4616 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617
4618 if (ret == NOTDONE)
4619 {
4620 /*
4621 * Must be a variable or function name.
4622 * Can also be a curly-braces kind of name: {expr}.
4623 */
4624 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004625 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 if (alias != NULL)
4627 s = alias;
4628
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004629 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004630 ret = FAIL;
4631 else
4632 {
4633 if (**arg == '(') /* recursive! */
4634 {
4635 /* If "s" is the name of a variable of type VAR_FUNC
4636 * use its contents. */
4637 s = deref_func_name(s, &len);
4638
4639 /* Invoke the function. */
4640 ret = get_func_tv(s, len, rettv, arg,
4641 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004642 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004643 /* Stop the expression evaluation when immediately
4644 * aborting on error, or when an interrupt occurred or
4645 * an exception was thrown but not caught. */
4646 if (aborting())
4647 {
4648 if (ret == OK)
4649 clear_tv(rettv);
4650 ret = FAIL;
4651 }
4652 }
4653 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004654 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004655 else
4656 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 }
4658
4659 if (alias != NULL)
4660 vim_free(alias);
4661 }
4662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 *arg = skipwhite(*arg);
4664
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004665 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4666 * expr(expr). */
4667 if (ret == OK)
4668 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 /*
4671 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4672 */
4673 if (ret == OK && evaluate && end_leader > start_leader)
4674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 int error = FALSE;
4676
4677 val = get_tv_number_chk(rettv, &error);
4678 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004680 clear_tv(rettv);
4681 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 else
4684 {
4685 while (end_leader > start_leader)
4686 {
4687 --end_leader;
4688 if (*end_leader == '!')
4689 val = !val;
4690 else if (*end_leader == '-')
4691 val = -val;
4692 }
4693 clear_tv(rettv);
4694 rettv->v_type = VAR_NUMBER;
4695 rettv->vval.v_number = val;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698
4699 return ret;
4700}
4701
4702/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004703 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4704 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4706 */
4707 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004708eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004710 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004712 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713{
4714 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004715 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004717 long len = -1;
4718 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004720 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004722 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724 if (verbose)
4725 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 return FAIL;
4727 }
4728
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 /*
4732 * dict.name
4733 */
4734 key = *arg + 1;
4735 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4736 ;
4737 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004740 }
4741 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004743 /*
4744 * something[idx]
4745 *
4746 * Get the (first) variable from inside the [].
4747 */
4748 *arg = skipwhite(*arg + 1);
4749 if (**arg == ':')
4750 empty1 = TRUE;
4751 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4752 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4754 {
4755 /* not a number or string */
4756 clear_tv(&var1);
4757 return FAIL;
4758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759
4760 /*
4761 * Get the second variable from inside the [:].
4762 */
4763 if (**arg == ':')
4764 {
4765 range = TRUE;
4766 *arg = skipwhite(*arg + 1);
4767 if (**arg == ']')
4768 empty2 = TRUE;
4769 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 if (!empty1)
4772 clear_tv(&var1);
4773 return FAIL;
4774 }
4775 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4776 {
4777 /* not a number or string */
4778 if (!empty1)
4779 clear_tv(&var1);
4780 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004781 return FAIL;
4782 }
4783 }
4784
4785 /* Check for the ']'. */
4786 if (**arg != ']')
4787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004788 if (verbose)
4789 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 clear_tv(&var1);
4791 if (range)
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 }
4797
4798 if (evaluate)
4799 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004800 n1 = 0;
4801 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 n1 = get_tv_number(&var1);
4804 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 }
4806 if (range)
4807 {
4808 if (empty2)
4809 n2 = -1;
4810 else
4811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004812 n2 = get_tv_number(&var2);
4813 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 }
4815 }
4816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 {
4819 case VAR_NUMBER:
4820 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 len = (long)STRLEN(s);
4823 if (range)
4824 {
4825 /* The resulting variable is a substring. If the indexes
4826 * are out of range the result is empty. */
4827 if (n1 < 0)
4828 {
4829 n1 = len + n1;
4830 if (n1 < 0)
4831 n1 = 0;
4832 }
4833 if (n2 < 0)
4834 n2 = len + n2;
4835 else if (n2 >= len)
4836 n2 = len;
4837 if (n1 >= len || n2 < 0 || n1 > n2)
4838 s = NULL;
4839 else
4840 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4841 }
4842 else
4843 {
4844 /* The resulting variable is a string of a single
4845 * character. If the index is too big or negative the
4846 * result is empty. */
4847 if (n1 >= len || n1 < 0)
4848 s = NULL;
4849 else
4850 s = vim_strnsave(s + n1, 1);
4851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(rettv);
4853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 break;
4856
4857 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 if (n1 < 0)
4860 n1 = len + n1;
4861 if (!empty1 && (n1 < 0 || n1 >= len))
4862 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004863 /* For a range we allow invalid values and return an empty
4864 * list. A list index out of range is an error. */
4865 if (!range)
4866 {
4867 if (verbose)
4868 EMSGN(_(e_listidx), n1);
4869 return FAIL;
4870 }
4871 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004872 }
4873 if (range)
4874 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 list_T *l;
4876 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877
4878 if (n2 < 0)
4879 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004880 else if (n2 >= len)
4881 n2 = len - 1;
4882 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004883 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004884 l = list_alloc();
4885 if (l == NULL)
4886 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004888 n1 <= n2; ++n1)
4889 {
4890 if (list_append_tv(l, &item->li_tv) == FAIL)
4891 {
4892 list_free(l);
4893 return FAIL;
4894 }
4895 item = item->li_next;
4896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 clear_tv(rettv);
4898 rettv->v_type = VAR_LIST;
4899 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004900 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 }
4902 else
4903 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004904 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 clear_tv(rettv);
4906 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004907 }
4908 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004909
4910 case VAR_DICT:
4911 if (range)
4912 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004913 if (verbose)
4914 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004915 if (len == -1)
4916 clear_tv(&var1);
4917 return FAIL;
4918 }
4919 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004920 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921
4922 if (len == -1)
4923 {
4924 key = get_tv_string(&var1);
4925 if (*key == NUL)
4926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004927 if (verbose)
4928 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004929 clear_tv(&var1);
4930 return FAIL;
4931 }
4932 }
4933
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004934 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004936 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004937 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 if (len == -1)
4939 clear_tv(&var1);
4940 if (item == NULL)
4941 return FAIL;
4942
4943 copy_tv(&item->di_tv, &var1);
4944 clear_tv(rettv);
4945 *rettv = var1;
4946 }
4947 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 }
4949 }
4950
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951 return OK;
4952}
4953
4954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 * Get an option value.
4956 * "arg" points to the '&' or '+' before the option name.
4957 * "arg" is advanced to character after the option name.
4958 * Return OK or FAIL.
4959 */
4960 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004963 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 int evaluate;
4965{
4966 char_u *option_end;
4967 long numval;
4968 char_u *stringval;
4969 int opt_type;
4970 int c;
4971 int working = (**arg == '+'); /* has("+option") */
4972 int ret = OK;
4973 int opt_flags;
4974
4975 /*
4976 * Isolate the option name and find its value.
4977 */
4978 option_end = find_option_end(arg, &opt_flags);
4979 if (option_end == NULL)
4980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004981 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 EMSG2(_("E112: Option name missing: %s"), *arg);
4983 return FAIL;
4984 }
4985
4986 if (!evaluate)
4987 {
4988 *arg = option_end;
4989 return OK;
4990 }
4991
4992 c = *option_end;
4993 *option_end = NUL;
4994 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997 if (opt_type == -3) /* invalid name */
4998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004999 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 EMSG2(_("E113: Unknown option: %s"), *arg);
5001 ret = FAIL;
5002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005003 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 if (opt_type == -2) /* hidden string option */
5006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_STRING;
5008 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010 else if (opt_type == -1) /* hidden number option */
5011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005012 rettv->v_type = VAR_NUMBER;
5013 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 else if (opt_type == 1) /* number option */
5016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017 rettv->v_type = VAR_NUMBER;
5018 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 else /* string option */
5021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022 rettv->v_type = VAR_STRING;
5023 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 }
5026 else if (working && (opt_type == -2 || opt_type == -1))
5027 ret = FAIL;
5028
5029 *option_end = c; /* put back for error messages */
5030 *arg = option_end;
5031
5032 return ret;
5033}
5034
5035/*
5036 * Allocate a variable for a string constant.
5037 * Return OK or FAIL.
5038 */
5039 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 int evaluate;
5044{
5045 char_u *p;
5046 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 int extra = 0;
5048
5049 /*
5050 * Find the end of the string, skipping backslashed characters.
5051 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 if (*p == '\\' && p[1] != NUL)
5055 {
5056 ++p;
5057 /* A "\<x>" form occupies at least 4 characters, and produces up
5058 * to 6 characters: reserve space for 2 extra */
5059 if (*p == '<')
5060 extra += 2;
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063
5064 if (*p != '"')
5065 {
5066 EMSG2(_("E114: Missing quote: %s"), *arg);
5067 return FAIL;
5068 }
5069
5070 /* If only parsing, set *arg and return here */
5071 if (!evaluate)
5072 {
5073 *arg = p + 1;
5074 return OK;
5075 }
5076
5077 /*
5078 * Copy the string into allocated memory, handling backslashed
5079 * characters.
5080 */
5081 name = alloc((unsigned)(p - *arg + extra));
5082 if (name == NULL)
5083 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005084 rettv->v_type = VAR_STRING;
5085 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 if (*p == '\\')
5090 {
5091 switch (*++p)
5092 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 case 'b': *name++ = BS; ++p; break;
5094 case 'e': *name++ = ESC; ++p; break;
5095 case 'f': *name++ = FF; ++p; break;
5096 case 'n': *name++ = NL; ++p; break;
5097 case 'r': *name++ = CAR; ++p; break;
5098 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100 case 'X': /* hex: "\x1", "\x12" */
5101 case 'x':
5102 case 'u': /* Unicode: "\u0023" */
5103 case 'U':
5104 if (vim_isxdigit(p[1]))
5105 {
5106 int n, nr;
5107 int c = toupper(*p);
5108
5109 if (c == 'X')
5110 n = 2;
5111 else
5112 n = 4;
5113 nr = 0;
5114 while (--n >= 0 && vim_isxdigit(p[1]))
5115 {
5116 ++p;
5117 nr = (nr << 4) + hex2nr(*p);
5118 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#ifdef FEAT_MBYTE
5121 /* For "\u" store the number according to
5122 * 'encoding'. */
5123 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 else
5126#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 break;
5130
5131 /* octal: "\1", "\12", "\123" */
5132 case '0':
5133 case '1':
5134 case '2':
5135 case '3':
5136 case '4':
5137 case '5':
5138 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 case '7': *name = *p++ - '0';
5140 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 *name = (*name << 3) + *p++ - '0';
5143 if (*p >= '0' && *p <= '7')
5144 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148
5149 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (extra != 0)
5152 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 break;
5155 }
5156 /* FALLTHROUGH */
5157
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 break;
5160 }
5161 }
5162 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 *arg = p + 1;
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 return OK;
5170}
5171
5172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 * Return OK or FAIL.
5175 */
5176 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 int evaluate;
5181{
5182 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005183 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005184 int reduce = 0;
5185
5186 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005188 */
5189 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5190 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005192 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005194 break;
5195 ++reduce;
5196 ++p;
5197 }
5198 }
5199
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005201 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005203 return FAIL;
5204 }
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005207 if (!evaluate)
5208 {
5209 *arg = p + 1;
5210 return OK;
5211 }
5212
5213 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005215 */
5216 str = alloc((unsigned)((p - *arg) - reduce));
5217 if (str == NULL)
5218 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 rettv->v_type = VAR_STRING;
5220 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005221
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005225 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005227 break;
5228 ++p;
5229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005233 *arg = p + 1;
5234
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005235 return OK;
5236}
5237
5238/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 * Allocate a variable for a List and fill it from "*arg".
5240 * Return OK or FAIL.
5241 */
5242 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005243get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005245 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 int evaluate;
5247{
Bram Moolenaar33570922005-01-25 22:26:29 +00005248 list_T *l = NULL;
5249 typval_T tv;
5250 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251
5252 if (evaluate)
5253 {
5254 l = list_alloc();
5255 if (l == NULL)
5256 return FAIL;
5257 }
5258
5259 *arg = skipwhite(*arg + 1);
5260 while (**arg != ']' && **arg != NUL)
5261 {
5262 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5263 goto failret;
5264 if (evaluate)
5265 {
5266 item = listitem_alloc();
5267 if (item != NULL)
5268 {
5269 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005270 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 list_append(l, item);
5272 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005273 else
5274 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 }
5276
5277 if (**arg == ']')
5278 break;
5279 if (**arg != ',')
5280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 goto failret;
5283 }
5284 *arg = skipwhite(*arg + 1);
5285 }
5286
5287 if (**arg != ']')
5288 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290failret:
5291 if (evaluate)
5292 list_free(l);
5293 return FAIL;
5294 }
5295
5296 *arg = skipwhite(*arg + 1);
5297 if (evaluate)
5298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 rettv->v_type = VAR_LIST;
5300 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 ++l->lv_refcount;
5302 }
5303
5304 return OK;
5305}
5306
5307/*
5308 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005309 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005311 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312list_alloc()
5313{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005314 list_T *l;
5315
5316 l = (list_T *)alloc_clear(sizeof(list_T));
5317 if (l != NULL)
5318 {
5319 /* Prepend the list to the list of lists for garbage collection. */
5320 if (first_list != NULL)
5321 first_list->lv_used_prev = l;
5322 l->lv_used_prev = NULL;
5323 l->lv_used_next = first_list;
5324 first_list = l;
5325 }
5326 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327}
5328
5329/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005330 * Allocate an empty list for a return value.
5331 * Returns OK or FAIL.
5332 */
5333 static int
5334rettv_list_alloc(rettv)
5335 typval_T *rettv;
5336{
5337 list_T *l = list_alloc();
5338
5339 if (l == NULL)
5340 return FAIL;
5341
5342 rettv->vval.v_list = l;
5343 rettv->v_type = VAR_LIST;
5344 ++l->lv_refcount;
5345 return OK;
5346}
5347
5348/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 * Unreference a list: decrement the reference count and free it when it
5350 * becomes zero.
5351 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005352 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005356 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5357 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358}
5359
5360/*
5361 * Free a list, including all items it points to.
5362 * Ignores the reference count.
5363 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005364 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005366 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367{
Bram Moolenaar33570922005-01-25 22:26:29 +00005368 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369
Bram Moolenaard9fba312005-06-26 22:34:35 +00005370 /* Avoid that recursive reference to the list frees us again. */
5371 l->lv_refcount = DEL_REFCOUNT;
5372
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005373 /* Remove the list from the list of lists for garbage collection. */
5374 if (l->lv_used_prev == NULL)
5375 first_list = l->lv_used_next;
5376 else
5377 l->lv_used_prev->lv_used_next = l->lv_used_next;
5378 if (l->lv_used_next != NULL)
5379 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5380
Bram Moolenaard9fba312005-06-26 22:34:35 +00005381 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005383 /* Remove the item before deleting it. */
5384 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 listitem_free(item);
5386 }
5387 vim_free(l);
5388}
5389
5390/*
5391 * Allocate a list item.
5392 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394listitem_alloc()
5395{
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397}
5398
5399/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005400 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 */
5402 static void
5403listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005404 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 vim_free(item);
5408}
5409
5410/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005411 * Remove a list item from a List and free it. Also clears the value.
5412 */
5413 static void
5414listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 list_T *l;
5416 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005417{
5418 list_remove(l, item, item);
5419 listitem_free(item);
5420}
5421
5422/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 * Get the number of items in a list.
5424 */
5425 static long
5426list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 if (l == NULL)
5430 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005431 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432}
5433
5434/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005435 * Return TRUE when two lists have exactly the same values.
5436 */
5437 static int
5438list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005439 list_T *l1;
5440 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005441 int ic; /* ignore case for strings */
5442{
Bram Moolenaar33570922005-01-25 22:26:29 +00005443 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005444
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005445 if (list_len(l1) != list_len(l2))
5446 return FALSE;
5447
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005448 for (item1 = l1->lv_first, item2 = l2->lv_first;
5449 item1 != NULL && item2 != NULL;
5450 item1 = item1->li_next, item2 = item2->li_next)
5451 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5452 return FALSE;
5453 return item1 == NULL && item2 == NULL;
5454}
5455
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005456#if defined(FEAT_PYTHON) || defined(PROTO)
5457/*
5458 * Return the dictitem that an entry in a hashtable points to.
5459 */
5460 dictitem_T *
5461dict_lookup(hi)
5462 hashitem_T *hi;
5463{
5464 return HI2DI(hi);
5465}
5466#endif
5467
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005468/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005469 * Return TRUE when two dictionaries have exactly the same key/values.
5470 */
5471 static int
5472dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005473 dict_T *d1;
5474 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005475 int ic; /* ignore case for strings */
5476{
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 hashitem_T *hi;
5478 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005479 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005480
5481 if (dict_len(d1) != dict_len(d2))
5482 return FALSE;
5483
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005484 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005485 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005486 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005487 if (!HASHITEM_EMPTY(hi))
5488 {
5489 item2 = dict_find(d2, hi->hi_key, -1);
5490 if (item2 == NULL)
5491 return FALSE;
5492 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5493 return FALSE;
5494 --todo;
5495 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005496 }
5497 return TRUE;
5498}
5499
5500/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005501 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005502 * Compares the items just like "==" would compare them, but strings and
5503 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504 */
5505 static int
5506tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005507 typval_T *tv1;
5508 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005509 int ic; /* ignore case */
5510{
5511 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005512 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005514 if (tv1->v_type != tv2->v_type)
5515 return FALSE;
5516
5517 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005518 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005519 case VAR_LIST:
5520 /* recursive! */
5521 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5522
5523 case VAR_DICT:
5524 /* recursive! */
5525 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5526
5527 case VAR_FUNC:
5528 return (tv1->vval.v_string != NULL
5529 && tv2->vval.v_string != NULL
5530 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5531
5532 case VAR_NUMBER:
5533 return tv1->vval.v_number == tv2->vval.v_number;
5534
5535 case VAR_STRING:
5536 s1 = get_tv_string_buf(tv1, buf1);
5537 s2 = get_tv_string_buf(tv2, buf2);
5538 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005539 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005540
5541 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005542 return TRUE;
5543}
5544
5545/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 * Locate item with index "n" in list "l" and return it.
5547 * A negative index is counted from the end; -1 is the last item.
5548 * Returns NULL when "n" is out of range.
5549 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 long n;
5554{
Bram Moolenaar33570922005-01-25 22:26:29 +00005555 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 long idx;
5557
5558 if (l == NULL)
5559 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005560
5561 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005563 n = l->lv_len + n;
5564
5565 /* Check for index out of range. */
5566 if (n < 0 || n >= l->lv_len)
5567 return NULL;
5568
5569 /* When there is a cached index may start search from there. */
5570 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005572 if (n < l->lv_idx / 2)
5573 {
5574 /* closest to the start of the list */
5575 item = l->lv_first;
5576 idx = 0;
5577 }
5578 else if (n > (l->lv_idx + l->lv_len) / 2)
5579 {
5580 /* closest to the end of the list */
5581 item = l->lv_last;
5582 idx = l->lv_len - 1;
5583 }
5584 else
5585 {
5586 /* closest to the cached index */
5587 item = l->lv_idx_item;
5588 idx = l->lv_idx;
5589 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 }
5591 else
5592 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005593 if (n < l->lv_len / 2)
5594 {
5595 /* closest to the start of the list */
5596 item = l->lv_first;
5597 idx = 0;
5598 }
5599 else
5600 {
5601 /* closest to the end of the list */
5602 item = l->lv_last;
5603 idx = l->lv_len - 1;
5604 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005606
5607 while (n > idx)
5608 {
5609 /* search forward */
5610 item = item->li_next;
5611 ++idx;
5612 }
5613 while (n < idx)
5614 {
5615 /* search backward */
5616 item = item->li_prev;
5617 --idx;
5618 }
5619
5620 /* cache the used index */
5621 l->lv_idx = idx;
5622 l->lv_idx_item = item;
5623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624 return item;
5625}
5626
5627/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005628 * Get list item "l[idx]" as a number.
5629 */
5630 static long
5631list_find_nr(l, idx, errorp)
5632 list_T *l;
5633 long idx;
5634 int *errorp; /* set to TRUE when something wrong */
5635{
5636 listitem_T *li;
5637
5638 li = list_find(l, idx);
5639 if (li == NULL)
5640 {
5641 if (errorp != NULL)
5642 *errorp = TRUE;
5643 return -1L;
5644 }
5645 return get_tv_number_chk(&li->li_tv, errorp);
5646}
5647
5648/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005649 * Locate "item" list "l" and return its index.
5650 * Returns -1 when "item" is not in the list.
5651 */
5652 static long
5653list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005654 list_T *l;
5655 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005656{
5657 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005658 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005659
5660 if (l == NULL)
5661 return -1;
5662 idx = 0;
5663 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5664 ++idx;
5665 if (li == NULL)
5666 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005667 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005668}
5669
5670/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 * Append item "item" to the end of list "l".
5672 */
5673 static void
5674list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005675 list_T *l;
5676 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677{
5678 if (l->lv_last == NULL)
5679 {
5680 /* empty list */
5681 l->lv_first = item;
5682 l->lv_last = item;
5683 item->li_prev = NULL;
5684 }
5685 else
5686 {
5687 l->lv_last->li_next = item;
5688 item->li_prev = l->lv_last;
5689 l->lv_last = item;
5690 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005691 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 item->li_next = NULL;
5693}
5694
5695/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005697 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 */
5699 static int
5700list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 list_T *l;
5702 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005704 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705
Bram Moolenaar05159a02005-02-26 23:04:13 +00005706 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005707 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005708 copy_tv(tv, &li->li_tv);
5709 list_append(l, li);
5710 return OK;
5711}
5712
5713/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005714 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005715 * Return FAIL when out of memory.
5716 */
5717 int
5718list_append_dict(list, dict)
5719 list_T *list;
5720 dict_T *dict;
5721{
5722 listitem_T *li = listitem_alloc();
5723
5724 if (li == NULL)
5725 return FAIL;
5726 li->li_tv.v_type = VAR_DICT;
5727 li->li_tv.v_lock = 0;
5728 li->li_tv.vval.v_dict = dict;
5729 list_append(list, li);
5730 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 return OK;
5732}
5733
5734/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005735 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005736 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005737 * Returns FAIL when out of memory.
5738 */
5739 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005740list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005741 list_T *l;
5742 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005743 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005744{
5745 listitem_T *li = listitem_alloc();
5746
5747 if (li == NULL)
5748 return FAIL;
5749 list_append(l, li);
5750 li->li_tv.v_type = VAR_STRING;
5751 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005752 if (str == NULL)
5753 li->li_tv.vval.v_string = NULL;
5754 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005755 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005756 return FAIL;
5757 return OK;
5758}
5759
5760/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005761 * Append "n" to list "l".
5762 * Returns FAIL when out of memory.
5763 */
5764 static int
5765list_append_number(l, n)
5766 list_T *l;
5767 varnumber_T n;
5768{
5769 listitem_T *li;
5770
5771 li = listitem_alloc();
5772 if (li == NULL)
5773 return FAIL;
5774 li->li_tv.v_type = VAR_NUMBER;
5775 li->li_tv.v_lock = 0;
5776 li->li_tv.vval.v_number = n;
5777 list_append(l, li);
5778 return OK;
5779}
5780
5781/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005782 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005783 * If "item" is NULL append at the end.
5784 * Return FAIL when out of memory.
5785 */
5786 static int
5787list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 list_T *l;
5789 typval_T *tv;
5790 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005791{
Bram Moolenaar33570922005-01-25 22:26:29 +00005792 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005793
5794 if (ni == NULL)
5795 return FAIL;
5796 copy_tv(tv, &ni->li_tv);
5797 if (item == NULL)
5798 /* Append new item at end of list. */
5799 list_append(l, ni);
5800 else
5801 {
5802 /* Insert new item before existing item. */
5803 ni->li_prev = item->li_prev;
5804 ni->li_next = item;
5805 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005806 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005807 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005808 ++l->lv_idx;
5809 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005810 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005811 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005812 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005813 l->lv_idx_item = NULL;
5814 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005815 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005816 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005817 }
5818 return OK;
5819}
5820
5821/*
5822 * Extend "l1" with "l2".
5823 * If "bef" is NULL append at the end, otherwise insert before this item.
5824 * Returns FAIL when out of memory.
5825 */
5826 static int
5827list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 list_T *l1;
5829 list_T *l2;
5830 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005831{
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005833
5834 for (item = l2->lv_first; item != NULL; item = item->li_next)
5835 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5836 return FAIL;
5837 return OK;
5838}
5839
5840/*
5841 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5842 * Return FAIL when out of memory.
5843 */
5844 static int
5845list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005846 list_T *l1;
5847 list_T *l2;
5848 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005849{
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005851
5852 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005853 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005854 if (l == NULL)
5855 return FAIL;
5856 tv->v_type = VAR_LIST;
5857 tv->vval.v_list = l;
5858
5859 /* append all items from the second list */
5860 return list_extend(l, l2, NULL);
5861}
5862
5863/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005864 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005866 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 * Returns NULL when out of memory.
5868 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005869 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005870list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005873 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874{
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 list_T *copy;
5876 listitem_T *item;
5877 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878
5879 if (orig == NULL)
5880 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881
5882 copy = list_alloc();
5883 if (copy != NULL)
5884 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005885 if (copyID != 0)
5886 {
5887 /* Do this before adding the items, because one of the items may
5888 * refer back to this list. */
5889 orig->lv_copyID = copyID;
5890 orig->lv_copylist = copy;
5891 }
5892 for (item = orig->lv_first; item != NULL && !got_int;
5893 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 {
5895 ni = listitem_alloc();
5896 if (ni == NULL)
5897 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005899 {
5900 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5901 {
5902 vim_free(ni);
5903 break;
5904 }
5905 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 list_append(copy, ni);
5909 }
5910 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005911 if (item != NULL)
5912 {
5913 list_unref(copy);
5914 copy = NULL;
5915 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 }
5917
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 return copy;
5919}
5920
5921/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005923 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005925 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005926list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 list_T *l;
5928 listitem_T *item;
5929 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930{
Bram Moolenaar33570922005-01-25 22:26:29 +00005931 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005933 /* notify watchers */
5934 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005936 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005937 list_fix_watch(l, ip);
5938 if (ip == item2)
5939 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005941
5942 if (item2->li_next == NULL)
5943 l->lv_last = item->li_prev;
5944 else
5945 item2->li_next->li_prev = item->li_prev;
5946 if (item->li_prev == NULL)
5947 l->lv_first = item2->li_next;
5948 else
5949 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005950 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951}
5952
5953/*
5954 * Return an allocated string with the string representation of a list.
5955 * May return NULL.
5956 */
5957 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005958list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005960 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961{
5962 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963
5964 if (tv->vval.v_list == NULL)
5965 return NULL;
5966 ga_init2(&ga, (int)sizeof(char), 80);
5967 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005968 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005969 {
5970 vim_free(ga.ga_data);
5971 return NULL;
5972 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 ga_append(&ga, ']');
5974 ga_append(&ga, NUL);
5975 return (char_u *)ga.ga_data;
5976}
5977
5978/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005979 * Join list "l" into a string in "*gap", using separator "sep".
5980 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005981 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005983 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005984list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987 char_u *sep;
5988 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005989 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990{
5991 int first = TRUE;
5992 char_u *tofree;
5993 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 char_u *s;
5996
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005997 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005998 {
5999 if (first)
6000 first = FALSE;
6001 else
6002 ga_concat(gap, sep);
6003
6004 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006005 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006006 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006007 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006008 if (s != NULL)
6009 ga_concat(gap, s);
6010 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006011 if (s == NULL)
6012 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006013 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006014 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006015}
6016
6017/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006018 * Garbage collection for lists and dictionaries.
6019 *
6020 * We use reference counts to be able to free most items right away when they
6021 * are no longer used. But for composite items it's possible that it becomes
6022 * unused while the reference count is > 0: When there is a recursive
6023 * reference. Example:
6024 * :let l = [1, 2, 3]
6025 * :let d = {9: l}
6026 * :let l[1] = d
6027 *
6028 * Since this is quite unusual we handle this with garbage collection: every
6029 * once in a while find out which lists and dicts are not referenced from any
6030 * variable.
6031 *
6032 * Here is a good reference text about garbage collection (refers to Python
6033 * but it applies to all reference-counting mechanisms):
6034 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006035 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036
6037/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006038 * Do garbage collection for lists and dicts.
6039 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 int
6042garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043{
6044 dict_T *dd;
6045 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 int copyID = ++current_copyID;
6047 buf_T *buf;
6048 win_T *wp;
6049 int i;
6050 funccall_T *fc;
6051 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006052#ifdef FEAT_WINDOWS
6053 tabpage_T *tp;
6054#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055
6056 /*
6057 * 1. Go through all accessible variables and mark all lists and dicts
6058 * with copyID.
6059 */
6060 /* script-local variables */
6061 for (i = 1; i <= ga_scripts.ga_len; ++i)
6062 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6063
6064 /* buffer-local variables */
6065 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6066 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6067
6068 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006069 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006070 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6071
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006072#ifdef FEAT_WINDOWS
6073 /* tabpage-local variables */
6074 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6075 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6076#endif
6077
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006078 /* global variables */
6079 set_ref_in_ht(&globvarht, copyID);
6080
6081 /* function-local variables */
6082 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6083 {
6084 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6085 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6086 }
6087
6088 /*
6089 * 2. Go through the list of dicts and free items without the copyID.
6090 */
6091 for (dd = first_dict; dd != NULL; )
6092 if (dd->dv_copyID != copyID)
6093 {
6094 dict_free(dd);
6095 did_free = TRUE;
6096
6097 /* restart, next dict may also have been freed */
6098 dd = first_dict;
6099 }
6100 else
6101 dd = dd->dv_used_next;
6102
6103 /*
6104 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006105 * But don't free a list that has a watcher (used in a for loop), these
6106 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006107 */
6108 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006109 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 {
6111 list_free(ll);
6112 did_free = TRUE;
6113
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006114 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 ll = first_list;
6116 }
6117 else
6118 ll = ll->lv_used_next;
6119
6120 return did_free;
6121}
6122
6123/*
6124 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6125 */
6126 static void
6127set_ref_in_ht(ht, copyID)
6128 hashtab_T *ht;
6129 int copyID;
6130{
6131 int todo;
6132 hashitem_T *hi;
6133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006134 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006135 for (hi = ht->ht_array; todo > 0; ++hi)
6136 if (!HASHITEM_EMPTY(hi))
6137 {
6138 --todo;
6139 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6140 }
6141}
6142
6143/*
6144 * Mark all lists and dicts referenced through list "l" with "copyID".
6145 */
6146 static void
6147set_ref_in_list(l, copyID)
6148 list_T *l;
6149 int copyID;
6150{
6151 listitem_T *li;
6152
6153 for (li = l->lv_first; li != NULL; li = li->li_next)
6154 set_ref_in_item(&li->li_tv, copyID);
6155}
6156
6157/*
6158 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6159 */
6160 static void
6161set_ref_in_item(tv, copyID)
6162 typval_T *tv;
6163 int copyID;
6164{
6165 dict_T *dd;
6166 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006167
6168 switch (tv->v_type)
6169 {
6170 case VAR_DICT:
6171 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006172 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006173 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006174 /* Didn't see this dict yet. */
6175 dd->dv_copyID = copyID;
6176 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006177 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006178 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006179
6180 case VAR_LIST:
6181 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006182 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006183 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006184 /* Didn't see this list yet. */
6185 ll->lv_copyID = copyID;
6186 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006187 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006188 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006189 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006190 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006191}
6192
6193/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006194 * Allocate an empty header for a dictionary.
6195 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006196 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006197dict_alloc()
6198{
Bram Moolenaar33570922005-01-25 22:26:29 +00006199 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006200
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006202 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006203 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006204 /* Add the list to the hashtable for garbage collection. */
6205 if (first_dict != NULL)
6206 first_dict->dv_used_prev = d;
6207 d->dv_used_next = first_dict;
6208 d->dv_used_prev = NULL;
6209
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006211 d->dv_lock = 0;
6212 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006213 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006214 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006216}
6217
6218/*
6219 * Unreference a Dictionary: decrement the reference count and free it when it
6220 * becomes zero.
6221 */
6222 static void
6223dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006224 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006225{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006226 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6227 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006228}
6229
6230/*
6231 * Free a Dictionary, including all items it contains.
6232 * Ignores the reference count.
6233 */
6234 static void
6235dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006236 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006237{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006238 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006240 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006241
Bram Moolenaard9fba312005-06-26 22:34:35 +00006242 /* Avoid that recursive reference to the dict frees us again. */
6243 d->dv_refcount = DEL_REFCOUNT;
6244
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006245 /* Remove the dict from the list of dicts for garbage collection. */
6246 if (d->dv_used_prev == NULL)
6247 first_dict = d->dv_used_next;
6248 else
6249 d->dv_used_prev->dv_used_next = d->dv_used_next;
6250 if (d->dv_used_next != NULL)
6251 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6252
6253 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006254 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006255 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006256 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258 if (!HASHITEM_EMPTY(hi))
6259 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006260 /* Remove the item before deleting it, just in case there is
6261 * something recursive causing trouble. */
6262 di = HI2DI(hi);
6263 hash_remove(&d->dv_hashtab, hi);
6264 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006265 --todo;
6266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006267 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006269 vim_free(d);
6270}
6271
6272/*
6273 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 * The "key" is copied to the new item.
6275 * Note that the value of the item "di_tv" still needs to be initialized!
6276 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006277 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006279dictitem_alloc(key)
6280 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006281{
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006283
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006284 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006288 di->di_flags = 0;
6289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006290 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006291}
6292
6293/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294 * Make a copy of a Dictionary item.
6295 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006297dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006298 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006299{
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006301
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006302 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6303 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006304 if (di != NULL)
6305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006306 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006307 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308 copy_tv(&org->di_tv, &di->di_tv);
6309 }
6310 return di;
6311}
6312
6313/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006314 * Remove item "item" from Dictionary "dict" and free it.
6315 */
6316 static void
6317dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 dict_T *dict;
6319 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320{
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006324 if (HASHITEM_EMPTY(hi))
6325 EMSG2(_(e_intern2), "dictitem_remove()");
6326 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006328 dictitem_free(item);
6329}
6330
6331/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006332 * Free a dict item. Also clears the value.
6333 */
6334 static void
6335dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006336 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006338 clear_tv(&item->di_tv);
6339 vim_free(item);
6340}
6341
6342/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006343 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6344 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006345 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006346 * Returns NULL when out of memory.
6347 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006348 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006349dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006351 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006352 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006353{
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 dict_T *copy;
6355 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006356 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006357 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006358
6359 if (orig == NULL)
6360 return NULL;
6361
6362 copy = dict_alloc();
6363 if (copy != NULL)
6364 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006365 if (copyID != 0)
6366 {
6367 orig->dv_copyID = copyID;
6368 orig->dv_copydict = copy;
6369 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006370 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006371 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006372 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006373 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006375 --todo;
6376
6377 di = dictitem_alloc(hi->hi_key);
6378 if (di == NULL)
6379 break;
6380 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006381 {
6382 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6383 copyID) == FAIL)
6384 {
6385 vim_free(di);
6386 break;
6387 }
6388 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006389 else
6390 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6391 if (dict_add(copy, di) == FAIL)
6392 {
6393 dictitem_free(di);
6394 break;
6395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006397 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006398
Bram Moolenaare9a41262005-01-15 22:18:47 +00006399 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 if (todo > 0)
6401 {
6402 dict_unref(copy);
6403 copy = NULL;
6404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006405 }
6406
6407 return copy;
6408}
6409
6410/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006411 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006413 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006414 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006415dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 dict_T *d;
6417 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006418{
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420}
6421
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 * Add a number or string entry to dictionary "d".
6424 * When "str" is NULL use number "nr", otherwise use "str".
6425 * Returns FAIL when out of memory and when key already exists.
6426 */
6427 int
6428dict_add_nr_str(d, key, nr, str)
6429 dict_T *d;
6430 char *key;
6431 long nr;
6432 char_u *str;
6433{
6434 dictitem_T *item;
6435
6436 item = dictitem_alloc((char_u *)key);
6437 if (item == NULL)
6438 return FAIL;
6439 item->di_tv.v_lock = 0;
6440 if (str == NULL)
6441 {
6442 item->di_tv.v_type = VAR_NUMBER;
6443 item->di_tv.vval.v_number = nr;
6444 }
6445 else
6446 {
6447 item->di_tv.v_type = VAR_STRING;
6448 item->di_tv.vval.v_string = vim_strsave(str);
6449 }
6450 if (dict_add(d, item) == FAIL)
6451 {
6452 dictitem_free(item);
6453 return FAIL;
6454 }
6455 return OK;
6456}
6457
6458/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006459 * Get the number of items in a Dictionary.
6460 */
6461 static long
6462dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006464{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 if (d == NULL)
6466 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006467 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006468}
6469
6470/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471 * Find item "key[len]" in Dictionary "d".
6472 * If "len" is negative use strlen(key).
6473 * Returns NULL when not found.
6474 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006476dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006478 char_u *key;
6479 int len;
6480{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006481#define AKEYLEN 200
6482 char_u buf[AKEYLEN];
6483 char_u *akey;
6484 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006486
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006487 if (len < 0)
6488 akey = key;
6489 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006490 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006491 tofree = akey = vim_strnsave(key, len);
6492 if (akey == NULL)
6493 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006494 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006495 else
6496 {
6497 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006498 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006499 akey = buf;
6500 }
6501
Bram Moolenaar33570922005-01-25 22:26:29 +00006502 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006503 vim_free(tofree);
6504 if (HASHITEM_EMPTY(hi))
6505 return NULL;
6506 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507}
6508
6509/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006510 * Get a string item from a dictionary.
6511 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006512 * Returns NULL if the entry doesn't exist or out of memory.
6513 */
6514 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006515get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006516 dict_T *d;
6517 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006518 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006519{
6520 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006521 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006522
6523 di = dict_find(d, key, -1);
6524 if (di == NULL)
6525 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006526 s = get_tv_string(&di->di_tv);
6527 if (save && s != NULL)
6528 s = vim_strsave(s);
6529 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006530}
6531
6532/*
6533 * Get a number item from a dictionary.
6534 * Returns 0 if the entry doesn't exist or out of memory.
6535 */
6536 long
6537get_dict_number(d, key)
6538 dict_T *d;
6539 char_u *key;
6540{
6541 dictitem_T *di;
6542
6543 di = dict_find(d, key, -1);
6544 if (di == NULL)
6545 return 0;
6546 return get_tv_number(&di->di_tv);
6547}
6548
6549/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006550 * Return an allocated string with the string representation of a Dictionary.
6551 * May return NULL.
6552 */
6553 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006554dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006556 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557{
6558 garray_T ga;
6559 int first = TRUE;
6560 char_u *tofree;
6561 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006565 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006567 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006568 return NULL;
6569 ga_init2(&ga, (int)sizeof(char), 80);
6570 ga_append(&ga, '{');
6571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006572 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006574 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006575 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006577 --todo;
6578
6579 if (first)
6580 first = FALSE;
6581 else
6582 ga_concat(&ga, (char_u *)", ");
6583
6584 tofree = string_quote(hi->hi_key, FALSE);
6585 if (tofree != NULL)
6586 {
6587 ga_concat(&ga, tofree);
6588 vim_free(tofree);
6589 }
6590 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006591 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006592 if (s != NULL)
6593 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 if (s == NULL)
6596 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006599 if (todo > 0)
6600 {
6601 vim_free(ga.ga_data);
6602 return NULL;
6603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006604
6605 ga_append(&ga, '}');
6606 ga_append(&ga, NUL);
6607 return (char_u *)ga.ga_data;
6608}
6609
6610/*
6611 * Allocate a variable for a Dictionary and fill it from "*arg".
6612 * Return OK or FAIL. Returns NOTDONE for {expr}.
6613 */
6614 static int
6615get_dict_tv(arg, rettv, evaluate)
6616 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 int evaluate;
6619{
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 dict_T *d = NULL;
6621 typval_T tvkey;
6622 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006623 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006625 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006626 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006627
6628 /*
6629 * First check if it's not a curly-braces thing: {expr}.
6630 * Must do this without evaluating, otherwise a function may be called
6631 * twice. Unfortunately this means we need to call eval1() twice for the
6632 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006633 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006634 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006635 if (*start != '}')
6636 {
6637 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6638 return FAIL;
6639 if (*start == '}')
6640 return NOTDONE;
6641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006642
6643 if (evaluate)
6644 {
6645 d = dict_alloc();
6646 if (d == NULL)
6647 return FAIL;
6648 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006649 tvkey.v_type = VAR_UNKNOWN;
6650 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006651
6652 *arg = skipwhite(*arg + 1);
6653 while (**arg != '}' && **arg != NUL)
6654 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006655 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006656 goto failret;
6657 if (**arg != ':')
6658 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006659 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006660 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661 goto failret;
6662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006663 key = get_tv_string_buf_chk(&tvkey, buf);
6664 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006666 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6667 if (key != NULL)
6668 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006669 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006670 goto failret;
6671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006672
6673 *arg = skipwhite(*arg + 1);
6674 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6675 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006676 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006677 goto failret;
6678 }
6679 if (evaluate)
6680 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006681 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006682 if (item != NULL)
6683 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006684 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006685 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 clear_tv(&tv);
6687 goto failret;
6688 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006689 item = dictitem_alloc(key);
6690 clear_tv(&tvkey);
6691 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006693 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006694 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006695 if (dict_add(d, item) == FAIL)
6696 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006697 }
6698 }
6699
6700 if (**arg == '}')
6701 break;
6702 if (**arg != ',')
6703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006704 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006705 goto failret;
6706 }
6707 *arg = skipwhite(*arg + 1);
6708 }
6709
6710 if (**arg != '}')
6711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006712 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713failret:
6714 if (evaluate)
6715 dict_free(d);
6716 return FAIL;
6717 }
6718
6719 *arg = skipwhite(*arg + 1);
6720 if (evaluate)
6721 {
6722 rettv->v_type = VAR_DICT;
6723 rettv->vval.v_dict = d;
6724 ++d->dv_refcount;
6725 }
6726
6727 return OK;
6728}
6729
Bram Moolenaar8c711452005-01-14 21:53:12 +00006730/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006731 * Return a string with the string representation of a variable.
6732 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006735 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006736 * May return NULL;
6737 */
6738 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006739echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006740 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006741 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006743 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 static int recurse = 0;
6746 char_u *r = NULL;
6747
Bram Moolenaar33570922005-01-25 22:26:29 +00006748 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006749 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006750 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006751 *tofree = NULL;
6752 return NULL;
6753 }
6754 ++recurse;
6755
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006756 switch (tv->v_type)
6757 {
6758 case VAR_FUNC:
6759 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006760 r = tv->vval.v_string;
6761 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006762
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006764 if (tv->vval.v_list == NULL)
6765 {
6766 *tofree = NULL;
6767 r = NULL;
6768 }
6769 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6770 {
6771 *tofree = NULL;
6772 r = (char_u *)"[...]";
6773 }
6774 else
6775 {
6776 tv->vval.v_list->lv_copyID = copyID;
6777 *tofree = list2string(tv, copyID);
6778 r = *tofree;
6779 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006780 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006781
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006783 if (tv->vval.v_dict == NULL)
6784 {
6785 *tofree = NULL;
6786 r = NULL;
6787 }
6788 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6789 {
6790 *tofree = NULL;
6791 r = (char_u *)"{...}";
6792 }
6793 else
6794 {
6795 tv->vval.v_dict->dv_copyID = copyID;
6796 *tofree = dict2string(tv, copyID);
6797 r = *tofree;
6798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006799 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006800
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006801 case VAR_STRING:
6802 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006803 *tofree = NULL;
6804 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006806
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006807 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006811
6812 --recurse;
6813 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814}
6815
6816/*
6817 * Return a string with the string representation of a variable.
6818 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6819 * "numbuf" is used for a number.
6820 * Puts quotes around strings, so that they can be parsed back by eval().
6821 * May return NULL;
6822 */
6823 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006824tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006825 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 char_u **tofree;
6827 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006828 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006829{
6830 switch (tv->v_type)
6831 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006832 case VAR_FUNC:
6833 *tofree = string_quote(tv->vval.v_string, TRUE);
6834 return *tofree;
6835 case VAR_STRING:
6836 *tofree = string_quote(tv->vval.v_string, FALSE);
6837 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006839 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006840 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006842 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006843 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006845 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846}
6847
6848/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006849 * Return string "str" in ' quotes, doubling ' characters.
6850 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006851 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006852 */
6853 static char_u *
6854string_quote(str, function)
6855 char_u *str;
6856 int function;
6857{
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006859 char_u *p, *r, *s;
6860
Bram Moolenaar33570922005-01-25 22:26:29 +00006861 len = (function ? 13 : 3);
6862 if (str != NULL)
6863 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006864 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006865 for (p = str; *p != NUL; mb_ptr_adv(p))
6866 if (*p == '\'')
6867 ++len;
6868 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869 s = r = alloc(len);
6870 if (r != NULL)
6871 {
6872 if (function)
6873 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006874 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875 r += 10;
6876 }
6877 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006878 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006879 if (str != NULL)
6880 for (p = str; *p != NUL; )
6881 {
6882 if (*p == '\'')
6883 *r++ = '\'';
6884 MB_COPY_CHAR(p, r);
6885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 if (function)
6888 *r++ = ')';
6889 *r++ = NUL;
6890 }
6891 return s;
6892}
6893
6894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 * Get the value of an environment variable.
6896 * "arg" is pointing to the '$'. It is advanced to after the name.
6897 * If the environment variable was not set, silently assume it is empty.
6898 * Always return OK.
6899 */
6900 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006901get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 int evaluate;
6905{
6906 char_u *string = NULL;
6907 int len;
6908 int cc;
6909 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006910 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
6912 ++*arg;
6913 name = *arg;
6914 len = get_env_len(arg);
6915 if (evaluate)
6916 {
6917 if (len != 0)
6918 {
6919 cc = name[len];
6920 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006921 /* first try vim_getenv(), fast for normal environment vars */
6922 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006924 {
6925 if (!mustfree)
6926 string = vim_strsave(string);
6927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 else
6929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006930 if (mustfree)
6931 vim_free(string);
6932
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 /* next try expanding things like $VIM and ${HOME} */
6934 string = expand_env_save(name - 1);
6935 if (string != NULL && *string == '$')
6936 {
6937 vim_free(string);
6938 string = NULL;
6939 }
6940 }
6941 name[len] = cc;
6942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006943 rettv->v_type = VAR_STRING;
6944 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946
6947 return OK;
6948}
6949
6950/*
6951 * Array with names and number of arguments of all internal functions
6952 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6953 */
6954static struct fst
6955{
6956 char *f_name; /* function name */
6957 char f_min_argc; /* minimal number of arguments */
6958 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006959 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961} functions[] =
6962{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006963 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {"append", 2, 2, f_append},
6965 {"argc", 0, 0, f_argc},
6966 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006967 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006969 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {"bufexists", 1, 1, f_bufexists},
6971 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6972 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6973 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6974 {"buflisted", 1, 1, f_buflisted},
6975 {"bufloaded", 1, 1, f_bufloaded},
6976 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006977 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {"bufwinnr", 1, 1, f_bufwinnr},
6979 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006980 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006982 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {"char2nr", 1, 1, f_char2nr},
6984 {"cindent", 1, 1, f_cindent},
6985 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006986#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006987 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006988 {"complete_add", 1, 1, f_complete_add},
6989 {"complete_check", 0, 0, f_complete_check},
6990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006992 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006993 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006995 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006996 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {"delete", 1, 1, f_delete},
6998 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006999 {"diff_filler", 1, 1, f_diff_filler},
7000 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007001 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007003 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {"eventhandler", 0, 0, f_eventhandler},
7005 {"executable", 1, 1, f_executable},
7006 {"exists", 1, 1, f_exists},
7007 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007008 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007009 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7011 {"filereadable", 1, 1, f_filereadable},
7012 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007013 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007014 {"finddir", 1, 3, f_finddir},
7015 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {"fnamemodify", 2, 2, f_fnamemodify},
7017 {"foldclosed", 1, 1, f_foldclosed},
7018 {"foldclosedend", 1, 1, f_foldclosedend},
7019 {"foldlevel", 1, 1, f_foldlevel},
7020 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007021 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007023 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007025 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007026 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {"getbufvar", 2, 2, f_getbufvar},
7028 {"getchar", 0, 1, f_getchar},
7029 {"getcharmod", 0, 0, f_getcharmod},
7030 {"getcmdline", 0, 0, f_getcmdline},
7031 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007032 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007034 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007035 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {"getfsize", 1, 1, f_getfsize},
7037 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007038 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007039 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007040 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007041 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007042 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007043 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007045 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {"getwinposx", 0, 0, f_getwinposx},
7047 {"getwinposy", 0, 0, f_getwinposy},
7048 {"getwinvar", 2, 2, f_getwinvar},
7049 {"glob", 1, 1, f_glob},
7050 {"globpath", 2, 2, f_globpath},
7051 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007052 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007053 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7055 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7056 {"histadd", 2, 2, f_histadd},
7057 {"histdel", 1, 2, f_histdel},
7058 {"histget", 1, 2, f_histget},
7059 {"histnr", 1, 1, f_histnr},
7060 {"hlID", 1, 1, f_hlID},
7061 {"hlexists", 1, 1, f_hlexists},
7062 {"hostname", 0, 0, f_hostname},
7063 {"iconv", 3, 3, f_iconv},
7064 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007065 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007066 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007068 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 {"inputrestore", 0, 0, f_inputrestore},
7070 {"inputsave", 0, 0, f_inputsave},
7071 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007072 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007074 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007076 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007077 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007079 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {"libcall", 3, 3, f_libcall},
7081 {"libcallnr", 3, 3, f_libcallnr},
7082 {"line", 1, 1, f_line},
7083 {"line2byte", 1, 1, f_line2byte},
7084 {"lispindent", 1, 1, f_lispindent},
7085 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007086 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007087 {"maparg", 1, 3, f_maparg},
7088 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007089 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007090 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007091 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007092 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007093 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007094 {"max", 1, 1, f_max},
7095 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007096#ifdef vim_mkdir
7097 {"mkdir", 1, 3, f_mkdir},
7098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"mode", 0, 0, f_mode},
7100 {"nextnonblank", 1, 1, f_nextnonblank},
7101 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007102 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007104 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007105 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007107 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007108 {"reltime", 0, 2, f_reltime},
7109 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {"remote_expr", 2, 3, f_remote_expr},
7111 {"remote_foreground", 1, 1, f_remote_foreground},
7112 {"remote_peek", 1, 2, f_remote_peek},
7113 {"remote_read", 1, 1, f_remote_read},
7114 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007115 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007117 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007119 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007120 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007121 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007122 {"searchpair", 3, 6, f_searchpair},
7123 {"searchpairpos", 3, 6, f_searchpairpos},
7124 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {"server2client", 2, 2, f_server2client},
7126 {"serverlist", 0, 0, f_serverlist},
7127 {"setbufvar", 3, 3, f_setbufvar},
7128 {"setcmdpos", 1, 1, f_setcmdpos},
7129 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007130 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007131 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007132 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007134 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 {"setwinvar", 3, 3, f_setwinvar},
7136 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007137 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007138 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007139 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007140 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007141 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007142 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143#ifdef HAVE_STRFTIME
7144 {"strftime", 1, 2, f_strftime},
7145#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007146 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007147 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {"strlen", 1, 1, f_strlen},
7149 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007150 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {"strtrans", 1, 1, f_strtrans},
7152 {"submatch", 1, 1, f_submatch},
7153 {"substitute", 4, 4, f_substitute},
7154 {"synID", 3, 3, f_synID},
7155 {"synIDattr", 2, 3, f_synIDattr},
7156 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007157 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007159 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007160 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007161 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007162 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007164 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {"tolower", 1, 1, f_tolower},
7166 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007167 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {"virtcol", 1, 1, f_virtcol},
7171 {"visualmode", 0, 1, f_visualmode},
7172 {"winbufnr", 1, 1, f_winbufnr},
7173 {"wincol", 0, 0, f_wincol},
7174 {"winheight", 1, 1, f_winheight},
7175 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007176 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007178 {"winrestview", 1, 1, f_winrestview},
7179 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007181 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182};
7183
7184#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7185
7186/*
7187 * Function given to ExpandGeneric() to obtain the list of internal
7188 * or user defined function names.
7189 */
7190 char_u *
7191get_function_name(xp, idx)
7192 expand_T *xp;
7193 int idx;
7194{
7195 static int intidx = -1;
7196 char_u *name;
7197
7198 if (idx == 0)
7199 intidx = -1;
7200 if (intidx < 0)
7201 {
7202 name = get_user_func_name(xp, idx);
7203 if (name != NULL)
7204 return name;
7205 }
7206 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7207 {
7208 STRCPY(IObuff, functions[intidx].f_name);
7209 STRCAT(IObuff, "(");
7210 if (functions[intidx].f_max_argc == 0)
7211 STRCAT(IObuff, ")");
7212 return IObuff;
7213 }
7214
7215 return NULL;
7216}
7217
7218/*
7219 * Function given to ExpandGeneric() to obtain the list of internal or
7220 * user defined variable or function names.
7221 */
7222/*ARGSUSED*/
7223 char_u *
7224get_expr_name(xp, idx)
7225 expand_T *xp;
7226 int idx;
7227{
7228 static int intidx = -1;
7229 char_u *name;
7230
7231 if (idx == 0)
7232 intidx = -1;
7233 if (intidx < 0)
7234 {
7235 name = get_function_name(xp, idx);
7236 if (name != NULL)
7237 return name;
7238 }
7239 return get_user_var_name(xp, ++intidx);
7240}
7241
7242#endif /* FEAT_CMDL_COMPL */
7243
7244/*
7245 * Find internal function in table above.
7246 * Return index, or -1 if not found
7247 */
7248 static int
7249find_internal_func(name)
7250 char_u *name; /* name of the function */
7251{
7252 int first = 0;
7253 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7254 int cmp;
7255 int x;
7256
7257 /*
7258 * Find the function name in the table. Binary search.
7259 */
7260 while (first <= last)
7261 {
7262 x = first + ((unsigned)(last - first) >> 1);
7263 cmp = STRCMP(name, functions[x].f_name);
7264 if (cmp < 0)
7265 last = x - 1;
7266 else if (cmp > 0)
7267 first = x + 1;
7268 else
7269 return x;
7270 }
7271 return -1;
7272}
7273
7274/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7276 * name it contains, otherwise return "name".
7277 */
7278 static char_u *
7279deref_func_name(name, lenp)
7280 char_u *name;
7281 int *lenp;
7282{
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 int cc;
7285
7286 cc = name[*lenp];
7287 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007288 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007289 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007291 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007293 {
7294 *lenp = 0;
7295 return (char_u *)""; /* just in case */
7296 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299 }
7300
7301 return name;
7302}
7303
7304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 * Allocate a variable for the result of a function.
7306 * Return OK or FAIL.
7307 */
7308 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7310 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 char_u *name; /* name of the function */
7312 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 char_u **arg; /* argument, pointing to the '(' */
7315 linenr_T firstline; /* first line of range */
7316 linenr_T lastline; /* last line of range */
7317 int *doesrange; /* return: function handled range */
7318 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 char_u *argp;
7322 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007323 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int argcount = 0; /* number of arguments found */
7325
7326 /*
7327 * Get the arguments.
7328 */
7329 argp = *arg;
7330 while (argcount < MAX_FUNC_ARGS)
7331 {
7332 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7333 if (*argp == ')' || *argp == ',' || *argp == NUL)
7334 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7336 {
7337 ret = FAIL;
7338 break;
7339 }
7340 ++argcount;
7341 if (*argp != ',')
7342 break;
7343 }
7344 if (*argp == ')')
7345 ++argp;
7346 else
7347 ret = FAIL;
7348
7349 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007350 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 {
7354 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007355 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007357 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359
7360 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007361 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
7363 *arg = skipwhite(argp);
7364 return ret;
7365}
7366
7367
7368/*
7369 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007370 * Return OK when the function can't be called, FAIL otherwise.
7371 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 */
7373 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007374call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 char_u *name; /* name of the function */
7377 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007380 typval_T *argvars; /* vars for arguments, must have "argcount"
7381 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 linenr_T firstline; /* first line of range */
7383 linenr_T lastline; /* last line of range */
7384 int *doesrange; /* return: function handled range */
7385 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389#define ERROR_UNKNOWN 0
7390#define ERROR_TOOMANY 1
7391#define ERROR_TOOFEW 2
7392#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393#define ERROR_DICT 4
7394#define ERROR_NONE 5
7395#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 int error = ERROR_NONE;
7397 int i;
7398 int llen;
7399 ufunc_T *fp;
7400 int cc;
7401#define FLEN_FIXED 40
7402 char_u fname_buf[FLEN_FIXED + 1];
7403 char_u *fname;
7404
7405 /*
7406 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7407 * Change <SNR>123_name() to K_SNR 123_name().
7408 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7409 */
7410 cc = name[len];
7411 name[len] = NUL;
7412 llen = eval_fname_script(name);
7413 if (llen > 0)
7414 {
7415 fname_buf[0] = K_SPECIAL;
7416 fname_buf[1] = KS_EXTRA;
7417 fname_buf[2] = (int)KE_SNR;
7418 i = 3;
7419 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7420 {
7421 if (current_SID <= 0)
7422 error = ERROR_SCRIPT;
7423 else
7424 {
7425 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7426 i = (int)STRLEN(fname_buf);
7427 }
7428 }
7429 if (i + STRLEN(name + llen) < FLEN_FIXED)
7430 {
7431 STRCPY(fname_buf + i, name + llen);
7432 fname = fname_buf;
7433 }
7434 else
7435 {
7436 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7437 if (fname == NULL)
7438 error = ERROR_OTHER;
7439 else
7440 {
7441 mch_memmove(fname, fname_buf, (size_t)i);
7442 STRCPY(fname + i, name + llen);
7443 }
7444 }
7445 }
7446 else
7447 fname = name;
7448
7449 *doesrange = FALSE;
7450
7451
7452 /* execute the function if no errors detected and executing */
7453 if (evaluate && error == ERROR_NONE)
7454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 error = ERROR_UNKNOWN;
7457
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007458 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
7460 /*
7461 * User defined function.
7462 */
7463 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007466 /* Trigger FuncUndefined event, may load the function. */
7467 if (fp == NULL
7468 && apply_autocmds(EVENT_FUNCUNDEFINED,
7469 fname, fname, TRUE, NULL)
7470 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007472 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 fp = find_func(fname);
7474 }
7475#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007476 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007477 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007478 {
7479 /* loaded a package, search for the function again */
7480 fp = find_func(fname);
7481 }
7482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (fp != NULL)
7484 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007485 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007487 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007489 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007491 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007492 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 else
7494 {
7495 /*
7496 * Call the user function.
7497 * Save and restore search patterns, script variables and
7498 * redo buffer.
7499 */
7500 save_search_patterns();
7501 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007502 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007503 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007505 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7506 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7507 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007508 /* Function was unreferenced while being used, free it
7509 * now. */
7510 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 restoreRedobuff();
7512 restore_search_patterns();
7513 error = ERROR_NONE;
7514 }
7515 }
7516 }
7517 else
7518 {
7519 /*
7520 * Find the function name in the table, call its implementation.
7521 */
7522 i = find_internal_func(fname);
7523 if (i >= 0)
7524 {
7525 if (argcount < functions[i].f_min_argc)
7526 error = ERROR_TOOFEW;
7527 else if (argcount > functions[i].f_max_argc)
7528 error = ERROR_TOOMANY;
7529 else
7530 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007531 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 error = ERROR_NONE;
7534 }
7535 }
7536 }
7537 /*
7538 * The function call (or "FuncUndefined" autocommand sequence) might
7539 * have been aborted by an error, an interrupt, or an explicitly thrown
7540 * exception that has not been caught so far. This situation can be
7541 * tested for by calling aborting(). For an error in an internal
7542 * function or for the "E132" error in call_user_func(), however, the
7543 * throw point at which the "force_abort" flag (temporarily reset by
7544 * emsg()) is normally updated has not been reached yet. We need to
7545 * update that flag first to make aborting() reliable.
7546 */
7547 update_force_abort();
7548 }
7549 if (error == ERROR_NONE)
7550 ret = OK;
7551
7552 /*
7553 * Report an error unless the argument evaluation or function call has been
7554 * cancelled due to an aborting error, an interrupt, or an exception.
7555 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 if (!aborting())
7557 {
7558 switch (error)
7559 {
7560 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007561 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 break;
7563 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007564 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 break;
7566 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007567 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 name);
7569 break;
7570 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007571 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 name);
7573 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007575 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007576 name);
7577 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 }
7579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580
7581 name[len] = cc;
7582 if (fname != name && fname != fname_buf)
7583 vim_free(fname);
7584
7585 return ret;
7586}
7587
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007588/*
7589 * Give an error message with a function name. Handle <SNR> things.
7590 */
7591 static void
7592emsg_funcname(msg, name)
7593 char *msg;
7594 char_u *name;
7595{
7596 char_u *p;
7597
7598 if (*name == K_SPECIAL)
7599 p = concat_str((char_u *)"<SNR>", name + 3);
7600 else
7601 p = name;
7602 EMSG2(_(msg), p);
7603 if (p != name)
7604 vim_free(p);
7605}
7606
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607/*********************************************
7608 * Implementation of the built-in functions
7609 */
7610
7611/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007612 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 */
7614 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007615f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 typval_T *argvars;
7617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007624 if ((l = argvars[0].vval.v_list) != NULL
7625 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7626 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 }
7629 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007630 EMSG(_(e_listreq));
7631}
7632
7633/*
7634 * "append(lnum, string/list)" function
7635 */
7636 static void
7637f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 typval_T *argvars;
7639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007640{
7641 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007642 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007643 list_T *l = NULL;
7644 listitem_T *li = NULL;
7645 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007646 long added = 0;
7647
Bram Moolenaar0d660222005-01-07 21:51:51 +00007648 lnum = get_tv_lnum(argvars);
7649 if (lnum >= 0
7650 && lnum <= curbuf->b_ml.ml_line_count
7651 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007652 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007653 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007654 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007655 l = argvars[1].vval.v_list;
7656 if (l == NULL)
7657 return;
7658 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007659 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007660 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007661 for (;;)
7662 {
7663 if (l == NULL)
7664 tv = &argvars[1]; /* append a string */
7665 else if (li == NULL)
7666 break; /* end of list */
7667 else
7668 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007669 line = get_tv_string_chk(tv);
7670 if (line == NULL) /* type error */
7671 {
7672 rettv->vval.v_number = 1; /* Failed */
7673 break;
7674 }
7675 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007676 ++added;
7677 if (l == NULL)
7678 break;
7679 li = li->li_next;
7680 }
7681
7682 appended_lines_mark(lnum, added);
7683 if (curwin->w_cursor.lnum > lnum)
7684 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007686 else
7687 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
7690/*
7691 * "argc()" function
7692 */
7693/* ARGSUSED */
7694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 typval_T *argvars;
7697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007699 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700}
7701
7702/*
7703 * "argidx()" function
7704 */
7705/* ARGSUSED */
7706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007707f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007708 typval_T *argvars;
7709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007711 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712}
7713
7714/*
7715 * "argv(nr)" function
7716 */
7717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 typval_T *argvars;
7720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 int idx;
7723
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007724 if (argvars[0].v_type != VAR_UNKNOWN)
7725 {
7726 idx = get_tv_number_chk(&argvars[0], NULL);
7727 if (idx >= 0 && idx < ARGCOUNT)
7728 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7729 else
7730 rettv->vval.v_string = NULL;
7731 rettv->v_type = VAR_STRING;
7732 }
7733 else if (rettv_list_alloc(rettv) == OK)
7734 for (idx = 0; idx < ARGCOUNT; ++idx)
7735 list_append_string(rettv->vval.v_list,
7736 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737}
7738
7739/*
7740 * "browse(save, title, initdir, default)" function
7741 */
7742/* ARGSUSED */
7743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 typval_T *argvars;
7746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748#ifdef FEAT_BROWSE
7749 int save;
7750 char_u *title;
7751 char_u *initdir;
7752 char_u *defname;
7753 char_u buf[NUMBUFLEN];
7754 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007755 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007757 save = get_tv_number_chk(&argvars[0], &error);
7758 title = get_tv_string_chk(&argvars[1]);
7759 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7760 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 if (error || title == NULL || initdir == NULL || defname == NULL)
7763 rettv->vval.v_string = NULL;
7764 else
7765 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007766 do_browse(save ? BROWSE_SAVE : 0,
7767 title, defname, NULL, initdir, NULL, curbuf);
7768#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007770#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007771 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007772}
7773
7774/*
7775 * "browsedir(title, initdir)" function
7776 */
7777/* ARGSUSED */
7778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 typval_T *argvars;
7781 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007782{
7783#ifdef FEAT_BROWSE
7784 char_u *title;
7785 char_u *initdir;
7786 char_u buf[NUMBUFLEN];
7787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007788 title = get_tv_string_chk(&argvars[0]);
7789 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007791 if (title == NULL || initdir == NULL)
7792 rettv->vval.v_string = NULL;
7793 else
7794 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007795 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007799 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800}
7801
Bram Moolenaar33570922005-01-25 22:26:29 +00007802static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804/*
7805 * Find a buffer by number or exact name.
7806 */
7807 static buf_T *
7808find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007813 if (avar->v_type == VAR_NUMBER)
7814 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007815 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007817 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007818 if (buf == NULL)
7819 {
7820 /* No full path name match, try a match with a URL or a "nofile"
7821 * buffer, these don't use the full path. */
7822 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7823 if (buf->b_fname != NULL
7824 && (path_with_url(buf->b_fname)
7825#ifdef FEAT_QUICKFIX
7826 || bt_nofile(buf)
7827#endif
7828 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007830 break;
7831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 }
7833 return buf;
7834}
7835
7836/*
7837 * "bufexists(expr)" function
7838 */
7839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007841 typval_T *argvars;
7842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845}
7846
7847/*
7848 * "buflisted(expr)" function
7849 */
7850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 typval_T *argvars;
7853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 buf_T *buf;
7856
7857 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859}
7860
7861/*
7862 * "bufloaded(expr)" function
7863 */
7864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 typval_T *argvars;
7867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 buf_T *buf;
7870
7871 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007872 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873}
7874
Bram Moolenaar33570922005-01-25 22:26:29 +00007875static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007876
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877/*
7878 * Get buffer by number or pattern.
7879 */
7880 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007881get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 int save_magic;
7886 char_u *save_cpo;
7887 buf_T *buf;
7888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007889 if (tv->v_type == VAR_NUMBER)
7890 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007891 if (tv->v_type != VAR_STRING)
7892 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (name == NULL || *name == NUL)
7894 return curbuf;
7895 if (name[0] == '$' && name[1] == NUL)
7896 return lastbuf;
7897
7898 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7899 save_magic = p_magic;
7900 p_magic = TRUE;
7901 save_cpo = p_cpo;
7902 p_cpo = (char_u *)"";
7903
7904 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7905 TRUE, FALSE));
7906
7907 p_magic = save_magic;
7908 p_cpo = save_cpo;
7909
7910 /* If not found, try expanding the name, like done for bufexists(). */
7911 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007912 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913
7914 return buf;
7915}
7916
7917/*
7918 * "bufname(expr)" function
7919 */
7920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 typval_T *argvars;
7923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925 buf_T *buf;
7926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007927 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 buf = get_buf_tv(&argvars[0]);
7930 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 --emsg_off;
7936}
7937
7938/*
7939 * "bufnr(expr)" function
7940 */
7941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 typval_T *argvars;
7944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945{
7946 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007947 int error = FALSE;
7948 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007950 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007952 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007953 --emsg_off;
7954
7955 /* If the buffer isn't found and the second argument is not zero create a
7956 * new buffer. */
7957 if (buf == NULL
7958 && argvars[1].v_type != VAR_UNKNOWN
7959 && get_tv_number_chk(&argvars[1], &error) != 0
7960 && !error
7961 && (name = get_tv_string_chk(&argvars[0])) != NULL
7962 && !error)
7963 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7964
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
7971/*
7972 * "bufwinnr(nr)" function
7973 */
7974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007975f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 typval_T *argvars;
7977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979#ifdef FEAT_WINDOWS
7980 win_T *wp;
7981 int winnr = 0;
7982#endif
7983 buf_T *buf;
7984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007985 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007987 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988#ifdef FEAT_WINDOWS
7989 for (wp = firstwin; wp; wp = wp->w_next)
7990 {
7991 ++winnr;
7992 if (wp->w_buffer == buf)
7993 break;
7994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998#endif
7999 --emsg_off;
8000}
8001
8002/*
8003 * "byte2line(byte)" function
8004 */
8005/*ARGSUSED*/
8006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008007f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008008 typval_T *argvars;
8009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
8011#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013#else
8014 long boff = 0;
8015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008016 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 (linenr_T)0, &boff);
8022#endif
8023}
8024
8025/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008026 * "byteidx()" function
8027 */
8028/*ARGSUSED*/
8029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 typval_T *argvars;
8032 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008033{
8034#ifdef FEAT_MBYTE
8035 char_u *t;
8036#endif
8037 char_u *str;
8038 long idx;
8039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008040 str = get_tv_string_chk(&argvars[0]);
8041 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008042 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008043 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008044 return;
8045
8046#ifdef FEAT_MBYTE
8047 t = str;
8048 for ( ; idx > 0; idx--)
8049 {
8050 if (*t == NUL) /* EOL reached */
8051 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008052 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008053 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008054 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008055#else
8056 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008057 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008058#endif
8059}
8060
8061/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008062 * "call(func, arglist)" function
8063 */
8064 static void
8065f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 typval_T *argvars;
8067 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008068{
8069 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008070 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008071 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008073 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008075
8076 rettv->vval.v_number = 0;
8077 if (argvars[1].v_type != VAR_LIST)
8078 {
8079 EMSG(_(e_listreq));
8080 return;
8081 }
8082 if (argvars[1].vval.v_list == NULL)
8083 return;
8084
8085 if (argvars[0].v_type == VAR_FUNC)
8086 func = argvars[0].vval.v_string;
8087 else
8088 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 if (*func == NUL)
8090 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008091
Bram Moolenaare9a41262005-01-15 22:18:47 +00008092 if (argvars[2].v_type != VAR_UNKNOWN)
8093 {
8094 if (argvars[2].v_type != VAR_DICT)
8095 {
8096 EMSG(_(e_dictreq));
8097 return;
8098 }
8099 selfdict = argvars[2].vval.v_dict;
8100 }
8101
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008102 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8103 item = item->li_next)
8104 {
8105 if (argc == MAX_FUNC_ARGS)
8106 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008107 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008108 break;
8109 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008110 /* Make a copy of each argument. This is needed to be able to set
8111 * v_lock to VAR_FIXED in the copy without changing the original list.
8112 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008113 copy_tv(&item->li_tv, &argv[argc++]);
8114 }
8115
8116 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008117 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008118 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8119 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008120
8121 /* Free the arguments. */
8122 while (argc > 0)
8123 clear_tv(&argv[--argc]);
8124}
8125
8126/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008127 * "changenr()" function
8128 */
8129/*ARGSUSED*/
8130 static void
8131f_changenr(argvars, rettv)
8132 typval_T *argvars;
8133 typval_T *rettv;
8134{
8135 rettv->vval.v_number = curbuf->b_u_seq_cur;
8136}
8137
8138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 * "char2nr(string)" function
8140 */
8141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008143 typval_T *argvars;
8144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145{
8146#ifdef FEAT_MBYTE
8147 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 else
8150#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008151 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152}
8153
8154/*
8155 * "cindent(lnum)" function
8156 */
8157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008159 typval_T *argvars;
8160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
8162#ifdef FEAT_CINDENT
8163 pos_T pos;
8164 linenr_T lnum;
8165
8166 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008167 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8169 {
8170 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008171 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 curwin->w_cursor = pos;
8173 }
8174 else
8175#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177}
8178
8179/*
8180 * "col(string)" function
8181 */
8182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008183f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 typval_T *argvars;
8185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
8187 colnr_T col = 0;
8188 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008189 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008191 fp = var2fpos(&argvars[0], FALSE, &fnum);
8192 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {
8194 if (fp->col == MAXCOL)
8195 {
8196 /* '> can be MAXCOL, get the length of the line then */
8197 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008198 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 else
8200 col = MAXCOL;
8201 }
8202 else
8203 {
8204 col = fp->col + 1;
8205#ifdef FEAT_VIRTUALEDIT
8206 /* col(".") when the cursor is on the NUL at the end of the line
8207 * because of "coladd" can be seen as an extra column. */
8208 if (virtual_active() && fp == &curwin->w_cursor)
8209 {
8210 char_u *p = ml_get_cursor();
8211
8212 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8213 curwin->w_virtcol - curwin->w_cursor.coladd))
8214 {
8215# ifdef FEAT_MBYTE
8216 int l;
8217
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008218 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 col += l;
8220# else
8221 if (*p != NUL && p[1] == NUL)
8222 ++col;
8223# endif
8224 }
8225 }
8226#endif
8227 }
8228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008229 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230}
8231
Bram Moolenaar572cb562005-08-05 21:35:02 +00008232#if defined(FEAT_INS_EXPAND)
8233/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008234 * "complete()" function
8235 */
8236/*ARGSUSED*/
8237 static void
8238f_complete(argvars, rettv)
8239 typval_T *argvars;
8240 typval_T *rettv;
8241{
8242 int startcol;
8243
8244 if ((State & INSERT) == 0)
8245 {
8246 EMSG(_("E785: complete() can only be used in Insert mode"));
8247 return;
8248 }
8249 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8250 {
8251 EMSG(_(e_invarg));
8252 return;
8253 }
8254
8255 startcol = get_tv_number_chk(&argvars[0], NULL);
8256 if (startcol <= 0)
8257 return;
8258
8259 set_completion(startcol - 1, argvars[1].vval.v_list);
8260}
8261
8262/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008263 * "complete_add()" function
8264 */
8265/*ARGSUSED*/
8266 static void
8267f_complete_add(argvars, rettv)
8268 typval_T *argvars;
8269 typval_T *rettv;
8270{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008271 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008272}
8273
8274/*
8275 * "complete_check()" function
8276 */
8277/*ARGSUSED*/
8278 static void
8279f_complete_check(argvars, rettv)
8280 typval_T *argvars;
8281 typval_T *rettv;
8282{
8283 int saved = RedrawingDisabled;
8284
8285 RedrawingDisabled = 0;
8286 ins_compl_check_keys(0);
8287 rettv->vval.v_number = compl_interrupted;
8288 RedrawingDisabled = saved;
8289}
8290#endif
8291
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292/*
8293 * "confirm(message, buttons[, default [, type]])" function
8294 */
8295/*ARGSUSED*/
8296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008297f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 typval_T *argvars;
8299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8302 char_u *message;
8303 char_u *buttons = NULL;
8304 char_u buf[NUMBUFLEN];
8305 char_u buf2[NUMBUFLEN];
8306 int def = 1;
8307 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008308 char_u *typestr;
8309 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 message = get_tv_string_chk(&argvars[0]);
8312 if (message == NULL)
8313 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8317 if (buttons == NULL)
8318 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008321 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008324 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8325 if (typestr == NULL)
8326 error = TRUE;
8327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008329 switch (TOUPPER_ASC(*typestr))
8330 {
8331 case 'E': type = VIM_ERROR; break;
8332 case 'Q': type = VIM_QUESTION; break;
8333 case 'I': type = VIM_INFO; break;
8334 case 'W': type = VIM_WARNING; break;
8335 case 'G': type = VIM_GENERIC; break;
8336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
8338 }
8339 }
8340 }
8341
8342 if (buttons == NULL || *buttons == NUL)
8343 buttons = (char_u *)_("&Ok");
8344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008345 if (error)
8346 rettv->vval.v_number = 0;
8347 else
8348 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 def, NULL);
8350#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008351 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352#endif
8353}
8354
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008355/*
8356 * "copy()" function
8357 */
8358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008360 typval_T *argvars;
8361 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008363 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008364}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365
8366/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008367 * "count()" function
8368 */
8369 static void
8370f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 typval_T *argvars;
8372 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008373{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008374 long n = 0;
8375 int ic = FALSE;
8376
Bram Moolenaare9a41262005-01-15 22:18:47 +00008377 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008378 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 listitem_T *li;
8380 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008381 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008382
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 if ((l = argvars[0].vval.v_list) != NULL)
8384 {
8385 li = l->lv_first;
8386 if (argvars[2].v_type != VAR_UNKNOWN)
8387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008388 int error = FALSE;
8389
8390 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008391 if (argvars[3].v_type != VAR_UNKNOWN)
8392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008393 idx = get_tv_number_chk(&argvars[3], &error);
8394 if (!error)
8395 {
8396 li = list_find(l, idx);
8397 if (li == NULL)
8398 EMSGN(_(e_listidx), idx);
8399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008401 if (error)
8402 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008403 }
8404
8405 for ( ; li != NULL; li = li->li_next)
8406 if (tv_equal(&li->li_tv, &argvars[1], ic))
8407 ++n;
8408 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008409 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008410 else if (argvars[0].v_type == VAR_DICT)
8411 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 int todo;
8413 dict_T *d;
8414 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008416 if ((d = argvars[0].vval.v_dict) != NULL)
8417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008418 int error = FALSE;
8419
Bram Moolenaare9a41262005-01-15 22:18:47 +00008420 if (argvars[2].v_type != VAR_UNKNOWN)
8421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008422 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008423 if (argvars[3].v_type != VAR_UNKNOWN)
8424 EMSG(_(e_invarg));
8425 }
8426
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008427 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008428 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008429 {
8430 if (!HASHITEM_EMPTY(hi))
8431 {
8432 --todo;
8433 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8434 ++n;
8435 }
8436 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008437 }
8438 }
8439 else
8440 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008441 rettv->vval.v_number = n;
8442}
8443
8444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8446 *
8447 * Checks the existence of a cscope connection.
8448 */
8449/*ARGSUSED*/
8450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 typval_T *argvars;
8453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455#ifdef FEAT_CSCOPE
8456 int num = 0;
8457 char_u *dbpath = NULL;
8458 char_u *prepend = NULL;
8459 char_u buf[NUMBUFLEN];
8460
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008461 if (argvars[0].v_type != VAR_UNKNOWN
8462 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464 num = (int)get_tv_number(&argvars[0]);
8465 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 }
8469
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008470 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008472 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473#endif
8474}
8475
8476/*
8477 * "cursor(lnum, col)" function
8478 *
8479 * Moves the cursor to the specified line and column
8480 */
8481/*ARGSUSED*/
8482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008484 typval_T *argvars;
8485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008488#ifdef FEAT_VIRTUALEDIT
8489 long coladd = 0;
8490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
Bram Moolenaara5525202006-03-02 22:52:09 +00008492 if (argvars[1].v_type == VAR_UNKNOWN)
8493 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008494 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008495
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008496 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008497 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008498 line = pos.lnum;
8499 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008500#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008501 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008502#endif
8503 }
8504 else
8505 {
8506 line = get_tv_lnum(argvars);
8507 col = get_tv_number_chk(&argvars[1], NULL);
8508#ifdef FEAT_VIRTUALEDIT
8509 if (argvars[2].v_type != VAR_UNKNOWN)
8510 coladd = get_tv_number_chk(&argvars[2], NULL);
8511#endif
8512 }
8513 if (line < 0 || col < 0
8514#ifdef FEAT_VIRTUALEDIT
8515 || coladd < 0
8516#endif
8517 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008518 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (line > 0)
8520 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 if (col > 0)
8522 curwin->w_cursor.col = col - 1;
8523#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008524 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525#endif
8526
8527 /* Make sure the cursor is in a valid position. */
8528 check_cursor();
8529#ifdef FEAT_MBYTE
8530 /* Correct cursor for multi-byte character. */
8531 if (has_mbyte)
8532 mb_adjust_cursor();
8533#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008534
8535 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536}
8537
8538/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 */
8541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008542f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008543 typval_T *argvars;
8544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008546 int noref = 0;
8547
8548 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008549 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008550 if (noref < 0 || noref > 1)
8551 EMSG(_(e_invarg));
8552 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008553 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554}
8555
8556/*
8557 * "delete()" function
8558 */
8559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 typval_T *argvars;
8562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568}
8569
8570/*
8571 * "did_filetype()" function
8572 */
8573/*ARGSUSED*/
8574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *argvars;
8577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583#endif
8584}
8585
8586/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008587 * "diff_filler()" function
8588 */
8589/*ARGSUSED*/
8590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 typval_T *argvars;
8593 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008594{
8595#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008596 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008597#endif
8598}
8599
8600/*
8601 * "diff_hlID()" function
8602 */
8603/*ARGSUSED*/
8604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008605f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 typval_T *argvars;
8607 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008608{
8609#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008611 static linenr_T prev_lnum = 0;
8612 static int changedtick = 0;
8613 static int fnum = 0;
8614 static int change_start = 0;
8615 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008616 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008617 int filler_lines;
8618 int col;
8619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008620 if (lnum < 0) /* ignore type error in {lnum} arg */
8621 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008622 if (lnum != prev_lnum
8623 || changedtick != curbuf->b_changedtick
8624 || fnum != curbuf->b_fnum)
8625 {
8626 /* New line, buffer, change: need to get the values. */
8627 filler_lines = diff_check(curwin, lnum);
8628 if (filler_lines < 0)
8629 {
8630 if (filler_lines == -1)
8631 {
8632 change_start = MAXCOL;
8633 change_end = -1;
8634 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8635 hlID = HLF_ADD; /* added line */
8636 else
8637 hlID = HLF_CHD; /* changed line */
8638 }
8639 else
8640 hlID = HLF_ADD; /* added line */
8641 }
8642 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008643 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008644 prev_lnum = lnum;
8645 changedtick = curbuf->b_changedtick;
8646 fnum = curbuf->b_fnum;
8647 }
8648
8649 if (hlID == HLF_CHD || hlID == HLF_TXD)
8650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008651 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008652 if (col >= change_start && col <= change_end)
8653 hlID = HLF_TXD; /* changed text */
8654 else
8655 hlID = HLF_CHD; /* changed line */
8656 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008657 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008658#endif
8659}
8660
8661/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008662 * "empty({expr})" function
8663 */
8664 static void
8665f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 typval_T *argvars;
8667 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008668{
8669 int n;
8670
8671 switch (argvars[0].v_type)
8672 {
8673 case VAR_STRING:
8674 case VAR_FUNC:
8675 n = argvars[0].vval.v_string == NULL
8676 || *argvars[0].vval.v_string == NUL;
8677 break;
8678 case VAR_NUMBER:
8679 n = argvars[0].vval.v_number == 0;
8680 break;
8681 case VAR_LIST:
8682 n = argvars[0].vval.v_list == NULL
8683 || argvars[0].vval.v_list->lv_first == NULL;
8684 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008685 case VAR_DICT:
8686 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008688 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008689 default:
8690 EMSG2(_(e_intern2), "f_empty()");
8691 n = 0;
8692 }
8693
8694 rettv->vval.v_number = n;
8695}
8696
8697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 * "escape({string}, {chars})" function
8699 */
8700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 typval_T *argvars;
8703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 char_u buf[NUMBUFLEN];
8706
Bram Moolenaar758711c2005-02-02 23:11:38 +00008707 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8708 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710}
8711
8712/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008713 * "eval()" function
8714 */
8715/*ARGSUSED*/
8716 static void
8717f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 typval_T *argvars;
8719 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008720{
8721 char_u *s;
8722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008723 s = get_tv_string_chk(&argvars[0]);
8724 if (s != NULL)
8725 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008727 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8728 {
8729 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008730 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008731 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008732 else if (*s != NUL)
8733 EMSG(_(e_trailing));
8734}
8735
8736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 * "eventhandler()" function
8738 */
8739/*ARGSUSED*/
8740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008742 typval_T *argvars;
8743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746}
8747
8748/*
8749 * "executable()" function
8750 */
8751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008752f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008753 typval_T *argvars;
8754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757}
8758
8759/*
8760 * "exists()" function
8761 */
8762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 typval_T *argvars;
8765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
8767 char_u *p;
8768 char_u *name;
8769 int n = FALSE;
8770 int len = 0;
8771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (*p == '$') /* environment variable */
8774 {
8775 /* first try "normal" environment variables (fast) */
8776 if (mch_getenv(p + 1) != NULL)
8777 n = TRUE;
8778 else
8779 {
8780 /* try expanding things like $VIM and ${HOME} */
8781 p = expand_env_save(p);
8782 if (p != NULL && *p != '$')
8783 n = TRUE;
8784 vim_free(p);
8785 }
8786 }
8787 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 else if (*p == '*') /* internal or user defined function */
8790 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008791 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 }
8793 else if (*p == ':')
8794 {
8795 n = cmd_exists(p + 1);
8796 }
8797 else if (*p == '#')
8798 {
8799#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008800 if (p[1] == '#')
8801 n = autocmd_supported(p + 2);
8802 else
8803 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804#endif
8805 }
8806 else /* internal variable */
8807 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008808 char_u *tofree;
8809 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008811 /* get_name_len() takes care of expanding curly braces */
8812 name = p;
8813 len = get_name_len(&p, &tofree, TRUE, FALSE);
8814 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008816 if (tofree != NULL)
8817 name = tofree;
8818 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8819 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008821 /* handle d.key, l[idx], f(expr) */
8822 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8823 if (n)
8824 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
8826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008828 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 }
8830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832}
8833
8834/*
8835 * "expand()" function
8836 */
8837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 typval_T *argvars;
8840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 char_u *s;
8843 int len;
8844 char_u *errormsg;
8845 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8846 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->v_type = VAR_STRING;
8850 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (*s == '%' || *s == '#' || *s == '<')
8852 {
8853 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 --emsg_off;
8856 }
8857 else
8858 {
8859 /* When the optional second argument is non-zero, don't remove matches
8860 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008861 if (argvars[1].v_type != VAR_UNKNOWN
8862 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008864 if (!error)
8865 {
8866 ExpandInit(&xpc);
8867 xpc.xp_context = EXPAND_FILES;
8868 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008869 }
8870 else
8871 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 }
8873}
8874
8875/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008876 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008878 */
8879 static void
8880f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008881 typval_T *argvars;
8882 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008883{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008884 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008885 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008886 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008887 list_T *l1, *l2;
8888 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008889 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008891
Bram Moolenaare9a41262005-01-15 22:18:47 +00008892 l1 = argvars[0].vval.v_list;
8893 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008894 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8895 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008896 {
8897 if (argvars[2].v_type != VAR_UNKNOWN)
8898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 before = get_tv_number_chk(&argvars[2], &error);
8900 if (error)
8901 return; /* type error; errmsg already given */
8902
Bram Moolenaar758711c2005-02-02 23:11:38 +00008903 if (before == l1->lv_len)
8904 item = NULL;
8905 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008906 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008907 item = list_find(l1, before);
8908 if (item == NULL)
8909 {
8910 EMSGN(_(e_listidx), before);
8911 return;
8912 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008913 }
8914 }
8915 else
8916 item = NULL;
8917 list_extend(l1, l2, item);
8918
Bram Moolenaare9a41262005-01-15 22:18:47 +00008919 copy_tv(&argvars[0], rettv);
8920 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008922 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8923 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 dict_T *d1, *d2;
8925 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008926 char_u *action;
8927 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008928 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008929 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008930
8931 d1 = argvars[0].vval.v_dict;
8932 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008933 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8934 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008935 {
8936 /* Check the third argument. */
8937 if (argvars[2].v_type != VAR_UNKNOWN)
8938 {
8939 static char *(av[]) = {"keep", "force", "error"};
8940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 action = get_tv_string_chk(&argvars[2]);
8942 if (action == NULL)
8943 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008944 for (i = 0; i < 3; ++i)
8945 if (STRCMP(action, av[i]) == 0)
8946 break;
8947 if (i == 3)
8948 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008949 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008950 return;
8951 }
8952 }
8953 else
8954 action = (char_u *)"force";
8955
8956 /* Go over all entries in the second dict and add them to the
8957 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008958 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008961 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008963 --todo;
8964 di1 = dict_find(d1, hi2->hi_key, -1);
8965 if (di1 == NULL)
8966 {
8967 di1 = dictitem_copy(HI2DI(hi2));
8968 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8969 dictitem_free(di1);
8970 }
8971 else if (*action == 'e')
8972 {
8973 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8974 break;
8975 }
8976 else if (*action == 'f')
8977 {
8978 clear_tv(&di1->di_tv);
8979 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8980 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008981 }
8982 }
8983
Bram Moolenaare9a41262005-01-15 22:18:47 +00008984 copy_tv(&argvars[0], rettv);
8985 }
8986 }
8987 else
8988 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008989}
8990
8991/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008992 * "feedkeys()" function
8993 */
8994/*ARGSUSED*/
8995 static void
8996f_feedkeys(argvars, rettv)
8997 typval_T *argvars;
8998 typval_T *rettv;
8999{
9000 int remap = TRUE;
9001 char_u *keys, *flags;
9002 char_u nbuf[NUMBUFLEN];
9003 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009004 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009005
9006 rettv->vval.v_number = 0;
9007 keys = get_tv_string(&argvars[0]);
9008 if (*keys != NUL)
9009 {
9010 if (argvars[1].v_type != VAR_UNKNOWN)
9011 {
9012 flags = get_tv_string_buf(&argvars[1], nbuf);
9013 for ( ; *flags != NUL; ++flags)
9014 {
9015 switch (*flags)
9016 {
9017 case 'n': remap = FALSE; break;
9018 case 'm': remap = TRUE; break;
9019 case 't': typed = TRUE; break;
9020 }
9021 }
9022 }
9023
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009024 /* Need to escape K_SPECIAL and CSI before putting the string in the
9025 * typeahead buffer. */
9026 keys_esc = vim_strsave_escape_csi(keys);
9027 if (keys_esc != NULL)
9028 {
9029 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009030 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009031 vim_free(keys_esc);
9032 typebuf_was_filled = TRUE;
9033 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009034 }
9035}
9036
9037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 * "filereadable()" function
9039 */
9040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 typval_T *argvars;
9043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
9045 FILE *fd;
9046 char_u *p;
9047 int n;
9048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9051 {
9052 n = TRUE;
9053 fclose(fd);
9054 }
9055 else
9056 n = FALSE;
9057
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059}
9060
9061/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009062 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 * rights to write into.
9064 */
9065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 typval_T *argvars;
9068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009070 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071}
9072
Bram Moolenaar33570922005-01-25 22:26:29 +00009073static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009074
9075 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009076findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009077 typval_T *argvars;
9078 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009079 int dir;
9080{
9081#ifdef FEAT_SEARCHPATH
9082 char_u *fname;
9083 char_u *fresult = NULL;
9084 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9085 char_u *p;
9086 char_u pathbuf[NUMBUFLEN];
9087 int count = 1;
9088 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009089 int error = FALSE;
9090#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009091
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009092 rettv->vval.v_string = NULL;
9093 rettv->v_type = VAR_STRING;
9094
9095#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009097
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009098 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009100 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9101 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009102 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009103 else
9104 {
9105 if (*p != NUL)
9106 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009108 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009109 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009111 }
9112
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009113 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9114 error = TRUE;
9115
9116 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 do
9119 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009120 if (rettv->v_type == VAR_STRING)
9121 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009122 fresult = find_file_in_path_option(first ? fname : NULL,
9123 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009124 0, first, path, dir, NULL,
9125 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009126 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009127
9128 if (fresult != NULL && rettv->v_type == VAR_LIST)
9129 list_append_string(rettv->vval.v_list, fresult, -1);
9130
9131 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009133
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009134 if (rettv->v_type == VAR_STRING)
9135 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009136#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009137}
9138
Bram Moolenaar33570922005-01-25 22:26:29 +00009139static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9140static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009141
9142/*
9143 * Implementation of map() and filter().
9144 */
9145 static void
9146filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009147 typval_T *argvars;
9148 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009149 int map;
9150{
9151 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009152 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 listitem_T *li, *nli;
9154 list_T *l = NULL;
9155 dictitem_T *di;
9156 hashtab_T *ht;
9157 hashitem_T *hi;
9158 dict_T *d = NULL;
9159 typval_T save_val;
9160 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009161 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009162 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009163 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009164 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009165
9166 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009167 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009168 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009169 if ((l = argvars[0].vval.v_list) == NULL
9170 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009171 return;
9172 }
9173 else if (argvars[0].v_type == VAR_DICT)
9174 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009175 if ((d = argvars[0].vval.v_dict) == NULL
9176 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009177 return;
9178 }
9179 else
9180 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009181 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009182 return;
9183 }
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 expr = get_tv_string_buf_chk(&argvars[1], buf);
9186 /* On type errors, the preceding call has already displayed an error
9187 * message. Avoid a misleading error message for an empty string that
9188 * was not passed as argument. */
9189 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009191 prepare_vimvar(VV_VAL, &save_val);
9192 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009193
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009194 /* We reset "did_emsg" to be able to detect whether an error
9195 * occurred during evaluation of the expression. */
9196 save_did_emsg = did_emsg;
9197 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009201 prepare_vimvar(VV_KEY, &save_key);
9202 vimvars[VV_KEY].vv_type = VAR_STRING;
9203
9204 ht = &d->dv_hashtab;
9205 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009206 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009209 if (!HASHITEM_EMPTY(hi))
9210 {
9211 --todo;
9212 di = HI2DI(hi);
9213 if (tv_check_lock(di->di_tv.v_lock, msg))
9214 break;
9215 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009216 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009217 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 break;
9219 if (!map && rem)
9220 dictitem_remove(d, di);
9221 clear_tv(&vimvars[VV_KEY].vv_tv);
9222 }
9223 }
9224 hash_unlock(ht);
9225
9226 restore_vimvar(VV_KEY, &save_key);
9227 }
9228 else
9229 {
9230 for (li = l->lv_first; li != NULL; li = nli)
9231 {
9232 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009233 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009235 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009236 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009237 break;
9238 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009240 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009241 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009244
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009245 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009246 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009247
9248 copy_tv(&argvars[0], rettv);
9249}
9250
9251 static int
9252filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009253 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009254 char_u *expr;
9255 int map;
9256 int *remp;
9257{
Bram Moolenaar33570922005-01-25 22:26:29 +00009258 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009259 char_u *s;
9260
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009262 s = expr;
9263 if (eval1(&s, &rettv, TRUE) == FAIL)
9264 return FAIL;
9265 if (*s != NUL) /* check for trailing chars after expr */
9266 {
9267 EMSG2(_(e_invexpr2), s);
9268 return FAIL;
9269 }
9270 if (map)
9271 {
9272 /* map(): replace the list item value */
9273 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009274 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009275 *tv = rettv;
9276 }
9277 else
9278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 int error = FALSE;
9280
Bram Moolenaare9a41262005-01-15 22:18:47 +00009281 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009282 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009283 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009284 /* On type error, nothing has been removed; return FAIL to stop the
9285 * loop. The error message was given by get_tv_number_chk(). */
9286 if (error)
9287 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009291}
9292
9293/*
9294 * "filter()" function
9295 */
9296 static void
9297f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009298 typval_T *argvars;
9299 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009300{
9301 filter_map(argvars, rettv, FALSE);
9302}
9303
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009304/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009305 * "finddir({fname}[, {path}[, {count}]])" function
9306 */
9307 static void
9308f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009309 typval_T *argvars;
9310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009311{
9312 findfilendir(argvars, rettv, TRUE);
9313}
9314
9315/*
9316 * "findfile({fname}[, {path}[, {count}]])" function
9317 */
9318 static void
9319f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009320 typval_T *argvars;
9321 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009322{
9323 findfilendir(argvars, rettv, FALSE);
9324}
9325
9326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 * "fnamemodify({fname}, {mods})" function
9328 */
9329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009331 typval_T *argvars;
9332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 char_u *fname;
9335 char_u *mods;
9336 int usedlen = 0;
9337 int len;
9338 char_u *fbuf = NULL;
9339 char_u buf[NUMBUFLEN];
9340
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 fname = get_tv_string_chk(&argvars[0]);
9342 mods = get_tv_string_buf_chk(&argvars[1], buf);
9343 if (fname == NULL || mods == NULL)
9344 fname = NULL;
9345 else
9346 {
9347 len = (int)STRLEN(fname);
9348 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 vim_free(fbuf);
9357}
9358
Bram Moolenaar33570922005-01-25 22:26:29 +00009359static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361/*
9362 * "foldclosed()" function
9363 */
9364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009366 typval_T *argvars;
9367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 int end;
9369{
9370#ifdef FEAT_FOLDING
9371 linenr_T lnum;
9372 linenr_T first, last;
9373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9376 {
9377 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9378 {
9379 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 return;
9384 }
9385 }
9386#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009387 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388}
9389
9390/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009391 * "foldclosed()" function
9392 */
9393 static void
9394f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009397{
9398 foldclosed_both(argvars, rettv, FALSE);
9399}
9400
9401/*
9402 * "foldclosedend()" function
9403 */
9404 static void
9405f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009406 typval_T *argvars;
9407 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009408{
9409 foldclosed_both(argvars, rettv, TRUE);
9410}
9411
9412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 * "foldlevel()" function
9414 */
9415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 typval_T *argvars;
9418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420#ifdef FEAT_FOLDING
9421 linenr_T lnum;
9422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 else
9427#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429}
9430
9431/*
9432 * "foldtext()" function
9433 */
9434/*ARGSUSED*/
9435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 typval_T *argvars;
9438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440#ifdef FEAT_FOLDING
9441 linenr_T lnum;
9442 char_u *s;
9443 char_u *r;
9444 int len;
9445 char *txt;
9446#endif
9447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009448 rettv->v_type = VAR_STRING;
9449 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009451 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9452 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9453 <= curbuf->b_ml.ml_line_count
9454 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 {
9456 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009457 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9458 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 {
9460 if (!linewhite(lnum))
9461 break;
9462 ++lnum;
9463 }
9464
9465 /* Find interesting text in this line. */
9466 s = skipwhite(ml_get(lnum));
9467 /* skip C comment-start */
9468 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009471 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009472 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009473 {
9474 s = skipwhite(ml_get(lnum + 1));
9475 if (*s == '*')
9476 s = skipwhite(s + 1);
9477 }
9478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 txt = _("+-%s%3ld lines: ");
9480 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009481 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 + 20 /* for %3ld */
9483 + STRLEN(s))); /* concatenated */
9484 if (r != NULL)
9485 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009486 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9487 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9488 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 len = (int)STRLEN(r);
9490 STRCAT(r, s);
9491 /* remove 'foldmarker' and 'commentstring' */
9492 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 }
9495 }
9496#endif
9497}
9498
9499/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009500 * "foldtextresult(lnum)" function
9501 */
9502/*ARGSUSED*/
9503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009505 typval_T *argvars;
9506 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009507{
9508#ifdef FEAT_FOLDING
9509 linenr_T lnum;
9510 char_u *text;
9511 char_u buf[51];
9512 foldinfo_T foldinfo;
9513 int fold_count;
9514#endif
9515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->v_type = VAR_STRING;
9517 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009518#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 /* treat illegal types and illegal string values for {lnum} the same */
9521 if (lnum < 0)
9522 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009523 fold_count = foldedCount(curwin, lnum, &foldinfo);
9524 if (fold_count > 0)
9525 {
9526 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9527 &foldinfo, buf);
9528 if (text == buf)
9529 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009530 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009531 }
9532#endif
9533}
9534
9535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 * "foreground()" function
9537 */
9538/*ARGSUSED*/
9539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009541 typval_T *argvars;
9542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545#ifdef FEAT_GUI
9546 if (gui.in_use)
9547 gui_mch_set_foreground();
9548#else
9549# ifdef WIN32
9550 win32_set_foreground();
9551# endif
9552#endif
9553}
9554
9555/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009556 * "function()" function
9557 */
9558/*ARGSUSED*/
9559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009561 typval_T *argvars;
9562 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009563{
9564 char_u *s;
9565
Bram Moolenaara7043832005-01-21 11:56:39 +00009566 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009568 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 EMSG2(_(e_invarg2), s);
9570 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009571 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009572 else
9573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->vval.v_string = vim_strsave(s);
9575 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009576 }
9577}
9578
9579/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009580 * "garbagecollect()" function
9581 */
9582/*ARGSUSED*/
9583 static void
9584f_garbagecollect(argvars, rettv)
9585 typval_T *argvars;
9586 typval_T *rettv;
9587{
9588 garbage_collect();
9589}
9590
9591/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009592 * "get()" function
9593 */
9594 static void
9595f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009596 typval_T *argvars;
9597 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009598{
Bram Moolenaar33570922005-01-25 22:26:29 +00009599 listitem_T *li;
9600 list_T *l;
9601 dictitem_T *di;
9602 dict_T *d;
9603 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009604
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009606 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 int error = FALSE;
9610
9611 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9612 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009613 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009614 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009615 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616 else if (argvars[0].v_type == VAR_DICT)
9617 {
9618 if ((d = argvars[0].vval.v_dict) != NULL)
9619 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009620 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009621 if (di != NULL)
9622 tv = &di->di_tv;
9623 }
9624 }
9625 else
9626 EMSG2(_(e_listdictarg), "get()");
9627
9628 if (tv == NULL)
9629 {
9630 if (argvars[2].v_type == VAR_UNKNOWN)
9631 rettv->vval.v_number = 0;
9632 else
9633 copy_tv(&argvars[2], rettv);
9634 }
9635 else
9636 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009637}
9638
Bram Moolenaar342337a2005-07-21 21:11:17 +00009639static 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 +00009640
9641/*
9642 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009643 * Return a range (from start to end) of lines in rettv from the specified
9644 * buffer.
9645 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009646 */
9647 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009648get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009649 buf_T *buf;
9650 linenr_T start;
9651 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009652 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009653 typval_T *rettv;
9654{
9655 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009656
Bram Moolenaar342337a2005-07-21 21:11:17 +00009657 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009658 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009659 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009660 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009661 }
9662 else
9663 rettv->vval.v_number = 0;
9664
9665 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9666 return;
9667
9668 if (!retlist)
9669 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009670 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9671 p = ml_get_buf(buf, start, FALSE);
9672 else
9673 p = (char_u *)"";
9674
9675 rettv->v_type = VAR_STRING;
9676 rettv->vval.v_string = vim_strsave(p);
9677 }
9678 else
9679 {
9680 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009681 return;
9682
9683 if (start < 1)
9684 start = 1;
9685 if (end > buf->b_ml.ml_line_count)
9686 end = buf->b_ml.ml_line_count;
9687 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009688 if (list_append_string(rettv->vval.v_list,
9689 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009690 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009691 }
9692}
9693
9694/*
9695 * "getbufline()" function
9696 */
9697 static void
9698f_getbufline(argvars, rettv)
9699 typval_T *argvars;
9700 typval_T *rettv;
9701{
9702 linenr_T lnum;
9703 linenr_T end;
9704 buf_T *buf;
9705
9706 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9707 ++emsg_off;
9708 buf = get_buf_tv(&argvars[0]);
9709 --emsg_off;
9710
Bram Moolenaar661b1822005-07-28 22:36:45 +00009711 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009712 if (argvars[2].v_type == VAR_UNKNOWN)
9713 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009714 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009715 end = get_tv_lnum_buf(&argvars[2], buf);
9716
Bram Moolenaar342337a2005-07-21 21:11:17 +00009717 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009718}
9719
Bram Moolenaar0d660222005-01-07 21:51:51 +00009720/*
9721 * "getbufvar()" function
9722 */
9723 static void
9724f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009725 typval_T *argvars;
9726 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009727{
9728 buf_T *buf;
9729 buf_T *save_curbuf;
9730 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009731 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009733 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9734 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009735 ++emsg_off;
9736 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009737
9738 rettv->v_type = VAR_STRING;
9739 rettv->vval.v_string = NULL;
9740
9741 if (buf != NULL && varname != NULL)
9742 {
9743 if (*varname == '&') /* buffer-local-option */
9744 {
9745 /* set curbuf to be our buf, temporarily */
9746 save_curbuf = curbuf;
9747 curbuf = buf;
9748
9749 get_option_tv(&varname, rettv, TRUE);
9750
9751 /* restore previous notion of curbuf */
9752 curbuf = save_curbuf;
9753 }
9754 else
9755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009756 if (*varname == NUL)
9757 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9758 * scope prefix before the NUL byte is required by
9759 * find_var_in_ht(). */
9760 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009761 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009762 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009763 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009764 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009765 }
9766 }
9767
9768 --emsg_off;
9769}
9770
9771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 * "getchar()" function
9773 */
9774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009776 typval_T *argvars;
9777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009780 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781
9782 ++no_mapping;
9783 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009784 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 /* getchar(): blocking wait. */
9786 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009787 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 /* getchar(1): only check if char avail */
9789 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 else if (error || vpeekc() == NUL)
9791 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 n = 0;
9793 else
9794 /* getchar(0) and char avail: return char */
9795 n = safe_vgetc();
9796 --no_mapping;
9797 --allow_keys;
9798
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 if (IS_SPECIAL(n) || mod_mask != 0)
9801 {
9802 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9803 int i = 0;
9804
9805 /* Turn a special key into three bytes, plus modifier. */
9806 if (mod_mask != 0)
9807 {
9808 temp[i++] = K_SPECIAL;
9809 temp[i++] = KS_MODIFIER;
9810 temp[i++] = mod_mask;
9811 }
9812 if (IS_SPECIAL(n))
9813 {
9814 temp[i++] = K_SPECIAL;
9815 temp[i++] = K_SECOND(n);
9816 temp[i++] = K_THIRD(n);
9817 }
9818#ifdef FEAT_MBYTE
9819 else if (has_mbyte)
9820 i += (*mb_char2bytes)(n, temp + i);
9821#endif
9822 else
9823 temp[i++] = n;
9824 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->v_type = VAR_STRING;
9826 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 }
9828}
9829
9830/*
9831 * "getcharmod()" function
9832 */
9833/*ARGSUSED*/
9834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009836 typval_T *argvars;
9837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840}
9841
9842/*
9843 * "getcmdline()" function
9844 */
9845/*ARGSUSED*/
9846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009847f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009848 typval_T *argvars;
9849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851 rettv->v_type = VAR_STRING;
9852 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853}
9854
9855/*
9856 * "getcmdpos()" function
9857 */
9858/*ARGSUSED*/
9859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009861 typval_T *argvars;
9862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
9867/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009868 * "getcmdtype()" function
9869 */
9870/*ARGSUSED*/
9871 static void
9872f_getcmdtype(argvars, rettv)
9873 typval_T *argvars;
9874 typval_T *rettv;
9875{
9876 rettv->v_type = VAR_STRING;
9877 rettv->vval.v_string = alloc(2);
9878 if (rettv->vval.v_string != NULL)
9879 {
9880 rettv->vval.v_string[0] = get_cmdline_type();
9881 rettv->vval.v_string[1] = NUL;
9882 }
9883}
9884
9885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 * "getcwd()" function
9887 */
9888/*ARGSUSED*/
9889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 typval_T *argvars;
9892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
9894 char_u cwd[MAXPATHL];
9895
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 else
9900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009903 if (rettv->vval.v_string != NULL)
9904 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905#endif
9906 }
9907}
9908
9909/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009910 * "getfontname()" function
9911 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009912/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 typval_T *argvars;
9916 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->v_type = VAR_STRING;
9919 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009920#ifdef FEAT_GUI
9921 if (gui.in_use)
9922 {
9923 GuiFont font;
9924 char_u *name = NULL;
9925
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009926 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009927 {
9928 /* Get the "Normal" font. Either the name saved by
9929 * hl_set_font_name() or from the font ID. */
9930 font = gui.norm_font;
9931 name = hl_get_font_name();
9932 }
9933 else
9934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009936 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9937 return;
9938 font = gui_mch_get_font(name, FALSE);
9939 if (font == NOFONT)
9940 return; /* Invalid font name, return empty string. */
9941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009943 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009944 gui_mch_free_font(font);
9945 }
9946#endif
9947}
9948
9949/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009950 * "getfperm({fname})" function
9951 */
9952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009954 typval_T *argvars;
9955 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009956{
9957 char_u *fname;
9958 struct stat st;
9959 char_u *perm = NULL;
9960 char_u flags[] = "rwx";
9961 int i;
9962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009966 if (mch_stat((char *)fname, &st) >= 0)
9967 {
9968 perm = vim_strsave((char_u *)"---------");
9969 if (perm != NULL)
9970 {
9971 for (i = 0; i < 9; i++)
9972 {
9973 if (st.st_mode & (1 << (8 - i)))
9974 perm[i] = flags[i % 3];
9975 }
9976 }
9977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009979}
9980
9981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 * "getfsize({fname})" function
9983 */
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 char_u *fname;
9990 struct stat st;
9991
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995
9996 if (mch_stat((char *)fname, &st) >= 0)
9997 {
9998 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
10003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005}
10006
10007/*
10008 * "getftime({fname})" function
10009 */
10010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010012 typval_T *argvars;
10013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
10015 char_u *fname;
10016 struct stat st;
10017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019
10020 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024}
10025
10026/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010027 * "getftype({fname})" function
10028 */
10029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010030f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010031 typval_T *argvars;
10032 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010033{
10034 char_u *fname;
10035 struct stat st;
10036 char_u *type = NULL;
10037 char *t;
10038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010042 if (mch_lstat((char *)fname, &st) >= 0)
10043 {
10044#ifdef S_ISREG
10045 if (S_ISREG(st.st_mode))
10046 t = "file";
10047 else if (S_ISDIR(st.st_mode))
10048 t = "dir";
10049# ifdef S_ISLNK
10050 else if (S_ISLNK(st.st_mode))
10051 t = "link";
10052# endif
10053# ifdef S_ISBLK
10054 else if (S_ISBLK(st.st_mode))
10055 t = "bdev";
10056# endif
10057# ifdef S_ISCHR
10058 else if (S_ISCHR(st.st_mode))
10059 t = "cdev";
10060# endif
10061# ifdef S_ISFIFO
10062 else if (S_ISFIFO(st.st_mode))
10063 t = "fifo";
10064# endif
10065# ifdef S_ISSOCK
10066 else if (S_ISSOCK(st.st_mode))
10067 t = "fifo";
10068# endif
10069 else
10070 t = "other";
10071#else
10072# ifdef S_IFMT
10073 switch (st.st_mode & S_IFMT)
10074 {
10075 case S_IFREG: t = "file"; break;
10076 case S_IFDIR: t = "dir"; break;
10077# ifdef S_IFLNK
10078 case S_IFLNK: t = "link"; break;
10079# endif
10080# ifdef S_IFBLK
10081 case S_IFBLK: t = "bdev"; break;
10082# endif
10083# ifdef S_IFCHR
10084 case S_IFCHR: t = "cdev"; break;
10085# endif
10086# ifdef S_IFIFO
10087 case S_IFIFO: t = "fifo"; break;
10088# endif
10089# ifdef S_IFSOCK
10090 case S_IFSOCK: t = "socket"; break;
10091# endif
10092 default: t = "other";
10093 }
10094# else
10095 if (mch_isdir(fname))
10096 t = "dir";
10097 else
10098 t = "file";
10099# endif
10100#endif
10101 type = vim_strsave((char_u *)t);
10102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010104}
10105
10106/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010107 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010108 */
10109 static void
10110f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010111 typval_T *argvars;
10112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010113{
10114 linenr_T lnum;
10115 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010116 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010117
10118 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010119 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010120 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010121 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010122 retlist = FALSE;
10123 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010124 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010125 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010126 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010127 retlist = TRUE;
10128 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010129
Bram Moolenaar342337a2005-07-21 21:11:17 +000010130 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010131}
10132
10133/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010134 * "getpos(string)" function
10135 */
10136 static void
10137f_getpos(argvars, rettv)
10138 typval_T *argvars;
10139 typval_T *rettv;
10140{
10141 pos_T *fp;
10142 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010143 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010144
10145 if (rettv_list_alloc(rettv) == OK)
10146 {
10147 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010148 fp = var2fpos(&argvars[0], TRUE, &fnum);
10149 if (fnum != -1)
10150 list_append_number(l, (varnumber_T)fnum);
10151 else
10152 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010153 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10154 : (varnumber_T)0);
10155 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10156 : (varnumber_T)0);
10157 list_append_number(l,
10158#ifdef FEAT_VIRTUALEDIT
10159 (fp != NULL) ? (varnumber_T)fp->coladd :
10160#endif
10161 (varnumber_T)0);
10162 }
10163 else
10164 rettv->vval.v_number = FALSE;
10165}
10166
10167/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010168 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010169 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010170/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010171 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010172f_getqflist(argvars, rettv)
10173 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010174 typval_T *rettv;
10175{
10176#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010177 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010178#endif
10179
10180 rettv->vval.v_number = FALSE;
10181#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010182 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010183 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010184 wp = NULL;
10185 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10186 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010187 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010188 if (wp == NULL)
10189 return;
10190 }
10191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010192 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010193 }
10194#endif
10195}
10196
10197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 * "getreg()" function
10199 */
10200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010201f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010202 typval_T *argvars;
10203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204{
10205 char_u *strregname;
10206 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010207 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010210 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 strregname = get_tv_string_chk(&argvars[0]);
10213 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010214 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 regname = (strregname == NULL ? '"' : *strregname);
10220 if (regname == 0)
10221 regname = '"';
10222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 rettv->vval.v_string = error ? NULL :
10225 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226}
10227
10228/*
10229 * "getregtype()" function
10230 */
10231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010232f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010233 typval_T *argvars;
10234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
10236 char_u *strregname;
10237 int regname;
10238 char_u buf[NUMBUFLEN + 2];
10239 long reglen = 0;
10240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010241 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 {
10243 strregname = get_tv_string_chk(&argvars[0]);
10244 if (strregname == NULL) /* type error; errmsg already given */
10245 {
10246 rettv->v_type = VAR_STRING;
10247 rettv->vval.v_string = NULL;
10248 return;
10249 }
10250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 else
10252 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010253 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254
10255 regname = (strregname == NULL ? '"' : *strregname);
10256 if (regname == 0)
10257 regname = '"';
10258
10259 buf[0] = NUL;
10260 buf[1] = NUL;
10261 switch (get_reg_type(regname, &reglen))
10262 {
10263 case MLINE: buf[0] = 'V'; break;
10264 case MCHAR: buf[0] = 'v'; break;
10265#ifdef FEAT_VISUAL
10266 case MBLOCK:
10267 buf[0] = Ctrl_V;
10268 sprintf((char *)buf + 1, "%ld", reglen + 1);
10269 break;
10270#endif
10271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010272 rettv->v_type = VAR_STRING;
10273 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274}
10275
10276/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010277 * "gettabwinvar()" function
10278 */
10279 static void
10280f_gettabwinvar(argvars, rettv)
10281 typval_T *argvars;
10282 typval_T *rettv;
10283{
10284 getwinvar(argvars, rettv, 1);
10285}
10286
10287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * "getwinposx()" function
10289 */
10290/*ARGSUSED*/
10291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 typval_T *argvars;
10294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297#ifdef FEAT_GUI
10298 if (gui.in_use)
10299 {
10300 int x, y;
10301
10302 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 }
10305#endif
10306}
10307
10308/*
10309 * "getwinposy()" function
10310 */
10311/*ARGSUSED*/
10312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010314 typval_T *argvars;
10315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318#ifdef FEAT_GUI
10319 if (gui.in_use)
10320 {
10321 int x, y;
10322
10323 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 }
10326#endif
10327}
10328
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010329/*
10330 * Find window specifed by "vp" in tabpage "tp".
10331 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010332 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010333find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010334 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010335 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010336{
10337#ifdef FEAT_WINDOWS
10338 win_T *wp;
10339#endif
10340 int nr;
10341
10342 nr = get_tv_number_chk(vp, NULL);
10343
10344#ifdef FEAT_WINDOWS
10345 if (nr < 0)
10346 return NULL;
10347 if (nr == 0)
10348 return curwin;
10349
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010350 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10351 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010352 if (--nr <= 0)
10353 break;
10354 return wp;
10355#else
10356 if (nr == 0 || nr == 1)
10357 return curwin;
10358 return NULL;
10359#endif
10360}
10361
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362/*
10363 * "getwinvar()" function
10364 */
10365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010367 typval_T *argvars;
10368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010370 getwinvar(argvars, rettv, 0);
10371}
10372
10373/*
10374 * getwinvar() and gettabwinvar()
10375 */
10376 static void
10377getwinvar(argvars, rettv, off)
10378 typval_T *argvars;
10379 typval_T *rettv;
10380 int off; /* 1 for gettabwinvar() */
10381{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 win_T *win, *oldcurwin;
10383 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010384 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010385 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010387#ifdef FEAT_WINDOWS
10388 if (off == 1)
10389 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10390 else
10391 tp = curtab;
10392#endif
10393 win = find_win_by_nr(&argvars[off], tp);
10394 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010395 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->v_type = VAR_STRING;
10398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399
10400 if (win != NULL && varname != NULL)
10401 {
10402 if (*varname == '&') /* window-local-option */
10403 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010404 /* Set curwin to be our win, temporarily. Also set curbuf, so
10405 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 oldcurwin = curwin;
10407 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010408 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411
10412 /* restore previous notion of curwin */
10413 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010414 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 }
10416 else
10417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 if (*varname == NUL)
10419 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10420 * scope prefix before the NUL byte is required by
10421 * find_var_in_ht(). */
10422 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010424 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010426 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 }
10428 }
10429
10430 --emsg_off;
10431}
10432
10433/*
10434 * "glob()" function
10435 */
10436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010438 typval_T *argvars;
10439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 expand_T xpc;
10442
10443 ExpandInit(&xpc);
10444 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010445 rettv->v_type = VAR_STRING;
10446 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448}
10449
10450/*
10451 * "globpath()" function
10452 */
10453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010455 typval_T *argvars;
10456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457{
10458 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010459 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 if (file == NULL)
10463 rettv->vval.v_string = NULL;
10464 else
10465 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466}
10467
10468/*
10469 * "has()" function
10470 */
10471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 typval_T *argvars;
10474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 int i;
10477 char_u *name;
10478 int n = FALSE;
10479 static char *(has_list[]) =
10480 {
10481#ifdef AMIGA
10482 "amiga",
10483# ifdef FEAT_ARP
10484 "arp",
10485# endif
10486#endif
10487#ifdef __BEOS__
10488 "beos",
10489#endif
10490#ifdef MSDOS
10491# ifdef DJGPP
10492 "dos32",
10493# else
10494 "dos16",
10495# endif
10496#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010497#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 "mac",
10499#endif
10500#if defined(MACOS_X_UNIX)
10501 "macunix",
10502#endif
10503#ifdef OS2
10504 "os2",
10505#endif
10506#ifdef __QNX__
10507 "qnx",
10508#endif
10509#ifdef RISCOS
10510 "riscos",
10511#endif
10512#ifdef UNIX
10513 "unix",
10514#endif
10515#ifdef VMS
10516 "vms",
10517#endif
10518#ifdef WIN16
10519 "win16",
10520#endif
10521#ifdef WIN32
10522 "win32",
10523#endif
10524#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10525 "win32unix",
10526#endif
10527#ifdef WIN64
10528 "win64",
10529#endif
10530#ifdef EBCDIC
10531 "ebcdic",
10532#endif
10533#ifndef CASE_INSENSITIVE_FILENAME
10534 "fname_case",
10535#endif
10536#ifdef FEAT_ARABIC
10537 "arabic",
10538#endif
10539#ifdef FEAT_AUTOCMD
10540 "autocmd",
10541#endif
10542#ifdef FEAT_BEVAL
10543 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010544# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10545 "balloon_multiline",
10546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547#endif
10548#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10549 "builtin_terms",
10550# ifdef ALL_BUILTIN_TCAPS
10551 "all_builtin_terms",
10552# endif
10553#endif
10554#ifdef FEAT_BYTEOFF
10555 "byte_offset",
10556#endif
10557#ifdef FEAT_CINDENT
10558 "cindent",
10559#endif
10560#ifdef FEAT_CLIENTSERVER
10561 "clientserver",
10562#endif
10563#ifdef FEAT_CLIPBOARD
10564 "clipboard",
10565#endif
10566#ifdef FEAT_CMDL_COMPL
10567 "cmdline_compl",
10568#endif
10569#ifdef FEAT_CMDHIST
10570 "cmdline_hist",
10571#endif
10572#ifdef FEAT_COMMENTS
10573 "comments",
10574#endif
10575#ifdef FEAT_CRYPT
10576 "cryptv",
10577#endif
10578#ifdef FEAT_CSCOPE
10579 "cscope",
10580#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010581#ifdef CURSOR_SHAPE
10582 "cursorshape",
10583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584#ifdef DEBUG
10585 "debug",
10586#endif
10587#ifdef FEAT_CON_DIALOG
10588 "dialog_con",
10589#endif
10590#ifdef FEAT_GUI_DIALOG
10591 "dialog_gui",
10592#endif
10593#ifdef FEAT_DIFF
10594 "diff",
10595#endif
10596#ifdef FEAT_DIGRAPHS
10597 "digraphs",
10598#endif
10599#ifdef FEAT_DND
10600 "dnd",
10601#endif
10602#ifdef FEAT_EMACS_TAGS
10603 "emacs_tags",
10604#endif
10605 "eval", /* always present, of course! */
10606#ifdef FEAT_EX_EXTRA
10607 "ex_extra",
10608#endif
10609#ifdef FEAT_SEARCH_EXTRA
10610 "extra_search",
10611#endif
10612#ifdef FEAT_FKMAP
10613 "farsi",
10614#endif
10615#ifdef FEAT_SEARCHPATH
10616 "file_in_path",
10617#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010618#if defined(UNIX) && !defined(USE_SYSTEM)
10619 "filterpipe",
10620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621#ifdef FEAT_FIND_ID
10622 "find_in_path",
10623#endif
10624#ifdef FEAT_FOLDING
10625 "folding",
10626#endif
10627#ifdef FEAT_FOOTER
10628 "footer",
10629#endif
10630#if !defined(USE_SYSTEM) && defined(UNIX)
10631 "fork",
10632#endif
10633#ifdef FEAT_GETTEXT
10634 "gettext",
10635#endif
10636#ifdef FEAT_GUI
10637 "gui",
10638#endif
10639#ifdef FEAT_GUI_ATHENA
10640# ifdef FEAT_GUI_NEXTAW
10641 "gui_neXtaw",
10642# else
10643 "gui_athena",
10644# endif
10645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646#ifdef FEAT_GUI_GTK
10647 "gui_gtk",
10648# ifdef HAVE_GTK2
10649 "gui_gtk2",
10650# endif
10651#endif
10652#ifdef FEAT_GUI_MAC
10653 "gui_mac",
10654#endif
10655#ifdef FEAT_GUI_MOTIF
10656 "gui_motif",
10657#endif
10658#ifdef FEAT_GUI_PHOTON
10659 "gui_photon",
10660#endif
10661#ifdef FEAT_GUI_W16
10662 "gui_win16",
10663#endif
10664#ifdef FEAT_GUI_W32
10665 "gui_win32",
10666#endif
10667#ifdef FEAT_HANGULIN
10668 "hangul_input",
10669#endif
10670#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10671 "iconv",
10672#endif
10673#ifdef FEAT_INS_EXPAND
10674 "insert_expand",
10675#endif
10676#ifdef FEAT_JUMPLIST
10677 "jumplist",
10678#endif
10679#ifdef FEAT_KEYMAP
10680 "keymap",
10681#endif
10682#ifdef FEAT_LANGMAP
10683 "langmap",
10684#endif
10685#ifdef FEAT_LIBCALL
10686 "libcall",
10687#endif
10688#ifdef FEAT_LINEBREAK
10689 "linebreak",
10690#endif
10691#ifdef FEAT_LISP
10692 "lispindent",
10693#endif
10694#ifdef FEAT_LISTCMDS
10695 "listcmds",
10696#endif
10697#ifdef FEAT_LOCALMAP
10698 "localmap",
10699#endif
10700#ifdef FEAT_MENU
10701 "menu",
10702#endif
10703#ifdef FEAT_SESSION
10704 "mksession",
10705#endif
10706#ifdef FEAT_MODIFY_FNAME
10707 "modify_fname",
10708#endif
10709#ifdef FEAT_MOUSE
10710 "mouse",
10711#endif
10712#ifdef FEAT_MOUSESHAPE
10713 "mouseshape",
10714#endif
10715#if defined(UNIX) || defined(VMS)
10716# ifdef FEAT_MOUSE_DEC
10717 "mouse_dec",
10718# endif
10719# ifdef FEAT_MOUSE_GPM
10720 "mouse_gpm",
10721# endif
10722# ifdef FEAT_MOUSE_JSB
10723 "mouse_jsbterm",
10724# endif
10725# ifdef FEAT_MOUSE_NET
10726 "mouse_netterm",
10727# endif
10728# ifdef FEAT_MOUSE_PTERM
10729 "mouse_pterm",
10730# endif
10731# ifdef FEAT_MOUSE_XTERM
10732 "mouse_xterm",
10733# endif
10734#endif
10735#ifdef FEAT_MBYTE
10736 "multi_byte",
10737#endif
10738#ifdef FEAT_MBYTE_IME
10739 "multi_byte_ime",
10740#endif
10741#ifdef FEAT_MULTI_LANG
10742 "multi_lang",
10743#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010744#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010745#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010746 "mzscheme",
10747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749#ifdef FEAT_OLE
10750 "ole",
10751#endif
10752#ifdef FEAT_OSFILETYPE
10753 "osfiletype",
10754#endif
10755#ifdef FEAT_PATH_EXTRA
10756 "path_extra",
10757#endif
10758#ifdef FEAT_PERL
10759#ifndef DYNAMIC_PERL
10760 "perl",
10761#endif
10762#endif
10763#ifdef FEAT_PYTHON
10764#ifndef DYNAMIC_PYTHON
10765 "python",
10766#endif
10767#endif
10768#ifdef FEAT_POSTSCRIPT
10769 "postscript",
10770#endif
10771#ifdef FEAT_PRINTER
10772 "printer",
10773#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010774#ifdef FEAT_PROFILE
10775 "profile",
10776#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010777#ifdef FEAT_RELTIME
10778 "reltime",
10779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780#ifdef FEAT_QUICKFIX
10781 "quickfix",
10782#endif
10783#ifdef FEAT_RIGHTLEFT
10784 "rightleft",
10785#endif
10786#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10787 "ruby",
10788#endif
10789#ifdef FEAT_SCROLLBIND
10790 "scrollbind",
10791#endif
10792#ifdef FEAT_CMDL_INFO
10793 "showcmd",
10794 "cmdline_info",
10795#endif
10796#ifdef FEAT_SIGNS
10797 "signs",
10798#endif
10799#ifdef FEAT_SMARTINDENT
10800 "smartindent",
10801#endif
10802#ifdef FEAT_SNIFF
10803 "sniff",
10804#endif
10805#ifdef FEAT_STL_OPT
10806 "statusline",
10807#endif
10808#ifdef FEAT_SUN_WORKSHOP
10809 "sun_workshop",
10810#endif
10811#ifdef FEAT_NETBEANS_INTG
10812 "netbeans_intg",
10813#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010814#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010815 "spell",
10816#endif
10817#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 "syntax",
10819#endif
10820#if defined(USE_SYSTEM) || !defined(UNIX)
10821 "system",
10822#endif
10823#ifdef FEAT_TAG_BINS
10824 "tag_binary",
10825#endif
10826#ifdef FEAT_TAG_OLDSTATIC
10827 "tag_old_static",
10828#endif
10829#ifdef FEAT_TAG_ANYWHITE
10830 "tag_any_white",
10831#endif
10832#ifdef FEAT_TCL
10833# ifndef DYNAMIC_TCL
10834 "tcl",
10835# endif
10836#endif
10837#ifdef TERMINFO
10838 "terminfo",
10839#endif
10840#ifdef FEAT_TERMRESPONSE
10841 "termresponse",
10842#endif
10843#ifdef FEAT_TEXTOBJ
10844 "textobjects",
10845#endif
10846#ifdef HAVE_TGETENT
10847 "tgetent",
10848#endif
10849#ifdef FEAT_TITLE
10850 "title",
10851#endif
10852#ifdef FEAT_TOOLBAR
10853 "toolbar",
10854#endif
10855#ifdef FEAT_USR_CMDS
10856 "user-commands", /* was accidentally included in 5.4 */
10857 "user_commands",
10858#endif
10859#ifdef FEAT_VIMINFO
10860 "viminfo",
10861#endif
10862#ifdef FEAT_VERTSPLIT
10863 "vertsplit",
10864#endif
10865#ifdef FEAT_VIRTUALEDIT
10866 "virtualedit",
10867#endif
10868#ifdef FEAT_VISUAL
10869 "visual",
10870#endif
10871#ifdef FEAT_VISUALEXTRA
10872 "visualextra",
10873#endif
10874#ifdef FEAT_VREPLACE
10875 "vreplace",
10876#endif
10877#ifdef FEAT_WILDIGN
10878 "wildignore",
10879#endif
10880#ifdef FEAT_WILDMENU
10881 "wildmenu",
10882#endif
10883#ifdef FEAT_WINDOWS
10884 "windows",
10885#endif
10886#ifdef FEAT_WAK
10887 "winaltkeys",
10888#endif
10889#ifdef FEAT_WRITEBACKUP
10890 "writebackup",
10891#endif
10892#ifdef FEAT_XIM
10893 "xim",
10894#endif
10895#ifdef FEAT_XFONTSET
10896 "xfontset",
10897#endif
10898#ifdef USE_XSMP
10899 "xsmp",
10900#endif
10901#ifdef USE_XSMP_INTERACT
10902 "xsmp_interact",
10903#endif
10904#ifdef FEAT_XCLIPBOARD
10905 "xterm_clipboard",
10906#endif
10907#ifdef FEAT_XTERM_SAVE
10908 "xterm_save",
10909#endif
10910#if defined(UNIX) && defined(FEAT_X11)
10911 "X11",
10912#endif
10913 NULL
10914 };
10915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 for (i = 0; has_list[i] != NULL; ++i)
10918 if (STRICMP(name, has_list[i]) == 0)
10919 {
10920 n = TRUE;
10921 break;
10922 }
10923
10924 if (n == FALSE)
10925 {
10926 if (STRNICMP(name, "patch", 5) == 0)
10927 n = has_patch(atoi((char *)name + 5));
10928 else if (STRICMP(name, "vim_starting") == 0)
10929 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010930#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10931 else if (STRICMP(name, "balloon_multiline") == 0)
10932 n = multiline_balloon_available();
10933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#ifdef DYNAMIC_TCL
10935 else if (STRICMP(name, "tcl") == 0)
10936 n = tcl_enabled(FALSE);
10937#endif
10938#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10939 else if (STRICMP(name, "iconv") == 0)
10940 n = iconv_enabled(FALSE);
10941#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010942#ifdef DYNAMIC_MZSCHEME
10943 else if (STRICMP(name, "mzscheme") == 0)
10944 n = mzscheme_enabled(FALSE);
10945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946#ifdef DYNAMIC_RUBY
10947 else if (STRICMP(name, "ruby") == 0)
10948 n = ruby_enabled(FALSE);
10949#endif
10950#ifdef DYNAMIC_PYTHON
10951 else if (STRICMP(name, "python") == 0)
10952 n = python_enabled(FALSE);
10953#endif
10954#ifdef DYNAMIC_PERL
10955 else if (STRICMP(name, "perl") == 0)
10956 n = perl_enabled(FALSE);
10957#endif
10958#ifdef FEAT_GUI
10959 else if (STRICMP(name, "gui_running") == 0)
10960 n = (gui.in_use || gui.starting);
10961# ifdef FEAT_GUI_W32
10962 else if (STRICMP(name, "gui_win32s") == 0)
10963 n = gui_is_win32s();
10964# endif
10965# ifdef FEAT_BROWSE
10966 else if (STRICMP(name, "browse") == 0)
10967 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10968# endif
10969#endif
10970#ifdef FEAT_SYN_HL
10971 else if (STRICMP(name, "syntax_items") == 0)
10972 n = syntax_present(curbuf);
10973#endif
10974#if defined(WIN3264)
10975 else if (STRICMP(name, "win95") == 0)
10976 n = mch_windows95();
10977#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010978#ifdef FEAT_NETBEANS_INTG
10979 else if (STRICMP(name, "netbeans_enabled") == 0)
10980 n = usingNetbeans;
10981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 }
10983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010988 * "has_key()" function
10989 */
10990 static void
10991f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010992 typval_T *argvars;
10993 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010994{
10995 rettv->vval.v_number = 0;
10996 if (argvars[0].v_type != VAR_DICT)
10997 {
10998 EMSG(_(e_dictreq));
10999 return;
11000 }
11001 if (argvars[0].vval.v_dict == NULL)
11002 return;
11003
11004 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011005 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006}
11007
11008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 * "hasmapto()" function
11010 */
11011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011013 typval_T *argvars;
11014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015{
11016 char_u *name;
11017 char_u *mode;
11018 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011019 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011022 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 mode = (char_u *)"nvo";
11024 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011027 if (argvars[2].v_type != VAR_UNKNOWN)
11028 abbr = get_tv_number(&argvars[2]);
11029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030
Bram Moolenaar2c932302006-03-18 21:42:09 +000011031 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011032 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035}
11036
11037/*
11038 * "histadd()" function
11039 */
11040/*ARGSUSED*/
11041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011043 typval_T *argvars;
11044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045{
11046#ifdef FEAT_CMDHIST
11047 int histype;
11048 char_u *str;
11049 char_u buf[NUMBUFLEN];
11050#endif
11051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 if (check_restricted() || check_secure())
11054 return;
11055#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11057 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 if (histype >= 0)
11059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 if (*str != NUL)
11062 {
11063 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 return;
11066 }
11067 }
11068#endif
11069}
11070
11071/*
11072 * "histdel()" function
11073 */
11074/*ARGSUSED*/
11075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011077 typval_T *argvars;
11078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079{
11080#ifdef FEAT_CMDHIST
11081 int n;
11082 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011083 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11086 if (str == NULL)
11087 n = 0;
11088 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011091 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011093 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 else
11096 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 get_tv_string_buf(&argvars[1], buf));
11099 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102#endif
11103}
11104
11105/*
11106 * "histget()" function
11107 */
11108/*ARGSUSED*/
11109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011111 typval_T *argvars;
11112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113{
11114#ifdef FEAT_CMDHIST
11115 int type;
11116 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11120 if (str == NULL)
11121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011123 {
11124 type = get_histtype(str);
11125 if (argvars[1].v_type == VAR_UNKNOWN)
11126 idx = get_history_idx(type);
11127 else
11128 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11129 /* -1 on type error */
11130 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136}
11137
11138/*
11139 * "histnr()" function
11140 */
11141/*ARGSUSED*/
11142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011144 typval_T *argvars;
11145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
11147 int i;
11148
11149#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 char_u *history = get_tv_string_chk(&argvars[0]);
11151
11152 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 if (i >= HIST_CMD && i < HIST_COUNT)
11154 i = get_history_idx(i);
11155 else
11156#endif
11157 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159}
11160
11161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162 * "highlightID(name)" function
11163 */
11164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 typval_T *argvars;
11167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170}
11171
11172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173 * "highlight_exists()" function
11174 */
11175 static void
11176f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011177 typval_T *argvars;
11178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011179{
11180 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11181}
11182
11183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 * "hostname()" function
11185 */
11186/*ARGSUSED*/
11187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011189 typval_T *argvars;
11190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191{
11192 char_u hostname[256];
11193
11194 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->v_type = VAR_STRING;
11196 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197}
11198
11199/*
11200 * iconv() function
11201 */
11202/*ARGSUSED*/
11203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011204f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011205 typval_T *argvars;
11206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
11208#ifdef FEAT_MBYTE
11209 char_u buf1[NUMBUFLEN];
11210 char_u buf2[NUMBUFLEN];
11211 char_u *from, *to, *str;
11212 vimconv_T vimconv;
11213#endif
11214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->v_type = VAR_STRING;
11216 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217
11218#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 str = get_tv_string(&argvars[0]);
11220 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11221 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 vimconv.vc_type = CONV_NONE;
11223 convert_setup(&vimconv, from, to);
11224
11225 /* If the encodings are equal, no conversion needed. */
11226 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011227 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230
11231 convert_setup(&vimconv, NULL, NULL);
11232 vim_free(from);
11233 vim_free(to);
11234#endif
11235}
11236
11237/*
11238 * "indent()" function
11239 */
11240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011242 typval_T *argvars;
11243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
11245 linenr_T lnum;
11246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011249 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011254/*
11255 * "index()" function
11256 */
11257 static void
11258f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011259 typval_T *argvars;
11260 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011261{
Bram Moolenaar33570922005-01-25 22:26:29 +000011262 list_T *l;
11263 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011264 long idx = 0;
11265 int ic = FALSE;
11266
11267 rettv->vval.v_number = -1;
11268 if (argvars[0].v_type != VAR_LIST)
11269 {
11270 EMSG(_(e_listreq));
11271 return;
11272 }
11273 l = argvars[0].vval.v_list;
11274 if (l != NULL)
11275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011276 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011277 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 int error = FALSE;
11280
Bram Moolenaar758711c2005-02-02 23:11:38 +000011281 /* Start at specified item. Use the cached index that list_find()
11282 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011283 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011284 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011285 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011286 ic = get_tv_number_chk(&argvars[3], &error);
11287 if (error)
11288 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011289 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011290
Bram Moolenaar758711c2005-02-02 23:11:38 +000011291 for ( ; item != NULL; item = item->li_next, ++idx)
11292 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011293 {
11294 rettv->vval.v_number = idx;
11295 break;
11296 }
11297 }
11298}
11299
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300static int inputsecret_flag = 0;
11301
11302/*
11303 * "input()" function
11304 * Also handles inputsecret() when inputsecret is set.
11305 */
11306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011308 typval_T *argvars;
11309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 char_u *p = NULL;
11313 int c;
11314 char_u buf[NUMBUFLEN];
11315 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011317 int xp_type = EXPAND_NOTHING;
11318 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321
11322#ifdef NO_CONSOLE_INPUT
11323 /* While starting up, there is no place to enter text. */
11324 if (no_console_input())
11325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 return;
11328 }
11329#endif
11330
11331 cmd_silent = FALSE; /* Want to see the prompt. */
11332 if (prompt != NULL)
11333 {
11334 /* Only the part of the message after the last NL is considered as
11335 * prompt for the command line */
11336 p = vim_strrchr(prompt, '\n');
11337 if (p == NULL)
11338 p = prompt;
11339 else
11340 {
11341 ++p;
11342 c = *p;
11343 *p = NUL;
11344 msg_start();
11345 msg_clr_eos();
11346 msg_puts_attr(prompt, echo_attr);
11347 msg_didout = FALSE;
11348 msg_starthere();
11349 *p = c;
11350 }
11351 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011353 if (argvars[1].v_type != VAR_UNKNOWN)
11354 {
11355 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11356 if (defstr != NULL)
11357 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358
Bram Moolenaar4463f292005-09-25 22:20:24 +000011359 if (argvars[2].v_type != VAR_UNKNOWN)
11360 {
11361 char_u *xp_name;
11362 int xp_namelen;
11363 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011364
Bram Moolenaar4463f292005-09-25 22:20:24 +000011365 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011366
Bram Moolenaar4463f292005-09-25 22:20:24 +000011367 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11368 if (xp_name == NULL)
11369 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011370
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011371 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011372
Bram Moolenaar4463f292005-09-25 22:20:24 +000011373 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11374 &xp_arg) == FAIL)
11375 return;
11376 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011377 }
11378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 if (defstr != NULL)
11380 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011381 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11382 xp_type, xp_arg);
11383
11384 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011386 /* since the user typed this, no need to wait for return */
11387 need_wait_return = FALSE;
11388 msg_didout = FALSE;
11389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 cmd_silent = cmd_silent_save;
11391}
11392
11393/*
11394 * "inputdialog()" function
11395 */
11396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011398 typval_T *argvars;
11399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
11401#if defined(FEAT_GUI_TEXTDIALOG)
11402 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11403 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11404 {
11405 char_u *message;
11406 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011409 message = get_tv_string_chk(&argvars[0]);
11410 if (argvars[1].v_type != VAR_UNKNOWN
11411 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011412 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 else
11414 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 if (message != NULL && defstr != NULL
11416 && do_dialog(VIM_QUESTION, NULL, message,
11417 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 else
11420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 if (message != NULL && defstr != NULL
11422 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011423 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 rettv->vval.v_string = vim_strsave(
11425 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 }
11431 else
11432#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434}
11435
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011436/*
11437 * "inputlist()" function
11438 */
11439 static void
11440f_inputlist(argvars, rettv)
11441 typval_T *argvars;
11442 typval_T *rettv;
11443{
11444 listitem_T *li;
11445 int selected;
11446 int mouse_used;
11447
11448 rettv->vval.v_number = 0;
11449#ifdef NO_CONSOLE_INPUT
11450 /* While starting up, there is no place to enter text. */
11451 if (no_console_input())
11452 return;
11453#endif
11454 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11455 {
11456 EMSG2(_(e_listarg), "inputlist()");
11457 return;
11458 }
11459
11460 msg_start();
11461 lines_left = Rows; /* avoid more prompt */
11462 msg_scroll = TRUE;
11463 msg_clr_eos();
11464
11465 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11466 {
11467 msg_puts(get_tv_string(&li->li_tv));
11468 msg_putchar('\n');
11469 }
11470
11471 /* Ask for choice. */
11472 selected = prompt_for_number(&mouse_used);
11473 if (mouse_used)
11474 selected -= lines_left;
11475
11476 rettv->vval.v_number = selected;
11477}
11478
11479
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11481
11482/*
11483 * "inputrestore()" function
11484 */
11485/*ARGSUSED*/
11486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 typval_T *argvars;
11489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490{
11491 if (ga_userinput.ga_len > 0)
11492 {
11493 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11495 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 }
11498 else if (p_verbose > 1)
11499 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011500 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 }
11503}
11504
11505/*
11506 * "inputsave()" function
11507 */
11508/*ARGSUSED*/
11509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *argvars;
11512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
11514 /* Add an entry to the stack of typehead storage. */
11515 if (ga_grow(&ga_userinput, 1) == OK)
11516 {
11517 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11518 + ga_userinput.ga_len);
11519 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 }
11522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524}
11525
11526/*
11527 * "inputsecret()" function
11528 */
11529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 typval_T *argvars;
11532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533{
11534 ++cmdline_star;
11535 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537 --cmdline_star;
11538 --inputsecret_flag;
11539}
11540
11541/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011542 * "insert()" function
11543 */
11544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011546 typval_T *argvars;
11547 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011548{
11549 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011550 listitem_T *item;
11551 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011552 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011553
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011554 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011555 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011556 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011557 else if ((l = argvars[0].vval.v_list) != NULL
11558 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011559 {
11560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011561 before = get_tv_number_chk(&argvars[2], &error);
11562 if (error)
11563 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011564
Bram Moolenaar758711c2005-02-02 23:11:38 +000011565 if (before == l->lv_len)
11566 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011567 else
11568 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011569 item = list_find(l, before);
11570 if (item == NULL)
11571 {
11572 EMSGN(_(e_listidx), before);
11573 l = NULL;
11574 }
11575 }
11576 if (l != NULL)
11577 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011578 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011579 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011580 }
11581 }
11582}
11583
11584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 * "isdirectory()" function
11586 */
11587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011589 typval_T *argvars;
11590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593}
11594
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011595/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011596 * "islocked()" function
11597 */
11598 static void
11599f_islocked(argvars, rettv)
11600 typval_T *argvars;
11601 typval_T *rettv;
11602{
11603 lval_T lv;
11604 char_u *end;
11605 dictitem_T *di;
11606
11607 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011608 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11609 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011610 if (end != NULL && lv.ll_name != NULL)
11611 {
11612 if (*end != NUL)
11613 EMSG(_(e_trailing));
11614 else
11615 {
11616 if (lv.ll_tv == NULL)
11617 {
11618 if (check_changedtick(lv.ll_name))
11619 rettv->vval.v_number = 1; /* always locked */
11620 else
11621 {
11622 di = find_var(lv.ll_name, NULL);
11623 if (di != NULL)
11624 {
11625 /* Consider a variable locked when:
11626 * 1. the variable itself is locked
11627 * 2. the value of the variable is locked.
11628 * 3. the List or Dict value is locked.
11629 */
11630 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11631 || tv_islocked(&di->di_tv));
11632 }
11633 }
11634 }
11635 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011636 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011637 else if (lv.ll_newkey != NULL)
11638 EMSG2(_(e_dictkey), lv.ll_newkey);
11639 else if (lv.ll_list != NULL)
11640 /* List item. */
11641 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11642 else
11643 /* Dictionary item. */
11644 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11645 }
11646 }
11647
11648 clear_lval(&lv);
11649}
11650
Bram Moolenaar33570922005-01-25 22:26:29 +000011651static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011652
11653/*
11654 * Turn a dict into a list:
11655 * "what" == 0: list of keys
11656 * "what" == 1: list of values
11657 * "what" == 2: list of items
11658 */
11659 static void
11660dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011661 typval_T *argvars;
11662 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011663 int what;
11664{
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 list_T *l2;
11666 dictitem_T *di;
11667 hashitem_T *hi;
11668 listitem_T *li;
11669 listitem_T *li2;
11670 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011671 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011672
11673 rettv->vval.v_number = 0;
11674 if (argvars[0].v_type != VAR_DICT)
11675 {
11676 EMSG(_(e_dictreq));
11677 return;
11678 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011679 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011680 return;
11681
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011682 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011683 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011684
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011685 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011688 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011689 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011690 --todo;
11691 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011692
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011693 li = listitem_alloc();
11694 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011695 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011696 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011697
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011698 if (what == 0)
11699 {
11700 /* keys() */
11701 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011702 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011703 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11704 }
11705 else if (what == 1)
11706 {
11707 /* values() */
11708 copy_tv(&di->di_tv, &li->li_tv);
11709 }
11710 else
11711 {
11712 /* items() */
11713 l2 = list_alloc();
11714 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011715 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011716 li->li_tv.vval.v_list = l2;
11717 if (l2 == NULL)
11718 break;
11719 ++l2->lv_refcount;
11720
11721 li2 = listitem_alloc();
11722 if (li2 == NULL)
11723 break;
11724 list_append(l2, li2);
11725 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011726 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011727 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11728
11729 li2 = listitem_alloc();
11730 if (li2 == NULL)
11731 break;
11732 list_append(l2, li2);
11733 copy_tv(&di->di_tv, &li2->li_tv);
11734 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011735 }
11736 }
11737}
11738
11739/*
11740 * "items(dict)" function
11741 */
11742 static void
11743f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 typval_T *argvars;
11745 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011746{
11747 dict_list(argvars, rettv, 2);
11748}
11749
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011751 * "join()" function
11752 */
11753 static void
11754f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011755 typval_T *argvars;
11756 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011757{
11758 garray_T ga;
11759 char_u *sep;
11760
11761 rettv->vval.v_number = 0;
11762 if (argvars[0].v_type != VAR_LIST)
11763 {
11764 EMSG(_(e_listreq));
11765 return;
11766 }
11767 if (argvars[0].vval.v_list == NULL)
11768 return;
11769 if (argvars[1].v_type == VAR_UNKNOWN)
11770 sep = (char_u *)" ";
11771 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011772 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011773
11774 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011775
11776 if (sep != NULL)
11777 {
11778 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011779 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780 ga_append(&ga, NUL);
11781 rettv->vval.v_string = (char_u *)ga.ga_data;
11782 }
11783 else
11784 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011785}
11786
11787/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011788 * "keys()" function
11789 */
11790 static void
11791f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011792 typval_T *argvars;
11793 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011794{
11795 dict_list(argvars, rettv, 0);
11796}
11797
11798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 * "last_buffer_nr()" function.
11800 */
11801/*ARGSUSED*/
11802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806{
11807 int n = 0;
11808 buf_T *buf;
11809
11810 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11811 if (n < buf->b_fnum)
11812 n = buf->b_fnum;
11813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011815}
11816
11817/*
11818 * "len()" function
11819 */
11820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011822 typval_T *argvars;
11823 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011824{
11825 switch (argvars[0].v_type)
11826 {
11827 case VAR_STRING:
11828 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = (varnumber_T)STRLEN(
11830 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011831 break;
11832 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011834 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011835 case VAR_DICT:
11836 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11837 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011838 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011839 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011840 break;
11841 }
11842}
11843
Bram Moolenaar33570922005-01-25 22:26:29 +000011844static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011845
11846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011847libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011848 typval_T *argvars;
11849 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011850 int type;
11851{
11852#ifdef FEAT_LIBCALL
11853 char_u *string_in;
11854 char_u **string_result;
11855 int nr_result;
11856#endif
11857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011859 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011863
11864 if (check_restricted() || check_secure())
11865 return;
11866
11867#ifdef FEAT_LIBCALL
11868 /* The first two args must be strings, otherwise its meaningless */
11869 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11870 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011871 string_in = NULL;
11872 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011873 string_in = argvars[2].vval.v_string;
11874 if (type == VAR_NUMBER)
11875 string_result = NULL;
11876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011878 if (mch_libcall(argvars[0].vval.v_string,
11879 argvars[1].vval.v_string,
11880 string_in,
11881 argvars[2].vval.v_number,
11882 string_result,
11883 &nr_result) == OK
11884 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011886 }
11887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888}
11889
11890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891 * "libcall()" function
11892 */
11893 static void
11894f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011895 typval_T *argvars;
11896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897{
11898 libcall_common(argvars, rettv, VAR_STRING);
11899}
11900
11901/*
11902 * "libcallnr()" function
11903 */
11904 static void
11905f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011906 typval_T *argvars;
11907 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011908{
11909 libcall_common(argvars, rettv, VAR_NUMBER);
11910}
11911
11912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 * "line(string)" function
11914 */
11915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011917 typval_T *argvars;
11918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919{
11920 linenr_T lnum = 0;
11921 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011922 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011924 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 if (fp != NULL)
11926 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928}
11929
11930/*
11931 * "line2byte(lnum)" function
11932 */
11933/*ARGSUSED*/
11934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011936 typval_T *argvars;
11937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938{
11939#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941#else
11942 linenr_T lnum;
11943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011944 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011945 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11949 if (rettv->vval.v_number >= 0)
11950 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951#endif
11952}
11953
11954/*
11955 * "lispindent(lnum)" function
11956 */
11957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 typval_T *argvars;
11960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961{
11962#ifdef FEAT_LISP
11963 pos_T pos;
11964 linenr_T lnum;
11965
11966 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11969 {
11970 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011971 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972 curwin->w_cursor = pos;
11973 }
11974 else
11975#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977}
11978
11979/*
11980 * "localtime()" function
11981 */
11982/*ARGSUSED*/
11983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 typval_T *argvars;
11986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989}
11990
Bram Moolenaar33570922005-01-25 22:26:29 +000011991static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992
11993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011995 typval_T *argvars;
11996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 int exact;
11998{
11999 char_u *keys;
12000 char_u *which;
12001 char_u buf[NUMBUFLEN];
12002 char_u *keys_buf = NULL;
12003 char_u *rhs;
12004 int mode;
12005 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012006 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007
12008 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 rettv->v_type = VAR_STRING;
12010 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012012 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 if (*keys == NUL)
12014 return;
12015
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012016 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012019 if (argvars[2].v_type != VAR_UNKNOWN)
12020 abbr = get_tv_number(&argvars[2]);
12021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 else
12023 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 if (which == NULL)
12025 return;
12026
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 mode = get_map_mode(&which, 0);
12028
12029 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012030 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 vim_free(keys_buf);
12032 if (rhs != NULL)
12033 {
12034 ga_init(&ga);
12035 ga.ga_itemsize = 1;
12036 ga.ga_growsize = 40;
12037
12038 while (*rhs != NUL)
12039 ga_concat(&ga, str2special(&rhs, FALSE));
12040
12041 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 }
12044}
12045
12046/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012047 * "map()" function
12048 */
12049 static void
12050f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012051 typval_T *argvars;
12052 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012053{
12054 filter_map(argvars, rettv, TRUE);
12055}
12056
12057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012058 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 */
12060 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012061f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 typval_T *argvars;
12063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012065 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066}
12067
12068/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012069 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 */
12071 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012072f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012073 typval_T *argvars;
12074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012076 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077}
12078
Bram Moolenaar33570922005-01-25 22:26:29 +000012079static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080
12081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012083 typval_T *argvars;
12084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 int type;
12086{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012087 char_u *str = NULL;
12088 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 char_u *pat;
12090 regmatch_T regmatch;
12091 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012092 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 char_u *save_cpo;
12094 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012095 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012096 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012097 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012098 list_T *l = NULL;
12099 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012100 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012101 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102
12103 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12104 save_cpo = p_cpo;
12105 p_cpo = (char_u *)"";
12106
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012107 rettv->vval.v_number = -1;
12108 if (type == 3)
12109 {
12110 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012111 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012112 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012113 }
12114 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116 rettv->v_type = VAR_STRING;
12117 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012120 if (argvars[0].v_type == VAR_LIST)
12121 {
12122 if ((l = argvars[0].vval.v_list) == NULL)
12123 goto theend;
12124 li = l->lv_first;
12125 }
12126 else
12127 expr = str = get_tv_string(&argvars[0]);
12128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012129 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12130 if (pat == NULL)
12131 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012132
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012135 int error = FALSE;
12136
12137 start = get_tv_number_chk(&argvars[2], &error);
12138 if (error)
12139 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012140 if (l != NULL)
12141 {
12142 li = list_find(l, start);
12143 if (li == NULL)
12144 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012145 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012146 }
12147 else
12148 {
12149 if (start < 0)
12150 start = 0;
12151 if (start > (long)STRLEN(str))
12152 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012153 /* When "count" argument is there ignore matches before "start",
12154 * otherwise skip part of the string. Differs when pattern is "^"
12155 * or "\<". */
12156 if (argvars[3].v_type != VAR_UNKNOWN)
12157 startcol = start;
12158 else
12159 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012162 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012163 nth = get_tv_number_chk(&argvars[3], &error);
12164 if (error)
12165 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 }
12167
12168 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12169 if (regmatch.regprog != NULL)
12170 {
12171 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012172
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012173 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012174 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012175 if (l != NULL)
12176 {
12177 if (li == NULL)
12178 {
12179 match = FALSE;
12180 break;
12181 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012182 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012183 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012184 if (str == NULL)
12185 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012186 }
12187
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012188 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012189
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012190 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012191 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012192 if (l == NULL && !match)
12193 break;
12194
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012195 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012196 if (l != NULL)
12197 {
12198 li = li->li_next;
12199 ++idx;
12200 }
12201 else
12202 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012203#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012204 startcol = (colnr_T)(regmatch.startp[0]
12205 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012206#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012207 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012208#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012209 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012210 }
12211
12212 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012214 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012216 int i;
12217
12218 /* return list with matched string and submatches */
12219 for (i = 0; i < NSUBEXP; ++i)
12220 {
12221 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012222 {
12223 if (list_append_string(rettv->vval.v_list,
12224 (char_u *)"", 0) == FAIL)
12225 break;
12226 }
12227 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012228 regmatch.startp[i],
12229 (int)(regmatch.endp[i] - regmatch.startp[i]))
12230 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012231 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012232 }
12233 }
12234 else if (type == 2)
12235 {
12236 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012237 if (l != NULL)
12238 copy_tv(&li->li_tv, rettv);
12239 else
12240 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012242 }
12243 else if (l != NULL)
12244 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245 else
12246 {
12247 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012248 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 (varnumber_T)(regmatch.startp[0] - str);
12250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012251 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012253 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254 }
12255 }
12256 vim_free(regmatch.regprog);
12257 }
12258
12259theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012260 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 p_cpo = save_cpo;
12262}
12263
12264/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012265 * "match()" function
12266 */
12267 static void
12268f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012269 typval_T *argvars;
12270 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012271{
12272 find_some_match(argvars, rettv, 1);
12273}
12274
12275/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012276 * "matcharg()" function
12277 */
12278 static void
12279f_matcharg(argvars, rettv)
12280 typval_T *argvars;
12281 typval_T *rettv;
12282{
12283 if (rettv_list_alloc(rettv) == OK)
12284 {
12285#ifdef FEAT_SEARCH_EXTRA
12286 int mi = get_tv_number(&argvars[0]);
12287
12288 if (mi >= 1 && mi <= 3)
12289 {
12290 list_append_string(rettv->vval.v_list,
12291 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12292 list_append_string(rettv->vval.v_list,
12293 curwin->w_match_pat[mi - 1], -1);
12294 }
12295#endif
12296 }
12297}
12298
12299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300 * "matchend()" function
12301 */
12302 static void
12303f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012304 typval_T *argvars;
12305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012306{
12307 find_some_match(argvars, rettv, 0);
12308}
12309
12310/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012311 * "matchlist()" function
12312 */
12313 static void
12314f_matchlist(argvars, rettv)
12315 typval_T *argvars;
12316 typval_T *rettv;
12317{
12318 find_some_match(argvars, rettv, 3);
12319}
12320
12321/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012322 * "matchstr()" function
12323 */
12324 static void
12325f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012326 typval_T *argvars;
12327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012328{
12329 find_some_match(argvars, rettv, 2);
12330}
12331
Bram Moolenaar33570922005-01-25 22:26:29 +000012332static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012333
12334 static void
12335max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012336 typval_T *argvars;
12337 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012338 int domax;
12339{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012340 long n = 0;
12341 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012342 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012343
12344 if (argvars[0].v_type == VAR_LIST)
12345 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 list_T *l;
12347 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012348
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012349 l = argvars[0].vval.v_list;
12350 if (l != NULL)
12351 {
12352 li = l->lv_first;
12353 if (li != NULL)
12354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012355 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012356 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012357 {
12358 li = li->li_next;
12359 if (li == NULL)
12360 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012361 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012362 if (domax ? i > n : i < n)
12363 n = i;
12364 }
12365 }
12366 }
12367 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012368 else if (argvars[0].v_type == VAR_DICT)
12369 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012370 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012371 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012372 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012373 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012374
12375 d = argvars[0].vval.v_dict;
12376 if (d != NULL)
12377 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012378 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012380 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012381 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012382 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012383 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012385 if (first)
12386 {
12387 n = i;
12388 first = FALSE;
12389 }
12390 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012391 n = i;
12392 }
12393 }
12394 }
12395 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012396 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012397 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012398 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012399}
12400
12401/*
12402 * "max()" function
12403 */
12404 static void
12405f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012406 typval_T *argvars;
12407 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012408{
12409 max_min(argvars, rettv, TRUE);
12410}
12411
12412/*
12413 * "min()" function
12414 */
12415 static void
12416f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012417 typval_T *argvars;
12418 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012419{
12420 max_min(argvars, rettv, FALSE);
12421}
12422
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012423static int mkdir_recurse __ARGS((char_u *dir, int prot));
12424
12425/*
12426 * Create the directory in which "dir" is located, and higher levels when
12427 * needed.
12428 */
12429 static int
12430mkdir_recurse(dir, prot)
12431 char_u *dir;
12432 int prot;
12433{
12434 char_u *p;
12435 char_u *updir;
12436 int r = FAIL;
12437
12438 /* Get end of directory name in "dir".
12439 * We're done when it's "/" or "c:/". */
12440 p = gettail_sep(dir);
12441 if (p <= get_past_head(dir))
12442 return OK;
12443
12444 /* If the directory exists we're done. Otherwise: create it.*/
12445 updir = vim_strnsave(dir, (int)(p - dir));
12446 if (updir == NULL)
12447 return FAIL;
12448 if (mch_isdir(updir))
12449 r = OK;
12450 else if (mkdir_recurse(updir, prot) == OK)
12451 r = vim_mkdir_emsg(updir, prot);
12452 vim_free(updir);
12453 return r;
12454}
12455
12456#ifdef vim_mkdir
12457/*
12458 * "mkdir()" function
12459 */
12460 static void
12461f_mkdir(argvars, rettv)
12462 typval_T *argvars;
12463 typval_T *rettv;
12464{
12465 char_u *dir;
12466 char_u buf[NUMBUFLEN];
12467 int prot = 0755;
12468
12469 rettv->vval.v_number = FAIL;
12470 if (check_restricted() || check_secure())
12471 return;
12472
12473 dir = get_tv_string_buf(&argvars[0], buf);
12474 if (argvars[1].v_type != VAR_UNKNOWN)
12475 {
12476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012477 prot = get_tv_number_chk(&argvars[2], NULL);
12478 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012479 mkdir_recurse(dir, prot);
12480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012481 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012482}
12483#endif
12484
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 * "mode()" function
12487 */
12488/*ARGSUSED*/
12489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012491 typval_T *argvars;
12492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493{
12494 char_u buf[2];
12495
12496#ifdef FEAT_VISUAL
12497 if (VIsual_active)
12498 {
12499 if (VIsual_select)
12500 buf[0] = VIsual_mode + 's' - 'v';
12501 else
12502 buf[0] = VIsual_mode;
12503 }
12504 else
12505#endif
12506 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12507 buf[0] = 'r';
12508 else if (State & INSERT)
12509 {
12510 if (State & REPLACE_FLAG)
12511 buf[0] = 'R';
12512 else
12513 buf[0] = 'i';
12514 }
12515 else if (State & CMDLINE)
12516 buf[0] = 'c';
12517 else
12518 buf[0] = 'n';
12519
12520 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 rettv->vval.v_string = vim_strsave(buf);
12522 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523}
12524
12525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526 * "nextnonblank()" function
12527 */
12528 static void
12529f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012530 typval_T *argvars;
12531 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532{
12533 linenr_T lnum;
12534
12535 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538 {
12539 lnum = 0;
12540 break;
12541 }
12542 if (*skipwhite(ml_get(lnum)) != NUL)
12543 break;
12544 }
12545 rettv->vval.v_number = lnum;
12546}
12547
12548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549 * "nr2char()" function
12550 */
12551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012553 typval_T *argvars;
12554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555{
12556 char_u buf[NUMBUFLEN];
12557
12558#ifdef FEAT_MBYTE
12559 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561 else
12562#endif
12563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 buf[1] = NUL;
12566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012567 rettv->v_type = VAR_STRING;
12568 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012569}
12570
12571/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012572 * "pathshorten()" function
12573 */
12574 static void
12575f_pathshorten(argvars, rettv)
12576 typval_T *argvars;
12577 typval_T *rettv;
12578{
12579 char_u *p;
12580
12581 rettv->v_type = VAR_STRING;
12582 p = get_tv_string_chk(&argvars[0]);
12583 if (p == NULL)
12584 rettv->vval.v_string = NULL;
12585 else
12586 {
12587 p = vim_strsave(p);
12588 rettv->vval.v_string = p;
12589 if (p != NULL)
12590 shorten_dir(p);
12591 }
12592}
12593
12594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595 * "prevnonblank()" function
12596 */
12597 static void
12598f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012599 typval_T *argvars;
12600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012601{
12602 linenr_T lnum;
12603
12604 lnum = get_tv_lnum(argvars);
12605 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12606 lnum = 0;
12607 else
12608 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12609 --lnum;
12610 rettv->vval.v_number = lnum;
12611}
12612
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012613#ifdef HAVE_STDARG_H
12614/* This dummy va_list is here because:
12615 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12616 * - locally in the function results in a "used before set" warning
12617 * - using va_start() to initialize it gives "function with fixed args" error */
12618static va_list ap;
12619#endif
12620
Bram Moolenaar8c711452005-01-14 21:53:12 +000012621/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012622 * "printf()" function
12623 */
12624 static void
12625f_printf(argvars, rettv)
12626 typval_T *argvars;
12627 typval_T *rettv;
12628{
12629 rettv->v_type = VAR_STRING;
12630 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012631#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012632 {
12633 char_u buf[NUMBUFLEN];
12634 int len;
12635 char_u *s;
12636 int saved_did_emsg = did_emsg;
12637 char *fmt;
12638
12639 /* Get the required length, allocate the buffer and do it for real. */
12640 did_emsg = FALSE;
12641 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012642 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012643 if (!did_emsg)
12644 {
12645 s = alloc(len + 1);
12646 if (s != NULL)
12647 {
12648 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012649 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012650 }
12651 }
12652 did_emsg |= saved_did_emsg;
12653 }
12654#endif
12655}
12656
12657/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012658 * "pumvisible()" function
12659 */
12660/*ARGSUSED*/
12661 static void
12662f_pumvisible(argvars, rettv)
12663 typval_T *argvars;
12664 typval_T *rettv;
12665{
12666 rettv->vval.v_number = 0;
12667#ifdef FEAT_INS_EXPAND
12668 if (pum_visible())
12669 rettv->vval.v_number = 1;
12670#endif
12671}
12672
12673/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012674 * "range()" function
12675 */
12676 static void
12677f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012678 typval_T *argvars;
12679 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012680{
12681 long start;
12682 long end;
12683 long stride = 1;
12684 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012688 if (argvars[1].v_type == VAR_UNKNOWN)
12689 {
12690 end = start - 1;
12691 start = 0;
12692 }
12693 else
12694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012695 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012698 }
12699
12700 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012701 if (error)
12702 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012703 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012704 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012705 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012706 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012707 else
12708 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012709 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012710 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012711 if (list_append_number(rettv->vval.v_list,
12712 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012713 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012714 }
12715}
12716
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012717/*
12718 * "readfile()" function
12719 */
12720 static void
12721f_readfile(argvars, rettv)
12722 typval_T *argvars;
12723 typval_T *rettv;
12724{
12725 int binary = FALSE;
12726 char_u *fname;
12727 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012728 listitem_T *li;
12729#define FREAD_SIZE 200 /* optimized for text lines */
12730 char_u buf[FREAD_SIZE];
12731 int readlen; /* size of last fread() */
12732 int buflen; /* nr of valid chars in buf[] */
12733 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12734 int tolist; /* first byte in buf[] still to be put in list */
12735 int chop; /* how many CR to chop off */
12736 char_u *prev = NULL; /* previously read bytes, if any */
12737 int prevlen = 0; /* length of "prev" if not NULL */
12738 char_u *s;
12739 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012740 long maxline = MAXLNUM;
12741 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012742
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012743 if (argvars[1].v_type != VAR_UNKNOWN)
12744 {
12745 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12746 binary = TRUE;
12747 if (argvars[2].v_type != VAR_UNKNOWN)
12748 maxline = get_tv_number(&argvars[2]);
12749 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012750
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012751 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012752 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012753
12754 /* Always open the file in binary mode, library functions have a mind of
12755 * their own about CR-LF conversion. */
12756 fname = get_tv_string(&argvars[0]);
12757 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12758 {
12759 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12760 return;
12761 }
12762
12763 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012764 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012765 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012766 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012767 buflen = filtd + readlen;
12768 tolist = 0;
12769 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12770 {
12771 if (buf[filtd] == '\n' || readlen <= 0)
12772 {
12773 /* Only when in binary mode add an empty list item when the
12774 * last line ends in a '\n'. */
12775 if (!binary && readlen == 0 && filtd == 0)
12776 break;
12777
12778 /* Found end-of-line or end-of-file: add a text line to the
12779 * list. */
12780 chop = 0;
12781 if (!binary)
12782 while (filtd - chop - 1 >= tolist
12783 && buf[filtd - chop - 1] == '\r')
12784 ++chop;
12785 len = filtd - tolist - chop;
12786 if (prev == NULL)
12787 s = vim_strnsave(buf + tolist, len);
12788 else
12789 {
12790 s = alloc((unsigned)(prevlen + len + 1));
12791 if (s != NULL)
12792 {
12793 mch_memmove(s, prev, prevlen);
12794 vim_free(prev);
12795 prev = NULL;
12796 mch_memmove(s + prevlen, buf + tolist, len);
12797 s[prevlen + len] = NUL;
12798 }
12799 }
12800 tolist = filtd + 1;
12801
12802 li = listitem_alloc();
12803 if (li == NULL)
12804 {
12805 vim_free(s);
12806 break;
12807 }
12808 li->li_tv.v_type = VAR_STRING;
12809 li->li_tv.v_lock = 0;
12810 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012811 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012812
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012813 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012814 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012815 if (readlen <= 0)
12816 break;
12817 }
12818 else if (buf[filtd] == NUL)
12819 buf[filtd] = '\n';
12820 }
12821 if (readlen <= 0)
12822 break;
12823
12824 if (tolist == 0)
12825 {
12826 /* "buf" is full, need to move text to an allocated buffer */
12827 if (prev == NULL)
12828 {
12829 prev = vim_strnsave(buf, buflen);
12830 prevlen = buflen;
12831 }
12832 else
12833 {
12834 s = alloc((unsigned)(prevlen + buflen));
12835 if (s != NULL)
12836 {
12837 mch_memmove(s, prev, prevlen);
12838 mch_memmove(s + prevlen, buf, buflen);
12839 vim_free(prev);
12840 prev = s;
12841 prevlen += buflen;
12842 }
12843 }
12844 filtd = 0;
12845 }
12846 else
12847 {
12848 mch_memmove(buf, buf + tolist, buflen - tolist);
12849 filtd -= tolist;
12850 }
12851 }
12852
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012853 /*
12854 * For a negative line count use only the lines at the end of the file,
12855 * free the rest.
12856 */
12857 if (maxline < 0)
12858 while (cnt > -maxline)
12859 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012860 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012861 --cnt;
12862 }
12863
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012864 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012865 fclose(fd);
12866}
12867
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012868#if defined(FEAT_RELTIME)
12869static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12870
12871/*
12872 * Convert a List to proftime_T.
12873 * Return FAIL when there is something wrong.
12874 */
12875 static int
12876list2proftime(arg, tm)
12877 typval_T *arg;
12878 proftime_T *tm;
12879{
12880 long n1, n2;
12881 int error = FALSE;
12882
12883 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12884 || arg->vval.v_list->lv_len != 2)
12885 return FAIL;
12886 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12887 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12888# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012889 tm->HighPart = n1;
12890 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012891# else
12892 tm->tv_sec = n1;
12893 tm->tv_usec = n2;
12894# endif
12895 return error ? FAIL : OK;
12896}
12897#endif /* FEAT_RELTIME */
12898
12899/*
12900 * "reltime()" function
12901 */
12902 static void
12903f_reltime(argvars, rettv)
12904 typval_T *argvars;
12905 typval_T *rettv;
12906{
12907#ifdef FEAT_RELTIME
12908 proftime_T res;
12909 proftime_T start;
12910
12911 if (argvars[0].v_type == VAR_UNKNOWN)
12912 {
12913 /* No arguments: get current time. */
12914 profile_start(&res);
12915 }
12916 else if (argvars[1].v_type == VAR_UNKNOWN)
12917 {
12918 if (list2proftime(&argvars[0], &res) == FAIL)
12919 return;
12920 profile_end(&res);
12921 }
12922 else
12923 {
12924 /* Two arguments: compute the difference. */
12925 if (list2proftime(&argvars[0], &start) == FAIL
12926 || list2proftime(&argvars[1], &res) == FAIL)
12927 return;
12928 profile_sub(&res, &start);
12929 }
12930
12931 if (rettv_list_alloc(rettv) == OK)
12932 {
12933 long n1, n2;
12934
12935# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012936 n1 = res.HighPart;
12937 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012938# else
12939 n1 = res.tv_sec;
12940 n2 = res.tv_usec;
12941# endif
12942 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12943 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12944 }
12945#endif
12946}
12947
12948/*
12949 * "reltimestr()" function
12950 */
12951 static void
12952f_reltimestr(argvars, rettv)
12953 typval_T *argvars;
12954 typval_T *rettv;
12955{
12956#ifdef FEAT_RELTIME
12957 proftime_T tm;
12958#endif
12959
12960 rettv->v_type = VAR_STRING;
12961 rettv->vval.v_string = NULL;
12962#ifdef FEAT_RELTIME
12963 if (list2proftime(&argvars[0], &tm) == OK)
12964 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12965#endif
12966}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012967
Bram Moolenaar0d660222005-01-07 21:51:51 +000012968#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12969static void make_connection __ARGS((void));
12970static int check_connection __ARGS((void));
12971
12972 static void
12973make_connection()
12974{
12975 if (X_DISPLAY == NULL
12976# ifdef FEAT_GUI
12977 && !gui.in_use
12978# endif
12979 )
12980 {
12981 x_force_connect = TRUE;
12982 setup_term_clip();
12983 x_force_connect = FALSE;
12984 }
12985}
12986
12987 static int
12988check_connection()
12989{
12990 make_connection();
12991 if (X_DISPLAY == NULL)
12992 {
12993 EMSG(_("E240: No connection to Vim server"));
12994 return FAIL;
12995 }
12996 return OK;
12997}
12998#endif
12999
13000#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013001static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013002
13003 static void
13004remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013005 typval_T *argvars;
13006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013007 int expr;
13008{
13009 char_u *server_name;
13010 char_u *keys;
13011 char_u *r = NULL;
13012 char_u buf[NUMBUFLEN];
13013# ifdef WIN32
13014 HWND w;
13015# else
13016 Window w;
13017# endif
13018
13019 if (check_restricted() || check_secure())
13020 return;
13021
13022# ifdef FEAT_X11
13023 if (check_connection() == FAIL)
13024 return;
13025# endif
13026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013027 server_name = get_tv_string_chk(&argvars[0]);
13028 if (server_name == NULL)
13029 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013030 keys = get_tv_string_buf(&argvars[1], buf);
13031# ifdef WIN32
13032 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13033# else
13034 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13035 < 0)
13036# endif
13037 {
13038 if (r != NULL)
13039 EMSG(r); /* sending worked but evaluation failed */
13040 else
13041 EMSG2(_("E241: Unable to send to %s"), server_name);
13042 return;
13043 }
13044
13045 rettv->vval.v_string = r;
13046
13047 if (argvars[2].v_type != VAR_UNKNOWN)
13048 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013049 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013050 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013051 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013052
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013053 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013054 v.di_tv.v_type = VAR_STRING;
13055 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 idvar = get_tv_string_chk(&argvars[2]);
13057 if (idvar != NULL)
13058 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013059 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013060 }
13061}
13062#endif
13063
13064/*
13065 * "remote_expr()" function
13066 */
13067/*ARGSUSED*/
13068 static void
13069f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013070 typval_T *argvars;
13071 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013072{
13073 rettv->v_type = VAR_STRING;
13074 rettv->vval.v_string = NULL;
13075#ifdef FEAT_CLIENTSERVER
13076 remote_common(argvars, rettv, TRUE);
13077#endif
13078}
13079
13080/*
13081 * "remote_foreground()" function
13082 */
13083/*ARGSUSED*/
13084 static void
13085f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013086 typval_T *argvars;
13087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013088{
13089 rettv->vval.v_number = 0;
13090#ifdef FEAT_CLIENTSERVER
13091# ifdef WIN32
13092 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 {
13094 char_u *server_name = get_tv_string_chk(&argvars[0]);
13095
13096 if (server_name != NULL)
13097 serverForeground(server_name);
13098 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013099# else
13100 /* Send a foreground() expression to the server. */
13101 argvars[1].v_type = VAR_STRING;
13102 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13103 argvars[2].v_type = VAR_UNKNOWN;
13104 remote_common(argvars, rettv, TRUE);
13105 vim_free(argvars[1].vval.v_string);
13106# endif
13107#endif
13108}
13109
13110/*ARGSUSED*/
13111 static void
13112f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013113 typval_T *argvars;
13114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013115{
13116#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013117 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013118 char_u *s = NULL;
13119# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013120 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013121# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013122 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013123
13124 if (check_restricted() || check_secure())
13125 {
13126 rettv->vval.v_number = -1;
13127 return;
13128 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013129 serverid = get_tv_string_chk(&argvars[0]);
13130 if (serverid == NULL)
13131 {
13132 rettv->vval.v_number = -1;
13133 return; /* type error; errmsg already given */
13134 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013136 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013137 if (n == 0)
13138 rettv->vval.v_number = -1;
13139 else
13140 {
13141 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13142 rettv->vval.v_number = (s != NULL);
13143 }
13144# else
13145 rettv->vval.v_number = 0;
13146 if (check_connection() == FAIL)
13147 return;
13148
13149 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013151# endif
13152
13153 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 char_u *retvar;
13156
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 v.di_tv.v_type = VAR_STRING;
13158 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 retvar = get_tv_string_chk(&argvars[1]);
13160 if (retvar != NULL)
13161 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013162 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013163 }
13164#else
13165 rettv->vval.v_number = -1;
13166#endif
13167}
13168
13169/*ARGSUSED*/
13170 static void
13171f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013172 typval_T *argvars;
13173 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013174{
13175 char_u *r = NULL;
13176
13177#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 char_u *serverid = get_tv_string_chk(&argvars[0]);
13179
13180 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013181 {
13182# ifdef WIN32
13183 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013184 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013185
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013186 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013187 if (n != 0)
13188 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13189 if (r == NULL)
13190# else
13191 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013192 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013193# endif
13194 EMSG(_("E277: Unable to read a server reply"));
13195 }
13196#endif
13197 rettv->v_type = VAR_STRING;
13198 rettv->vval.v_string = r;
13199}
13200
13201/*
13202 * "remote_send()" function
13203 */
13204/*ARGSUSED*/
13205 static void
13206f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T *argvars;
13208 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013209{
13210 rettv->v_type = VAR_STRING;
13211 rettv->vval.v_string = NULL;
13212#ifdef FEAT_CLIENTSERVER
13213 remote_common(argvars, rettv, FALSE);
13214#endif
13215}
13216
13217/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013218 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219 */
13220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *argvars;
13223 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013224{
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 list_T *l;
13226 listitem_T *item, *item2;
13227 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013229 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013230 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 dict_T *d;
13232 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013233
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013234 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013235 if (argvars[0].v_type == VAR_DICT)
13236 {
13237 if (argvars[2].v_type != VAR_UNKNOWN)
13238 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013239 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013240 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242 key = get_tv_string_chk(&argvars[1]);
13243 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013245 di = dict_find(d, key, -1);
13246 if (di == NULL)
13247 EMSG2(_(e_dictkey), key);
13248 else
13249 {
13250 *rettv = di->di_tv;
13251 init_tv(&di->di_tv);
13252 dictitem_remove(d, di);
13253 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013254 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013255 }
13256 }
13257 else if (argvars[0].v_type != VAR_LIST)
13258 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013259 else if ((l = argvars[0].vval.v_list) != NULL
13260 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 int error = FALSE;
13263
13264 idx = get_tv_number_chk(&argvars[1], &error);
13265 if (error)
13266 ; /* type error: do nothing, errmsg already given */
13267 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013268 EMSGN(_(e_listidx), idx);
13269 else
13270 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013271 if (argvars[2].v_type == VAR_UNKNOWN)
13272 {
13273 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013274 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013275 *rettv = item->li_tv;
13276 vim_free(item);
13277 }
13278 else
13279 {
13280 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 end = get_tv_number_chk(&argvars[2], &error);
13282 if (error)
13283 ; /* type error: do nothing */
13284 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013285 EMSGN(_(e_listidx), end);
13286 else
13287 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013288 int cnt = 0;
13289
13290 for (li = item; li != NULL; li = li->li_next)
13291 {
13292 ++cnt;
13293 if (li == item2)
13294 break;
13295 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013296 if (li == NULL) /* didn't find "item2" after "item" */
13297 EMSG(_(e_invrange));
13298 else
13299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013300 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013301 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013302 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013303 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013304 l->lv_first = item;
13305 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013306 item->li_prev = NULL;
13307 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013308 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013309 }
13310 }
13311 }
13312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013313 }
13314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315}
13316
13317/*
13318 * "rename({from}, {to})" function
13319 */
13320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *argvars;
13323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324{
13325 char_u buf[NUMBUFLEN];
13326
13327 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13331 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332}
13333
13334/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013335 * "repeat()" function
13336 */
13337/*ARGSUSED*/
13338 static void
13339f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013340 typval_T *argvars;
13341 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013342{
13343 char_u *p;
13344 int n;
13345 int slen;
13346 int len;
13347 char_u *r;
13348 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013349
13350 n = get_tv_number(&argvars[1]);
13351 if (argvars[0].v_type == VAR_LIST)
13352 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013353 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013354 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013355 if (list_extend(rettv->vval.v_list,
13356 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013357 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013358 }
13359 else
13360 {
13361 p = get_tv_string(&argvars[0]);
13362 rettv->v_type = VAR_STRING;
13363 rettv->vval.v_string = NULL;
13364
13365 slen = (int)STRLEN(p);
13366 len = slen * n;
13367 if (len <= 0)
13368 return;
13369
13370 r = alloc(len + 1);
13371 if (r != NULL)
13372 {
13373 for (i = 0; i < n; i++)
13374 mch_memmove(r + i * slen, p, (size_t)slen);
13375 r[len] = NUL;
13376 }
13377
13378 rettv->vval.v_string = r;
13379 }
13380}
13381
13382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 * "resolve()" function
13384 */
13385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 typval_T *argvars;
13388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389{
13390 char_u *p;
13391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013392 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393#ifdef FEAT_SHORTCUT
13394 {
13395 char_u *v = NULL;
13396
13397 v = mch_resolve_shortcut(p);
13398 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 }
13403#else
13404# ifdef HAVE_READLINK
13405 {
13406 char_u buf[MAXPATHL + 1];
13407 char_u *cpy;
13408 int len;
13409 char_u *remain = NULL;
13410 char_u *q;
13411 int is_relative_to_current = FALSE;
13412 int has_trailing_pathsep = FALSE;
13413 int limit = 100;
13414
13415 p = vim_strsave(p);
13416
13417 if (p[0] == '.' && (vim_ispathsep(p[1])
13418 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13419 is_relative_to_current = TRUE;
13420
13421 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013422 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 has_trailing_pathsep = TRUE;
13424
13425 q = getnextcomp(p);
13426 if (*q != NUL)
13427 {
13428 /* Separate the first path component in "p", and keep the
13429 * remainder (beginning with the path separator). */
13430 remain = vim_strsave(q - 1);
13431 q[-1] = NUL;
13432 }
13433
13434 for (;;)
13435 {
13436 for (;;)
13437 {
13438 len = readlink((char *)p, (char *)buf, MAXPATHL);
13439 if (len <= 0)
13440 break;
13441 buf[len] = NUL;
13442
13443 if (limit-- == 0)
13444 {
13445 vim_free(p);
13446 vim_free(remain);
13447 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 goto fail;
13450 }
13451
13452 /* Ensure that the result will have a trailing path separator
13453 * if the argument has one. */
13454 if (remain == NULL && has_trailing_pathsep)
13455 add_pathsep(buf);
13456
13457 /* Separate the first path component in the link value and
13458 * concatenate the remainders. */
13459 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13460 if (*q != NUL)
13461 {
13462 if (remain == NULL)
13463 remain = vim_strsave(q - 1);
13464 else
13465 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013466 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 if (cpy != NULL)
13468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 vim_free(remain);
13470 remain = cpy;
13471 }
13472 }
13473 q[-1] = NUL;
13474 }
13475
13476 q = gettail(p);
13477 if (q > p && *q == NUL)
13478 {
13479 /* Ignore trailing path separator. */
13480 q[-1] = NUL;
13481 q = gettail(p);
13482 }
13483 if (q > p && !mch_isFullName(buf))
13484 {
13485 /* symlink is relative to directory of argument */
13486 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13487 if (cpy != NULL)
13488 {
13489 STRCPY(cpy, p);
13490 STRCPY(gettail(cpy), buf);
13491 vim_free(p);
13492 p = cpy;
13493 }
13494 }
13495 else
13496 {
13497 vim_free(p);
13498 p = vim_strsave(buf);
13499 }
13500 }
13501
13502 if (remain == NULL)
13503 break;
13504
13505 /* Append the first path component of "remain" to "p". */
13506 q = getnextcomp(remain + 1);
13507 len = q - remain - (*q != NUL);
13508 cpy = vim_strnsave(p, STRLEN(p) + len);
13509 if (cpy != NULL)
13510 {
13511 STRNCAT(cpy, remain, len);
13512 vim_free(p);
13513 p = cpy;
13514 }
13515 /* Shorten "remain". */
13516 if (*q != NUL)
13517 STRCPY(remain, q - 1);
13518 else
13519 {
13520 vim_free(remain);
13521 remain = NULL;
13522 }
13523 }
13524
13525 /* If the result is a relative path name, make it explicitly relative to
13526 * the current directory if and only if the argument had this form. */
13527 if (!vim_ispathsep(*p))
13528 {
13529 if (is_relative_to_current
13530 && *p != NUL
13531 && !(p[0] == '.'
13532 && (p[1] == NUL
13533 || vim_ispathsep(p[1])
13534 || (p[1] == '.'
13535 && (p[2] == NUL
13536 || vim_ispathsep(p[2]))))))
13537 {
13538 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013539 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540 if (cpy != NULL)
13541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 vim_free(p);
13543 p = cpy;
13544 }
13545 }
13546 else if (!is_relative_to_current)
13547 {
13548 /* Strip leading "./". */
13549 q = p;
13550 while (q[0] == '.' && vim_ispathsep(q[1]))
13551 q += 2;
13552 if (q > p)
13553 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13554 }
13555 }
13556
13557 /* Ensure that the result will have no trailing path separator
13558 * if the argument had none. But keep "/" or "//". */
13559 if (!has_trailing_pathsep)
13560 {
13561 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013562 if (after_pathsep(p, q))
13563 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 }
13565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 }
13568# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570# endif
13571#endif
13572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574
13575#ifdef HAVE_READLINK
13576fail:
13577#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579}
13580
13581/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013582 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 */
13584 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013585f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *argvars;
13587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588{
Bram Moolenaar33570922005-01-25 22:26:29 +000013589 list_T *l;
13590 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592 rettv->vval.v_number = 0;
13593 if (argvars[0].v_type != VAR_LIST)
13594 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013595 else if ((l = argvars[0].vval.v_list) != NULL
13596 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013597 {
13598 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013599 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013600 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601 while (li != NULL)
13602 {
13603 ni = li->li_prev;
13604 list_append(l, li);
13605 li = ni;
13606 }
13607 rettv->vval.v_list = l;
13608 rettv->v_type = VAR_LIST;
13609 ++l->lv_refcount;
13610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611}
13612
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013613#define SP_NOMOVE 0x01 /* don't move cursor */
13614#define SP_REPEAT 0x02 /* repeat to find outer pair */
13615#define SP_RETCOUNT 0x04 /* return matchcount */
13616#define SP_SETPCMARK 0x08 /* set previous context mark */
13617#define SP_START 0x10 /* accept match at start position */
13618#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13619#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013620
Bram Moolenaar33570922005-01-25 22:26:29 +000013621static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013622
13623/*
13624 * Get flags for a search function.
13625 * Possibly sets "p_ws".
13626 * Returns BACKWARD, FORWARD or zero (for an error).
13627 */
13628 static int
13629get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013630 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013631 int *flagsp;
13632{
13633 int dir = FORWARD;
13634 char_u *flags;
13635 char_u nbuf[NUMBUFLEN];
13636 int mask;
13637
13638 if (varp->v_type != VAR_UNKNOWN)
13639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013640 flags = get_tv_string_buf_chk(varp, nbuf);
13641 if (flags == NULL)
13642 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013643 while (*flags != NUL)
13644 {
13645 switch (*flags)
13646 {
13647 case 'b': dir = BACKWARD; break;
13648 case 'w': p_ws = TRUE; break;
13649 case 'W': p_ws = FALSE; break;
13650 default: mask = 0;
13651 if (flagsp != NULL)
13652 switch (*flags)
13653 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013654 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013655 case 'e': mask = SP_END; break;
13656 case 'm': mask = SP_RETCOUNT; break;
13657 case 'n': mask = SP_NOMOVE; break;
13658 case 'p': mask = SP_SUBPAT; break;
13659 case 'r': mask = SP_REPEAT; break;
13660 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013661 }
13662 if (mask == 0)
13663 {
13664 EMSG2(_(e_invarg2), flags);
13665 dir = 0;
13666 }
13667 else
13668 *flagsp |= mask;
13669 }
13670 if (dir == 0)
13671 break;
13672 ++flags;
13673 }
13674 }
13675 return dir;
13676}
13677
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013679 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013681 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013682search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013683 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013684 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013685 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013687 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 char_u *pat;
13689 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013690 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 int save_p_ws = p_ws;
13692 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013693 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013694 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013695 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013696 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013699 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013700 if (dir == 0)
13701 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013702 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013703 if (flags & SP_START)
13704 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013705 if (flags & SP_END)
13706 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013707
13708 /* Optional extra argument: line number to stop searching. */
13709 if (argvars[1].v_type != VAR_UNKNOWN
13710 && argvars[2].v_type != VAR_UNKNOWN)
13711 {
13712 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13713 if (lnum_stop < 0)
13714 goto theend;
13715 }
13716
Bram Moolenaar231334e2005-07-25 20:46:57 +000013717 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013718 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013719 * Check to make sure only those flags are set.
13720 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13721 * flags cannot be set. Check for that condition also.
13722 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013723 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013724 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013727 goto theend;
13728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013730 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013731 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13732 options, RE_SEARCH, (linenr_T)lnum_stop);
13733 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013735 if (flags & SP_SUBPAT)
13736 retval = subpatnum;
13737 else
13738 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013739 if (flags & SP_SETPCMARK)
13740 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013742 if (match_pos != NULL)
13743 {
13744 /* Store the match cursor position */
13745 match_pos->lnum = pos.lnum;
13746 match_pos->col = pos.col + 1;
13747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 /* "/$" will put the cursor after the end of the line, may need to
13749 * correct that here */
13750 check_cursor();
13751 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013752
13753 /* If 'n' flag is used: restore cursor position. */
13754 if (flags & SP_NOMOVE)
13755 curwin->w_cursor = save_cursor;
13756theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013758
13759 return retval;
13760}
13761
13762/*
13763 * "search()" function
13764 */
13765 static void
13766f_search(argvars, rettv)
13767 typval_T *argvars;
13768 typval_T *rettv;
13769{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013770 int flags = 0;
13771
13772 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773}
13774
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013776 * "searchdecl()" function
13777 */
13778 static void
13779f_searchdecl(argvars, rettv)
13780 typval_T *argvars;
13781 typval_T *rettv;
13782{
13783 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013784 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013785 int error = FALSE;
13786 char_u *name;
13787
13788 rettv->vval.v_number = 1; /* default: FAIL */
13789
13790 name = get_tv_string_chk(&argvars[0]);
13791 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013792 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013793 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013794 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13795 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13796 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013797 if (!error && name != NULL)
13798 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013799 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013800}
13801
13802/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013803 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013805 static int
13806searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013807 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013808 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809{
13810 char_u *spat, *mpat, *epat;
13811 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 int dir;
13814 int flags = 0;
13815 char_u nbuf1[NUMBUFLEN];
13816 char_u nbuf2[NUMBUFLEN];
13817 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013818 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013819 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 spat = get_tv_string_chk(&argvars[0]);
13823 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13824 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13825 if (spat == NULL || mpat == NULL || epat == NULL)
13826 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 /* Handle the optional fourth argument: flags */
13829 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013830 if (dir == 0)
13831 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013832
13833 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013834 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13835 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013836 if ((flags & (SP_END | SP_SUBPAT)) != 0
13837 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013838 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013839 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013840 goto theend;
13841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013843 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013844 if (argvars[3].v_type == VAR_UNKNOWN
13845 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 skip = (char_u *)"";
13847 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013850 if (argvars[5].v_type != VAR_UNKNOWN)
13851 {
13852 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13853 if (lnum_stop < 0)
13854 goto theend;
13855 }
13856 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 if (skip == NULL)
13858 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013860 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13861 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013862
13863theend:
13864 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013865
13866 return retval;
13867}
13868
13869/*
13870 * "searchpair()" function
13871 */
13872 static void
13873f_searchpair(argvars, rettv)
13874 typval_T *argvars;
13875 typval_T *rettv;
13876{
13877 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13878}
13879
13880/*
13881 * "searchpairpos()" function
13882 */
13883 static void
13884f_searchpairpos(argvars, rettv)
13885 typval_T *argvars;
13886 typval_T *rettv;
13887{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013888 pos_T match_pos;
13889 int lnum = 0;
13890 int col = 0;
13891
13892 rettv->vval.v_number = 0;
13893
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013894 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013895 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013896
13897 if (searchpair_cmn(argvars, &match_pos) > 0)
13898 {
13899 lnum = match_pos.lnum;
13900 col = match_pos.col;
13901 }
13902
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013903 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13904 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013905}
13906
13907/*
13908 * Search for a start/middle/end thing.
13909 * Used by searchpair(), see its documentation for the details.
13910 * Returns 0 or -1 for no match,
13911 */
13912 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013913do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013914 char_u *spat; /* start pattern */
13915 char_u *mpat; /* middle pattern */
13916 char_u *epat; /* end pattern */
13917 int dir; /* BACKWARD or FORWARD */
13918 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013919 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013920 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013921 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013922{
13923 char_u *save_cpo;
13924 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13925 long retval = 0;
13926 pos_T pos;
13927 pos_T firstpos;
13928 pos_T foundpos;
13929 pos_T save_cursor;
13930 pos_T save_pos;
13931 int n;
13932 int r;
13933 int nest = 1;
13934 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013935 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013936
13937 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13938 save_cpo = p_cpo;
13939 p_cpo = (char_u *)"";
13940
13941 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13942 * start/middle/end (pat3, for the top pair). */
13943 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13944 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13945 if (pat2 == NULL || pat3 == NULL)
13946 goto theend;
13947 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13948 if (*mpat == NUL)
13949 STRCPY(pat3, pat2);
13950 else
13951 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13952 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013953 if (flags & SP_START)
13954 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013955
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 save_cursor = curwin->w_cursor;
13957 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013958 clearpos(&firstpos);
13959 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 pat = pat3;
13961 for (;;)
13962 {
13963 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013964 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13966 /* didn't find it or found the first match again: FAIL */
13967 break;
13968
13969 if (firstpos.lnum == 0)
13970 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013971 if (equalpos(pos, foundpos))
13972 {
13973 /* Found the same position again. Can happen with a pattern that
13974 * has "\zs" at the end and searching backwards. Advance one
13975 * character and try again. */
13976 if (dir == BACKWARD)
13977 decl(&pos);
13978 else
13979 incl(&pos);
13980 }
13981 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982
13983 /* If the skip pattern matches, ignore this match. */
13984 if (*skip != NUL)
13985 {
13986 save_pos = curwin->w_cursor;
13987 curwin->w_cursor = pos;
13988 r = eval_to_bool(skip, &err, NULL, FALSE);
13989 curwin->w_cursor = save_pos;
13990 if (err)
13991 {
13992 /* Evaluating {skip} caused an error, break here. */
13993 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013994 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 break;
13996 }
13997 if (r)
13998 continue;
13999 }
14000
14001 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14002 {
14003 /* Found end when searching backwards or start when searching
14004 * forward: nested pair. */
14005 ++nest;
14006 pat = pat2; /* nested, don't search for middle */
14007 }
14008 else
14009 {
14010 /* Found end when searching forward or start when searching
14011 * backward: end of (nested) pair; or found middle in outer pair. */
14012 if (--nest == 1)
14013 pat = pat3; /* outer level, search for middle */
14014 }
14015
14016 if (nest == 0)
14017 {
14018 /* Found the match: return matchcount or line number. */
14019 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014020 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014022 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014023 if (flags & SP_SETPCMARK)
14024 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 curwin->w_cursor = pos;
14026 if (!(flags & SP_REPEAT))
14027 break;
14028 nest = 1; /* search for next unmatched */
14029 }
14030 }
14031
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014032 if (match_pos != NULL)
14033 {
14034 /* Store the match cursor position */
14035 match_pos->lnum = curwin->w_cursor.lnum;
14036 match_pos->col = curwin->w_cursor.col + 1;
14037 }
14038
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014040 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 curwin->w_cursor = save_cursor;
14042
14043theend:
14044 vim_free(pat2);
14045 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014047
14048 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049}
14050
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014051/*
14052 * "searchpos()" function
14053 */
14054 static void
14055f_searchpos(argvars, rettv)
14056 typval_T *argvars;
14057 typval_T *rettv;
14058{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014059 pos_T match_pos;
14060 int lnum = 0;
14061 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014062 int n;
14063 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014064
14065 rettv->vval.v_number = 0;
14066
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014067 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014068 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014069
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014070 n = search_cmn(argvars, &match_pos, &flags);
14071 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014072 {
14073 lnum = match_pos.lnum;
14074 col = match_pos.col;
14075 }
14076
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014077 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14078 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014079 if (flags & SP_SUBPAT)
14080 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014081}
14082
14083
Bram Moolenaar0d660222005-01-07 21:51:51 +000014084/*ARGSUSED*/
14085 static void
14086f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *argvars;
14088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090#ifdef FEAT_CLIENTSERVER
14091 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 char_u *server = get_tv_string_chk(&argvars[0]);
14093 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094
Bram Moolenaar0d660222005-01-07 21:51:51 +000014095 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 if (server == NULL || reply == NULL)
14097 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 if (check_restricted() || check_secure())
14099 return;
14100# ifdef FEAT_X11
14101 if (check_connection() == FAIL)
14102 return;
14103# endif
14104
14105 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014107 EMSG(_("E258: Unable to send to client"));
14108 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014110 rettv->vval.v_number = 0;
14111#else
14112 rettv->vval.v_number = -1;
14113#endif
14114}
14115
14116/*ARGSUSED*/
14117 static void
14118f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014119 typval_T *argvars;
14120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121{
14122 char_u *r = NULL;
14123
14124#ifdef FEAT_CLIENTSERVER
14125# ifdef WIN32
14126 r = serverGetVimNames();
14127# else
14128 make_connection();
14129 if (X_DISPLAY != NULL)
14130 r = serverGetVimNames(X_DISPLAY);
14131# endif
14132#endif
14133 rettv->v_type = VAR_STRING;
14134 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135}
14136
14137/*
14138 * "setbufvar()" function
14139 */
14140/*ARGSUSED*/
14141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 typval_T *argvars;
14144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145{
14146 buf_T *buf;
14147#ifdef FEAT_AUTOCMD
14148 aco_save_T aco;
14149#else
14150 buf_T *save_curbuf;
14151#endif
14152 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014153 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 char_u nbuf[NUMBUFLEN];
14155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 rettv->vval.v_number = 0;
14157
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 if (check_restricted() || check_secure())
14159 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14161 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014162 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 varp = &argvars[2];
14164
14165 if (buf != NULL && varname != NULL && varp != NULL)
14166 {
14167 /* set curbuf to be our buf, temporarily */
14168#ifdef FEAT_AUTOCMD
14169 aucmd_prepbuf(&aco, buf);
14170#else
14171 save_curbuf = curbuf;
14172 curbuf = buf;
14173#endif
14174
14175 if (*varname == '&')
14176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 long numval;
14178 char_u *strval;
14179 int error = FALSE;
14180
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 numval = get_tv_number_chk(varp, &error);
14183 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 if (!error && strval != NULL)
14185 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 }
14187 else
14188 {
14189 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14190 if (bufvarname != NULL)
14191 {
14192 STRCPY(bufvarname, "b:");
14193 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014194 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 vim_free(bufvarname);
14196 }
14197 }
14198
14199 /* reset notion of buffer */
14200#ifdef FEAT_AUTOCMD
14201 aucmd_restbuf(&aco);
14202#else
14203 curbuf = save_curbuf;
14204#endif
14205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206}
14207
14208/*
14209 * "setcmdpos()" function
14210 */
14211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014212f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 typval_T *argvars;
14214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 int pos = (int)get_tv_number(&argvars[0]) - 1;
14217
14218 if (pos >= 0)
14219 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220}
14221
14222/*
14223 * "setline()" function
14224 */
14225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 typval_T *argvars;
14228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
14230 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014231 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014232 list_T *l = NULL;
14233 listitem_T *li = NULL;
14234 long added = 0;
14235 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014237 lnum = get_tv_lnum(&argvars[0]);
14238 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014240 l = argvars[1].vval.v_list;
14241 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014243 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014244 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014245
14246 rettv->vval.v_number = 0; /* OK */
14247 for (;;)
14248 {
14249 if (l != NULL)
14250 {
14251 /* list argument, get next string */
14252 if (li == NULL)
14253 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014255 li = li->li_next;
14256 }
14257
14258 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014260 break;
14261 if (lnum <= curbuf->b_ml.ml_line_count)
14262 {
14263 /* existing line, replace it */
14264 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14265 {
14266 changed_bytes(lnum, 0);
14267 check_cursor_col();
14268 rettv->vval.v_number = 0; /* OK */
14269 }
14270 }
14271 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14272 {
14273 /* lnum is one past the last line, append the line */
14274 ++added;
14275 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14276 rettv->vval.v_number = 0; /* OK */
14277 }
14278
14279 if (l == NULL) /* only one string argument */
14280 break;
14281 ++lnum;
14282 }
14283
14284 if (added > 0)
14285 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286}
14287
14288/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014289 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014290 */
14291/*ARGSUSED*/
14292 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014293set_qf_ll_list(wp, list_arg, action_arg, rettv)
14294 win_T *wp;
14295 typval_T *list_arg;
14296 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014297 typval_T *rettv;
14298{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014299#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014300 char_u *act;
14301 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014302#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014303
Bram Moolenaar2641f772005-03-25 21:58:17 +000014304 rettv->vval.v_number = -1;
14305
14306#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014307 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014308 EMSG(_(e_listreq));
14309 else
14310 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014311 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014312
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014313 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014314 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014315 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 if (act == NULL)
14317 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014318 if (*act == 'a' || *act == 'r')
14319 action = *act;
14320 }
14321
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014322 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014323 rettv->vval.v_number = 0;
14324 }
14325#endif
14326}
14327
14328/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014329 * "setloclist()" function
14330 */
14331/*ARGSUSED*/
14332 static void
14333f_setloclist(argvars, rettv)
14334 typval_T *argvars;
14335 typval_T *rettv;
14336{
14337 win_T *win;
14338
14339 rettv->vval.v_number = -1;
14340
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014341 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014342 if (win != NULL)
14343 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14344}
14345
14346/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014347 * "setpos()" function
14348 */
14349/*ARGSUSED*/
14350 static void
14351f_setpos(argvars, rettv)
14352 typval_T *argvars;
14353 typval_T *rettv;
14354{
14355 pos_T pos;
14356 int fnum;
14357 char_u *name;
14358
14359 name = get_tv_string_chk(argvars);
14360 if (name != NULL)
14361 {
14362 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14363 {
14364 --pos.col;
14365 if (name[0] == '.') /* cursor */
14366 {
14367 if (fnum == curbuf->b_fnum)
14368 {
14369 curwin->w_cursor = pos;
14370 check_cursor();
14371 }
14372 else
14373 EMSG(_(e_invarg));
14374 }
14375 else if (name[0] == '\'') /* mark */
14376 (void)setmark_pos(name[1], &pos, fnum);
14377 else
14378 EMSG(_(e_invarg));
14379 }
14380 }
14381}
14382
14383/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014384 * "setqflist()" function
14385 */
14386/*ARGSUSED*/
14387 static void
14388f_setqflist(argvars, rettv)
14389 typval_T *argvars;
14390 typval_T *rettv;
14391{
14392 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14393}
14394
14395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 * "setreg()" function
14397 */
14398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014400 typval_T *argvars;
14401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402{
14403 int regname;
14404 char_u *strregname;
14405 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 int append;
14408 char_u yank_type;
14409 long block_len;
14410
14411 block_len = -1;
14412 yank_type = MAUTO;
14413 append = FALSE;
14414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014416 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 if (strregname == NULL)
14419 return; /* type error; errmsg already given */
14420 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 if (regname == 0 || regname == '@')
14422 regname = '"';
14423 else if (regname == '=')
14424 return;
14425
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014428 stropt = get_tv_string_chk(&argvars[2]);
14429 if (stropt == NULL)
14430 return; /* type error */
14431 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 switch (*stropt)
14433 {
14434 case 'a': case 'A': /* append */
14435 append = TRUE;
14436 break;
14437 case 'v': case 'c': /* character-wise selection */
14438 yank_type = MCHAR;
14439 break;
14440 case 'V': case 'l': /* line-wise selection */
14441 yank_type = MLINE;
14442 break;
14443#ifdef FEAT_VISUAL
14444 case 'b': case Ctrl_V: /* block-wise selection */
14445 yank_type = MBLOCK;
14446 if (VIM_ISDIGIT(stropt[1]))
14447 {
14448 ++stropt;
14449 block_len = getdigits(&stropt) - 1;
14450 --stropt;
14451 }
14452 break;
14453#endif
14454 }
14455 }
14456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 strval = get_tv_string_chk(&argvars[1]);
14458 if (strval != NULL)
14459 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462}
14463
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014464/*
14465 * "settabwinvar()" function
14466 */
14467 static void
14468f_settabwinvar(argvars, rettv)
14469 typval_T *argvars;
14470 typval_T *rettv;
14471{
14472 setwinvar(argvars, rettv, 1);
14473}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474
14475/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014476 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014480 typval_T *argvars;
14481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014483 setwinvar(argvars, rettv, 0);
14484}
14485
14486/*
14487 * "setwinvar()" and "settabwinvar()" functions
14488 */
14489 static void
14490setwinvar(argvars, rettv, off)
14491 typval_T *argvars;
14492 typval_T *rettv;
14493 int off;
14494{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 win_T *win;
14496#ifdef FEAT_WINDOWS
14497 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014498 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499#endif
14500 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014503 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 rettv->vval.v_number = 0;
14506
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 if (check_restricted() || check_secure())
14508 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014509
14510#ifdef FEAT_WINDOWS
14511 if (off == 1)
14512 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14513 else
14514 tp = curtab;
14515#endif
14516 win = find_win_by_nr(&argvars[off], tp);
14517 varname = get_tv_string_chk(&argvars[off + 1]);
14518 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519
14520 if (win != NULL && varname != NULL && varp != NULL)
14521 {
14522#ifdef FEAT_WINDOWS
14523 /* set curwin to be our win, temporarily */
14524 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014525 save_curtab = curtab;
14526 goto_tabpage_tp(tp);
14527 if (!win_valid(win))
14528 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 curwin = win;
14530 curbuf = curwin->w_buffer;
14531#endif
14532
14533 if (*varname == '&')
14534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 long numval;
14536 char_u *strval;
14537 int error = FALSE;
14538
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 numval = get_tv_number_chk(varp, &error);
14541 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 if (!error && strval != NULL)
14543 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 }
14545 else
14546 {
14547 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14548 if (winvarname != NULL)
14549 {
14550 STRCPY(winvarname, "w:");
14551 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014552 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 vim_free(winvarname);
14554 }
14555 }
14556
14557#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014558 /* Restore current tabpage and window, if still valid (autocomands can
14559 * make them invalid). */
14560 if (valid_tabpage(save_curtab))
14561 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 if (win_valid(save_curwin))
14563 {
14564 curwin = save_curwin;
14565 curbuf = curwin->w_buffer;
14566 }
14567#endif
14568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569}
14570
14571/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014572 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 */
14574 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014576 typval_T *argvars;
14577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581 p = get_tv_string(&argvars[0]);
14582 rettv->vval.v_string = vim_strsave(p);
14583 simplify_filename(rettv->vval.v_string); /* simplify in place */
14584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585}
14586
Bram Moolenaar0d660222005-01-07 21:51:51 +000014587static int
14588#ifdef __BORLANDC__
14589 _RTLENTRYF
14590#endif
14591 item_compare __ARGS((const void *s1, const void *s2));
14592static int
14593#ifdef __BORLANDC__
14594 _RTLENTRYF
14595#endif
14596 item_compare2 __ARGS((const void *s1, const void *s2));
14597
14598static int item_compare_ic;
14599static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014600static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601#define ITEM_COMPARE_FAIL 999
14602
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606 static int
14607#ifdef __BORLANDC__
14608_RTLENTRYF
14609#endif
14610item_compare(s1, s2)
14611 const void *s1;
14612 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614 char_u *p1, *p2;
14615 char_u *tofree1, *tofree2;
14616 int res;
14617 char_u numbuf1[NUMBUFLEN];
14618 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014620 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14621 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622 if (item_compare_ic)
14623 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625 res = STRCMP(p1, p2);
14626 vim_free(tofree1);
14627 vim_free(tofree2);
14628 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629}
14630
14631 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632#ifdef __BORLANDC__
14633_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014635item_compare2(s1, s2)
14636 const void *s1;
14637 const void *s2;
14638{
14639 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014640 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014641 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014644 /* shortcut after failure in previous call; compare all items equal */
14645 if (item_compare_func_err)
14646 return 0;
14647
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014648 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14649 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14651 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652
14653 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014654 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014655 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656 clear_tv(&argv[0]);
14657 clear_tv(&argv[1]);
14658
14659 if (res == FAIL)
14660 res = ITEM_COMPARE_FAIL;
14661 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 /* return value has wrong type */
14663 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14664 if (item_compare_func_err)
14665 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014666 clear_tv(&rettv);
14667 return res;
14668}
14669
14670/*
14671 * "sort({list})" function
14672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014673 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014674f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014675 typval_T *argvars;
14676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677{
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 list_T *l;
14679 listitem_T *li;
14680 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014681 long len;
14682 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683
Bram Moolenaar0d660222005-01-07 21:51:51 +000014684 rettv->vval.v_number = 0;
14685 if (argvars[0].v_type != VAR_LIST)
14686 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 else
14688 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014690 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691 return;
14692 rettv->vval.v_list = l;
14693 rettv->v_type = VAR_LIST;
14694 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695
Bram Moolenaar0d660222005-01-07 21:51:51 +000014696 len = list_len(l);
14697 if (len <= 1)
14698 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699
Bram Moolenaar0d660222005-01-07 21:51:51 +000014700 item_compare_ic = FALSE;
14701 item_compare_func = NULL;
14702 if (argvars[1].v_type != VAR_UNKNOWN)
14703 {
14704 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014705 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706 else
14707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014708 int error = FALSE;
14709
14710 i = get_tv_number_chk(&argvars[1], &error);
14711 if (error)
14712 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 if (i == 1)
14714 item_compare_ic = TRUE;
14715 else
14716 item_compare_func = get_tv_string(&argvars[1]);
14717 }
14718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722 if (ptrs == NULL)
14723 return;
14724 i = 0;
14725 for (li = l->lv_first; li != NULL; li = li->li_next)
14726 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014728 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729 /* test the compare function */
14730 if (item_compare_func != NULL
14731 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14732 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014733 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014735 {
14736 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738 item_compare_func == NULL ? item_compare : item_compare2);
14739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014740 if (!item_compare_func_err)
14741 {
14742 /* Clear the List and append the items in the sorted order. */
14743 l->lv_first = l->lv_last = NULL;
14744 l->lv_len = 0;
14745 for (i = 0; i < len; ++i)
14746 list_append(l, ptrs[i]);
14747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014748 }
14749
14750 vim_free(ptrs);
14751 }
14752}
14753
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014754/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014755 * "soundfold({word})" function
14756 */
14757 static void
14758f_soundfold(argvars, rettv)
14759 typval_T *argvars;
14760 typval_T *rettv;
14761{
14762 char_u *s;
14763
14764 rettv->v_type = VAR_STRING;
14765 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014766#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014767 rettv->vval.v_string = eval_soundfold(s);
14768#else
14769 rettv->vval.v_string = vim_strsave(s);
14770#endif
14771}
14772
14773/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014774 * "spellbadword()" function
14775 */
14776/* ARGSUSED */
14777 static void
14778f_spellbadword(argvars, rettv)
14779 typval_T *argvars;
14780 typval_T *rettv;
14781{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014782 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014783 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014784 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014785
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014786 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014787 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014788
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014789#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014790 if (argvars[0].v_type == VAR_UNKNOWN)
14791 {
14792 /* Find the start and length of the badly spelled word. */
14793 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14794 if (len != 0)
14795 word = ml_get_cursor();
14796 }
14797 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14798 {
14799 char_u *str = get_tv_string_chk(&argvars[0]);
14800 int capcol = -1;
14801
14802 if (str != NULL)
14803 {
14804 /* Check the argument for spelling. */
14805 while (*str != NUL)
14806 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014807 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014808 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014809 {
14810 word = str;
14811 break;
14812 }
14813 str += len;
14814 }
14815 }
14816 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014817#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014818
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014819 list_append_string(rettv->vval.v_list, word, len);
14820 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014821 attr == HLF_SPB ? "bad" :
14822 attr == HLF_SPR ? "rare" :
14823 attr == HLF_SPL ? "local" :
14824 attr == HLF_SPC ? "caps" :
14825 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014826}
14827
14828/*
14829 * "spellsuggest()" function
14830 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014831/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014832 static void
14833f_spellsuggest(argvars, rettv)
14834 typval_T *argvars;
14835 typval_T *rettv;
14836{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014837#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014838 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014839 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014840 int maxcount;
14841 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014842 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014843 listitem_T *li;
14844 int need_capital = FALSE;
14845#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014846
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014847 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014848 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014849
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014850#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014851 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14852 {
14853 str = get_tv_string(&argvars[0]);
14854 if (argvars[1].v_type != VAR_UNKNOWN)
14855 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014856 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014857 if (maxcount <= 0)
14858 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014859 if (argvars[2].v_type != VAR_UNKNOWN)
14860 {
14861 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14862 if (typeerr)
14863 return;
14864 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014865 }
14866 else
14867 maxcount = 25;
14868
Bram Moolenaar4770d092006-01-12 23:22:24 +000014869 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014870
14871 for (i = 0; i < ga.ga_len; ++i)
14872 {
14873 str = ((char_u **)ga.ga_data)[i];
14874
14875 li = listitem_alloc();
14876 if (li == NULL)
14877 vim_free(str);
14878 else
14879 {
14880 li->li_tv.v_type = VAR_STRING;
14881 li->li_tv.v_lock = 0;
14882 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014883 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014884 }
14885 }
14886 ga_clear(&ga);
14887 }
14888#endif
14889}
14890
Bram Moolenaar0d660222005-01-07 21:51:51 +000014891 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014892f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014893 typval_T *argvars;
14894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014895{
14896 char_u *str;
14897 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014898 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014899 regmatch_T regmatch;
14900 char_u patbuf[NUMBUFLEN];
14901 char_u *save_cpo;
14902 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014903 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014904 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014905 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014906
14907 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14908 save_cpo = p_cpo;
14909 p_cpo = (char_u *)"";
14910
14911 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014912 if (argvars[1].v_type != VAR_UNKNOWN)
14913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014914 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14915 if (pat == NULL)
14916 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014919 }
14920 if (pat == NULL || *pat == NUL)
14921 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014922
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014923 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014925 if (typeerr)
14926 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927
Bram Moolenaar0d660222005-01-07 21:51:51 +000014928 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14929 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014932 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014933 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014934 if (*str == NUL)
14935 match = FALSE; /* empty item at the end */
14936 else
14937 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938 if (match)
14939 end = regmatch.startp[0];
14940 else
14941 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014942 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14943 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014945 if (list_append_string(rettv->vval.v_list, str,
14946 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014947 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014948 }
14949 if (!match)
14950 break;
14951 /* Advance to just after the match. */
14952 if (regmatch.endp[0] > str)
14953 col = 0;
14954 else
14955 {
14956 /* Don't get stuck at the same match. */
14957#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014958 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959#else
14960 col = 1;
14961#endif
14962 }
14963 str = regmatch.endp[0];
14964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970}
14971
Bram Moolenaar2c932302006-03-18 21:42:09 +000014972/*
14973 * "str2nr()" function
14974 */
14975 static void
14976f_str2nr(argvars, rettv)
14977 typval_T *argvars;
14978 typval_T *rettv;
14979{
14980 int base = 10;
14981 char_u *p;
14982 long n;
14983
14984 if (argvars[1].v_type != VAR_UNKNOWN)
14985 {
14986 base = get_tv_number(&argvars[1]);
14987 if (base != 8 && base != 10 && base != 16)
14988 {
14989 EMSG(_(e_invarg));
14990 return;
14991 }
14992 }
14993
14994 p = skipwhite(get_tv_string(&argvars[0]));
14995 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14996 rettv->vval.v_number = n;
14997}
14998
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999#ifdef HAVE_STRFTIME
15000/*
15001 * "strftime({format}[, {time}])" function
15002 */
15003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015005 typval_T *argvars;
15006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007{
15008 char_u result_buf[256];
15009 struct tm *curtime;
15010 time_t seconds;
15011 char_u *p;
15012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015013 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015016 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 seconds = time(NULL);
15018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 curtime = localtime(&seconds);
15021 /* MSVC returns NULL for an invalid value of seconds. */
15022 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015023 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 else
15025 {
15026# ifdef FEAT_MBYTE
15027 vimconv_T conv;
15028 char_u *enc;
15029
15030 conv.vc_type = CONV_NONE;
15031 enc = enc_locale();
15032 convert_setup(&conv, p_enc, enc);
15033 if (conv.vc_type != CONV_NONE)
15034 p = string_convert(&conv, p, NULL);
15035# endif
15036 if (p != NULL)
15037 (void)strftime((char *)result_buf, sizeof(result_buf),
15038 (char *)p, curtime);
15039 else
15040 result_buf[0] = NUL;
15041
15042# ifdef FEAT_MBYTE
15043 if (conv.vc_type != CONV_NONE)
15044 vim_free(p);
15045 convert_setup(&conv, enc, p_enc);
15046 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015047 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048 else
15049# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051
15052# ifdef FEAT_MBYTE
15053 /* Release conversion descriptors */
15054 convert_setup(&conv, NULL, NULL);
15055 vim_free(enc);
15056# endif
15057 }
15058}
15059#endif
15060
15061/*
15062 * "stridx()" function
15063 */
15064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 typval_T *argvars;
15067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068{
15069 char_u buf[NUMBUFLEN];
15070 char_u *needle;
15071 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015072 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 needle = get_tv_string_chk(&argvars[1]);
15077 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015078 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015079 if (needle == NULL || haystack == NULL)
15080 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081
Bram Moolenaar33570922005-01-25 22:26:29 +000015082 if (argvars[2].v_type != VAR_UNKNOWN)
15083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015084 int error = FALSE;
15085
15086 start_idx = get_tv_number_chk(&argvars[2], &error);
15087 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015089 if (start_idx >= 0)
15090 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 }
15092
15093 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15094 if (pos != NULL)
15095 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096}
15097
15098/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015099 * "string()" function
15100 */
15101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015102f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015103 typval_T *argvars;
15104 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015105{
15106 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015107 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015110 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015111 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015112 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113}
15114
15115/*
15116 * "strlen()" function
15117 */
15118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015119f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015120 typval_T *argvars;
15121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015123 rettv->vval.v_number = (varnumber_T)(STRLEN(
15124 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125}
15126
15127/*
15128 * "strpart()" function
15129 */
15130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 typval_T *argvars;
15133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134{
15135 char_u *p;
15136 int n;
15137 int len;
15138 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 slen = (int)STRLEN(p);
15143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015144 n = get_tv_number_chk(&argvars[1], &error);
15145 if (error)
15146 len = 0;
15147 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015148 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 else
15150 len = slen - n; /* default len: all bytes that are available. */
15151
15152 /*
15153 * Only return the overlap between the specified part and the actual
15154 * string.
15155 */
15156 if (n < 0)
15157 {
15158 len += n;
15159 n = 0;
15160 }
15161 else if (n > slen)
15162 n = slen;
15163 if (len < 0)
15164 len = 0;
15165 else if (n + len > slen)
15166 len = slen - n;
15167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015168 rettv->v_type = VAR_STRING;
15169 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170}
15171
15172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173 * "strridx()" function
15174 */
15175 static void
15176f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015177 typval_T *argvars;
15178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179{
15180 char_u buf[NUMBUFLEN];
15181 char_u *needle;
15182 char_u *haystack;
15183 char_u *rest;
15184 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015185 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015187 needle = get_tv_string_chk(&argvars[1]);
15188 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015189
15190 rettv->vval.v_number = -1;
15191 if (needle == NULL || haystack == NULL)
15192 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015193
15194 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015195 if (argvars[2].v_type != VAR_UNKNOWN)
15196 {
15197 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015198 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015199 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015200 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015201 }
15202 else
15203 end_idx = haystack_len;
15204
Bram Moolenaar0d660222005-01-07 21:51:51 +000015205 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015206 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015208 lastmatch = haystack + end_idx;
15209 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015211 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212 for (rest = haystack; *rest != '\0'; ++rest)
15213 {
15214 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015215 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216 break;
15217 lastmatch = rest;
15218 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015219 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015220
15221 if (lastmatch == NULL)
15222 rettv->vval.v_number = -1;
15223 else
15224 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15225}
15226
15227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 * "strtrans()" function
15229 */
15230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *argvars;
15233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015235 rettv->v_type = VAR_STRING;
15236 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237}
15238
15239/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015240 * "submatch()" function
15241 */
15242 static void
15243f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015244 typval_T *argvars;
15245 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246{
15247 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015248 rettv->vval.v_string =
15249 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250}
15251
15252/*
15253 * "substitute()" function
15254 */
15255 static void
15256f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 typval_T *argvars;
15258 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015259{
15260 char_u patbuf[NUMBUFLEN];
15261 char_u subbuf[NUMBUFLEN];
15262 char_u flagsbuf[NUMBUFLEN];
15263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015264 char_u *str = get_tv_string_chk(&argvars[0]);
15265 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15266 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15267 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15268
Bram Moolenaar0d660222005-01-07 21:51:51 +000015269 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015270 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15271 rettv->vval.v_string = NULL;
15272 else
15273 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274}
15275
15276/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015277 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 */
15279/*ARGSUSED*/
15280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015281f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015282 typval_T *argvars;
15283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284{
15285 int id = 0;
15286#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015287 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 long col;
15289 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015290 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015292 lnum = get_tv_lnum(argvars); /* -1 on type error */
15293 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15294 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015296 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015297 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015298 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299#endif
15300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302}
15303
15304/*
15305 * "synIDattr(id, what [, mode])" function
15306 */
15307/*ARGSUSED*/
15308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015309f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 typval_T *argvars;
15311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312{
15313 char_u *p = NULL;
15314#ifdef FEAT_SYN_HL
15315 int id;
15316 char_u *what;
15317 char_u *mode;
15318 char_u modebuf[NUMBUFLEN];
15319 int modec;
15320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015321 id = get_tv_number(&argvars[0]);
15322 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015323 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015325 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 modec = TOLOWER_ASC(mode[0]);
15327 if (modec != 't' && modec != 'c'
15328#ifdef FEAT_GUI
15329 && modec != 'g'
15330#endif
15331 )
15332 modec = 0; /* replace invalid with current */
15333 }
15334 else
15335 {
15336#ifdef FEAT_GUI
15337 if (gui.in_use)
15338 modec = 'g';
15339 else
15340#endif
15341 if (t_colors > 1)
15342 modec = 'c';
15343 else
15344 modec = 't';
15345 }
15346
15347
15348 switch (TOLOWER_ASC(what[0]))
15349 {
15350 case 'b':
15351 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15352 p = highlight_color(id, what, modec);
15353 else /* bold */
15354 p = highlight_has_attr(id, HL_BOLD, modec);
15355 break;
15356
15357 case 'f': /* fg[#] */
15358 p = highlight_color(id, what, modec);
15359 break;
15360
15361 case 'i':
15362 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15363 p = highlight_has_attr(id, HL_INVERSE, modec);
15364 else /* italic */
15365 p = highlight_has_attr(id, HL_ITALIC, modec);
15366 break;
15367
15368 case 'n': /* name */
15369 p = get_highlight_name(NULL, id - 1);
15370 break;
15371
15372 case 'r': /* reverse */
15373 p = highlight_has_attr(id, HL_INVERSE, modec);
15374 break;
15375
15376 case 's': /* standout */
15377 p = highlight_has_attr(id, HL_STANDOUT, modec);
15378 break;
15379
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015380 case 'u':
15381 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15382 /* underline */
15383 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15384 else
15385 /* undercurl */
15386 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 break;
15388 }
15389
15390 if (p != NULL)
15391 p = vim_strsave(p);
15392#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 rettv->v_type = VAR_STRING;
15394 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395}
15396
15397/*
15398 * "synIDtrans(id)" function
15399 */
15400/*ARGSUSED*/
15401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015403 typval_T *argvars;
15404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405{
15406 int id;
15407
15408#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410
15411 if (id > 0)
15412 id = syn_get_final_id(id);
15413 else
15414#endif
15415 id = 0;
15416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418}
15419
15420/*
15421 * "system()" function
15422 */
15423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015425 typval_T *argvars;
15426 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015428 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015430 char_u *infile = NULL;
15431 char_u buf[NUMBUFLEN];
15432 int err = FALSE;
15433 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015435 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015436 {
15437 /*
15438 * Write the string to a temp file, to be used for input of the shell
15439 * command.
15440 */
15441 if ((infile = vim_tempname('i')) == NULL)
15442 {
15443 EMSG(_(e_notmp));
15444 return;
15445 }
15446
15447 fd = mch_fopen((char *)infile, WRITEBIN);
15448 if (fd == NULL)
15449 {
15450 EMSG2(_(e_notopen), infile);
15451 goto done;
15452 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015453 p = get_tv_string_buf_chk(&argvars[1], buf);
15454 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015455 {
15456 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015457 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015458 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015459 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15460 err = TRUE;
15461 if (fclose(fd) != 0)
15462 err = TRUE;
15463 if (err)
15464 {
15465 EMSG(_("E677: Error writing temp file"));
15466 goto done;
15467 }
15468 }
15469
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015470 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15471 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015472
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473#ifdef USE_CR
15474 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015475 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 {
15477 char_u *s;
15478
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015479 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 {
15481 if (*s == CAR)
15482 *s = NL;
15483 }
15484 }
15485#else
15486# ifdef USE_CRNL
15487 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015488 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 {
15490 char_u *s, *d;
15491
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015492 d = res;
15493 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 {
15495 if (s[0] == CAR && s[1] == NL)
15496 ++s;
15497 *d++ = *s;
15498 }
15499 *d = NUL;
15500 }
15501# endif
15502#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015503
15504done:
15505 if (infile != NULL)
15506 {
15507 mch_remove(infile);
15508 vim_free(infile);
15509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015510 rettv->v_type = VAR_STRING;
15511 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512}
15513
15514/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015515 * "tabpagebuflist()" function
15516 */
15517/* ARGSUSED */
15518 static void
15519f_tabpagebuflist(argvars, rettv)
15520 typval_T *argvars;
15521 typval_T *rettv;
15522{
15523#ifndef FEAT_WINDOWS
15524 rettv->vval.v_number = 0;
15525#else
15526 tabpage_T *tp;
15527 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015528
15529 if (argvars[0].v_type == VAR_UNKNOWN)
15530 wp = firstwin;
15531 else
15532 {
15533 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15534 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015535 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015536 }
15537 if (wp == NULL)
15538 rettv->vval.v_number = 0;
15539 else
15540 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015541 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015542 rettv->vval.v_number = 0;
15543 else
15544 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015545 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015546 if (list_append_number(rettv->vval.v_list,
15547 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015548 break;
15549 }
15550 }
15551#endif
15552}
15553
15554
15555/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015556 * "tabpagenr()" function
15557 */
15558/* ARGSUSED */
15559 static void
15560f_tabpagenr(argvars, rettv)
15561 typval_T *argvars;
15562 typval_T *rettv;
15563{
15564 int nr = 1;
15565#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015566 char_u *arg;
15567
15568 if (argvars[0].v_type != VAR_UNKNOWN)
15569 {
15570 arg = get_tv_string_chk(&argvars[0]);
15571 nr = 0;
15572 if (arg != NULL)
15573 {
15574 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015575 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015576 else
15577 EMSG2(_(e_invexpr2), arg);
15578 }
15579 }
15580 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015581 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015582#endif
15583 rettv->vval.v_number = nr;
15584}
15585
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015586
15587#ifdef FEAT_WINDOWS
15588static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15589
15590/*
15591 * Common code for tabpagewinnr() and winnr().
15592 */
15593 static int
15594get_winnr(tp, argvar)
15595 tabpage_T *tp;
15596 typval_T *argvar;
15597{
15598 win_T *twin;
15599 int nr = 1;
15600 win_T *wp;
15601 char_u *arg;
15602
15603 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15604 if (argvar->v_type != VAR_UNKNOWN)
15605 {
15606 arg = get_tv_string_chk(argvar);
15607 if (arg == NULL)
15608 nr = 0; /* type error; errmsg already given */
15609 else if (STRCMP(arg, "$") == 0)
15610 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15611 else if (STRCMP(arg, "#") == 0)
15612 {
15613 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15614 if (twin == NULL)
15615 nr = 0;
15616 }
15617 else
15618 {
15619 EMSG2(_(e_invexpr2), arg);
15620 nr = 0;
15621 }
15622 }
15623
15624 if (nr > 0)
15625 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15626 wp != twin; wp = wp->w_next)
15627 ++nr;
15628 return nr;
15629}
15630#endif
15631
15632/*
15633 * "tabpagewinnr()" function
15634 */
15635/* ARGSUSED */
15636 static void
15637f_tabpagewinnr(argvars, rettv)
15638 typval_T *argvars;
15639 typval_T *rettv;
15640{
15641 int nr = 1;
15642#ifdef FEAT_WINDOWS
15643 tabpage_T *tp;
15644
15645 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15646 if (tp == NULL)
15647 nr = 0;
15648 else
15649 nr = get_winnr(tp, &argvars[1]);
15650#endif
15651 rettv->vval.v_number = nr;
15652}
15653
15654
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015655/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015656 * "tagfiles()" function
15657 */
15658/*ARGSUSED*/
15659 static void
15660f_tagfiles(argvars, rettv)
15661 typval_T *argvars;
15662 typval_T *rettv;
15663{
15664 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015665 tagname_T tn;
15666 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015667
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015668 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015669 {
15670 rettv->vval.v_number = 0;
15671 return;
15672 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015673
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015674 for (first = TRUE; ; first = FALSE)
15675 if (get_tagfname(&tn, first, fname) == FAIL
15676 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015677 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015678 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015679}
15680
15681/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015682 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015683 */
15684 static void
15685f_taglist(argvars, rettv)
15686 typval_T *argvars;
15687 typval_T *rettv;
15688{
15689 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015690
15691 tag_pattern = get_tv_string(&argvars[0]);
15692
15693 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015694 if (*tag_pattern == NUL)
15695 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015696
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015697 if (rettv_list_alloc(rettv) == OK)
15698 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015699}
15700
15701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 * "tempname()" function
15703 */
15704/*ARGSUSED*/
15705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015706f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015707 typval_T *argvars;
15708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709{
15710 static int x = 'A';
15711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015712 rettv->v_type = VAR_STRING;
15713 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714
15715 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15716 * names. Skip 'I' and 'O', they are used for shell redirection. */
15717 do
15718 {
15719 if (x == 'Z')
15720 x = '0';
15721 else if (x == '9')
15722 x = 'A';
15723 else
15724 {
15725#ifdef EBCDIC
15726 if (x == 'I')
15727 x = 'J';
15728 else if (x == 'R')
15729 x = 'S';
15730 else
15731#endif
15732 ++x;
15733 }
15734 } while (x == 'I' || x == 'O');
15735}
15736
15737/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015738 * "test(list)" function: Just checking the walls...
15739 */
15740/*ARGSUSED*/
15741 static void
15742f_test(argvars, rettv)
15743 typval_T *argvars;
15744 typval_T *rettv;
15745{
15746 /* Used for unit testing. Change the code below to your liking. */
15747#if 0
15748 listitem_T *li;
15749 list_T *l;
15750 char_u *bad, *good;
15751
15752 if (argvars[0].v_type != VAR_LIST)
15753 return;
15754 l = argvars[0].vval.v_list;
15755 if (l == NULL)
15756 return;
15757 li = l->lv_first;
15758 if (li == NULL)
15759 return;
15760 bad = get_tv_string(&li->li_tv);
15761 li = li->li_next;
15762 if (li == NULL)
15763 return;
15764 good = get_tv_string(&li->li_tv);
15765 rettv->vval.v_number = test_edit_score(bad, good);
15766#endif
15767}
15768
15769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 * "tolower(string)" function
15771 */
15772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 typval_T *argvars;
15775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776{
15777 char_u *p;
15778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015779 p = vim_strsave(get_tv_string(&argvars[0]));
15780 rettv->v_type = VAR_STRING;
15781 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782
15783 if (p != NULL)
15784 while (*p != NUL)
15785 {
15786#ifdef FEAT_MBYTE
15787 int l;
15788
15789 if (enc_utf8)
15790 {
15791 int c, lc;
15792
15793 c = utf_ptr2char(p);
15794 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015795 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 /* TODO: reallocate string when byte count changes. */
15797 if (utf_char2len(lc) == l)
15798 utf_char2bytes(lc, p);
15799 p += l;
15800 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015801 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 p += l; /* skip multi-byte character */
15803 else
15804#endif
15805 {
15806 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15807 ++p;
15808 }
15809 }
15810}
15811
15812/*
15813 * "toupper(string)" function
15814 */
15815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015816f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015817 typval_T *argvars;
15818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015820 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015821 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822}
15823
15824/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015825 * "tr(string, fromstr, tostr)" function
15826 */
15827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015828f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 typval_T *argvars;
15830 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015831{
15832 char_u *instr;
15833 char_u *fromstr;
15834 char_u *tostr;
15835 char_u *p;
15836#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015837 int inlen;
15838 int fromlen;
15839 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015840 int idx;
15841 char_u *cpstr;
15842 int cplen;
15843 int first = TRUE;
15844#endif
15845 char_u buf[NUMBUFLEN];
15846 char_u buf2[NUMBUFLEN];
15847 garray_T ga;
15848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015849 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15851 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015852
15853 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015854 rettv->v_type = VAR_STRING;
15855 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015856 if (fromstr == NULL || tostr == NULL)
15857 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015858 ga_init2(&ga, (int)sizeof(char), 80);
15859
15860#ifdef FEAT_MBYTE
15861 if (!has_mbyte)
15862#endif
15863 /* not multi-byte: fromstr and tostr must be the same length */
15864 if (STRLEN(fromstr) != STRLEN(tostr))
15865 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015866#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015867error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015868#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015869 EMSG2(_(e_invarg2), fromstr);
15870 ga_clear(&ga);
15871 return;
15872 }
15873
15874 /* fromstr and tostr have to contain the same number of chars */
15875 while (*instr != NUL)
15876 {
15877#ifdef FEAT_MBYTE
15878 if (has_mbyte)
15879 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015880 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015881 cpstr = instr;
15882 cplen = inlen;
15883 idx = 0;
15884 for (p = fromstr; *p != NUL; p += fromlen)
15885 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015886 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015887 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15888 {
15889 for (p = tostr; *p != NUL; p += tolen)
15890 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015891 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015892 if (idx-- == 0)
15893 {
15894 cplen = tolen;
15895 cpstr = p;
15896 break;
15897 }
15898 }
15899 if (*p == NUL) /* tostr is shorter than fromstr */
15900 goto error;
15901 break;
15902 }
15903 ++idx;
15904 }
15905
15906 if (first && cpstr == instr)
15907 {
15908 /* Check that fromstr and tostr have the same number of
15909 * (multi-byte) characters. Done only once when a character
15910 * of instr doesn't appear in fromstr. */
15911 first = FALSE;
15912 for (p = tostr; *p != NUL; p += tolen)
15913 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015914 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015915 --idx;
15916 }
15917 if (idx != 0)
15918 goto error;
15919 }
15920
15921 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015922 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015923 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015924
15925 instr += inlen;
15926 }
15927 else
15928#endif
15929 {
15930 /* When not using multi-byte chars we can do it faster. */
15931 p = vim_strchr(fromstr, *instr);
15932 if (p != NULL)
15933 ga_append(&ga, tostr[p - fromstr]);
15934 else
15935 ga_append(&ga, *instr);
15936 ++instr;
15937 }
15938 }
15939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015940 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015941}
15942
15943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944 * "type(expr)" function
15945 */
15946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015947f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015948 typval_T *argvars;
15949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015951 int n;
15952
15953 switch (argvars[0].v_type)
15954 {
15955 case VAR_NUMBER: n = 0; break;
15956 case VAR_STRING: n = 1; break;
15957 case VAR_FUNC: n = 2; break;
15958 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015959 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015960 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15961 }
15962 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963}
15964
15965/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015966 * "values(dict)" function
15967 */
15968 static void
15969f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015970 typval_T *argvars;
15971 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015972{
15973 dict_list(argvars, rettv, 1);
15974}
15975
15976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977 * "virtcol(string)" function
15978 */
15979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015980f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015981 typval_T *argvars;
15982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983{
15984 colnr_T vcol = 0;
15985 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015986 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015988 fp = var2fpos(&argvars[0], FALSE, &fnum);
15989 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15990 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 {
15992 getvvcol(curwin, fp, NULL, NULL, &vcol);
15993 ++vcol;
15994 }
15995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015996 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997}
15998
15999/*
16000 * "visualmode()" function
16001 */
16002/*ARGSUSED*/
16003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016004f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016005 typval_T *argvars;
16006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016007{
16008#ifdef FEAT_VISUAL
16009 char_u str[2];
16010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016011 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 str[0] = curbuf->b_visual_mode_eval;
16013 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016014 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015
16016 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016017 if ((argvars[0].v_type == VAR_NUMBER
16018 && argvars[0].vval.v_number != 0)
16019 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016020 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 curbuf->b_visual_mode_eval = NUL;
16022#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016023 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024#endif
16025}
16026
16027/*
16028 * "winbufnr(nr)" function
16029 */
16030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016031f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016032 typval_T *argvars;
16033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034{
16035 win_T *wp;
16036
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016037 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016041 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042}
16043
16044/*
16045 * "wincol()" function
16046 */
16047/*ARGSUSED*/
16048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016049f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016050 typval_T *argvars;
16051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052{
16053 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055}
16056
16057/*
16058 * "winheight(nr)" function
16059 */
16060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016061f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016062 typval_T *argvars;
16063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064{
16065 win_T *wp;
16066
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016067 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016068 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016069 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016071 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072}
16073
16074/*
16075 * "winline()" function
16076 */
16077/*ARGSUSED*/
16078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016079f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016080 typval_T *argvars;
16081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082{
16083 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016084 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085}
16086
16087/*
16088 * "winnr()" function
16089 */
16090/* ARGSUSED */
16091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016093 typval_T *argvars;
16094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095{
16096 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016097
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016099 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016101 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102}
16103
16104/*
16105 * "winrestcmd()" function
16106 */
16107/* ARGSUSED */
16108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016109f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016110 typval_T *argvars;
16111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112{
16113#ifdef FEAT_WINDOWS
16114 win_T *wp;
16115 int winnr = 1;
16116 garray_T ga;
16117 char_u buf[50];
16118
16119 ga_init2(&ga, (int)sizeof(char), 70);
16120 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16121 {
16122 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16123 ga_concat(&ga, buf);
16124# ifdef FEAT_VERTSPLIT
16125 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16126 ga_concat(&ga, buf);
16127# endif
16128 ++winnr;
16129 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016130 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016132 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016134 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016136 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137}
16138
16139/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016140 * "winrestview()" function
16141 */
16142/* ARGSUSED */
16143 static void
16144f_winrestview(argvars, rettv)
16145 typval_T *argvars;
16146 typval_T *rettv;
16147{
16148 dict_T *dict;
16149
16150 if (argvars[0].v_type != VAR_DICT
16151 || (dict = argvars[0].vval.v_dict) == NULL)
16152 EMSG(_(e_invarg));
16153 else
16154 {
16155 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16156 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16157#ifdef FEAT_VIRTUALEDIT
16158 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16159#endif
16160 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016161 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016162
16163 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16164#ifdef FEAT_DIFF
16165 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16166#endif
16167 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16168 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16169
16170 check_cursor();
16171 changed_cline_bef_curs();
16172 invalidate_botline();
16173 redraw_later(VALID);
16174
16175 if (curwin->w_topline == 0)
16176 curwin->w_topline = 1;
16177 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16178 curwin->w_topline = curbuf->b_ml.ml_line_count;
16179#ifdef FEAT_DIFF
16180 check_topfill(curwin, TRUE);
16181#endif
16182 }
16183}
16184
16185/*
16186 * "winsaveview()" function
16187 */
16188/* ARGSUSED */
16189 static void
16190f_winsaveview(argvars, rettv)
16191 typval_T *argvars;
16192 typval_T *rettv;
16193{
16194 dict_T *dict;
16195
16196 dict = dict_alloc();
16197 if (dict == NULL)
16198 return;
16199 rettv->v_type = VAR_DICT;
16200 rettv->vval.v_dict = dict;
16201 ++dict->dv_refcount;
16202
16203 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16204 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16205#ifdef FEAT_VIRTUALEDIT
16206 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16207#endif
16208 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16209
16210 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16211#ifdef FEAT_DIFF
16212 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16213#endif
16214 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16215 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16216}
16217
16218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 * "winwidth(nr)" function
16220 */
16221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016223 typval_T *argvars;
16224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225{
16226 win_T *wp;
16227
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016228 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 else
16232#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016233 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236#endif
16237}
16238
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016240 * "writefile()" function
16241 */
16242 static void
16243f_writefile(argvars, rettv)
16244 typval_T *argvars;
16245 typval_T *rettv;
16246{
16247 int binary = FALSE;
16248 char_u *fname;
16249 FILE *fd;
16250 listitem_T *li;
16251 char_u *s;
16252 int ret = 0;
16253 int c;
16254
16255 if (argvars[0].v_type != VAR_LIST)
16256 {
16257 EMSG2(_(e_listarg), "writefile()");
16258 return;
16259 }
16260 if (argvars[0].vval.v_list == NULL)
16261 return;
16262
16263 if (argvars[2].v_type != VAR_UNKNOWN
16264 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16265 binary = TRUE;
16266
16267 /* Always open the file in binary mode, library functions have a mind of
16268 * their own about CR-LF conversion. */
16269 fname = get_tv_string(&argvars[1]);
16270 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16271 {
16272 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16273 ret = -1;
16274 }
16275 else
16276 {
16277 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16278 li = li->li_next)
16279 {
16280 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16281 {
16282 if (*s == '\n')
16283 c = putc(NUL, fd);
16284 else
16285 c = putc(*s, fd);
16286 if (c == EOF)
16287 {
16288 ret = -1;
16289 break;
16290 }
16291 }
16292 if (!binary || li->li_next != NULL)
16293 if (putc('\n', fd) == EOF)
16294 {
16295 ret = -1;
16296 break;
16297 }
16298 if (ret < 0)
16299 {
16300 EMSG(_(e_write));
16301 break;
16302 }
16303 }
16304 fclose(fd);
16305 }
16306
16307 rettv->vval.v_number = ret;
16308}
16309
16310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016312 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 */
16314 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016315var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016316 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016318 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016320 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016322 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323
Bram Moolenaara5525202006-03-02 22:52:09 +000016324 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016325 if (varp->v_type == VAR_LIST)
16326 {
16327 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016328 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016329 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016330
16331 l = varp->vval.v_list;
16332 if (l == NULL)
16333 return NULL;
16334
16335 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016336 pos.lnum = list_find_nr(l, 0L, &error);
16337 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016338 return NULL; /* invalid line number */
16339
16340 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016341 pos.col = list_find_nr(l, 1L, &error);
16342 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016343 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016344 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016345 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016346 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016347 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016348 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016349
Bram Moolenaara5525202006-03-02 22:52:09 +000016350#ifdef FEAT_VIRTUALEDIT
16351 /* Get the virtual offset. Defaults to zero. */
16352 pos.coladd = list_find_nr(l, 2L, &error);
16353 if (error)
16354 pos.coladd = 0;
16355#endif
16356
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016357 return &pos;
16358 }
16359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016360 name = get_tv_string_chk(varp);
16361 if (name == NULL)
16362 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 if (name[0] == '.') /* cursor */
16364 return &curwin->w_cursor;
16365 if (name[0] == '\'') /* mark */
16366 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016367 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016368 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16369 return NULL;
16370 return pp;
16371 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016372
16373#ifdef FEAT_VIRTUALEDIT
16374 pos.coladd = 0;
16375#endif
16376
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016377 if (name[0] == 'w' && lnum)
16378 {
16379 pos.col = 0;
16380 if (name[1] == '0') /* "w0": first visible line */
16381 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016382 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016383 pos.lnum = curwin->w_topline;
16384 return &pos;
16385 }
16386 else if (name[1] == '$') /* "w$": last visible line */
16387 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016388 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016389 pos.lnum = curwin->w_botline - 1;
16390 return &pos;
16391 }
16392 }
16393 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 {
16395 if (lnum)
16396 {
16397 pos.lnum = curbuf->b_ml.ml_line_count;
16398 pos.col = 0;
16399 }
16400 else
16401 {
16402 pos.lnum = curwin->w_cursor.lnum;
16403 pos.col = (colnr_T)STRLEN(ml_get_curline());
16404 }
16405 return &pos;
16406 }
16407 return NULL;
16408}
16409
16410/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016411 * Convert list in "arg" into a position and optional file number.
16412 * When "fnump" is NULL there is no file number, only 3 items.
16413 * Note that the column is passed on as-is, the caller may want to decrement
16414 * it to use 1 for the first column.
16415 * Return FAIL when conversion is not possible, doesn't check the position for
16416 * validity.
16417 */
16418 static int
16419list2fpos(arg, posp, fnump)
16420 typval_T *arg;
16421 pos_T *posp;
16422 int *fnump;
16423{
16424 list_T *l = arg->vval.v_list;
16425 long i = 0;
16426 long n;
16427
16428 /* List must be: [fnum, lnum, col, coladd] */
16429 if (arg->v_type != VAR_LIST || l == NULL
16430 || l->lv_len != (fnump == NULL ? 3 : 4))
16431 return FAIL;
16432
16433 if (fnump != NULL)
16434 {
16435 n = list_find_nr(l, i++, NULL); /* fnum */
16436 if (n < 0)
16437 return FAIL;
16438 if (n == 0)
16439 n = curbuf->b_fnum; /* current buffer */
16440 *fnump = n;
16441 }
16442
16443 n = list_find_nr(l, i++, NULL); /* lnum */
16444 if (n < 0)
16445 return FAIL;
16446 posp->lnum = n;
16447
16448 n = list_find_nr(l, i++, NULL); /* col */
16449 if (n < 0)
16450 return FAIL;
16451 posp->col = n;
16452
16453#ifdef FEAT_VIRTUALEDIT
16454 n = list_find_nr(l, i, NULL);
16455 if (n < 0)
16456 return FAIL;
16457 posp->coladd = n;
16458#endif
16459
16460 return OK;
16461}
16462
16463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 * Get the length of an environment variable name.
16465 * Advance "arg" to the first character after the name.
16466 * Return 0 for error.
16467 */
16468 static int
16469get_env_len(arg)
16470 char_u **arg;
16471{
16472 char_u *p;
16473 int len;
16474
16475 for (p = *arg; vim_isIDc(*p); ++p)
16476 ;
16477 if (p == *arg) /* no name found */
16478 return 0;
16479
16480 len = (int)(p - *arg);
16481 *arg = p;
16482 return len;
16483}
16484
16485/*
16486 * Get the length of the name of a function or internal variable.
16487 * "arg" is advanced to the first non-white character after the name.
16488 * Return 0 if something is wrong.
16489 */
16490 static int
16491get_id_len(arg)
16492 char_u **arg;
16493{
16494 char_u *p;
16495 int len;
16496
16497 /* Find the end of the name. */
16498 for (p = *arg; eval_isnamec(*p); ++p)
16499 ;
16500 if (p == *arg) /* no name found */
16501 return 0;
16502
16503 len = (int)(p - *arg);
16504 *arg = skipwhite(p);
16505
16506 return len;
16507}
16508
16509/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016510 * Get the length of the name of a variable or function.
16511 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016513 * Return -1 if curly braces expansion failed.
16514 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 * If the name contains 'magic' {}'s, expand them and return the
16516 * expanded name in an allocated string via 'alias' - caller must free.
16517 */
16518 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016519get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 char_u **arg;
16521 char_u **alias;
16522 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016523 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524{
16525 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526 char_u *p;
16527 char_u *expr_start;
16528 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529
16530 *alias = NULL; /* default to no alias */
16531
16532 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16533 && (*arg)[2] == (int)KE_SNR)
16534 {
16535 /* hard coded <SNR>, already translated */
16536 *arg += 3;
16537 return get_id_len(arg) + 3;
16538 }
16539 len = eval_fname_script(*arg);
16540 if (len > 0)
16541 {
16542 /* literal "<SID>", "s:" or "<SNR>" */
16543 *arg += len;
16544 }
16545
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016547 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016549 p = find_name_end(*arg, &expr_start, &expr_end,
16550 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 if (expr_start != NULL)
16552 {
16553 char_u *temp_string;
16554
16555 if (!evaluate)
16556 {
16557 len += (int)(p - *arg);
16558 *arg = skipwhite(p);
16559 return len;
16560 }
16561
16562 /*
16563 * Include any <SID> etc in the expanded string:
16564 * Thus the -len here.
16565 */
16566 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16567 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016568 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016569 *alias = temp_string;
16570 *arg = skipwhite(p);
16571 return (int)STRLEN(temp_string);
16572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573
16574 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016575 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 EMSG2(_(e_invexpr2), *arg);
16577
16578 return len;
16579}
16580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016581/*
16582 * Find the end of a variable or function name, taking care of magic braces.
16583 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16584 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016585 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016586 * Return a pointer to just after the name. Equal to "arg" if there is no
16587 * valid name.
16588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016590find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 char_u *arg;
16592 char_u **expr_start;
16593 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016594 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016596 int mb_nest = 0;
16597 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 char_u *p;
16599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016600 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016602 *expr_start = NULL;
16603 *expr_end = NULL;
16604 }
16605
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016606 /* Quick check for valid starting character. */
16607 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16608 return arg;
16609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016610 for (p = arg; *p != NUL
16611 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016612 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016613 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016614 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016615 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016616 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016617 if (*p == '\'')
16618 {
16619 /* skip over 'string' to avoid counting [ and ] inside it. */
16620 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16621 ;
16622 if (*p == NUL)
16623 break;
16624 }
16625 else if (*p == '"')
16626 {
16627 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16628 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16629 if (*p == '\\' && p[1] != NUL)
16630 ++p;
16631 if (*p == NUL)
16632 break;
16633 }
16634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016635 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637 if (*p == '[')
16638 ++br_nest;
16639 else if (*p == ']')
16640 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645 if (*p == '{')
16646 {
16647 mb_nest++;
16648 if (expr_start != NULL && *expr_start == NULL)
16649 *expr_start = p;
16650 }
16651 else if (*p == '}')
16652 {
16653 mb_nest--;
16654 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16655 *expr_end = p;
16656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 }
16659
16660 return p;
16661}
16662
16663/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016664 * Expands out the 'magic' {}'s in a variable/function name.
16665 * Note that this can call itself recursively, to deal with
16666 * constructs like foo{bar}{baz}{bam}
16667 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16668 * "in_start" ^
16669 * "expr_start" ^
16670 * "expr_end" ^
16671 * "in_end" ^
16672 *
16673 * Returns a new allocated string, which the caller must free.
16674 * Returns NULL for failure.
16675 */
16676 static char_u *
16677make_expanded_name(in_start, expr_start, expr_end, in_end)
16678 char_u *in_start;
16679 char_u *expr_start;
16680 char_u *expr_end;
16681 char_u *in_end;
16682{
16683 char_u c1;
16684 char_u *retval = NULL;
16685 char_u *temp_result;
16686 char_u *nextcmd = NULL;
16687
16688 if (expr_end == NULL || in_end == NULL)
16689 return NULL;
16690 *expr_start = NUL;
16691 *expr_end = NUL;
16692 c1 = *in_end;
16693 *in_end = NUL;
16694
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016695 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016696 if (temp_result != NULL && nextcmd == NULL)
16697 {
16698 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16699 + (in_end - expr_end) + 1));
16700 if (retval != NULL)
16701 {
16702 STRCPY(retval, in_start);
16703 STRCAT(retval, temp_result);
16704 STRCAT(retval, expr_end + 1);
16705 }
16706 }
16707 vim_free(temp_result);
16708
16709 *in_end = c1; /* put char back for error messages */
16710 *expr_start = '{';
16711 *expr_end = '}';
16712
16713 if (retval != NULL)
16714 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016715 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016716 if (expr_start != NULL)
16717 {
16718 /* Further expansion! */
16719 temp_result = make_expanded_name(retval, expr_start,
16720 expr_end, temp_result);
16721 vim_free(retval);
16722 retval = temp_result;
16723 }
16724 }
16725
16726 return retval;
16727}
16728
16729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016731 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 */
16733 static int
16734eval_isnamec(c)
16735 int c;
16736{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016737 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16738}
16739
16740/*
16741 * Return TRUE if character "c" can be used as the first character in a
16742 * variable or function name (excluding '{' and '}').
16743 */
16744 static int
16745eval_isnamec1(c)
16746 int c;
16747{
16748 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749}
16750
16751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752 * Set number v: variable to "val".
16753 */
16754 void
16755set_vim_var_nr(idx, val)
16756 int idx;
16757 long val;
16758{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016759 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760}
16761
16762/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016763 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764 */
16765 long
16766get_vim_var_nr(idx)
16767 int idx;
16768{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016769 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770}
16771
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016772#if defined(FEAT_AUTOCMD) || defined(PROTO)
16773/*
16774 * Get string v: variable value. Uses a static buffer, can only be used once.
16775 */
16776 char_u *
16777get_vim_var_str(idx)
16778 int idx;
16779{
16780 return get_tv_string(&vimvars[idx].vv_tv);
16781}
16782#endif
16783
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784/*
16785 * Set v:count, v:count1 and v:prevcount.
16786 */
16787 void
16788set_vcount(count, count1)
16789 long count;
16790 long count1;
16791{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016792 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16793 vimvars[VV_COUNT].vv_nr = count;
16794 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795}
16796
16797/*
16798 * Set string v: variable to a copy of "val".
16799 */
16800 void
16801set_vim_var_string(idx, val, len)
16802 int idx;
16803 char_u *val;
16804 int len; /* length of "val" to use or -1 (whole string) */
16805{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016806 /* Need to do this (at least) once, since we can't initialize a union.
16807 * Will always be invoked when "v:progname" is set. */
16808 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16809
Bram Moolenaare9a41262005-01-15 22:18:47 +000016810 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016812 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016814 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016816 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817}
16818
16819/*
16820 * Set v:register if needed.
16821 */
16822 void
16823set_reg_var(c)
16824 int c;
16825{
16826 char_u regname;
16827
16828 if (c == 0 || c == ' ')
16829 regname = '"';
16830 else
16831 regname = c;
16832 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016833 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 set_vim_var_string(VV_REG, &regname, 1);
16835}
16836
16837/*
16838 * Get or set v:exception. If "oldval" == NULL, return the current value.
16839 * Otherwise, restore the value to "oldval" and return NULL.
16840 * Must always be called in pairs to save and restore v:exception! Does not
16841 * take care of memory allocations.
16842 */
16843 char_u *
16844v_exception(oldval)
16845 char_u *oldval;
16846{
16847 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016848 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849
Bram Moolenaare9a41262005-01-15 22:18:47 +000016850 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 return NULL;
16852}
16853
16854/*
16855 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16856 * Otherwise, restore the value to "oldval" and return NULL.
16857 * Must always be called in pairs to save and restore v:throwpoint! Does not
16858 * take care of memory allocations.
16859 */
16860 char_u *
16861v_throwpoint(oldval)
16862 char_u *oldval;
16863{
16864 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016865 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866
Bram Moolenaare9a41262005-01-15 22:18:47 +000016867 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 return NULL;
16869}
16870
16871#if defined(FEAT_AUTOCMD) || defined(PROTO)
16872/*
16873 * Set v:cmdarg.
16874 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16875 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16876 * Must always be called in pairs!
16877 */
16878 char_u *
16879set_cmdarg(eap, oldarg)
16880 exarg_T *eap;
16881 char_u *oldarg;
16882{
16883 char_u *oldval;
16884 char_u *newval;
16885 unsigned len;
16886
Bram Moolenaare9a41262005-01-15 22:18:47 +000016887 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016888 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016890 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016891 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016892 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 }
16894
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016895 if (eap->force_bin == FORCE_BIN)
16896 len = 6;
16897 else if (eap->force_bin == FORCE_NOBIN)
16898 len = 8;
16899 else
16900 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016901
16902 if (eap->read_edit)
16903 len += 7;
16904
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016905 if (eap->force_ff != 0)
16906 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16907# ifdef FEAT_MBYTE
16908 if (eap->force_enc != 0)
16909 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016910 if (eap->bad_char != 0)
16911 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016912# endif
16913
16914 newval = alloc(len + 1);
16915 if (newval == NULL)
16916 return NULL;
16917
16918 if (eap->force_bin == FORCE_BIN)
16919 sprintf((char *)newval, " ++bin");
16920 else if (eap->force_bin == FORCE_NOBIN)
16921 sprintf((char *)newval, " ++nobin");
16922 else
16923 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016924
16925 if (eap->read_edit)
16926 STRCAT(newval, " ++edit");
16927
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016928 if (eap->force_ff != 0)
16929 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16930 eap->cmd + eap->force_ff);
16931# ifdef FEAT_MBYTE
16932 if (eap->force_enc != 0)
16933 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16934 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016935 if (eap->bad_char != 0)
16936 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16937 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016938# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016939 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016940 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941}
16942#endif
16943
16944/*
16945 * Get the value of internal variable "name".
16946 * Return OK or FAIL.
16947 */
16948 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016949get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 char_u *name;
16951 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016952 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016953 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954{
16955 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016956 typval_T *tv = NULL;
16957 typval_T atv;
16958 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960
16961 /* truncate the name, so that we can use strcmp() */
16962 cc = name[len];
16963 name[len] = NUL;
16964
16965 /*
16966 * Check for "b:changedtick".
16967 */
16968 if (STRCMP(name, "b:changedtick") == 0)
16969 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016970 atv.v_type = VAR_NUMBER;
16971 atv.vval.v_number = curbuf->b_changedtick;
16972 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 }
16974
16975 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 * Check for user-defined variables.
16977 */
16978 else
16979 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016980 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 }
16984
Bram Moolenaare9a41262005-01-15 22:18:47 +000016985 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016987 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016988 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989 ret = FAIL;
16990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016991 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016992 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993
16994 name[len] = cc;
16995
16996 return ret;
16997}
16998
16999/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017000 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17001 * Also handle function call with Funcref variable: func(expr)
17002 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17003 */
17004 static int
17005handle_subscript(arg, rettv, evaluate, verbose)
17006 char_u **arg;
17007 typval_T *rettv;
17008 int evaluate; /* do more than finding the end */
17009 int verbose; /* give error messages */
17010{
17011 int ret = OK;
17012 dict_T *selfdict = NULL;
17013 char_u *s;
17014 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017015 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017016
17017 while (ret == OK
17018 && (**arg == '['
17019 || (**arg == '.' && rettv->v_type == VAR_DICT)
17020 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17021 && !vim_iswhite(*(*arg - 1)))
17022 {
17023 if (**arg == '(')
17024 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017025 /* need to copy the funcref so that we can clear rettv */
17026 functv = *rettv;
17027 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017028
17029 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017030 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017031 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017032 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17033 &len, evaluate, selfdict);
17034
17035 /* Clear the funcref afterwards, so that deleting it while
17036 * evaluating the arguments is possible (see test55). */
17037 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017038
17039 /* Stop the expression evaluation when immediately aborting on
17040 * error, or when an interrupt occurred or an exception was thrown
17041 * but not caught. */
17042 if (aborting())
17043 {
17044 if (ret == OK)
17045 clear_tv(rettv);
17046 ret = FAIL;
17047 }
17048 dict_unref(selfdict);
17049 selfdict = NULL;
17050 }
17051 else /* **arg == '[' || **arg == '.' */
17052 {
17053 dict_unref(selfdict);
17054 if (rettv->v_type == VAR_DICT)
17055 {
17056 selfdict = rettv->vval.v_dict;
17057 if (selfdict != NULL)
17058 ++selfdict->dv_refcount;
17059 }
17060 else
17061 selfdict = NULL;
17062 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17063 {
17064 clear_tv(rettv);
17065 ret = FAIL;
17066 }
17067 }
17068 }
17069 dict_unref(selfdict);
17070 return ret;
17071}
17072
17073/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017074 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17075 * value).
17076 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017077 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017078alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017079{
Bram Moolenaar33570922005-01-25 22:26:29 +000017080 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017081}
17082
17083/*
17084 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 * The string "s" must have been allocated, it is consumed.
17086 * Return NULL for out of memory, the variable otherwise.
17087 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017089alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 char_u *s;
17091{
Bram Moolenaar33570922005-01-25 22:26:29 +000017092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017094 rettv = alloc_tv();
17095 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017097 rettv->v_type = VAR_STRING;
17098 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099 }
17100 else
17101 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017102 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103}
17104
17105/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017106 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017108 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017109free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017110 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111{
17112 if (varp != NULL)
17113 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017114 switch (varp->v_type)
17115 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017116 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017117 func_unref(varp->vval.v_string);
17118 /*FALLTHROUGH*/
17119 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017120 vim_free(varp->vval.v_string);
17121 break;
17122 case VAR_LIST:
17123 list_unref(varp->vval.v_list);
17124 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017125 case VAR_DICT:
17126 dict_unref(varp->vval.v_dict);
17127 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017128 case VAR_NUMBER:
17129 case VAR_UNKNOWN:
17130 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017131 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017132 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017133 break;
17134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135 vim_free(varp);
17136 }
17137}
17138
17139/*
17140 * Free the memory for a variable value and set the value to NULL or 0.
17141 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017142 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017143clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145{
17146 if (varp != NULL)
17147 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017148 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017150 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017151 func_unref(varp->vval.v_string);
17152 /*FALLTHROUGH*/
17153 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017154 vim_free(varp->vval.v_string);
17155 varp->vval.v_string = NULL;
17156 break;
17157 case VAR_LIST:
17158 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017159 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017160 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017161 case VAR_DICT:
17162 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017163 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017164 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017166 varp->vval.v_number = 0;
17167 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168 case VAR_UNKNOWN:
17169 break;
17170 default:
17171 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017173 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 }
17175}
17176
17177/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017178 * Set the value of a variable to NULL without freeing items.
17179 */
17180 static void
17181init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017182 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017183{
17184 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017185 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186}
17187
17188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 * Get the number value of a variable.
17190 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017191 * For incompatible types, return 0.
17192 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17193 * caller of incompatible types: it sets *denote to TRUE if "denote"
17194 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 */
17196 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017197get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017200 int error = FALSE;
17201
17202 return get_tv_number_chk(varp, &error); /* return 0L on error */
17203}
17204
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017205 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017206get_tv_number_chk(varp, denote)
17207 typval_T *varp;
17208 int *denote;
17209{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017210 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017212 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017214 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017215 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017216 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017217 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017218 break;
17219 case VAR_STRING:
17220 if (varp->vval.v_string != NULL)
17221 vim_str2nr(varp->vval.v_string, NULL, NULL,
17222 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017223 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017224 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017225 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017226 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017227 case VAR_DICT:
17228 EMSG(_("E728: Using a Dictionary as a number"));
17229 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017230 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017231 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017232 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 if (denote == NULL) /* useful for values that must be unsigned */
17235 n = -1;
17236 else
17237 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017238 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239}
17240
17241/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017242 * Get the lnum from the first argument.
17243 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017244 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 */
17246 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017247get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017248 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249{
Bram Moolenaar33570922005-01-25 22:26:29 +000017250 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 linenr_T lnum;
17252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017253 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254 if (lnum == 0) /* no valid number, try using line() */
17255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017256 rettv.v_type = VAR_NUMBER;
17257 f_line(argvars, &rettv);
17258 lnum = rettv.vval.v_number;
17259 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260 }
17261 return lnum;
17262}
17263
17264/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017265 * Get the lnum from the first argument.
17266 * Also accepts "$", then "buf" is used.
17267 * Returns 0 on error.
17268 */
17269 static linenr_T
17270get_tv_lnum_buf(argvars, buf)
17271 typval_T *argvars;
17272 buf_T *buf;
17273{
17274 if (argvars[0].v_type == VAR_STRING
17275 && argvars[0].vval.v_string != NULL
17276 && argvars[0].vval.v_string[0] == '$'
17277 && buf != NULL)
17278 return buf->b_ml.ml_line_count;
17279 return get_tv_number_chk(&argvars[0], NULL);
17280}
17281
17282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 * Get the string value of a variable.
17284 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017285 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17286 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 * If the String variable has never been set, return an empty string.
17288 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017289 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17290 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 */
17292 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017294 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295{
17296 static char_u mybuf[NUMBUFLEN];
17297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017298 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299}
17300
17301 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017303 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017304 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017306 char_u *res = get_tv_string_buf_chk(varp, buf);
17307
17308 return res != NULL ? res : (char_u *)"";
17309}
17310
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017311 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017312get_tv_string_chk(varp)
17313 typval_T *varp;
17314{
17315 static char_u mybuf[NUMBUFLEN];
17316
17317 return get_tv_string_buf_chk(varp, mybuf);
17318}
17319
17320 static char_u *
17321get_tv_string_buf_chk(varp, buf)
17322 typval_T *varp;
17323 char_u *buf;
17324{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017325 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017327 case VAR_NUMBER:
17328 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17329 return buf;
17330 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017331 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017332 break;
17333 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017334 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017335 break;
17336 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017337 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017338 break;
17339 case VAR_STRING:
17340 if (varp->vval.v_string != NULL)
17341 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017342 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017343 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017344 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017345 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017347 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348}
17349
17350/*
17351 * Find variable "name" in the list of variables.
17352 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017353 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017354 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017355 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017358find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017363 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364
Bram Moolenaara7043832005-01-21 11:56:39 +000017365 ht = find_var_ht(name, &varname);
17366 if (htp != NULL)
17367 *htp = ht;
17368 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017370 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371}
17372
17373/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017374 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017375 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017378find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017380 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017381 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017382{
Bram Moolenaar33570922005-01-25 22:26:29 +000017383 hashitem_T *hi;
17384
17385 if (*varname == NUL)
17386 {
17387 /* Must be something like "s:", otherwise "ht" would be NULL. */
17388 switch (varname[-2])
17389 {
17390 case 's': return &SCRIPT_SV(current_SID).sv_var;
17391 case 'g': return &globvars_var;
17392 case 'v': return &vimvars_var;
17393 case 'b': return &curbuf->b_bufvar;
17394 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017395#ifdef FEAT_WINDOWS
17396 case 't': return &curtab->tp_winvar;
17397#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017398 case 'l': return current_funccal == NULL
17399 ? NULL : &current_funccal->l_vars_var;
17400 case 'a': return current_funccal == NULL
17401 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 }
17403 return NULL;
17404 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017405
17406 hi = hash_find(ht, varname);
17407 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017408 {
17409 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017410 * worked find the variable again. Don't auto-load a script if it was
17411 * loaded already, otherwise it would be loaded every time when
17412 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017413 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017414 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017415 hi = hash_find(ht, varname);
17416 if (HASHITEM_EMPTY(hi))
17417 return NULL;
17418 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017419 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017420}
17421
17422/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017423 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017424 * Set "varname" to the start of name without ':'.
17425 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017426 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017427find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 char_u *name;
17429 char_u **varname;
17430{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017431 hashitem_T *hi;
17432
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433 if (name[1] != ':')
17434 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017435 /* The name must not start with a colon or #. */
17436 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 return NULL;
17438 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017439
17440 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017441 hi = hash_find(&compat_hashtab, name);
17442 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017443 return &compat_hashtab;
17444
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017446 return &globvarht; /* global variable */
17447 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 }
17449 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017450 if (*name == 'g') /* global variable */
17451 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017452 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17453 */
17454 if (vim_strchr(name + 2, ':') != NULL
17455 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017456 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017458 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017460 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017461#ifdef FEAT_WINDOWS
17462 if (*name == 't') /* tab page variable */
17463 return &curtab->tp_vars.dv_hashtab;
17464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 if (*name == 'v') /* v: variable */
17466 return &vimvarht;
17467 if (*name == 'a' && current_funccal != NULL) /* function argument */
17468 return &current_funccal->l_avars.dv_hashtab;
17469 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17470 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 if (*name == 's' /* script variable */
17472 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17473 return &SCRIPT_VARS(current_SID);
17474 return NULL;
17475}
17476
17477/*
17478 * Get the string value of a (global/local) variable.
17479 * Returns NULL when it doesn't exist.
17480 */
17481 char_u *
17482get_var_value(name)
17483 char_u *name;
17484{
Bram Moolenaar33570922005-01-25 22:26:29 +000017485 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486
Bram Moolenaara7043832005-01-21 11:56:39 +000017487 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 if (v == NULL)
17489 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017490 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491}
17492
17493/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017494 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 * sourcing this script and when executing functions defined in the script.
17496 */
17497 void
17498new_script_vars(id)
17499 scid_T id;
17500{
Bram Moolenaara7043832005-01-21 11:56:39 +000017501 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017502 hashtab_T *ht;
17503 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017504
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17506 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017507 /* Re-allocating ga_data means that an ht_array pointing to
17508 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017510 for (i = 1; i <= ga_scripts.ga_len; ++i)
17511 {
17512 ht = &SCRIPT_VARS(i);
17513 if (ht->ht_mask == HT_INIT_SIZE - 1)
17514 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 sv = &SCRIPT_SV(i);
17516 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017517 }
17518
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 while (ga_scripts.ga_len < id)
17520 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017521 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17522 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 }
17525 }
17526}
17527
17528/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017529 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17530 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 */
17532 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017533init_var_dict(dict, dict_var)
17534 dict_T *dict;
17535 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536{
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 hash_init(&dict->dv_hashtab);
17538 dict->dv_refcount = 99999;
17539 dict_var->di_tv.vval.v_dict = dict;
17540 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017541 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017542 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17543 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544}
17545
17546/*
17547 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017548 * Frees all allocated variables and the value they contain.
17549 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 */
17551 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017552vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017553 hashtab_T *ht;
17554{
17555 vars_clear_ext(ht, TRUE);
17556}
17557
17558/*
17559 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17560 */
17561 static void
17562vars_clear_ext(ht, free_val)
17563 hashtab_T *ht;
17564 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565{
Bram Moolenaara7043832005-01-21 11:56:39 +000017566 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017567 hashitem_T *hi;
17568 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569
Bram Moolenaar33570922005-01-25 22:26:29 +000017570 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017571 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017572 for (hi = ht->ht_array; todo > 0; ++hi)
17573 {
17574 if (!HASHITEM_EMPTY(hi))
17575 {
17576 --todo;
17577
Bram Moolenaar33570922005-01-25 22:26:29 +000017578 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017579 * ht_array might change then. hash_clear() takes care of it
17580 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017581 v = HI2DI(hi);
17582 if (free_val)
17583 clear_tv(&v->di_tv);
17584 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17585 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017586 }
17587 }
17588 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017589 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590}
17591
Bram Moolenaara7043832005-01-21 11:56:39 +000017592/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017593 * Delete a variable from hashtab "ht" at item "hi".
17594 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017597delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 hashtab_T *ht;
17599 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600{
Bram Moolenaar33570922005-01-25 22:26:29 +000017601 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017602
17603 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017604 clear_tv(&di->di_tv);
17605 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606}
17607
17608/*
17609 * List the value of one internal variable.
17610 */
17611 static void
17612list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017613 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 char_u *prefix;
17615{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017616 char_u *tofree;
17617 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017618 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017619
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017620 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017621 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017622 s == NULL ? (char_u *)"" : s);
17623 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624}
17625
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 static void
17627list_one_var_a(prefix, name, type, string)
17628 char_u *prefix;
17629 char_u *name;
17630 int type;
17631 char_u *string;
17632{
17633 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17634 if (name != NULL) /* "a:" vars don't have a name stored */
17635 msg_puts(name);
17636 msg_putchar(' ');
17637 msg_advance(22);
17638 if (type == VAR_NUMBER)
17639 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017640 else if (type == VAR_FUNC)
17641 msg_putchar('*');
17642 else if (type == VAR_LIST)
17643 {
17644 msg_putchar('[');
17645 if (*string == '[')
17646 ++string;
17647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017648 else if (type == VAR_DICT)
17649 {
17650 msg_putchar('{');
17651 if (*string == '{')
17652 ++string;
17653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 else
17655 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017656
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017658
17659 if (type == VAR_FUNC)
17660 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661}
17662
17663/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 * If the variable already exists, the value is updated.
17666 * Otherwise the variable is created.
17667 */
17668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017669set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017671 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017672 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673{
Bram Moolenaar33570922005-01-25 22:26:29 +000017674 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017676 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017677 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017680 {
17681 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17682 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17683 ? name[2] : name[0]))
17684 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017685 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017686 return;
17687 }
17688 if (function_exists(name))
17689 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017690 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017691 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017692 return;
17693 }
17694 }
17695
Bram Moolenaara7043832005-01-21 11:56:39 +000017696 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017697 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017698 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017699 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017700 return;
17701 }
17702
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017703 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017704 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017706 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017707 if (var_check_ro(v->di_flags, name)
17708 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017709 return;
17710 if (v->di_tv.v_type != tv->v_type
17711 && !((v->di_tv.v_type == VAR_STRING
17712 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 && (tv->v_type == VAR_STRING
17714 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017715 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017716 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017717 return;
17718 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017719
17720 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017721 * Handle setting internal v: variables separately: we don't change
17722 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017723 */
17724 if (ht == &vimvarht)
17725 {
17726 if (v->di_tv.v_type == VAR_STRING)
17727 {
17728 vim_free(v->di_tv.vval.v_string);
17729 if (copy || tv->v_type != VAR_STRING)
17730 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17731 else
17732 {
17733 /* Take over the string to avoid an extra alloc/free. */
17734 v->di_tv.vval.v_string = tv->vval.v_string;
17735 tv->vval.v_string = NULL;
17736 }
17737 }
17738 else if (v->di_tv.v_type != VAR_NUMBER)
17739 EMSG2(_(e_intern2), "set_var()");
17740 else
17741 v->di_tv.vval.v_number = get_tv_number(tv);
17742 return;
17743 }
17744
17745 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 }
17747 else /* add a new variable */
17748 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017749 /* Make sure the variable name is valid. */
17750 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017751 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17752 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017753 {
17754 EMSG2(_(e_illvar), varname);
17755 return;
17756 }
17757
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017758 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17759 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017760 if (v == NULL)
17761 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017762 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017765 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 return;
17767 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017768 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017771 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017772 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017773 else
17774 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017776 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017777 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779}
17780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017781/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017782 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17783 * Also give an error message.
17784 */
17785 static int
17786var_check_ro(flags, name)
17787 int flags;
17788 char_u *name;
17789{
17790 if (flags & DI_FLAGS_RO)
17791 {
17792 EMSG2(_(e_readonlyvar), name);
17793 return TRUE;
17794 }
17795 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17796 {
17797 EMSG2(_(e_readonlysbx), name);
17798 return TRUE;
17799 }
17800 return FALSE;
17801}
17802
17803/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017804 * Return TRUE if typeval "tv" is set to be locked (immutable).
17805 * Also give an error message, using "name".
17806 */
17807 static int
17808tv_check_lock(lock, name)
17809 int lock;
17810 char_u *name;
17811{
17812 if (lock & VAR_LOCKED)
17813 {
17814 EMSG2(_("E741: Value is locked: %s"),
17815 name == NULL ? (char_u *)_("Unknown") : name);
17816 return TRUE;
17817 }
17818 if (lock & VAR_FIXED)
17819 {
17820 EMSG2(_("E742: Cannot change value of %s"),
17821 name == NULL ? (char_u *)_("Unknown") : name);
17822 return TRUE;
17823 }
17824 return FALSE;
17825}
17826
17827/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017828 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017829 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017830 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017833copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017834 typval_T *from;
17835 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017837 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017838 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017839 switch (from->v_type)
17840 {
17841 case VAR_NUMBER:
17842 to->vval.v_number = from->vval.v_number;
17843 break;
17844 case VAR_STRING:
17845 case VAR_FUNC:
17846 if (from->vval.v_string == NULL)
17847 to->vval.v_string = NULL;
17848 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017849 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017850 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017851 if (from->v_type == VAR_FUNC)
17852 func_ref(to->vval.v_string);
17853 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017854 break;
17855 case VAR_LIST:
17856 if (from->vval.v_list == NULL)
17857 to->vval.v_list = NULL;
17858 else
17859 {
17860 to->vval.v_list = from->vval.v_list;
17861 ++to->vval.v_list->lv_refcount;
17862 }
17863 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017864 case VAR_DICT:
17865 if (from->vval.v_dict == NULL)
17866 to->vval.v_dict = NULL;
17867 else
17868 {
17869 to->vval.v_dict = from->vval.v_dict;
17870 ++to->vval.v_dict->dv_refcount;
17871 }
17872 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017873 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017874 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017875 break;
17876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877}
17878
17879/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017880 * Make a copy of an item.
17881 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017882 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17883 * reference to an already copied list/dict can be used.
17884 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017885 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017886 static int
17887item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017888 typval_T *from;
17889 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017890 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017891 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017892{
17893 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017894 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017895
Bram Moolenaar33570922005-01-25 22:26:29 +000017896 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017897 {
17898 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017899 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017900 }
17901 ++recurse;
17902
17903 switch (from->v_type)
17904 {
17905 case VAR_NUMBER:
17906 case VAR_STRING:
17907 case VAR_FUNC:
17908 copy_tv(from, to);
17909 break;
17910 case VAR_LIST:
17911 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017912 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017913 if (from->vval.v_list == NULL)
17914 to->vval.v_list = NULL;
17915 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17916 {
17917 /* use the copy made earlier */
17918 to->vval.v_list = from->vval.v_list->lv_copylist;
17919 ++to->vval.v_list->lv_refcount;
17920 }
17921 else
17922 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17923 if (to->vval.v_list == NULL)
17924 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017925 break;
17926 case VAR_DICT:
17927 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017928 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017929 if (from->vval.v_dict == NULL)
17930 to->vval.v_dict = NULL;
17931 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17932 {
17933 /* use the copy made earlier */
17934 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17935 ++to->vval.v_dict->dv_refcount;
17936 }
17937 else
17938 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17939 if (to->vval.v_dict == NULL)
17940 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017941 break;
17942 default:
17943 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017944 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017945 }
17946 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017947 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017948}
17949
17950/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 * ":echo expr1 ..." print each argument separated with a space, add a
17952 * newline at the end.
17953 * ":echon expr1 ..." print each argument plain.
17954 */
17955 void
17956ex_echo(eap)
17957 exarg_T *eap;
17958{
17959 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017960 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017961 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962 char_u *p;
17963 int needclr = TRUE;
17964 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017965 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966
17967 if (eap->skip)
17968 ++emsg_skip;
17969 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17970 {
17971 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017972 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 {
17974 /*
17975 * Report the invalid expression unless the expression evaluation
17976 * has been cancelled due to an aborting error, an interrupt, or an
17977 * exception.
17978 */
17979 if (!aborting())
17980 EMSG2(_(e_invexpr2), p);
17981 break;
17982 }
17983 if (!eap->skip)
17984 {
17985 if (atstart)
17986 {
17987 atstart = FALSE;
17988 /* Call msg_start() after eval1(), evaluating the expression
17989 * may cause a message to appear. */
17990 if (eap->cmdidx == CMD_echo)
17991 msg_start();
17992 }
17993 else if (eap->cmdidx == CMD_echo)
17994 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017995 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017996 if (p != NULL)
17997 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017999 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018001 if (*p != TAB && needclr)
18002 {
18003 /* remove any text still there from the command */
18004 msg_clr_eos();
18005 needclr = FALSE;
18006 }
18007 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 }
18009 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018010 {
18011#ifdef FEAT_MBYTE
18012 if (has_mbyte)
18013 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018014 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018015
18016 (void)msg_outtrans_len_attr(p, i, echo_attr);
18017 p += i - 1;
18018 }
18019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018021 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018024 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018026 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 arg = skipwhite(arg);
18028 }
18029 eap->nextcmd = check_nextcmd(arg);
18030
18031 if (eap->skip)
18032 --emsg_skip;
18033 else
18034 {
18035 /* remove text that may still be there from the command */
18036 if (needclr)
18037 msg_clr_eos();
18038 if (eap->cmdidx == CMD_echo)
18039 msg_end();
18040 }
18041}
18042
18043/*
18044 * ":echohl {name}".
18045 */
18046 void
18047ex_echohl(eap)
18048 exarg_T *eap;
18049{
18050 int id;
18051
18052 id = syn_name2id(eap->arg);
18053 if (id == 0)
18054 echo_attr = 0;
18055 else
18056 echo_attr = syn_id2attr(id);
18057}
18058
18059/*
18060 * ":execute expr1 ..." execute the result of an expression.
18061 * ":echomsg expr1 ..." Print a message
18062 * ":echoerr expr1 ..." Print an error
18063 * Each gets spaces around each argument and a newline at the end for
18064 * echo commands
18065 */
18066 void
18067ex_execute(eap)
18068 exarg_T *eap;
18069{
18070 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018071 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 int ret = OK;
18073 char_u *p;
18074 garray_T ga;
18075 int len;
18076 int save_did_emsg;
18077
18078 ga_init2(&ga, 1, 80);
18079
18080 if (eap->skip)
18081 ++emsg_skip;
18082 while (*arg != NUL && *arg != '|' && *arg != '\n')
18083 {
18084 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 {
18087 /*
18088 * Report the invalid expression unless the expression evaluation
18089 * has been cancelled due to an aborting error, an interrupt, or an
18090 * exception.
18091 */
18092 if (!aborting())
18093 EMSG2(_(e_invexpr2), p);
18094 ret = FAIL;
18095 break;
18096 }
18097
18098 if (!eap->skip)
18099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 len = (int)STRLEN(p);
18102 if (ga_grow(&ga, len + 2) == FAIL)
18103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018104 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 ret = FAIL;
18106 break;
18107 }
18108 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 ga.ga_len += len;
18112 }
18113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 arg = skipwhite(arg);
18116 }
18117
18118 if (ret != FAIL && ga.ga_data != NULL)
18119 {
18120 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018123 out_flush();
18124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 else if (eap->cmdidx == CMD_echoerr)
18126 {
18127 /* We don't want to abort following commands, restore did_emsg. */
18128 save_did_emsg = did_emsg;
18129 EMSG((char_u *)ga.ga_data);
18130 if (!force_abort)
18131 did_emsg = save_did_emsg;
18132 }
18133 else if (eap->cmdidx == CMD_execute)
18134 do_cmdline((char_u *)ga.ga_data,
18135 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18136 }
18137
18138 ga_clear(&ga);
18139
18140 if (eap->skip)
18141 --emsg_skip;
18142
18143 eap->nextcmd = check_nextcmd(arg);
18144}
18145
18146/*
18147 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18148 * "arg" points to the "&" or '+' when called, to "option" when returning.
18149 * Returns NULL when no option name found. Otherwise pointer to the char
18150 * after the option name.
18151 */
18152 static char_u *
18153find_option_end(arg, opt_flags)
18154 char_u **arg;
18155 int *opt_flags;
18156{
18157 char_u *p = *arg;
18158
18159 ++p;
18160 if (*p == 'g' && p[1] == ':')
18161 {
18162 *opt_flags = OPT_GLOBAL;
18163 p += 2;
18164 }
18165 else if (*p == 'l' && p[1] == ':')
18166 {
18167 *opt_flags = OPT_LOCAL;
18168 p += 2;
18169 }
18170 else
18171 *opt_flags = 0;
18172
18173 if (!ASCII_ISALPHA(*p))
18174 return NULL;
18175 *arg = p;
18176
18177 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18178 p += 4; /* termcap option */
18179 else
18180 while (ASCII_ISALPHA(*p))
18181 ++p;
18182 return p;
18183}
18184
18185/*
18186 * ":function"
18187 */
18188 void
18189ex_function(eap)
18190 exarg_T *eap;
18191{
18192 char_u *theline;
18193 int j;
18194 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 char_u *name = NULL;
18197 char_u *p;
18198 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018199 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 garray_T newargs;
18201 garray_T newlines;
18202 int varargs = FALSE;
18203 int mustend = FALSE;
18204 int flags = 0;
18205 ufunc_T *fp;
18206 int indent;
18207 int nesting;
18208 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018209 dictitem_T *v;
18210 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018211 static int func_nr = 0; /* number for nameless function */
18212 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018213 hashtab_T *ht;
18214 int todo;
18215 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018216 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217
18218 /*
18219 * ":function" without argument: list functions.
18220 */
18221 if (ends_excmd(*eap->arg))
18222 {
18223 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018224 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018225 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018226 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018227 {
18228 if (!HASHITEM_EMPTY(hi))
18229 {
18230 --todo;
18231 fp = HI2UF(hi);
18232 if (!isdigit(*fp->uf_name))
18233 list_func_head(fp, FALSE);
18234 }
18235 }
18236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 eap->nextcmd = check_nextcmd(eap->arg);
18238 return;
18239 }
18240
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018241 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018242 * ":function /pat": list functions matching pattern.
18243 */
18244 if (*eap->arg == '/')
18245 {
18246 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18247 if (!eap->skip)
18248 {
18249 regmatch_T regmatch;
18250
18251 c = *p;
18252 *p = NUL;
18253 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18254 *p = c;
18255 if (regmatch.regprog != NULL)
18256 {
18257 regmatch.rm_ic = p_ic;
18258
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018259 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018260 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18261 {
18262 if (!HASHITEM_EMPTY(hi))
18263 {
18264 --todo;
18265 fp = HI2UF(hi);
18266 if (!isdigit(*fp->uf_name)
18267 && vim_regexec(&regmatch, fp->uf_name, 0))
18268 list_func_head(fp, FALSE);
18269 }
18270 }
18271 }
18272 }
18273 if (*p == '/')
18274 ++p;
18275 eap->nextcmd = check_nextcmd(p);
18276 return;
18277 }
18278
18279 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018280 * Get the function name. There are these situations:
18281 * func normal function name
18282 * "name" == func, "fudi.fd_dict" == NULL
18283 * dict.func new dictionary entry
18284 * "name" == NULL, "fudi.fd_dict" set,
18285 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18286 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018287 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018288 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18289 * dict.func existing dict entry that's not a Funcref
18290 * "name" == NULL, "fudi.fd_dict" set,
18291 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018294 name = trans_function_name(&p, eap->skip, 0, &fudi);
18295 paren = (vim_strchr(p, '(') != NULL);
18296 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 {
18298 /*
18299 * Return on an invalid expression in braces, unless the expression
18300 * evaluation has been cancelled due to an aborting error, an
18301 * interrupt, or an exception.
18302 */
18303 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018305 if (!eap->skip && fudi.fd_newkey != NULL)
18306 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018307 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 else
18311 eap->skip = TRUE;
18312 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018313
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 /* An error in a function call during evaluation of an expression in magic
18315 * braces should not cause the function not to be defined. */
18316 saved_did_emsg = did_emsg;
18317 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318
18319 /*
18320 * ":function func" with only function name: list function.
18321 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018322 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 {
18324 if (!ends_excmd(*skipwhite(p)))
18325 {
18326 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018327 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 }
18329 eap->nextcmd = check_nextcmd(p);
18330 if (eap->nextcmd != NULL)
18331 *p = NUL;
18332 if (!eap->skip && !got_int)
18333 {
18334 fp = find_func(name);
18335 if (fp != NULL)
18336 {
18337 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018338 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018340 if (FUNCLINE(fp, j) == NULL)
18341 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342 msg_putchar('\n');
18343 msg_outnum((long)(j + 1));
18344 if (j < 9)
18345 msg_putchar(' ');
18346 if (j < 99)
18347 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018348 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 out_flush(); /* show a line at a time */
18350 ui_breakcheck();
18351 }
18352 if (!got_int)
18353 {
18354 msg_putchar('\n');
18355 msg_puts((char_u *)" endfunction");
18356 }
18357 }
18358 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018359 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018361 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 }
18363
18364 /*
18365 * ":function name(arg1, arg2)" Define function.
18366 */
18367 p = skipwhite(p);
18368 if (*p != '(')
18369 {
18370 if (!eap->skip)
18371 {
18372 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018373 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 }
18375 /* attempt to continue by skipping some text */
18376 if (vim_strchr(p, '(') != NULL)
18377 p = vim_strchr(p, '(');
18378 }
18379 p = skipwhite(p + 1);
18380
18381 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18382 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18383
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018384 if (!eap->skip)
18385 {
18386 /* Check the name of the function. */
18387 if (name != NULL)
18388 arg = name;
18389 else
18390 arg = fudi.fd_newkey;
18391 if (arg != NULL)
18392 {
18393 if (*arg == K_SPECIAL)
18394 j = 3;
18395 else
18396 j = 0;
18397 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18398 : eval_isnamec(arg[j])))
18399 ++j;
18400 if (arg[j] != NUL)
18401 emsg_funcname(_(e_invarg2), arg);
18402 }
18403 }
18404
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 /*
18406 * Isolate the arguments: "arg1, arg2, ...)"
18407 */
18408 while (*p != ')')
18409 {
18410 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18411 {
18412 varargs = TRUE;
18413 p += 3;
18414 mustend = TRUE;
18415 }
18416 else
18417 {
18418 arg = p;
18419 while (ASCII_ISALNUM(*p) || *p == '_')
18420 ++p;
18421 if (arg == p || isdigit(*arg)
18422 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18423 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18424 {
18425 if (!eap->skip)
18426 EMSG2(_("E125: Illegal argument: %s"), arg);
18427 break;
18428 }
18429 if (ga_grow(&newargs, 1) == FAIL)
18430 goto erret;
18431 c = *p;
18432 *p = NUL;
18433 arg = vim_strsave(arg);
18434 if (arg == NULL)
18435 goto erret;
18436 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18437 *p = c;
18438 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 if (*p == ',')
18440 ++p;
18441 else
18442 mustend = TRUE;
18443 }
18444 p = skipwhite(p);
18445 if (mustend && *p != ')')
18446 {
18447 if (!eap->skip)
18448 EMSG2(_(e_invarg2), eap->arg);
18449 break;
18450 }
18451 }
18452 ++p; /* skip the ')' */
18453
Bram Moolenaare9a41262005-01-15 22:18:47 +000018454 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 for (;;)
18456 {
18457 p = skipwhite(p);
18458 if (STRNCMP(p, "range", 5) == 0)
18459 {
18460 flags |= FC_RANGE;
18461 p += 5;
18462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018463 else if (STRNCMP(p, "dict", 4) == 0)
18464 {
18465 flags |= FC_DICT;
18466 p += 4;
18467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 else if (STRNCMP(p, "abort", 5) == 0)
18469 {
18470 flags |= FC_ABORT;
18471 p += 5;
18472 }
18473 else
18474 break;
18475 }
18476
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018477 /* When there is a line break use what follows for the function body.
18478 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18479 if (*p == '\n')
18480 line_arg = p + 1;
18481 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 EMSG(_(e_trailing));
18483
18484 /*
18485 * Read the body of the function, until ":endfunction" is found.
18486 */
18487 if (KeyTyped)
18488 {
18489 /* Check if the function already exists, don't let the user type the
18490 * whole function before telling him it doesn't work! For a script we
18491 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018492 if (!eap->skip && !eap->forceit)
18493 {
18494 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18495 EMSG(_(e_funcdict));
18496 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018497 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018500 if (!eap->skip && did_emsg)
18501 goto erret;
18502
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503 msg_putchar('\n'); /* don't overwrite the function name */
18504 cmdline_row = msg_row;
18505 }
18506
18507 indent = 2;
18508 nesting = 0;
18509 for (;;)
18510 {
18511 msg_scroll = TRUE;
18512 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018513 sourcing_lnum_off = sourcing_lnum;
18514
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018515 if (line_arg != NULL)
18516 {
18517 /* Use eap->arg, split up in parts by line breaks. */
18518 theline = line_arg;
18519 p = vim_strchr(theline, '\n');
18520 if (p == NULL)
18521 line_arg += STRLEN(line_arg);
18522 else
18523 {
18524 *p = NUL;
18525 line_arg = p + 1;
18526 }
18527 }
18528 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 theline = getcmdline(':', 0L, indent);
18530 else
18531 theline = eap->getline(':', eap->cookie, indent);
18532 if (KeyTyped)
18533 lines_left = Rows - 1;
18534 if (theline == NULL)
18535 {
18536 EMSG(_("E126: Missing :endfunction"));
18537 goto erret;
18538 }
18539
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018540 /* Detect line continuation: sourcing_lnum increased more than one. */
18541 if (sourcing_lnum > sourcing_lnum_off + 1)
18542 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18543 else
18544 sourcing_lnum_off = 0;
18545
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 if (skip_until != NULL)
18547 {
18548 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18549 * don't check for ":endfunc". */
18550 if (STRCMP(theline, skip_until) == 0)
18551 {
18552 vim_free(skip_until);
18553 skip_until = NULL;
18554 }
18555 }
18556 else
18557 {
18558 /* skip ':' and blanks*/
18559 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18560 ;
18561
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018562 /* Check for "endfunction". */
18563 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018565 if (line_arg == NULL)
18566 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 break;
18568 }
18569
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018570 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 * at "end". */
18572 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18573 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018574 else if (STRNCMP(p, "if", 2) == 0
18575 || STRNCMP(p, "wh", 2) == 0
18576 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 || STRNCMP(p, "try", 3) == 0)
18578 indent += 2;
18579
18580 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018581 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018583 if (*p == '!')
18584 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 p += eval_fname_script(p);
18586 if (ASCII_ISALPHA(*p))
18587 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018588 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 if (*skipwhite(p) == '(')
18590 {
18591 ++nesting;
18592 indent += 2;
18593 }
18594 }
18595 }
18596
18597 /* Check for ":append" or ":insert". */
18598 p = skip_range(p, NULL);
18599 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18600 || (p[0] == 'i'
18601 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18602 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18603 skip_until = vim_strsave((char_u *)".");
18604
18605 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18606 arg = skipwhite(skiptowhite(p));
18607 if (arg[0] == '<' && arg[1] =='<'
18608 && ((p[0] == 'p' && p[1] == 'y'
18609 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18610 || (p[0] == 'p' && p[1] == 'e'
18611 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18612 || (p[0] == 't' && p[1] == 'c'
18613 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18614 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18615 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018616 || (p[0] == 'm' && p[1] == 'z'
18617 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 ))
18619 {
18620 /* ":python <<" continues until a dot, like ":append" */
18621 p = skipwhite(arg + 2);
18622 if (*p == NUL)
18623 skip_until = vim_strsave((char_u *)".");
18624 else
18625 skip_until = vim_strsave(p);
18626 }
18627 }
18628
18629 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018630 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018631 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018632 if (line_arg == NULL)
18633 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018635 }
18636
18637 /* Copy the line to newly allocated memory. get_one_sourceline()
18638 * allocates 250 bytes per line, this saves 80% on average. The cost
18639 * is an extra alloc/free. */
18640 p = vim_strsave(theline);
18641 if (p != NULL)
18642 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018643 if (line_arg == NULL)
18644 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018645 theline = p;
18646 }
18647
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018648 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18649
18650 /* Add NULL lines for continuation lines, so that the line count is
18651 * equal to the index in the growarray. */
18652 while (sourcing_lnum_off-- > 0)
18653 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018654
18655 /* Check for end of eap->arg. */
18656 if (line_arg != NULL && *line_arg == NUL)
18657 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 }
18659
18660 /* Don't define the function when skipping commands or when an error was
18661 * detected. */
18662 if (eap->skip || did_emsg)
18663 goto erret;
18664
18665 /*
18666 * If there are no errors, add the function
18667 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018668 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018669 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018670 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018671 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018672 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018673 emsg_funcname("E707: Function name conflicts with variable: %s",
18674 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018675 goto erret;
18676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018678 fp = find_func(name);
18679 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018681 if (!eap->forceit)
18682 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018683 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018684 goto erret;
18685 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018686 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018687 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018688 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018689 name);
18690 goto erret;
18691 }
18692 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018693 ga_clear_strings(&(fp->uf_args));
18694 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018695 vim_free(name);
18696 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 }
18699 else
18700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018701 char numbuf[20];
18702
18703 fp = NULL;
18704 if (fudi.fd_newkey == NULL && !eap->forceit)
18705 {
18706 EMSG(_(e_funcdict));
18707 goto erret;
18708 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018709 if (fudi.fd_di == NULL)
18710 {
18711 /* Can't add a function to a locked dictionary */
18712 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18713 goto erret;
18714 }
18715 /* Can't change an existing function if it is locked */
18716 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18717 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018718
18719 /* Give the function a sequential number. Can only be used with a
18720 * Funcref! */
18721 vim_free(name);
18722 sprintf(numbuf, "%d", ++func_nr);
18723 name = vim_strsave((char_u *)numbuf);
18724 if (name == NULL)
18725 goto erret;
18726 }
18727
18728 if (fp == NULL)
18729 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018730 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018731 {
18732 int slen, plen;
18733 char_u *scriptname;
18734
18735 /* Check that the autoload name matches the script name. */
18736 j = FAIL;
18737 if (sourcing_name != NULL)
18738 {
18739 scriptname = autoload_name(name);
18740 if (scriptname != NULL)
18741 {
18742 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018743 plen = (int)STRLEN(p);
18744 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018745 if (slen > plen && fnamecmp(p,
18746 sourcing_name + slen - plen) == 0)
18747 j = OK;
18748 vim_free(scriptname);
18749 }
18750 }
18751 if (j == FAIL)
18752 {
18753 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18754 goto erret;
18755 }
18756 }
18757
18758 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 if (fp == NULL)
18760 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018761
18762 if (fudi.fd_dict != NULL)
18763 {
18764 if (fudi.fd_di == NULL)
18765 {
18766 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018767 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018768 if (fudi.fd_di == NULL)
18769 {
18770 vim_free(fp);
18771 goto erret;
18772 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018773 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18774 {
18775 vim_free(fudi.fd_di);
18776 goto erret;
18777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018778 }
18779 else
18780 /* overwrite existing dict entry */
18781 clear_tv(&fudi.fd_di->di_tv);
18782 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018783 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018784 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018785 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018786
18787 /* behave like "dict" was used */
18788 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018789 }
18790
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018792 STRCPY(fp->uf_name, name);
18793 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018795 fp->uf_args = newargs;
18796 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018797#ifdef FEAT_PROFILE
18798 fp->uf_tml_count = NULL;
18799 fp->uf_tml_total = NULL;
18800 fp->uf_tml_self = NULL;
18801 fp->uf_profiling = FALSE;
18802 if (prof_def_func())
18803 func_do_profile(fp);
18804#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018805 fp->uf_varargs = varargs;
18806 fp->uf_flags = flags;
18807 fp->uf_calls = 0;
18808 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018809 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810
18811erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 ga_clear_strings(&newargs);
18813 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018814ret_free:
18815 vim_free(skip_until);
18816 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819}
18820
18821/*
18822 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018823 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018825 * flags:
18826 * TFN_INT: internal function name OK
18827 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 * Advances "pp" to just after the function name (if no error).
18829 */
18830 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018831trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 char_u **pp;
18833 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018834 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018835 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018837 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 char_u *start;
18839 char_u *end;
18840 int lead;
18841 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018844
18845 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018846 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018848
18849 /* Check for hard coded <SNR>: already translated function ID (from a user
18850 * command). */
18851 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18852 && (*pp)[2] == (int)KE_SNR)
18853 {
18854 *pp += 3;
18855 len = get_id_len(pp) + 3;
18856 return vim_strnsave(start, len);
18857 }
18858
18859 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18860 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018862 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018864
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018865 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18866 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018867 if (end == start)
18868 {
18869 if (!skip)
18870 EMSG(_("E129: Function name required"));
18871 goto theend;
18872 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018873 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018874 {
18875 /*
18876 * Report an invalid expression in braces, unless the expression
18877 * evaluation has been cancelled due to an aborting error, an
18878 * interrupt, or an exception.
18879 */
18880 if (!aborting())
18881 {
18882 if (end != NULL)
18883 EMSG2(_(e_invarg2), start);
18884 }
18885 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018886 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018887 goto theend;
18888 }
18889
18890 if (lv.ll_tv != NULL)
18891 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018892 if (fdp != NULL)
18893 {
18894 fdp->fd_dict = lv.ll_dict;
18895 fdp->fd_newkey = lv.ll_newkey;
18896 lv.ll_newkey = NULL;
18897 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018898 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018899 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18900 {
18901 name = vim_strsave(lv.ll_tv->vval.v_string);
18902 *pp = end;
18903 }
18904 else
18905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018906 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18907 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018908 EMSG(_(e_funcref));
18909 else
18910 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018911 name = NULL;
18912 }
18913 goto theend;
18914 }
18915
18916 if (lv.ll_name == NULL)
18917 {
18918 /* Error found, but continue after the function name. */
18919 *pp = end;
18920 goto theend;
18921 }
18922
18923 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018924 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018925 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018926 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18927 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18928 {
18929 /* When there was "s:" already or the name expanded to get a
18930 * leading "s:" then remove it. */
18931 lv.ll_name += 2;
18932 len -= 2;
18933 lead = 2;
18934 }
18935 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018936 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018937 {
18938 if (lead == 2) /* skip over "s:" */
18939 lv.ll_name += 2;
18940 len = (int)(end - lv.ll_name);
18941 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018942
18943 /*
18944 * Copy the function name to allocated memory.
18945 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18946 * Accept <SNR>123_name() outside a script.
18947 */
18948 if (skip)
18949 lead = 0; /* do nothing */
18950 else if (lead > 0)
18951 {
18952 lead = 3;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018953 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018954 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018955 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018956 if (current_SID <= 0)
18957 {
18958 EMSG(_(e_usingsid));
18959 goto theend;
18960 }
18961 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18962 lead += (int)STRLEN(sid_buf);
18963 }
18964 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018965 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018966 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018967 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018968 goto theend;
18969 }
18970 name = alloc((unsigned)(len + lead + 1));
18971 if (name != NULL)
18972 {
18973 if (lead > 0)
18974 {
18975 name[0] = K_SPECIAL;
18976 name[1] = KS_EXTRA;
18977 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018978 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018979 STRCPY(name + 3, sid_buf);
18980 }
18981 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18982 name[len + lead] = NUL;
18983 }
18984 *pp = end;
18985
18986theend:
18987 clear_lval(&lv);
18988 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989}
18990
18991/*
18992 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18993 * Return 2 if "p" starts with "s:".
18994 * Return 0 otherwise.
18995 */
18996 static int
18997eval_fname_script(p)
18998 char_u *p;
18999{
19000 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19001 || STRNICMP(p + 1, "SNR>", 4) == 0))
19002 return 5;
19003 if (p[0] == 's' && p[1] == ':')
19004 return 2;
19005 return 0;
19006}
19007
19008/*
19009 * Return TRUE if "p" starts with "<SID>" or "s:".
19010 * Only works if eval_fname_script() returned non-zero for "p"!
19011 */
19012 static int
19013eval_fname_sid(p)
19014 char_u *p;
19015{
19016 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19017}
19018
19019/*
19020 * List the head of the function: "name(arg1, arg2)".
19021 */
19022 static void
19023list_func_head(fp, indent)
19024 ufunc_T *fp;
19025 int indent;
19026{
19027 int j;
19028
19029 msg_start();
19030 if (indent)
19031 MSG_PUTS(" ");
19032 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019033 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 {
19035 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019036 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 }
19038 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019039 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019041 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 {
19043 if (j)
19044 MSG_PUTS(", ");
19045 msg_puts(FUNCARG(fp, j));
19046 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019047 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 {
19049 if (j)
19050 MSG_PUTS(", ");
19051 MSG_PUTS("...");
19052 }
19053 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019054 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019055 if (p_verbose > 0)
19056 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057}
19058
19059/*
19060 * Find a function by name, return pointer to it in ufuncs.
19061 * Return NULL for unknown function.
19062 */
19063 static ufunc_T *
19064find_func(name)
19065 char_u *name;
19066{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019067 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019069 hi = hash_find(&func_hashtab, name);
19070 if (!HASHITEM_EMPTY(hi))
19071 return HI2UF(hi);
19072 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073}
19074
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019075#if defined(EXITFREE) || defined(PROTO)
19076 void
19077free_all_functions()
19078{
19079 hashitem_T *hi;
19080
19081 /* Need to start all over every time, because func_free() may change the
19082 * hash table. */
19083 while (func_hashtab.ht_used > 0)
19084 for (hi = func_hashtab.ht_array; ; ++hi)
19085 if (!HASHITEM_EMPTY(hi))
19086 {
19087 func_free(HI2UF(hi));
19088 break;
19089 }
19090}
19091#endif
19092
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019093/*
19094 * Return TRUE if a function "name" exists.
19095 */
19096 static int
19097function_exists(name)
19098 char_u *name;
19099{
19100 char_u *p = name;
19101 int n = FALSE;
19102
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019103 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019104 if (p != NULL)
19105 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019106 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019107 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019108 else
19109 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019110 vim_free(p);
19111 }
19112 return n;
19113}
19114
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019115/*
19116 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019117 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019118 */
19119 static int
19120builtin_function(name)
19121 char_u *name;
19122{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019123 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19124 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019125}
19126
Bram Moolenaar05159a02005-02-26 23:04:13 +000019127#if defined(FEAT_PROFILE) || defined(PROTO)
19128/*
19129 * Start profiling function "fp".
19130 */
19131 static void
19132func_do_profile(fp)
19133 ufunc_T *fp;
19134{
19135 fp->uf_tm_count = 0;
19136 profile_zero(&fp->uf_tm_self);
19137 profile_zero(&fp->uf_tm_total);
19138 if (fp->uf_tml_count == NULL)
19139 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19140 (sizeof(int) * fp->uf_lines.ga_len));
19141 if (fp->uf_tml_total == NULL)
19142 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19143 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19144 if (fp->uf_tml_self == NULL)
19145 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19146 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19147 fp->uf_tml_idx = -1;
19148 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19149 || fp->uf_tml_self == NULL)
19150 return; /* out of memory */
19151
19152 fp->uf_profiling = TRUE;
19153}
19154
19155/*
19156 * Dump the profiling results for all functions in file "fd".
19157 */
19158 void
19159func_dump_profile(fd)
19160 FILE *fd;
19161{
19162 hashitem_T *hi;
19163 int todo;
19164 ufunc_T *fp;
19165 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019166 ufunc_T **sorttab;
19167 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019168
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019169 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019170 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19171
Bram Moolenaar05159a02005-02-26 23:04:13 +000019172 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19173 {
19174 if (!HASHITEM_EMPTY(hi))
19175 {
19176 --todo;
19177 fp = HI2UF(hi);
19178 if (fp->uf_profiling)
19179 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019180 if (sorttab != NULL)
19181 sorttab[st_len++] = fp;
19182
Bram Moolenaar05159a02005-02-26 23:04:13 +000019183 if (fp->uf_name[0] == K_SPECIAL)
19184 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19185 else
19186 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19187 if (fp->uf_tm_count == 1)
19188 fprintf(fd, "Called 1 time\n");
19189 else
19190 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19191 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19192 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19193 fprintf(fd, "\n");
19194 fprintf(fd, "count total (s) self (s)\n");
19195
19196 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19197 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019198 if (FUNCLINE(fp, i) == NULL)
19199 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019200 prof_func_line(fd, fp->uf_tml_count[i],
19201 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019202 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19203 }
19204 fprintf(fd, "\n");
19205 }
19206 }
19207 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019208
19209 if (sorttab != NULL && st_len > 0)
19210 {
19211 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19212 prof_total_cmp);
19213 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19214 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19215 prof_self_cmp);
19216 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19217 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019218}
Bram Moolenaar73830342005-02-28 22:48:19 +000019219
19220 static void
19221prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19222 FILE *fd;
19223 ufunc_T **sorttab;
19224 int st_len;
19225 char *title;
19226 int prefer_self; /* when equal print only self time */
19227{
19228 int i;
19229 ufunc_T *fp;
19230
19231 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19232 fprintf(fd, "count total (s) self (s) function\n");
19233 for (i = 0; i < 20 && i < st_len; ++i)
19234 {
19235 fp = sorttab[i];
19236 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19237 prefer_self);
19238 if (fp->uf_name[0] == K_SPECIAL)
19239 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19240 else
19241 fprintf(fd, " %s()\n", fp->uf_name);
19242 }
19243 fprintf(fd, "\n");
19244}
19245
19246/*
19247 * Print the count and times for one function or function line.
19248 */
19249 static void
19250prof_func_line(fd, count, total, self, prefer_self)
19251 FILE *fd;
19252 int count;
19253 proftime_T *total;
19254 proftime_T *self;
19255 int prefer_self; /* when equal print only self time */
19256{
19257 if (count > 0)
19258 {
19259 fprintf(fd, "%5d ", count);
19260 if (prefer_self && profile_equal(total, self))
19261 fprintf(fd, " ");
19262 else
19263 fprintf(fd, "%s ", profile_msg(total));
19264 if (!prefer_self && profile_equal(total, self))
19265 fprintf(fd, " ");
19266 else
19267 fprintf(fd, "%s ", profile_msg(self));
19268 }
19269 else
19270 fprintf(fd, " ");
19271}
19272
19273/*
19274 * Compare function for total time sorting.
19275 */
19276 static int
19277#ifdef __BORLANDC__
19278_RTLENTRYF
19279#endif
19280prof_total_cmp(s1, s2)
19281 const void *s1;
19282 const void *s2;
19283{
19284 ufunc_T *p1, *p2;
19285
19286 p1 = *(ufunc_T **)s1;
19287 p2 = *(ufunc_T **)s2;
19288 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19289}
19290
19291/*
19292 * Compare function for self time sorting.
19293 */
19294 static int
19295#ifdef __BORLANDC__
19296_RTLENTRYF
19297#endif
19298prof_self_cmp(s1, s2)
19299 const void *s1;
19300 const void *s2;
19301{
19302 ufunc_T *p1, *p2;
19303
19304 p1 = *(ufunc_T **)s1;
19305 p2 = *(ufunc_T **)s2;
19306 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19307}
19308
Bram Moolenaar05159a02005-02-26 23:04:13 +000019309#endif
19310
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019311/* The names of packages that once were loaded is remembered. */
19312static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19313
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019314/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019315 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019316 * Return TRUE if a package was loaded.
19317 */
19318 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019319script_autoload(name, reload)
19320 char_u *name;
19321 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019322{
19323 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019324 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019325 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019326 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019327
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019328 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019329 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019330 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019331 return FALSE;
19332
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019333 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019334
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019335 /* Find the name in the list of previously loaded package names. Skip
19336 * "autoload/", it's always the same. */
19337 for (i = 0; i < ga_loaded.ga_len; ++i)
19338 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19339 break;
19340 if (!reload && i < ga_loaded.ga_len)
19341 ret = FALSE; /* was loaded already */
19342 else
19343 {
19344 /* Remember the name if it wasn't loaded already. */
19345 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19346 {
19347 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19348 tofree = NULL;
19349 }
19350
19351 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019352 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019353 ret = TRUE;
19354 }
19355
19356 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019357 return ret;
19358}
19359
19360/*
19361 * Return the autoload script name for a function or variable name.
19362 * Returns NULL when out of memory.
19363 */
19364 static char_u *
19365autoload_name(name)
19366 char_u *name;
19367{
19368 char_u *p;
19369 char_u *scriptname;
19370
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019371 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019372 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19373 if (scriptname == NULL)
19374 return FALSE;
19375 STRCPY(scriptname, "autoload/");
19376 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019377 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019378 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019379 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019380 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019381 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019382}
19383
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19385
19386/*
19387 * Function given to ExpandGeneric() to obtain the list of user defined
19388 * function names.
19389 */
19390 char_u *
19391get_user_func_name(xp, idx)
19392 expand_T *xp;
19393 int idx;
19394{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019395 static long_u done;
19396 static hashitem_T *hi;
19397 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398
19399 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019401 done = 0;
19402 hi = func_hashtab.ht_array;
19403 }
19404 if (done < func_hashtab.ht_used)
19405 {
19406 if (done++ > 0)
19407 ++hi;
19408 while (HASHITEM_EMPTY(hi))
19409 ++hi;
19410 fp = HI2UF(hi);
19411
19412 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19413 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414
19415 cat_func_name(IObuff, fp);
19416 if (xp->xp_context != EXPAND_USER_FUNC)
19417 {
19418 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019419 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 STRCAT(IObuff, ")");
19421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 return IObuff;
19423 }
19424 return NULL;
19425}
19426
19427#endif /* FEAT_CMDL_COMPL */
19428
19429/*
19430 * Copy the function name of "fp" to buffer "buf".
19431 * "buf" must be able to hold the function name plus three bytes.
19432 * Takes care of script-local function names.
19433 */
19434 static void
19435cat_func_name(buf, fp)
19436 char_u *buf;
19437 ufunc_T *fp;
19438{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019439 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 {
19441 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019442 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 }
19444 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019445 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446}
19447
19448/*
19449 * ":delfunction {name}"
19450 */
19451 void
19452ex_delfunction(eap)
19453 exarg_T *eap;
19454{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019455 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 char_u *p;
19457 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019458 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459
19460 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019461 name = trans_function_name(&p, eap->skip, 0, &fudi);
19462 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019464 {
19465 if (fudi.fd_dict != NULL && !eap->skip)
19466 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 if (!ends_excmd(*skipwhite(p)))
19470 {
19471 vim_free(name);
19472 EMSG(_(e_trailing));
19473 return;
19474 }
19475 eap->nextcmd = check_nextcmd(p);
19476 if (eap->nextcmd != NULL)
19477 *p = NUL;
19478
19479 if (!eap->skip)
19480 fp = find_func(name);
19481 vim_free(name);
19482
19483 if (!eap->skip)
19484 {
19485 if (fp == NULL)
19486 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019487 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 return;
19489 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019490 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 {
19492 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19493 return;
19494 }
19495
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019496 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019498 /* Delete the dict item that refers to the function, it will
19499 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019500 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019502 else
19503 func_free(fp);
19504 }
19505}
19506
19507/*
19508 * Free a function and remove it from the list of functions.
19509 */
19510 static void
19511func_free(fp)
19512 ufunc_T *fp;
19513{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019514 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019515
19516 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019517 ga_clear_strings(&(fp->uf_args));
19518 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019519#ifdef FEAT_PROFILE
19520 vim_free(fp->uf_tml_count);
19521 vim_free(fp->uf_tml_total);
19522 vim_free(fp->uf_tml_self);
19523#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019524
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019525 /* remove the function from the function hashtable */
19526 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19527 if (HASHITEM_EMPTY(hi))
19528 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019529 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019530 hash_remove(&func_hashtab, hi);
19531
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019532 vim_free(fp);
19533}
19534
19535/*
19536 * Unreference a Function: decrement the reference count and free it when it
19537 * becomes zero. Only for numbered functions.
19538 */
19539 static void
19540func_unref(name)
19541 char_u *name;
19542{
19543 ufunc_T *fp;
19544
19545 if (name != NULL && isdigit(*name))
19546 {
19547 fp = find_func(name);
19548 if (fp == NULL)
19549 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019550 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019551 {
19552 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019553 * when "uf_calls" becomes zero. */
19554 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019555 func_free(fp);
19556 }
19557 }
19558}
19559
19560/*
19561 * Count a reference to a Function.
19562 */
19563 static void
19564func_ref(name)
19565 char_u *name;
19566{
19567 ufunc_T *fp;
19568
19569 if (name != NULL && isdigit(*name))
19570 {
19571 fp = find_func(name);
19572 if (fp == NULL)
19573 EMSG2(_(e_intern2), "func_ref()");
19574 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019575 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 }
19577}
19578
19579/*
19580 * Call a user function.
19581 */
19582 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019583call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 ufunc_T *fp; /* pointer to function */
19585 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019586 typval_T *argvars; /* arguments */
19587 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 linenr_T firstline; /* first line of range */
19589 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019590 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591{
Bram Moolenaar33570922005-01-25 22:26:29 +000019592 char_u *save_sourcing_name;
19593 linenr_T save_sourcing_lnum;
19594 scid_T save_current_SID;
19595 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 int save_did_emsg;
19597 static int depth = 0;
19598 dictitem_T *v;
19599 int fixvar_idx = 0; /* index in fixvar[] */
19600 int i;
19601 int ai;
19602 char_u numbuf[NUMBUFLEN];
19603 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019604#ifdef FEAT_PROFILE
19605 proftime_T wait_start;
19606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607
19608 /* If depth of calling is getting too high, don't execute the function */
19609 if (depth >= p_mfd)
19610 {
19611 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 rettv->v_type = VAR_NUMBER;
19613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 return;
19615 }
19616 ++depth;
19617
19618 line_breakcheck(); /* check for CTRL-C hit */
19619
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019620 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019623 fc.rettv = rettv;
19624 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 fc.linenr = 0;
19626 fc.returned = FALSE;
19627 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019629 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 fc.dbg_tick = debug_tick;
19631
Bram Moolenaar33570922005-01-25 22:26:29 +000019632 /*
19633 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19634 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19635 * each argument variable and saves a lot of time.
19636 */
19637 /*
19638 * Init l: variables.
19639 */
19640 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019641 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019642 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019643 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19644 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019645 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019646 name = v->di_key;
19647 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019648 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19649 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19650 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019651 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 v->di_tv.vval.v_dict = selfdict;
19653 ++selfdict->dv_refcount;
19654 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019655
Bram Moolenaar33570922005-01-25 22:26:29 +000019656 /*
19657 * Init a: variables.
19658 * Set a:0 to "argcount".
19659 * Set a:000 to a list with room for the "..." arguments.
19660 */
19661 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19662 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019663 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019664 v = &fc.fixvar[fixvar_idx++].var;
19665 STRCPY(v->di_key, "000");
19666 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19667 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19668 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019669 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 v->di_tv.vval.v_list = &fc.l_varlist;
19671 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19672 fc.l_varlist.lv_refcount = 99999;
19673
19674 /*
19675 * Set a:firstline to "firstline" and a:lastline to "lastline".
19676 * Set a:name to named arguments.
19677 * Set a:N to the "..." arguments.
19678 */
19679 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19680 (varnumber_T)firstline);
19681 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19682 (varnumber_T)lastline);
19683 for (i = 0; i < argcount; ++i)
19684 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019685 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019686 if (ai < 0)
19687 /* named argument a:name */
19688 name = FUNCARG(fp, i);
19689 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019690 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 /* "..." argument a:1, a:2, etc. */
19692 sprintf((char *)numbuf, "%d", ai + 1);
19693 name = numbuf;
19694 }
19695 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19696 {
19697 v = &fc.fixvar[fixvar_idx++].var;
19698 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19699 }
19700 else
19701 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019702 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19703 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019704 if (v == NULL)
19705 break;
19706 v->di_flags = DI_FLAGS_RO;
19707 }
19708 STRCPY(v->di_key, name);
19709 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19710
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019711 /* Note: the values are copied directly to avoid alloc/free.
19712 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019713 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019714 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019715
19716 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19717 {
19718 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19719 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019720 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019721 }
19722 }
19723
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 /* Don't redraw while executing the function. */
19725 ++RedrawingDisabled;
19726 save_sourcing_name = sourcing_name;
19727 save_sourcing_lnum = sourcing_lnum;
19728 sourcing_lnum = 1;
19729 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019730 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 if (sourcing_name != NULL)
19732 {
19733 if (save_sourcing_name != NULL
19734 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19735 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19736 else
19737 STRCPY(sourcing_name, "function ");
19738 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19739
19740 if (p_verbose >= 12)
19741 {
19742 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019743 verbose_enter_scroll();
19744
Bram Moolenaar555b2802005-05-19 21:08:39 +000019745 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 if (p_verbose >= 14)
19747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019749 char_u numbuf[NUMBUFLEN];
19750 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751
19752 msg_puts((char_u *)"(");
19753 for (i = 0; i < argcount; ++i)
19754 {
19755 if (i > 0)
19756 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019757 if (argvars[i].v_type == VAR_NUMBER)
19758 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 else
19760 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019761 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019762 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019764 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 }
19766 }
19767 msg_puts((char_u *)")");
19768 }
19769 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019770
19771 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 --no_wait_return;
19773 }
19774 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019775#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019776 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019777 {
19778 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19779 func_do_profile(fp);
19780 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019781 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019782 {
19783 ++fp->uf_tm_count;
19784 profile_start(&fp->uf_tm_start);
19785 profile_zero(&fp->uf_tm_children);
19786 }
19787 script_prof_save(&wait_start);
19788 }
19789#endif
19790
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019792 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 save_did_emsg = did_emsg;
19794 did_emsg = FALSE;
19795
19796 /* call do_cmdline() to execute the lines */
19797 do_cmdline(NULL, get_func_line, (void *)&fc,
19798 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19799
19800 --RedrawingDisabled;
19801
19802 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019803 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 clear_tv(rettv);
19806 rettv->v_type = VAR_NUMBER;
19807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 }
19809
Bram Moolenaar05159a02005-02-26 23:04:13 +000019810#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019811 if (do_profiling == PROF_YES && (fp->uf_profiling
19812 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019813 {
19814 profile_end(&fp->uf_tm_start);
19815 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19816 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019817 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019818 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019819 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019820 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19821 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019822 }
19823 }
19824#endif
19825
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 /* when being verbose, mention the return value */
19827 if (p_verbose >= 12)
19828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019830 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019833 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019835 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19836 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019839 char_u buf[MSG_BUF_LEN];
19840 char_u numbuf[NUMBUFLEN];
19841 char_u *tofree;
19842
Bram Moolenaar555b2802005-05-19 21:08:39 +000019843 /* The value may be very long. Skip the middle part, so that we
19844 * have some idea how it starts and ends. smsg() would always
19845 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019846 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019847 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019848 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019849 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 }
19851 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019852
19853 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 --no_wait_return;
19855 }
19856
19857 vim_free(sourcing_name);
19858 sourcing_name = save_sourcing_name;
19859 sourcing_lnum = save_sourcing_lnum;
19860 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019861#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019862 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019863 script_prof_restore(&wait_start);
19864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865
19866 if (p_verbose >= 12 && sourcing_name != NULL)
19867 {
19868 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019869 verbose_enter_scroll();
19870
Bram Moolenaar555b2802005-05-19 21:08:39 +000019871 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019873
19874 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 --no_wait_return;
19876 }
19877
19878 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019879 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880
Bram Moolenaar33570922005-01-25 22:26:29 +000019881 /* The a: variables typevals were not alloced, only free the allocated
19882 * variables. */
19883 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19884
19885 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 --depth;
19887}
19888
19889/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019890 * Add a number variable "name" to dict "dp" with value "nr".
19891 */
19892 static void
19893add_nr_var(dp, v, name, nr)
19894 dict_T *dp;
19895 dictitem_T *v;
19896 char *name;
19897 varnumber_T nr;
19898{
19899 STRCPY(v->di_key, name);
19900 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19901 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19902 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019903 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019904 v->di_tv.vval.v_number = nr;
19905}
19906
19907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 * ":return [expr]"
19909 */
19910 void
19911ex_return(eap)
19912 exarg_T *eap;
19913{
19914 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019915 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 int returning = FALSE;
19917
19918 if (current_funccal == NULL)
19919 {
19920 EMSG(_("E133: :return not inside a function"));
19921 return;
19922 }
19923
19924 if (eap->skip)
19925 ++emsg_skip;
19926
19927 eap->nextcmd = NULL;
19928 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019929 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 {
19931 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019934 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 }
19936 /* It's safer to return also on error. */
19937 else if (!eap->skip)
19938 {
19939 /*
19940 * Return unless the expression evaluation has been cancelled due to an
19941 * aborting error, an interrupt, or an exception.
19942 */
19943 if (!aborting())
19944 returning = do_return(eap, FALSE, TRUE, NULL);
19945 }
19946
19947 /* When skipping or the return gets pending, advance to the next command
19948 * in this line (!returning). Otherwise, ignore the rest of the line.
19949 * Following lines will be ignored by get_func_line(). */
19950 if (returning)
19951 eap->nextcmd = NULL;
19952 else if (eap->nextcmd == NULL) /* no argument */
19953 eap->nextcmd = check_nextcmd(arg);
19954
19955 if (eap->skip)
19956 --emsg_skip;
19957}
19958
19959/*
19960 * Return from a function. Possibly makes the return pending. Also called
19961 * for a pending return at the ":endtry" or after returning from an extra
19962 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019963 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 * FALSE when the return gets pending.
19966 */
19967 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969 exarg_T *eap;
19970 int reanimate;
19971 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973{
19974 int idx;
19975 struct condstack *cstack = eap->cstack;
19976
19977 if (reanimate)
19978 /* Undo the return. */
19979 current_funccal->returned = FALSE;
19980
19981 /*
19982 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19983 * not in its finally clause (which then is to be executed next) is found.
19984 * In this case, make the ":return" pending for execution at the ":endtry".
19985 * Otherwise, return normally.
19986 */
19987 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19988 if (idx >= 0)
19989 {
19990 cstack->cs_pending[idx] = CSTP_RETURN;
19991
19992 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993 /* A pending return again gets pending. "rettv" points to an
19994 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 else
19998 {
19999 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020000 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020002 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 {
20006 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020008 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 else
20010 EMSG(_(e_outofmem));
20011 }
20012 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020013 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014
20015 if (reanimate)
20016 {
20017 /* The pending return value could be overwritten by a ":return"
20018 * without argument in a finally clause; reset the default
20019 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 current_funccal->rettv->v_type = VAR_NUMBER;
20021 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 }
20023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 }
20026 else
20027 {
20028 current_funccal->returned = TRUE;
20029
20030 /* If the return is carried out now, store the return value. For
20031 * a return immediately after reanimation, the value is already
20032 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020033 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020036 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039 }
20040 }
20041
20042 return idx < 0;
20043}
20044
20045/*
20046 * Free the variable with a pending return value.
20047 */
20048 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020049discard_pending_return(rettv)
20050 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051{
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053}
20054
20055/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020056 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 * is an allocated string. Used by report_pending() for verbose messages.
20058 */
20059 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020060get_return_cmd(rettv)
20061 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020063 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020064 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020065 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020067 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020068 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020069 if (s == NULL)
20070 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071
20072 STRCPY(IObuff, ":return ");
20073 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20074 if (STRLEN(s) + 8 >= IOSIZE)
20075 STRCPY(IObuff + IOSIZE - 4, "...");
20076 vim_free(tofree);
20077 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078}
20079
20080/*
20081 * Get next function line.
20082 * Called by do_cmdline() to get the next line.
20083 * Returns allocated string, or NULL for end of function.
20084 */
20085/* ARGSUSED */
20086 char_u *
20087get_func_line(c, cookie, indent)
20088 int c; /* not used */
20089 void *cookie;
20090 int indent; /* not used */
20091{
Bram Moolenaar33570922005-01-25 22:26:29 +000020092 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020093 ufunc_T *fp = fcp->func;
20094 char_u *retval;
20095 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096
20097 /* If breakpoints have been added/deleted need to check for it. */
20098 if (fcp->dbg_tick != debug_tick)
20099 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020100 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 sourcing_lnum);
20102 fcp->dbg_tick = debug_tick;
20103 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020104#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020105 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020106 func_line_end(cookie);
20107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108
Bram Moolenaar05159a02005-02-26 23:04:13 +000020109 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020110 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20111 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 retval = NULL;
20113 else
20114 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020115 /* Skip NULL lines (continuation lines). */
20116 while (fcp->linenr < gap->ga_len
20117 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20118 ++fcp->linenr;
20119 if (fcp->linenr >= gap->ga_len)
20120 retval = NULL;
20121 else
20122 {
20123 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20124 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020125#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020126 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020127 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020128#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 }
20131
20132 /* Did we encounter a breakpoint? */
20133 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20134 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020135 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020137 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 sourcing_lnum);
20139 fcp->dbg_tick = debug_tick;
20140 }
20141
20142 return retval;
20143}
20144
Bram Moolenaar05159a02005-02-26 23:04:13 +000020145#if defined(FEAT_PROFILE) || defined(PROTO)
20146/*
20147 * Called when starting to read a function line.
20148 * "sourcing_lnum" must be correct!
20149 * When skipping lines it may not actually be executed, but we won't find out
20150 * until later and we need to store the time now.
20151 */
20152 void
20153func_line_start(cookie)
20154 void *cookie;
20155{
20156 funccall_T *fcp = (funccall_T *)cookie;
20157 ufunc_T *fp = fcp->func;
20158
20159 if (fp->uf_profiling && sourcing_lnum >= 1
20160 && sourcing_lnum <= fp->uf_lines.ga_len)
20161 {
20162 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020163 /* Skip continuation lines. */
20164 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20165 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020166 fp->uf_tml_execed = FALSE;
20167 profile_start(&fp->uf_tml_start);
20168 profile_zero(&fp->uf_tml_children);
20169 profile_get_wait(&fp->uf_tml_wait);
20170 }
20171}
20172
20173/*
20174 * Called when actually executing a function line.
20175 */
20176 void
20177func_line_exec(cookie)
20178 void *cookie;
20179{
20180 funccall_T *fcp = (funccall_T *)cookie;
20181 ufunc_T *fp = fcp->func;
20182
20183 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20184 fp->uf_tml_execed = TRUE;
20185}
20186
20187/*
20188 * Called when done with a function line.
20189 */
20190 void
20191func_line_end(cookie)
20192 void *cookie;
20193{
20194 funccall_T *fcp = (funccall_T *)cookie;
20195 ufunc_T *fp = fcp->func;
20196
20197 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20198 {
20199 if (fp->uf_tml_execed)
20200 {
20201 ++fp->uf_tml_count[fp->uf_tml_idx];
20202 profile_end(&fp->uf_tml_start);
20203 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020204 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020205 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20206 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020207 }
20208 fp->uf_tml_idx = -1;
20209 }
20210}
20211#endif
20212
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213/*
20214 * Return TRUE if the currently active function should be ended, because a
20215 * return was encountered or an error occured. Used inside a ":while".
20216 */
20217 int
20218func_has_ended(cookie)
20219 void *cookie;
20220{
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222
20223 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20224 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020225 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 || fcp->returned);
20227}
20228
20229/*
20230 * return TRUE if cookie indicates a function which "abort"s on errors.
20231 */
20232 int
20233func_has_abort(cookie)
20234 void *cookie;
20235{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020236 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237}
20238
20239#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20240typedef enum
20241{
20242 VAR_FLAVOUR_DEFAULT,
20243 VAR_FLAVOUR_SESSION,
20244 VAR_FLAVOUR_VIMINFO
20245} var_flavour_T;
20246
20247static var_flavour_T var_flavour __ARGS((char_u *varname));
20248
20249 static var_flavour_T
20250var_flavour(varname)
20251 char_u *varname;
20252{
20253 char_u *p = varname;
20254
20255 if (ASCII_ISUPPER(*p))
20256 {
20257 while (*(++p))
20258 if (ASCII_ISLOWER(*p))
20259 return VAR_FLAVOUR_SESSION;
20260 return VAR_FLAVOUR_VIMINFO;
20261 }
20262 else
20263 return VAR_FLAVOUR_DEFAULT;
20264}
20265#endif
20266
20267#if defined(FEAT_VIMINFO) || defined(PROTO)
20268/*
20269 * Restore global vars that start with a capital from the viminfo file
20270 */
20271 int
20272read_viminfo_varlist(virp, writing)
20273 vir_T *virp;
20274 int writing;
20275{
20276 char_u *tab;
20277 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020278 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279
20280 if (!writing && (find_viminfo_parameter('!') != NULL))
20281 {
20282 tab = vim_strchr(virp->vir_line + 1, '\t');
20283 if (tab != NULL)
20284 {
20285 *tab++ = '\0'; /* isolate the variable name */
20286 if (*tab == 'S') /* string var */
20287 is_string = TRUE;
20288
20289 tab = vim_strchr(tab, '\t');
20290 if (tab != NULL)
20291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 if (is_string)
20293 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020294 tv.v_type = VAR_STRING;
20295 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 }
20298 else
20299 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020300 tv.v_type = VAR_NUMBER;
20301 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020303 set_var(virp->vir_line + 1, &tv, FALSE);
20304 if (is_string)
20305 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 }
20307 }
20308 }
20309
20310 return viminfo_readline(virp);
20311}
20312
20313/*
20314 * Write global vars that start with a capital to the viminfo file
20315 */
20316 void
20317write_viminfo_varlist(fp)
20318 FILE *fp;
20319{
Bram Moolenaar33570922005-01-25 22:26:29 +000020320 hashitem_T *hi;
20321 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020322 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020323 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020324 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020325 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020326 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327
20328 if (find_viminfo_parameter('!') == NULL)
20329 return;
20330
20331 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020333 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020334 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020336 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020338 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020339 this_var = HI2DI(hi);
20340 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020341 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020342 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020343 {
20344 case VAR_STRING: s = "STR"; break;
20345 case VAR_NUMBER: s = "NUM"; break;
20346 default: continue;
20347 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020348 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020349 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020350 if (p != NULL)
20351 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020352 vim_free(tofree);
20353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 }
20355 }
20356}
20357#endif
20358
20359#if defined(FEAT_SESSION) || defined(PROTO)
20360 int
20361store_session_globals(fd)
20362 FILE *fd;
20363{
Bram Moolenaar33570922005-01-25 22:26:29 +000020364 hashitem_T *hi;
20365 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020366 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 char_u *p, *t;
20368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020369 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020370 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020374 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020375 this_var = HI2DI(hi);
20376 if ((this_var->di_tv.v_type == VAR_NUMBER
20377 || this_var->di_tv.v_type == VAR_STRING)
20378 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020380 /* Escape special characters with a backslash. Turn a LF and
20381 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020383 (char_u *)"\\\"\n\r");
20384 if (p == NULL) /* out of memory */
20385 break;
20386 for (t = p; *t != NUL; ++t)
20387 if (*t == '\n')
20388 *t = 'n';
20389 else if (*t == '\r')
20390 *t = 'r';
20391 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 this_var->di_key,
20393 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20394 : ' ',
20395 p,
20396 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20397 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020398 || put_eol(fd) == FAIL)
20399 {
20400 vim_free(p);
20401 return FAIL;
20402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 vim_free(p);
20404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 }
20406 }
20407 return OK;
20408}
20409#endif
20410
Bram Moolenaar661b1822005-07-28 22:36:45 +000020411/*
20412 * Display script name where an item was last set.
20413 * Should only be invoked when 'verbose' is non-zero.
20414 */
20415 void
20416last_set_msg(scriptID)
20417 scid_T scriptID;
20418{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020419 char_u *p;
20420
Bram Moolenaar661b1822005-07-28 22:36:45 +000020421 if (scriptID != 0)
20422 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020423 p = home_replace_save(NULL, get_scriptname(scriptID));
20424 if (p != NULL)
20425 {
20426 verbose_enter();
20427 MSG_PUTS(_("\n\tLast set from "));
20428 MSG_PUTS(p);
20429 vim_free(p);
20430 verbose_leave();
20431 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020432 }
20433}
20434
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435#endif /* FEAT_EVAL */
20436
20437#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20438
20439
20440#ifdef WIN3264
20441/*
20442 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20443 */
20444static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20445static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20446static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20447
20448/*
20449 * Get the short pathname of a file.
20450 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20451 */
20452 static int
20453get_short_pathname(fnamep, bufp, fnamelen)
20454 char_u **fnamep;
20455 char_u **bufp;
20456 int *fnamelen;
20457{
20458 int l,len;
20459 char_u *newbuf;
20460
20461 len = *fnamelen;
20462
20463 l = GetShortPathName(*fnamep, *fnamep, len);
20464 if (l > len - 1)
20465 {
20466 /* If that doesn't work (not enough space), then save the string
20467 * and try again with a new buffer big enough
20468 */
20469 newbuf = vim_strnsave(*fnamep, l);
20470 if (newbuf == NULL)
20471 return 0;
20472
20473 vim_free(*bufp);
20474 *fnamep = *bufp = newbuf;
20475
20476 l = GetShortPathName(*fnamep,*fnamep,l+1);
20477
20478 /* Really should always succeed, as the buffer is big enough */
20479 }
20480
20481 *fnamelen = l;
20482 return 1;
20483}
20484
20485/*
20486 * Create a short path name. Returns the length of the buffer it needs.
20487 * Doesn't copy over the end of the buffer passed in.
20488 */
20489 static int
20490shortpath_for_invalid_fname(fname, bufp, fnamelen)
20491 char_u **fname;
20492 char_u **bufp;
20493 int *fnamelen;
20494{
20495 char_u *s, *p, *pbuf2, *pbuf3;
20496 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020497 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498
20499 /* Make a copy */
20500 len2 = *fnamelen;
20501 pbuf2 = vim_strnsave(*fname, len2);
20502 pbuf3 = NULL;
20503
20504 s = pbuf2 + len2 - 1; /* Find the end */
20505 slen = 1;
20506 plen = len2;
20507
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020508 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 {
20510 --s;
20511 ++slen;
20512 --plen;
20513 }
20514
20515 do
20516 {
20517 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020518 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 {
20520 --s;
20521 ++slen;
20522 --plen;
20523 }
20524 if (s <= pbuf2)
20525 break;
20526
20527 /* Remeber the character that is about to be blatted */
20528 ch = *s;
20529 *s = 0; /* get_short_pathname requires a null-terminated string */
20530
20531 /* Try it in situ */
20532 p = pbuf2;
20533 if (!get_short_pathname(&p, &pbuf3, &plen))
20534 {
20535 vim_free(pbuf2);
20536 return -1;
20537 }
20538 *s = ch; /* Preserve the string */
20539 } while (plen == 0);
20540
20541 if (plen > 0)
20542 {
20543 /* Remeber the length of the new string. */
20544 *fnamelen = len = plen + slen;
20545 vim_free(*bufp);
20546 if (len > len2)
20547 {
20548 /* If there's not enough space in the currently allocated string,
20549 * then copy it to a buffer big enough.
20550 */
20551 *fname= *bufp = vim_strnsave(p, len);
20552 if (*fname == NULL)
20553 return -1;
20554 }
20555 else
20556 {
20557 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20558 *fname = *bufp = pbuf2;
20559 if (p != pbuf2)
20560 strncpy(*fname, p, plen);
20561 pbuf2 = NULL;
20562 }
20563 /* Concat the next bit */
20564 strncpy(*fname + plen, s, slen);
20565 (*fname)[len] = '\0';
20566 }
20567 vim_free(pbuf3);
20568 vim_free(pbuf2);
20569 return 0;
20570}
20571
20572/*
20573 * Get a pathname for a partial path.
20574 */
20575 static int
20576shortpath_for_partial(fnamep, bufp, fnamelen)
20577 char_u **fnamep;
20578 char_u **bufp;
20579 int *fnamelen;
20580{
20581 int sepcount, len, tflen;
20582 char_u *p;
20583 char_u *pbuf, *tfname;
20584 int hasTilde;
20585
20586 /* Count up the path seperators from the RHS.. so we know which part
20587 * of the path to return.
20588 */
20589 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020590 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 if (vim_ispathsep(*p))
20592 ++sepcount;
20593
20594 /* Need full path first (use expand_env() to remove a "~/") */
20595 hasTilde = (**fnamep == '~');
20596 if (hasTilde)
20597 pbuf = tfname = expand_env_save(*fnamep);
20598 else
20599 pbuf = tfname = FullName_save(*fnamep, FALSE);
20600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020601 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602
20603 if (!get_short_pathname(&tfname, &pbuf, &len))
20604 return -1;
20605
20606 if (len == 0)
20607 {
20608 /* Don't have a valid filename, so shorten the rest of the
20609 * path if we can. This CAN give us invalid 8.3 filenames, but
20610 * there's not a lot of point in guessing what it might be.
20611 */
20612 len = tflen;
20613 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20614 return -1;
20615 }
20616
20617 /* Count the paths backward to find the beginning of the desired string. */
20618 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020619 {
20620#ifdef FEAT_MBYTE
20621 if (has_mbyte)
20622 p -= mb_head_off(tfname, p);
20623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 if (vim_ispathsep(*p))
20625 {
20626 if (sepcount == 0 || (hasTilde && sepcount == 1))
20627 break;
20628 else
20629 sepcount --;
20630 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 if (hasTilde)
20633 {
20634 --p;
20635 if (p >= tfname)
20636 *p = '~';
20637 else
20638 return -1;
20639 }
20640 else
20641 ++p;
20642
20643 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20644 vim_free(*bufp);
20645 *fnamelen = (int)STRLEN(p);
20646 *bufp = pbuf;
20647 *fnamep = p;
20648
20649 return 0;
20650}
20651#endif /* WIN3264 */
20652
20653/*
20654 * Adjust a filename, according to a string of modifiers.
20655 * *fnamep must be NUL terminated when called. When returning, the length is
20656 * determined by *fnamelen.
20657 * Returns valid flags.
20658 * When there is an error, *fnamep is set to NULL.
20659 */
20660 int
20661modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20662 char_u *src; /* string with modifiers */
20663 int *usedlen; /* characters after src that are used */
20664 char_u **fnamep; /* file name so far */
20665 char_u **bufp; /* buffer for allocated file name or NULL */
20666 int *fnamelen; /* length of fnamep */
20667{
20668 int valid = 0;
20669 char_u *tail;
20670 char_u *s, *p, *pbuf;
20671 char_u dirname[MAXPATHL];
20672 int c;
20673 int has_fullname = 0;
20674#ifdef WIN3264
20675 int has_shortname = 0;
20676#endif
20677
20678repeat:
20679 /* ":p" - full path/file_name */
20680 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20681 {
20682 has_fullname = 1;
20683
20684 valid |= VALID_PATH;
20685 *usedlen += 2;
20686
20687 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20688 if ((*fnamep)[0] == '~'
20689#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20690 && ((*fnamep)[1] == '/'
20691# ifdef BACKSLASH_IN_FILENAME
20692 || (*fnamep)[1] == '\\'
20693# endif
20694 || (*fnamep)[1] == NUL)
20695
20696#endif
20697 )
20698 {
20699 *fnamep = expand_env_save(*fnamep);
20700 vim_free(*bufp); /* free any allocated file name */
20701 *bufp = *fnamep;
20702 if (*fnamep == NULL)
20703 return -1;
20704 }
20705
20706 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020707 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 {
20709 if (vim_ispathsep(*p)
20710 && p[1] == '.'
20711 && (p[2] == NUL
20712 || vim_ispathsep(p[2])
20713 || (p[2] == '.'
20714 && (p[3] == NUL || vim_ispathsep(p[3])))))
20715 break;
20716 }
20717
20718 /* FullName_save() is slow, don't use it when not needed. */
20719 if (*p != NUL || !vim_isAbsName(*fnamep))
20720 {
20721 *fnamep = FullName_save(*fnamep, *p != NUL);
20722 vim_free(*bufp); /* free any allocated file name */
20723 *bufp = *fnamep;
20724 if (*fnamep == NULL)
20725 return -1;
20726 }
20727
20728 /* Append a path separator to a directory. */
20729 if (mch_isdir(*fnamep))
20730 {
20731 /* Make room for one or two extra characters. */
20732 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20733 vim_free(*bufp); /* free any allocated file name */
20734 *bufp = *fnamep;
20735 if (*fnamep == NULL)
20736 return -1;
20737 add_pathsep(*fnamep);
20738 }
20739 }
20740
20741 /* ":." - path relative to the current directory */
20742 /* ":~" - path relative to the home directory */
20743 /* ":8" - shortname path - postponed till after */
20744 while (src[*usedlen] == ':'
20745 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20746 {
20747 *usedlen += 2;
20748 if (c == '8')
20749 {
20750#ifdef WIN3264
20751 has_shortname = 1; /* Postpone this. */
20752#endif
20753 continue;
20754 }
20755 pbuf = NULL;
20756 /* Need full path first (use expand_env() to remove a "~/") */
20757 if (!has_fullname)
20758 {
20759 if (c == '.' && **fnamep == '~')
20760 p = pbuf = expand_env_save(*fnamep);
20761 else
20762 p = pbuf = FullName_save(*fnamep, FALSE);
20763 }
20764 else
20765 p = *fnamep;
20766
20767 has_fullname = 0;
20768
20769 if (p != NULL)
20770 {
20771 if (c == '.')
20772 {
20773 mch_dirname(dirname, MAXPATHL);
20774 s = shorten_fname(p, dirname);
20775 if (s != NULL)
20776 {
20777 *fnamep = s;
20778 if (pbuf != NULL)
20779 {
20780 vim_free(*bufp); /* free any allocated file name */
20781 *bufp = pbuf;
20782 pbuf = NULL;
20783 }
20784 }
20785 }
20786 else
20787 {
20788 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20789 /* Only replace it when it starts with '~' */
20790 if (*dirname == '~')
20791 {
20792 s = vim_strsave(dirname);
20793 if (s != NULL)
20794 {
20795 *fnamep = s;
20796 vim_free(*bufp);
20797 *bufp = s;
20798 }
20799 }
20800 }
20801 vim_free(pbuf);
20802 }
20803 }
20804
20805 tail = gettail(*fnamep);
20806 *fnamelen = (int)STRLEN(*fnamep);
20807
20808 /* ":h" - head, remove "/file_name", can be repeated */
20809 /* Don't remove the first "/" or "c:\" */
20810 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20811 {
20812 valid |= VALID_HEAD;
20813 *usedlen += 2;
20814 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020815 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 --tail;
20817 *fnamelen = (int)(tail - *fnamep);
20818#ifdef VMS
20819 if (*fnamelen > 0)
20820 *fnamelen += 1; /* the path separator is part of the path */
20821#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020822 while (tail > s && !after_pathsep(s, tail))
20823 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 }
20825
20826 /* ":8" - shortname */
20827 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20828 {
20829 *usedlen += 2;
20830#ifdef WIN3264
20831 has_shortname = 1;
20832#endif
20833 }
20834
20835#ifdef WIN3264
20836 /* Check shortname after we have done 'heads' and before we do 'tails'
20837 */
20838 if (has_shortname)
20839 {
20840 pbuf = NULL;
20841 /* Copy the string if it is shortened by :h */
20842 if (*fnamelen < (int)STRLEN(*fnamep))
20843 {
20844 p = vim_strnsave(*fnamep, *fnamelen);
20845 if (p == 0)
20846 return -1;
20847 vim_free(*bufp);
20848 *bufp = *fnamep = p;
20849 }
20850
20851 /* Split into two implementations - makes it easier. First is where
20852 * there isn't a full name already, second is where there is.
20853 */
20854 if (!has_fullname && !vim_isAbsName(*fnamep))
20855 {
20856 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20857 return -1;
20858 }
20859 else
20860 {
20861 int l;
20862
20863 /* Simple case, already have the full-name
20864 * Nearly always shorter, so try first time. */
20865 l = *fnamelen;
20866 if (!get_short_pathname(fnamep, bufp, &l))
20867 return -1;
20868
20869 if (l == 0)
20870 {
20871 /* Couldn't find the filename.. search the paths.
20872 */
20873 l = *fnamelen;
20874 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20875 return -1;
20876 }
20877 *fnamelen = l;
20878 }
20879 }
20880#endif /* WIN3264 */
20881
20882 /* ":t" - tail, just the basename */
20883 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20884 {
20885 *usedlen += 2;
20886 *fnamelen -= (int)(tail - *fnamep);
20887 *fnamep = tail;
20888 }
20889
20890 /* ":e" - extension, can be repeated */
20891 /* ":r" - root, without extension, can be repeated */
20892 while (src[*usedlen] == ':'
20893 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20894 {
20895 /* find a '.' in the tail:
20896 * - for second :e: before the current fname
20897 * - otherwise: The last '.'
20898 */
20899 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20900 s = *fnamep - 2;
20901 else
20902 s = *fnamep + *fnamelen - 1;
20903 for ( ; s > tail; --s)
20904 if (s[0] == '.')
20905 break;
20906 if (src[*usedlen + 1] == 'e') /* :e */
20907 {
20908 if (s > tail)
20909 {
20910 *fnamelen += (int)(*fnamep - (s + 1));
20911 *fnamep = s + 1;
20912#ifdef VMS
20913 /* cut version from the extension */
20914 s = *fnamep + *fnamelen - 1;
20915 for ( ; s > *fnamep; --s)
20916 if (s[0] == ';')
20917 break;
20918 if (s > *fnamep)
20919 *fnamelen = s - *fnamep;
20920#endif
20921 }
20922 else if (*fnamep <= tail)
20923 *fnamelen = 0;
20924 }
20925 else /* :r */
20926 {
20927 if (s > tail) /* remove one extension */
20928 *fnamelen = (int)(s - *fnamep);
20929 }
20930 *usedlen += 2;
20931 }
20932
20933 /* ":s?pat?foo?" - substitute */
20934 /* ":gs?pat?foo?" - global substitute */
20935 if (src[*usedlen] == ':'
20936 && (src[*usedlen + 1] == 's'
20937 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20938 {
20939 char_u *str;
20940 char_u *pat;
20941 char_u *sub;
20942 int sep;
20943 char_u *flags;
20944 int didit = FALSE;
20945
20946 flags = (char_u *)"";
20947 s = src + *usedlen + 2;
20948 if (src[*usedlen + 1] == 'g')
20949 {
20950 flags = (char_u *)"g";
20951 ++s;
20952 }
20953
20954 sep = *s++;
20955 if (sep)
20956 {
20957 /* find end of pattern */
20958 p = vim_strchr(s, sep);
20959 if (p != NULL)
20960 {
20961 pat = vim_strnsave(s, (int)(p - s));
20962 if (pat != NULL)
20963 {
20964 s = p + 1;
20965 /* find end of substitution */
20966 p = vim_strchr(s, sep);
20967 if (p != NULL)
20968 {
20969 sub = vim_strnsave(s, (int)(p - s));
20970 str = vim_strnsave(*fnamep, *fnamelen);
20971 if (sub != NULL && str != NULL)
20972 {
20973 *usedlen = (int)(p + 1 - src);
20974 s = do_string_sub(str, pat, sub, flags);
20975 if (s != NULL)
20976 {
20977 *fnamep = s;
20978 *fnamelen = (int)STRLEN(s);
20979 vim_free(*bufp);
20980 *bufp = s;
20981 didit = TRUE;
20982 }
20983 }
20984 vim_free(sub);
20985 vim_free(str);
20986 }
20987 vim_free(pat);
20988 }
20989 }
20990 /* after using ":s", repeat all the modifiers */
20991 if (didit)
20992 goto repeat;
20993 }
20994 }
20995
20996 return valid;
20997}
20998
20999/*
21000 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21001 * "flags" can be "g" to do a global substitute.
21002 * Returns an allocated string, NULL for error.
21003 */
21004 char_u *
21005do_string_sub(str, pat, sub, flags)
21006 char_u *str;
21007 char_u *pat;
21008 char_u *sub;
21009 char_u *flags;
21010{
21011 int sublen;
21012 regmatch_T regmatch;
21013 int i;
21014 int do_all;
21015 char_u *tail;
21016 garray_T ga;
21017 char_u *ret;
21018 char_u *save_cpo;
21019
21020 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21021 save_cpo = p_cpo;
21022 p_cpo = (char_u *)"";
21023
21024 ga_init2(&ga, 1, 200);
21025
21026 do_all = (flags[0] == 'g');
21027
21028 regmatch.rm_ic = p_ic;
21029 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21030 if (regmatch.regprog != NULL)
21031 {
21032 tail = str;
21033 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21034 {
21035 /*
21036 * Get some space for a temporary buffer to do the substitution
21037 * into. It will contain:
21038 * - The text up to where the match is.
21039 * - The substituted text.
21040 * - The text after the match.
21041 */
21042 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21043 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21044 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21045 {
21046 ga_clear(&ga);
21047 break;
21048 }
21049
21050 /* copy the text up to where the match is */
21051 i = (int)(regmatch.startp[0] - tail);
21052 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21053 /* add the substituted text */
21054 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21055 + ga.ga_len + i, TRUE, TRUE, FALSE);
21056 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057 /* avoid getting stuck on a match with an empty string */
21058 if (tail == regmatch.endp[0])
21059 {
21060 if (*tail == NUL)
21061 break;
21062 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21063 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 }
21065 else
21066 {
21067 tail = regmatch.endp[0];
21068 if (*tail == NUL)
21069 break;
21070 }
21071 if (!do_all)
21072 break;
21073 }
21074
21075 if (ga.ga_data != NULL)
21076 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21077
21078 vim_free(regmatch.regprog);
21079 }
21080
21081 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21082 ga_clear(&ga);
21083 p_cpo = save_cpo;
21084
21085 return ret;
21086}
21087
21088#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */