blob: 8728608e8882640112fee59e018a9d4a420610ee [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000372#ifdef FEAT_WINDOWS
373static void list_tab_vars __ARGS((void));
374#endif
Bram Moolenaara40058a2005-07-11 22:42:07 +0000375static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000376static void list_script_vars __ARGS((void));
377static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
379static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
380static int check_changedtick __ARGS((char_u *arg));
381static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
382static void clear_lval __ARGS((lval_T *lp));
383static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
384static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
385static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
386static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
387static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
388static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
389static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
390static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
391static void item_lock __ARGS((typval_T *tv, int deep, int lock));
392static int tv_islocked __ARGS((typval_T *tv));
393
Bram Moolenaar33570922005-01-25 22:26:29 +0000394static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
395static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000403static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000408static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static listitem_T *listitem_alloc __ARGS((void));
410static void listitem_free __ARGS((listitem_T *item));
411static void listitem_remove __ARGS((list_T *l, listitem_T *item));
412static long list_len __ARGS((list_T *l));
413static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
414static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
415static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000417static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static void list_append __ARGS((list_T *l, listitem_T *item));
420static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000421static int list_append_string __ARGS((list_T *l, char_u *str, int len));
422static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
424static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
425static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000426static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
429static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000430static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
431static void set_ref_in_list __ARGS((list_T *l, int copyID));
432static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static void dict_unref __ARGS((dict_T *d));
434static void dict_free __ARGS((dict_T *d));
435static dictitem_T *dictitem_alloc __ARGS((char_u *key));
436static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
437static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
438static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static int dict_add __ARGS((dict_T *d, dictitem_T *item));
441static long dict_len __ARGS((dict_T *d));
442static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
446static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000447static char_u *string_quote __ARGS((char_u *str, int function));
448static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
449static int find_internal_func __ARGS((char_u *name));
450static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
451static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
452static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000453static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454
455static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000471static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000475#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000476static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000477static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
479#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
485static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000511static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000513static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000519static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000527static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000528static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000551static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000552static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000557static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000573static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000575static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000579#ifdef vim_mkdir
580static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
581#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000585static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000587static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000588static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000590static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000591static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000604static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000606static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000613static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000614static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000615static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000620static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000624static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625#ifdef HAVE_STRFTIME
626static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
628static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000640static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000641static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000642static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000643static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000644static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000646static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000660static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000663static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000665static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
666static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static int get_env_len __ARGS((char_u **arg));
668static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000669static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000670static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
671#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
672#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
673 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000674static 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 +0000675static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000676static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000677static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
678static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static typval_T *alloc_tv __ARGS((void));
680static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void init_tv __ARGS((typval_T *varp));
682static long get_tv_number __ARGS((typval_T *varp));
683static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000684static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static char_u *get_tv_string __ARGS((typval_T *varp));
686static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000687static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
691static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
692static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
693static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
694static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
695static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
696static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000697static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000699static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
701static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
702static int eval_fname_script __ARGS((char_u *p));
703static int eval_fname_sid __ARGS((char_u *p));
704static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static ufunc_T *find_func __ARGS((char_u *name));
706static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000707static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000708#ifdef FEAT_PROFILE
709static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000710static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
711static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
712static int
713# ifdef __BORLANDC__
714 _RTLENTRYF
715# endif
716 prof_total_cmp __ARGS((const void *s1, const void *s2));
717static int
718# ifdef __BORLANDC__
719 _RTLENTRYF
720# endif
721 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000722#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000723static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000724static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000725static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void func_free __ARGS((ufunc_T *fp));
727static void func_unref __ARGS((char_u *name));
728static void func_ref __ARGS((char_u *name));
729static 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));
730static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000731static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000732static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000733static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000735/* Character used as separated in autoload function/variable names. */
736#define AUTOLOAD_CHAR '#'
737
Bram Moolenaar33570922005-01-25 22:26:29 +0000738/*
739 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000740 */
741 void
742eval_init()
743{
Bram Moolenaar33570922005-01-25 22:26:29 +0000744 int i;
745 struct vimvar *p;
746
747 init_var_dict(&globvardict, &globvars_var);
748 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000749 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000750 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000751
752 for (i = 0; i < VV_LEN; ++i)
753 {
754 p = &vimvars[i];
755 STRCPY(p->vv_di.di_key, p->vv_name);
756 if (p->vv_flags & VV_RO)
757 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
758 else if (p->vv_flags & VV_RO_SBX)
759 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
760 else
761 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762
763 /* add to v: scope dict, unless the value is not always available */
764 if (p->vv_type != VAR_UNKNOWN)
765 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000766 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000767 /* add to compat scope dict */
768 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000770}
771
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000772#if defined(EXITFREE) || defined(PROTO)
773 void
774eval_clear()
775{
776 int i;
777 struct vimvar *p;
778
779 for (i = 0; i < VV_LEN; ++i)
780 {
781 p = &vimvars[i];
782 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000783 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000784 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000785 p->vv_di.di_tv.vval.v_string = NULL;
786 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000787 }
788 hash_clear(&vimvarht);
789 hash_clear(&compat_hashtab);
790
791 /* script-local variables */
792 for (i = 1; i <= ga_scripts.ga_len; ++i)
793 vars_clear(&SCRIPT_VARS(i));
794 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000795 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000796
797 /* global variables */
798 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000799
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000800 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000801 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000802 hash_clear(&func_hashtab);
803
804 /* unreferenced lists and dicts */
805 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000806}
807#endif
808
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 * Return the name of the executed function.
811 */
812 char_u *
813func_name(cookie)
814 void *cookie;
815{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817}
818
819/*
820 * Return the address holding the next breakpoint line for a funccall cookie.
821 */
822 linenr_T *
823func_breakpoint(cookie)
824 void *cookie;
825{
Bram Moolenaar33570922005-01-25 22:26:29 +0000826 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827}
828
829/*
830 * Return the address holding the debug tick for a funccall cookie.
831 */
832 int *
833func_dbg_tick(cookie)
834 void *cookie;
835{
Bram Moolenaar33570922005-01-25 22:26:29 +0000836 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837}
838
839/*
840 * Return the nesting level for a funccall cookie.
841 */
842 int
843func_level(cookie)
844 void *cookie;
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
849/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000850funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852/*
853 * Return TRUE when a function was ended by a ":return" command.
854 */
855 int
856current_func_returned()
857{
858 return current_funccal->returned;
859}
860
861
862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * Set an internal variable to a string value. Creates the variable if it does
864 * not already exist.
865 */
866 void
867set_internal_string_var(name, value)
868 char_u *name;
869 char_u *value;
870{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000871 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874 val = vim_strsave(value);
875 if (val != NULL)
876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000877 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000878 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000880 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000881 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 }
883 }
884}
885
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000886static lval_T *redir_lval = NULL;
887static char_u *redir_endp = NULL;
888static char_u *redir_varname = NULL;
889
890/*
891 * Start recording command output to a variable
892 * Returns OK if successfully completed the setup. FAIL otherwise.
893 */
894 int
895var_redir_start(name, append)
896 char_u *name;
897 int append; /* append to an existing variable */
898{
899 int save_emsg;
900 int err;
901 typval_T tv;
902
903 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000904 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000905 {
906 EMSG(_(e_invarg));
907 return FAIL;
908 }
909
910 redir_varname = vim_strsave(name);
911 if (redir_varname == NULL)
912 return FAIL;
913
914 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
915 if (redir_lval == NULL)
916 {
917 var_redir_stop();
918 return FAIL;
919 }
920
921 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000922 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
923 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000924 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
925 {
926 if (redir_endp != NULL && *redir_endp != NUL)
927 /* Trailing characters are present after the variable name */
928 EMSG(_(e_trailing));
929 else
930 EMSG(_(e_invarg));
931 var_redir_stop();
932 return FAIL;
933 }
934
935 /* check if we can write to the variable: set it to or append an empty
936 * string */
937 save_emsg = did_emsg;
938 did_emsg = FALSE;
939 tv.v_type = VAR_STRING;
940 tv.vval.v_string = (char_u *)"";
941 if (append)
942 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
943 else
944 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
945 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000946 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000947 if (err)
948 {
949 var_redir_stop();
950 return FAIL;
951 }
952 if (redir_lval->ll_newkey != NULL)
953 {
954 /* Dictionary item was created, don't do it again. */
955 vim_free(redir_lval->ll_newkey);
956 redir_lval->ll_newkey = NULL;
957 }
958
959 return OK;
960}
961
962/*
963 * Append "value[len]" to the variable set by var_redir_start().
964 */
965 void
966var_redir_str(value, len)
967 char_u *value;
968 int len;
969{
970 char_u *val;
971 typval_T tv;
972 int save_emsg;
973 int err;
974
975 if (redir_lval == NULL)
976 return;
977
978 if (len == -1)
979 /* Append the entire string */
980 val = vim_strsave(value);
981 else
982 /* Append only the specified number of characters */
983 val = vim_strnsave(value, len);
984 if (val == NULL)
985 return;
986
987 tv.v_type = VAR_STRING;
988 tv.vval.v_string = val;
989
990 save_emsg = did_emsg;
991 did_emsg = FALSE;
992 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
993 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000994 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000995 if (err)
996 var_redir_stop();
997
998 vim_free(tv.vval.v_string);
999}
1000
1001/*
1002 * Stop redirecting command output to a variable.
1003 */
1004 void
1005var_redir_stop()
1006{
1007 if (redir_lval != NULL)
1008 {
1009 clear_lval(redir_lval);
1010 vim_free(redir_lval);
1011 redir_lval = NULL;
1012 }
1013 vim_free(redir_varname);
1014 redir_varname = NULL;
1015}
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017# if defined(FEAT_MBYTE) || defined(PROTO)
1018 int
1019eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1020 char_u *enc_from;
1021 char_u *enc_to;
1022 char_u *fname_from;
1023 char_u *fname_to;
1024{
1025 int err = FALSE;
1026
1027 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1028 set_vim_var_string(VV_CC_TO, enc_to, -1);
1029 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1030 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1031 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1032 err = TRUE;
1033 set_vim_var_string(VV_CC_FROM, NULL, -1);
1034 set_vim_var_string(VV_CC_TO, NULL, -1);
1035 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1036 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1037
1038 if (err)
1039 return FAIL;
1040 return OK;
1041}
1042# endif
1043
1044# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1045 int
1046eval_printexpr(fname, args)
1047 char_u *fname;
1048 char_u *args;
1049{
1050 int err = FALSE;
1051
1052 set_vim_var_string(VV_FNAME_IN, fname, -1);
1053 set_vim_var_string(VV_CMDARG, args, -1);
1054 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1055 err = TRUE;
1056 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1057 set_vim_var_string(VV_CMDARG, NULL, -1);
1058
1059 if (err)
1060 {
1061 mch_remove(fname);
1062 return FAIL;
1063 }
1064 return OK;
1065}
1066# endif
1067
1068# if defined(FEAT_DIFF) || defined(PROTO)
1069 void
1070eval_diff(origfile, newfile, outfile)
1071 char_u *origfile;
1072 char_u *newfile;
1073 char_u *outfile;
1074{
1075 int err = FALSE;
1076
1077 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1078 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1079 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1080 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1081 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1082 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1083 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1084}
1085
1086 void
1087eval_patch(origfile, difffile, outfile)
1088 char_u *origfile;
1089 char_u *difffile;
1090 char_u *outfile;
1091{
1092 int err;
1093
1094 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1095 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1096 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1097 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1098 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1099 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1100 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1101}
1102# endif
1103
1104/*
1105 * Top level evaluation function, returning a boolean.
1106 * Sets "error" to TRUE if there was an error.
1107 * Return TRUE or FALSE.
1108 */
1109 int
1110eval_to_bool(arg, error, nextcmd, skip)
1111 char_u *arg;
1112 int *error;
1113 char_u **nextcmd;
1114 int skip; /* only parse, don't execute */
1115{
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 int retval = FALSE;
1118
1119 if (skip)
1120 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else
1124 {
1125 *error = FALSE;
1126 if (!skip)
1127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001128 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 }
1132 if (skip)
1133 --emsg_skip;
1134
1135 return retval;
1136}
1137
1138/*
1139 * Top level evaluation function, returning a string. If "skip" is TRUE,
1140 * only parsing to "nextcmd" is done, without reporting errors. Return
1141 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1142 */
1143 char_u *
1144eval_to_string_skip(arg, nextcmd, skip)
1145 char_u *arg;
1146 char_u **nextcmd;
1147 int skip; /* only parse, don't execute */
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 char_u *retval;
1151
1152 if (skip)
1153 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 retval = NULL;
1156 else
1157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 retval = vim_strsave(get_tv_string(&tv));
1159 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 if (skip)
1162 --emsg_skip;
1163
1164 return retval;
1165}
1166
1167/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001168 * Skip over an expression at "*pp".
1169 * Return FAIL for an error, OK otherwise.
1170 */
1171 int
1172skip_expr(pp)
1173 char_u **pp;
1174{
Bram Moolenaar33570922005-01-25 22:26:29 +00001175 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001176
1177 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001179}
1180
1181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 * Top level evaluation function, returning a string.
1183 * Return pointer to allocated memory, or NULL for failure.
1184 */
1185 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001186eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 char_u *arg;
1188 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001189 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001193 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 retval = NULL;
1197 else
1198 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001199 if (dolist && tv.v_type == VAR_LIST)
1200 {
1201 ga_init2(&ga, (int)sizeof(char), 80);
1202 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1203 ga_append(&ga, NUL);
1204 retval = (char_u *)ga.ga_data;
1205 }
1206 else
1207 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210
1211 return retval;
1212}
1213
1214/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001215 * Call eval_to_string() without using current local variables and using
1216 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
1218 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001219eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 char_u *arg;
1221 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001222 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223{
1224 char_u *retval;
1225 void *save_funccalp;
1226
1227 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001228 if (use_sandbox)
1229 ++sandbox;
1230 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001231 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001232 if (use_sandbox)
1233 --sandbox;
1234 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 restore_funccal(save_funccalp);
1236 return retval;
1237}
1238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239/*
1240 * Top level evaluation function, returning a number.
1241 * Evaluates "expr" silently.
1242 * Returns -1 for an error.
1243 */
1244 int
1245eval_to_number(expr)
1246 char_u *expr;
1247{
Bram Moolenaar33570922005-01-25 22:26:29 +00001248 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001250 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 ++emsg_off;
1253
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 retval = -1;
1256 else
1257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001258 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 --emsg_off;
1262
1263 return retval;
1264}
1265
Bram Moolenaara40058a2005-07-11 22:42:07 +00001266/*
1267 * Prepare v: variable "idx" to be used.
1268 * Save the current typeval in "save_tv".
1269 * When not used yet add the variable to the v: hashtable.
1270 */
1271 static void
1272prepare_vimvar(idx, save_tv)
1273 int idx;
1274 typval_T *save_tv;
1275{
1276 *save_tv = vimvars[idx].vv_tv;
1277 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1278 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1279}
1280
1281/*
1282 * Restore v: variable "idx" to typeval "save_tv".
1283 * When no longer defined, remove the variable from the v: hashtable.
1284 */
1285 static void
1286restore_vimvar(idx, save_tv)
1287 int idx;
1288 typval_T *save_tv;
1289{
1290 hashitem_T *hi;
1291
1292 clear_tv(&vimvars[idx].vv_tv);
1293 vimvars[idx].vv_tv = *save_tv;
1294 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1295 {
1296 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1297 if (HASHITEM_EMPTY(hi))
1298 EMSG2(_(e_intern2), "restore_vimvar()");
1299 else
1300 hash_remove(&vimvarht, hi);
1301 }
1302}
1303
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001304#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001305/*
1306 * Evaluate an expression to a list with suggestions.
1307 * For the "expr:" part of 'spellsuggest'.
1308 */
1309 list_T *
1310eval_spell_expr(badword, expr)
1311 char_u *badword;
1312 char_u *expr;
1313{
1314 typval_T save_val;
1315 typval_T rettv;
1316 list_T *list = NULL;
1317 char_u *p = skipwhite(expr);
1318
1319 /* Set "v:val" to the bad word. */
1320 prepare_vimvar(VV_VAL, &save_val);
1321 vimvars[VV_VAL].vv_type = VAR_STRING;
1322 vimvars[VV_VAL].vv_str = badword;
1323 if (p_verbose == 0)
1324 ++emsg_off;
1325
1326 if (eval1(&p, &rettv, TRUE) == OK)
1327 {
1328 if (rettv.v_type != VAR_LIST)
1329 clear_tv(&rettv);
1330 else
1331 list = rettv.vval.v_list;
1332 }
1333
1334 if (p_verbose == 0)
1335 --emsg_off;
1336 vimvars[VV_VAL].vv_str = NULL;
1337 restore_vimvar(VV_VAL, &save_val);
1338
1339 return list;
1340}
1341
1342/*
1343 * "list" is supposed to contain two items: a word and a number. Return the
1344 * word in "pp" and the number as the return value.
1345 * Return -1 if anything isn't right.
1346 * Used to get the good word and score from the eval_spell_expr() result.
1347 */
1348 int
1349get_spellword(list, pp)
1350 list_T *list;
1351 char_u **pp;
1352{
1353 listitem_T *li;
1354
1355 li = list->lv_first;
1356 if (li == NULL)
1357 return -1;
1358 *pp = get_tv_string(&li->li_tv);
1359
1360 li = li->li_next;
1361 if (li == NULL)
1362 return -1;
1363 return get_tv_number(&li->li_tv);
1364}
1365#endif
1366
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001367/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001368 * Top level evaluation function.
1369 * Returns an allocated typval_T with the result.
1370 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001371 */
1372 typval_T *
1373eval_expr(arg, nextcmd)
1374 char_u *arg;
1375 char_u **nextcmd;
1376{
1377 typval_T *tv;
1378
1379 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001380 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001381 {
1382 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001383 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001384 }
1385
1386 return tv;
1387}
1388
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1391/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001392 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001394 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001396 static int
1397call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *func;
1399 int argc;
1400 char_u **argv;
1401 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
Bram Moolenaar33570922005-01-25 22:26:29 +00001404 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 long n;
1406 int len;
1407 int i;
1408 int doesrange;
1409 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001410 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001414 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 for (i = 0; i < argc; i++)
1417 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001418 /* Pass a NULL or empty argument as an empty string */
1419 if (argv[i] == NULL || *argv[i] == NUL)
1420 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001421 argvars[i].v_type = VAR_STRING;
1422 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001423 continue;
1424 }
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /* Recognize a number argument, the others must be strings. */
1427 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1428 if (len != 0 && len == (int)STRLEN(argv[i]))
1429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001430 argvars[i].v_type = VAR_NUMBER;
1431 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 else
1434 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001435 argvars[i].v_type = VAR_STRING;
1436 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
1438 }
1439
1440 if (safe)
1441 {
1442 save_funccalp = save_funccal();
1443 ++sandbox;
1444 }
1445
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001446 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1447 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001449 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 if (safe)
1451 {
1452 --sandbox;
1453 restore_funccal(save_funccalp);
1454 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001455 vim_free(argvars);
1456
1457 if (ret == FAIL)
1458 clear_tv(rettv);
1459
1460 return ret;
1461}
1462
1463/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001464 * Call vimL function "func" and return the result as a string.
1465 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001466 * Uses argv[argc] for the function arguments.
1467 */
1468 void *
1469call_func_retstr(func, argc, argv, safe)
1470 char_u *func;
1471 int argc;
1472 char_u **argv;
1473 int safe; /* use the sandbox */
1474{
1475 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001476 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477
1478 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1479 return NULL;
1480
1481 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 return retval;
1484}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001485
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001486#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001487/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001488 * Call vimL function "func" and return the result as a number.
1489 * Returns -1 when calling the function fails.
1490 * Uses argv[argc] for the function arguments.
1491 */
1492 long
1493call_func_retnr(func, argc, argv, safe)
1494 char_u *func;
1495 int argc;
1496 char_u **argv;
1497 int safe; /* use the sandbox */
1498{
1499 typval_T rettv;
1500 long retval;
1501
1502 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1503 return -1;
1504
1505 retval = get_tv_number_chk(&rettv, NULL);
1506 clear_tv(&rettv);
1507 return retval;
1508}
1509#endif
1510
1511/*
1512 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001513 * Uses argv[argc] for the function arguments.
1514 */
1515 void *
1516call_func_retlist(func, argc, argv, safe)
1517 char_u *func;
1518 int argc;
1519 char_u **argv;
1520 int safe; /* use the sandbox */
1521{
1522 typval_T rettv;
1523
1524 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1525 return NULL;
1526
1527 if (rettv.v_type != VAR_LIST)
1528 {
1529 clear_tv(&rettv);
1530 return NULL;
1531 }
1532
1533 return rettv.vval.v_list;
1534}
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
1537
1538/*
1539 * Save the current function call pointer, and set it to NULL.
1540 * Used when executing autocommands and for ":source".
1541 */
1542 void *
1543save_funccal()
1544{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001545 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 current_funccal = NULL;
1548 return (void *)fc;
1549}
1550
1551 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001552restore_funccal(vfc)
1553 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001555 funccall_T *fc = (funccall_T *)vfc;
1556
1557 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558}
1559
Bram Moolenaar05159a02005-02-26 23:04:13 +00001560#if defined(FEAT_PROFILE) || defined(PROTO)
1561/*
1562 * Prepare profiling for entering a child or something else that is not
1563 * counted for the script/function itself.
1564 * Should always be called in pair with prof_child_exit().
1565 */
1566 void
1567prof_child_enter(tm)
1568 proftime_T *tm; /* place to store waittime */
1569{
1570 funccall_T *fc = current_funccal;
1571
1572 if (fc != NULL && fc->func->uf_profiling)
1573 profile_start(&fc->prof_child);
1574 script_prof_save(tm);
1575}
1576
1577/*
1578 * Take care of time spent in a child.
1579 * Should always be called after prof_child_enter().
1580 */
1581 void
1582prof_child_exit(tm)
1583 proftime_T *tm; /* where waittime was stored */
1584{
1585 funccall_T *fc = current_funccal;
1586
1587 if (fc != NULL && fc->func->uf_profiling)
1588 {
1589 profile_end(&fc->prof_child);
1590 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1591 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1592 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1593 }
1594 script_prof_restore(tm);
1595}
1596#endif
1597
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599#ifdef FEAT_FOLDING
1600/*
1601 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1602 * it in "*cp". Doesn't give error messages.
1603 */
1604 int
1605eval_foldexpr(arg, cp)
1606 char_u *arg;
1607 int *cp;
1608{
Bram Moolenaar33570922005-01-25 22:26:29 +00001609 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 int retval;
1611 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001612 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1613 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001616 if (use_sandbox)
1617 ++sandbox;
1618 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 retval = 0;
1622 else
1623 {
1624 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001625 if (tv.v_type == VAR_NUMBER)
1626 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001627 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 retval = 0;
1629 else
1630 {
1631 /* If the result is a string, check if there is a non-digit before
1632 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (!VIM_ISDIGIT(*s) && *s != '-')
1635 *cp = *s++;
1636 retval = atol((char *)s);
1637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 }
1640 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001641 if (use_sandbox)
1642 --sandbox;
1643 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645 return retval;
1646}
1647#endif
1648
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650 * ":let" list all variable values
1651 * ":let var1 var2" list variable values
1652 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 * ":let var += expr" assignment command.
1654 * ":let var -= expr" assignment command.
1655 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
1658 void
1659ex_let(eap)
1660 exarg_T *eap;
1661{
1662 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001663 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001664 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666 int var_count = 0;
1667 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001668 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001669 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaardb552d602006-03-23 22:59:57 +00001671 argend = skip_var_list(arg, &var_count, &semicolon);
1672 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001674 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1675 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001676 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001679 /*
1680 * ":let" without "=": list variables
1681 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 if (*arg == '[')
1683 EMSG(_(e_invarg));
1684 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001685 /* ":let var1 var2" */
1686 arg = list_arg_vars(eap, arg);
1687 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001689 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001690 list_glob_vars();
1691 list_buf_vars();
1692 list_win_vars();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001693#ifdef FEAT_WINDOWS
1694 list_tab_vars();
1695#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001696 list_script_vars();
1697 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001698 list_vim_vars();
1699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 eap->nextcmd = check_nextcmd(arg);
1701 }
1702 else
1703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 op[0] = '=';
1705 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001706 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001707 {
1708 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1709 op[0] = expr[-1]; /* +=, -= or .= */
1710 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (eap->skip)
1714 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (eap->skip)
1717 {
1718 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 --emsg_skip;
1721 }
1722 else if (i != FAIL)
1723 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
1728 }
1729}
1730
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731/*
1732 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1733 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001734 * When "nextchars" is not NULL it points to a string with characters that
1735 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1736 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 * Returns OK or FAIL;
1738 */
1739 static int
1740ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1741 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001742 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 int copy; /* copy values from "tv", don't move */
1744 int semicolon; /* from skip_var_list() */
1745 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747{
1748 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001749 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001751 listitem_T *item;
1752 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753
1754 if (*arg != '[')
1755 {
1756 /*
1757 * ":let var = expr" or ":for var in list"
1758 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001759 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 return FAIL;
1761 return OK;
1762 }
1763
1764 /*
1765 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1766 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001767 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 {
1769 EMSG(_(e_listreq));
1770 return FAIL;
1771 }
1772
1773 i = list_len(l);
1774 if (semicolon == 0 && var_count < i)
1775 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001776 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 return FAIL;
1778 }
1779 if (var_count - semicolon > i)
1780 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001781 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 return FAIL;
1783 }
1784
1785 item = l->lv_first;
1786 while (*arg != ']')
1787 {
1788 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 item = item->li_next;
1791 if (arg == NULL)
1792 return FAIL;
1793
1794 arg = skipwhite(arg);
1795 if (*arg == ';')
1796 {
1797 /* Put the rest of the list (may be empty) in the var after ';'.
1798 * Create a new list for this. */
1799 l = list_alloc();
1800 if (l == NULL)
1801 return FAIL;
1802 while (item != NULL)
1803 {
1804 list_append_tv(l, &item->li_tv);
1805 item = item->li_next;
1806 }
1807
1808 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001809 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 ltv.vval.v_list = l;
1811 l->lv_refcount = 1;
1812
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001813 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1814 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 clear_tv(&ltv);
1816 if (arg == NULL)
1817 return FAIL;
1818 break;
1819 }
1820 else if (*arg != ',' && *arg != ']')
1821 {
1822 EMSG2(_(e_intern2), "ex_let_vars()");
1823 return FAIL;
1824 }
1825 }
1826
1827 return OK;
1828}
1829
1830/*
1831 * Skip over assignable variable "var" or list of variables "[var, var]".
1832 * Used for ":let varvar = expr" and ":for varvar in expr".
1833 * For "[var, var]" increment "*var_count" for each variable.
1834 * for "[var, var; var]" set "semicolon".
1835 * Return NULL for an error.
1836 */
1837 static char_u *
1838skip_var_list(arg, var_count, semicolon)
1839 char_u *arg;
1840 int *var_count;
1841 int *semicolon;
1842{
1843 char_u *p, *s;
1844
1845 if (*arg == '[')
1846 {
1847 /* "[var, var]": find the matching ']'. */
1848 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001849 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 {
1851 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1852 s = skip_var_one(p);
1853 if (s == p)
1854 {
1855 EMSG2(_(e_invarg2), p);
1856 return NULL;
1857 }
1858 ++*var_count;
1859
1860 p = skipwhite(s);
1861 if (*p == ']')
1862 break;
1863 else if (*p == ';')
1864 {
1865 if (*semicolon == 1)
1866 {
1867 EMSG(_("Double ; in list of variables"));
1868 return NULL;
1869 }
1870 *semicolon = 1;
1871 }
1872 else if (*p != ',')
1873 {
1874 EMSG2(_(e_invarg2), p);
1875 return NULL;
1876 }
1877 }
1878 return p + 1;
1879 }
1880 else
1881 return skip_var_one(arg);
1882}
1883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001885 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1886 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001887 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 static char_u *
1889skip_var_one(arg)
1890 char_u *arg;
1891{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001892 if (*arg == '@' && arg[1] != NUL)
1893 return arg + 2;
1894 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1895 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896}
1897
Bram Moolenaara7043832005-01-21 11:56:39 +00001898/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 * List variables for hashtab "ht" with prefix "prefix".
1900 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001901 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001903list_hashtable_vars(ht, prefix, empty)
1904 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001905 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001906 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001907{
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 hashitem_T *hi;
1909 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001910 int todo;
1911
1912 todo = ht->ht_used;
1913 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1914 {
1915 if (!HASHITEM_EMPTY(hi))
1916 {
1917 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 di = HI2DI(hi);
1919 if (empty || di->di_tv.v_type != VAR_STRING
1920 || di->di_tv.vval.v_string != NULL)
1921 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 }
1923 }
1924}
1925
1926/*
1927 * List global variables.
1928 */
1929 static void
1930list_glob_vars()
1931{
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001933}
1934
1935/*
1936 * List buffer variables.
1937 */
1938 static void
1939list_buf_vars()
1940{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001941 char_u numbuf[NUMBUFLEN];
1942
Bram Moolenaar33570922005-01-25 22:26:29 +00001943 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944
1945 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1946 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001947}
1948
1949/*
1950 * List window variables.
1951 */
1952 static void
1953list_win_vars()
1954{
Bram Moolenaar33570922005-01-25 22:26:29 +00001955 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001956}
1957
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001958#ifdef FEAT_WINDOWS
1959/*
1960 * List tab page variables.
1961 */
1962 static void
1963list_tab_vars()
1964{
1965 list_hashtable_vars(&curtab->tp_vars.dv_hashtab, (char_u *)"t:", TRUE);
1966}
1967#endif
1968
Bram Moolenaara7043832005-01-21 11:56:39 +00001969/*
1970 * List Vim variables.
1971 */
1972 static void
1973list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974{
Bram Moolenaar33570922005-01-25 22:26:29 +00001975 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976}
1977
1978/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001979 * List script-local variables, if there is a script.
1980 */
1981 static void
1982list_script_vars()
1983{
1984 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1985 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1986}
1987
1988/*
1989 * List function variables, if there is a function.
1990 */
1991 static void
1992list_func_vars()
1993{
1994 if (current_funccal != NULL)
1995 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
1996 (char_u *)"l:", FALSE);
1997}
1998
1999/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000 * List variables in "arg".
2001 */
2002 static char_u *
2003list_arg_vars(eap, arg)
2004 exarg_T *eap;
2005 char_u *arg;
2006{
2007 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002008 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 char_u *name_start;
2011 char_u *arg_subsc;
2012 char_u *tofree;
2013 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014
2015 while (!ends_excmd(*arg) && !got_int)
2016 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002017 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002019 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002020 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2021 {
2022 emsg_severe = TRUE;
2023 EMSG(_(e_trailing));
2024 break;
2025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002029 /* get_name_len() takes care of expanding curly braces */
2030 name_start = name = arg;
2031 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2032 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 /* This is mainly to keep test 49 working: when expanding
2035 * curly braces fails overrule the exception error message. */
2036 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002038 emsg_severe = TRUE;
2039 EMSG2(_(e_invarg2), arg);
2040 break;
2041 }
2042 error = TRUE;
2043 }
2044 else
2045 {
2046 if (tofree != NULL)
2047 name = tofree;
2048 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 else
2051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052 /* handle d.key, l[idx], f(expr) */
2053 arg_subsc = arg;
2054 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002055 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002057 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002058 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002059 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002060 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 case 'g': list_glob_vars(); break;
2063 case 'b': list_buf_vars(); break;
2064 case 'w': list_win_vars(); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002065#ifdef FEAT_WINDOWS
2066 case 't': list_tab_vars(); break;
2067#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002069 case 's': list_script_vars(); break;
2070 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071 default:
2072 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 }
2075 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 {
2077 char_u numbuf[NUMBUFLEN];
2078 char_u *tf;
2079 int c;
2080 char_u *s;
2081
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002082 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002083 c = *arg;
2084 *arg = NUL;
2085 list_one_var_a((char_u *)"",
2086 arg == arg_subsc ? name : name_start,
2087 tv.v_type, s == NULL ? (char_u *)"" : s);
2088 *arg = c;
2089 vim_free(tf);
2090 }
2091 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 }
2094 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002095
2096 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098
2099 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 }
2101
2102 return arg;
2103}
2104
2105/*
2106 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2107 * Returns a pointer to the char just after the var name.
2108 * Returns NULL if there is an error.
2109 */
2110 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002112 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002113 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002114 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002116 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117{
2118 int c1;
2119 char_u *name;
2120 char_u *p;
2121 char_u *arg_end = NULL;
2122 int len;
2123 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002124 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125
2126 /*
2127 * ":let $VAR = expr": Set environment variable.
2128 */
2129 if (*arg == '$')
2130 {
2131 /* Find the end of the name. */
2132 ++arg;
2133 name = arg;
2134 len = get_env_len(&arg);
2135 if (len == 0)
2136 EMSG2(_(e_invarg2), name - 1);
2137 else
2138 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002139 if (op != NULL && (*op == '+' || *op == '-'))
2140 EMSG2(_(e_letwrong), op);
2141 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002142 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 EMSG(_(e_letunexp));
2144 else
2145 {
2146 c1 = name[len];
2147 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002148 p = get_tv_string_chk(tv);
2149 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002150 {
2151 int mustfree = FALSE;
2152 char_u *s = vim_getenv(name, &mustfree);
2153
2154 if (s != NULL)
2155 {
2156 p = tofree = concat_str(s, p);
2157 if (mustfree)
2158 vim_free(s);
2159 }
2160 }
2161 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002162 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002163 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002164 if (STRICMP(name, "HOME") == 0)
2165 init_homedir();
2166 else if (didset_vim && STRICMP(name, "VIM") == 0)
2167 didset_vim = FALSE;
2168 else if (didset_vimruntime
2169 && STRICMP(name, "VIMRUNTIME") == 0)
2170 didset_vimruntime = FALSE;
2171 arg_end = arg;
2172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 }
2176 }
2177 }
2178
2179 /*
2180 * ":let &option = expr": Set option value.
2181 * ":let &l:option = expr": Set local option value.
2182 * ":let &g:option = expr": Set global option value.
2183 */
2184 else if (*arg == '&')
2185 {
2186 /* Find the end of the name. */
2187 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002188 if (p == NULL || (endchars != NULL
2189 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 EMSG(_(e_letunexp));
2191 else
2192 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002193 long n;
2194 int opt_type;
2195 long numval;
2196 char_u *stringval = NULL;
2197 char_u *s;
2198
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 c1 = *p;
2200 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002201
2202 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002203 s = get_tv_string_chk(tv); /* != NULL if number or string */
2204 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002205 {
2206 opt_type = get_option_value(arg, &numval,
2207 &stringval, opt_flags);
2208 if ((opt_type == 1 && *op == '.')
2209 || (opt_type == 0 && *op != '.'))
2210 EMSG2(_(e_letwrong), op);
2211 else
2212 {
2213 if (opt_type == 1) /* number */
2214 {
2215 if (*op == '+')
2216 n = numval + n;
2217 else
2218 n = numval - n;
2219 }
2220 else if (opt_type == 0 && stringval != NULL) /* string */
2221 {
2222 s = concat_str(stringval, s);
2223 vim_free(stringval);
2224 stringval = s;
2225 }
2226 }
2227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002228 if (s != NULL)
2229 {
2230 set_option_value(arg, n, s, opt_flags);
2231 arg_end = p;
2232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002234 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 }
2236 }
2237
2238 /*
2239 * ":let @r = expr": Set register contents.
2240 */
2241 else if (*arg == '@')
2242 {
2243 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 if (op != NULL && (*op == '+' || *op == '-'))
2245 EMSG2(_(e_letwrong), op);
2246 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002247 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 EMSG(_(e_letunexp));
2249 else
2250 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251 char_u *tofree = NULL;
2252 char_u *s;
2253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002254 p = get_tv_string_chk(tv);
2255 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002256 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002257 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002258 if (s != NULL)
2259 {
2260 p = tofree = concat_str(s, p);
2261 vim_free(s);
2262 }
2263 }
2264 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002265 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 arg_end = arg + 1;
2268 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002269 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 }
2271 }
2272
2273 /*
2274 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002275 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002277 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002279 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002281 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2285 EMSG(_(e_letunexp));
2286 else
2287 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 arg_end = p;
2290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002292 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 }
2294
2295 else
2296 EMSG2(_(e_invarg2), arg);
2297
2298 return arg_end;
2299}
2300
2301/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002302 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2303 */
2304 static int
2305check_changedtick(arg)
2306 char_u *arg;
2307{
2308 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2309 {
2310 EMSG2(_(e_readonlyvar), arg);
2311 return TRUE;
2312 }
2313 return FALSE;
2314}
2315
2316/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 * Get an lval: variable, Dict item or List item that can be assigned a value
2318 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2319 * "name.key", "name.key[expr]" etc.
2320 * Indexing only works if "name" is an existing List or Dictionary.
2321 * "name" points to the start of the name.
2322 * If "rettv" is not NULL it points to the value to be assigned.
2323 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2324 * wrong; must end in space or cmd separator.
2325 *
2326 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002327 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002328 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 */
2330 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002331get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002333 typval_T *rettv;
2334 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 int unlet;
2336 int skip;
2337 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002338 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 char_u *expr_start, *expr_end;
2342 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002343 dictitem_T *v;
2344 typval_T var1;
2345 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002346 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002347 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002350 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002353 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354
2355 if (skip)
2356 {
2357 /* When skipping just find the end of the name. */
2358 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002359 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002360 }
2361
2362 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002363 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 if (expr_start != NULL)
2365 {
2366 /* Don't expand the name when we already know there is an error. */
2367 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2368 && *p != '[' && *p != '.')
2369 {
2370 EMSG(_(e_trailing));
2371 return NULL;
2372 }
2373
2374 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2375 if (lp->ll_exp_name == NULL)
2376 {
2377 /* Report an invalid expression in braces, unless the
2378 * expression evaluation has been cancelled due to an
2379 * aborting error, an interrupt, or an exception. */
2380 if (!aborting() && !quiet)
2381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002382 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 EMSG2(_(e_invarg2), name);
2384 return NULL;
2385 }
2386 }
2387 lp->ll_name = lp->ll_exp_name;
2388 }
2389 else
2390 lp->ll_name = name;
2391
2392 /* Without [idx] or .key we are done. */
2393 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2394 return p;
2395
2396 cc = *p;
2397 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002398 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 if (v == NULL && !quiet)
2400 EMSG2(_(e_undefvar), lp->ll_name);
2401 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 if (v == NULL)
2403 return NULL;
2404
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 /*
2406 * Loop until no more [idx] or .key is following.
2407 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002408 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2412 && !(lp->ll_tv->v_type == VAR_DICT
2413 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (!quiet)
2416 EMSG(_("E689: Can only index a List or Dictionary"));
2417 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002419 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 if (!quiet)
2422 EMSG(_("E708: [:] must come last"));
2423 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002425
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 len = -1;
2427 if (*p == '.')
2428 {
2429 key = p + 1;
2430 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2431 ;
2432 if (len == 0)
2433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 if (!quiet)
2435 EMSG(_(e_emptykey));
2436 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002437 }
2438 p = key + len;
2439 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002440 else
2441 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002443 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 if (*p == ':')
2445 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002446 else
2447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 empty1 = FALSE;
2449 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 if (get_tv_string_chk(&var1) == NULL)
2452 {
2453 /* not a number or string */
2454 clear_tv(&var1);
2455 return NULL;
2456 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 }
2458
2459 /* Optionally get the second index [ :expr]. */
2460 if (*p == ':')
2461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002466 if (!empty1)
2467 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002469 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (rettv != NULL && (rettv->v_type != VAR_LIST
2471 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (!quiet)
2474 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 if (!empty1)
2476 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 }
2479 p = skipwhite(p + 1);
2480 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 else
2483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2486 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 if (!empty1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002491 if (get_tv_string_chk(&var2) == NULL)
2492 {
2493 /* not a number or string */
2494 if (!empty1)
2495 clear_tv(&var1);
2496 clear_tv(&var2);
2497 return NULL;
2498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002501 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002502 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002504
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002506 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 if (!quiet)
2508 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 if (!empty1)
2510 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 }
2515
2516 /* Skip to past ']'. */
2517 ++p;
2518 }
2519
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 {
2522 if (len == -1)
2523 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002525 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 if (*key == NUL)
2527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (!quiet)
2529 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 }
2533 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 lp->ll_list = NULL;
2535 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002536 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002539 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 if (len == -1)
2545 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 }
2548 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002552 if (len == -1)
2553 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 p = NULL;
2556 break;
2557 }
2558 if (len == -1)
2559 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 }
2562 else
2563 {
2564 /*
2565 * Get the number and item for the only or first index of the List.
2566 */
2567 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 else
2570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002571 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 clear_tv(&var1);
2573 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002574 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 lp->ll_list = lp->ll_tv->vval.v_list;
2576 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2577 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 if (!quiet)
2580 EMSGN(_(e_listidx), lp->ll_n1);
2581 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 }
2585
2586 /*
2587 * May need to find the item or absolute index for the second
2588 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 * When no index given: "lp->ll_empty2" is TRUE.
2590 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002594 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 if (ni == NULL)
2600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSGN(_(e_listidx), lp->ll_n2);
2603 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 }
2607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2609 if (lp->ll_n1 < 0)
2610 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2611 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (!quiet)
2614 EMSGN(_(e_listidx), lp->ll_n2);
2615 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617 }
2618
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
2622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 return p;
2624}
2625
2626/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 */
2629 static void
2630clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632{
2633 vim_free(lp->ll_exp_name);
2634 vim_free(lp->ll_newkey);
2635}
2636
2637/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002638 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 */
2642 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002643set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002644 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002648 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649{
2650 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002651 listitem_T *ri;
2652 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653
2654 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 cc = *endp;
2659 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002660 if (op != NULL && *op != '=')
2661 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002662 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002663
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002664 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002665 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2666 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667 {
2668 if (tv_op(&tv, rettv, op) == OK)
2669 set_var(lp->ll_name, &tv, FALSE);
2670 clear_tv(&tv);
2671 }
2672 }
2673 else
2674 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002678 else if (tv_check_lock(lp->ll_newkey == NULL
2679 ? lp->ll_tv->v_lock
2680 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2681 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 else if (lp->ll_range)
2683 {
2684 /*
2685 * Assign the List values to the list items.
2686 */
2687 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 if (op != NULL && *op != '=')
2690 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2691 else
2692 {
2693 clear_tv(&lp->ll_li->li_tv);
2694 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2695 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 ri = ri->li_next;
2697 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2698 break;
2699 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002702 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 ri = NULL;
2705 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 lp->ll_li = lp->ll_li->li_next;
2709 ++lp->ll_n1;
2710 }
2711 if (ri != NULL)
2712 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 else if (lp->ll_empty2
2714 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 : lp->ll_n1 != lp->ll_n2)
2716 EMSG(_("E711: List value has not enough items"));
2717 }
2718 else
2719 {
2720 /*
2721 * Assign to a List or Dictionary item.
2722 */
2723 if (lp->ll_newkey != NULL)
2724 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 if (op != NULL && *op != '=')
2726 {
2727 EMSG2(_(e_letwrong), op);
2728 return;
2729 }
2730
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002732 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (di == NULL)
2734 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002735 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2736 {
2737 vim_free(di);
2738 return;
2739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002742 else if (op != NULL && *op != '=')
2743 {
2744 tv_op(lp->ll_tv, rettv, op);
2745 return;
2746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 /*
2751 * Assign the value to the variable or list item.
2752 */
2753 if (copy)
2754 copy_tv(rettv, lp->ll_tv);
2755 else
2756 {
2757 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002758 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002760 }
2761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002762}
2763
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002764/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002765 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2766 * Returns OK or FAIL.
2767 */
2768 static int
2769tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002770 typval_T *tv1;
2771 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 char_u *op;
2773{
2774 long n;
2775 char_u numbuf[NUMBUFLEN];
2776 char_u *s;
2777
2778 /* Can't do anything with a Funcref or a Dict on the right. */
2779 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2780 {
2781 switch (tv1->v_type)
2782 {
2783 case VAR_DICT:
2784 case VAR_FUNC:
2785 break;
2786
2787 case VAR_LIST:
2788 if (*op != '+' || tv2->v_type != VAR_LIST)
2789 break;
2790 /* List += List */
2791 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2792 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2793 return OK;
2794
2795 case VAR_NUMBER:
2796 case VAR_STRING:
2797 if (tv2->v_type == VAR_LIST)
2798 break;
2799 if (*op == '+' || *op == '-')
2800 {
2801 /* nr += nr or nr -= nr*/
2802 n = get_tv_number(tv1);
2803 if (*op == '+')
2804 n += get_tv_number(tv2);
2805 else
2806 n -= get_tv_number(tv2);
2807 clear_tv(tv1);
2808 tv1->v_type = VAR_NUMBER;
2809 tv1->vval.v_number = n;
2810 }
2811 else
2812 {
2813 /* str .= str */
2814 s = get_tv_string(tv1);
2815 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2816 clear_tv(tv1);
2817 tv1->v_type = VAR_STRING;
2818 tv1->vval.v_string = s;
2819 }
2820 return OK;
2821 }
2822 }
2823
2824 EMSG2(_(e_letwrong), op);
2825 return FAIL;
2826}
2827
2828/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002829 * Add a watcher to a list.
2830 */
2831 static void
2832list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 list_T *l;
2834 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002835{
2836 lw->lw_next = l->lv_watch;
2837 l->lv_watch = lw;
2838}
2839
2840/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002841 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002842 * No warning when it isn't found...
2843 */
2844 static void
2845list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002846 list_T *l;
2847 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848{
Bram Moolenaar33570922005-01-25 22:26:29 +00002849 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002850
2851 lwp = &l->lv_watch;
2852 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2853 {
2854 if (lw == lwrem)
2855 {
2856 *lwp = lw->lw_next;
2857 break;
2858 }
2859 lwp = &lw->lw_next;
2860 }
2861}
2862
2863/*
2864 * Just before removing an item from a list: advance watchers to the next
2865 * item.
2866 */
2867 static void
2868list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002869 list_T *l;
2870 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871{
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002873
2874 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2875 if (lw->lw_item == item)
2876 lw->lw_item = item->li_next;
2877}
2878
2879/*
2880 * Evaluate the expression used in a ":for var in expr" command.
2881 * "arg" points to "var".
2882 * Set "*errp" to TRUE for an error, FALSE otherwise;
2883 * Return a pointer that holds the info. Null when there is an error.
2884 */
2885 void *
2886eval_for_line(arg, errp, nextcmdp, skip)
2887 char_u *arg;
2888 int *errp;
2889 char_u **nextcmdp;
2890 int skip;
2891{
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002893 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T tv;
2895 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896
2897 *errp = TRUE; /* default: there is an error */
2898
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900 if (fi == NULL)
2901 return NULL;
2902
2903 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2904 if (expr == NULL)
2905 return fi;
2906
2907 expr = skipwhite(expr);
2908 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2909 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002910 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 return fi;
2912 }
2913
2914 if (skip)
2915 ++emsg_skip;
2916 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2917 {
2918 *errp = FALSE;
2919 if (!skip)
2920 {
2921 l = tv.vval.v_list;
2922 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002923 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002925 clear_tv(&tv);
2926 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002927 else
2928 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002929 /* No need to increment the refcount, it's already set for the
2930 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002931 fi->fi_list = l;
2932 list_add_watch(l, &fi->fi_lw);
2933 fi->fi_lw.lw_item = l->lv_first;
2934 }
2935 }
2936 }
2937 if (skip)
2938 --emsg_skip;
2939
2940 return fi;
2941}
2942
2943/*
2944 * Use the first item in a ":for" list. Advance to the next.
2945 * Assign the values to the variable (list). "arg" points to the first one.
2946 * Return TRUE when a valid item was found, FALSE when at end of list or
2947 * something wrong.
2948 */
2949 int
2950next_for_item(fi_void, arg)
2951 void *fi_void;
2952 char_u *arg;
2953{
Bram Moolenaar33570922005-01-25 22:26:29 +00002954 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002955 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002956 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957
2958 item = fi->fi_lw.lw_item;
2959 if (item == NULL)
2960 result = FALSE;
2961 else
2962 {
2963 fi->fi_lw.lw_item = item->li_next;
2964 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2965 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2966 }
2967 return result;
2968}
2969
2970/*
2971 * Free the structure used to store info used by ":for".
2972 */
2973 void
2974free_for_info(fi_void)
2975 void *fi_void;
2976{
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002978
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002979 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002980 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002982 list_unref(fi->fi_list);
2983 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984 vim_free(fi);
2985}
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2988
2989 void
2990set_context_for_expression(xp, arg, cmdidx)
2991 expand_T *xp;
2992 char_u *arg;
2993 cmdidx_T cmdidx;
2994{
2995 int got_eq = FALSE;
2996 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002997 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002999 if (cmdidx == CMD_let)
3000 {
3001 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003002 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003 {
3004 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003005 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 {
3007 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003008 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003009 if (vim_iswhite(*p))
3010 break;
3011 }
3012 return;
3013 }
3014 }
3015 else
3016 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3017 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 while ((xp->xp_pattern = vim_strpbrk(arg,
3019 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3020 {
3021 c = *xp->xp_pattern;
3022 if (c == '&')
3023 {
3024 c = xp->xp_pattern[1];
3025 if (c == '&')
3026 {
3027 ++xp->xp_pattern;
3028 xp->xp_context = cmdidx != CMD_let || got_eq
3029 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3030 }
3031 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003034 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3035 xp->xp_pattern += 2;
3036
3037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039 else if (c == '$')
3040 {
3041 /* environment variable */
3042 xp->xp_context = EXPAND_ENV_VARS;
3043 }
3044 else if (c == '=')
3045 {
3046 got_eq = TRUE;
3047 xp->xp_context = EXPAND_EXPRESSION;
3048 }
3049 else if (c == '<'
3050 && xp->xp_context == EXPAND_FUNCTIONS
3051 && vim_strchr(xp->xp_pattern, '(') == NULL)
3052 {
3053 /* Function name can start with "<SNR>" */
3054 break;
3055 }
3056 else if (cmdidx != CMD_let || got_eq)
3057 {
3058 if (c == '"') /* string */
3059 {
3060 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3061 if (c == '\\' && xp->xp_pattern[1] != NUL)
3062 ++xp->xp_pattern;
3063 xp->xp_context = EXPAND_NOTHING;
3064 }
3065 else if (c == '\'') /* literal string */
3066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003067 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3069 /* skip */ ;
3070 xp->xp_context = EXPAND_NOTHING;
3071 }
3072 else if (c == '|')
3073 {
3074 if (xp->xp_pattern[1] == '|')
3075 {
3076 ++xp->xp_pattern;
3077 xp->xp_context = EXPAND_EXPRESSION;
3078 }
3079 else
3080 xp->xp_context = EXPAND_COMMANDS;
3081 }
3082 else
3083 xp->xp_context = EXPAND_EXPRESSION;
3084 }
3085 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003086 /* Doesn't look like something valid, expand as an expression
3087 * anyway. */
3088 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 arg = xp->xp_pattern;
3090 if (*arg != NUL)
3091 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3092 /* skip */ ;
3093 }
3094 xp->xp_pattern = arg;
3095}
3096
3097#endif /* FEAT_CMDL_COMPL */
3098
3099/*
3100 * ":1,25call func(arg1, arg2)" function call.
3101 */
3102 void
3103ex_call(eap)
3104 exarg_T *eap;
3105{
3106 char_u *arg = eap->arg;
3107 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 linenr_T lnum;
3113 int doesrange;
3114 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003117 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3118 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 if (tofree == NULL)
3120 return;
3121
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003122 /* Increase refcount on dictionary, it could get deleted when evaluating
3123 * the arguments. */
3124 if (fudi.fd_dict != NULL)
3125 ++fudi.fd_dict->dv_refcount;
3126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3128 len = STRLEN(tofree);
3129 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
Bram Moolenaar532c7802005-01-27 14:44:31 +00003131 /* Skip white space to allow ":call func ()". Not good, but required for
3132 * backward compatibility. */
3133 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003134 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 if (*startarg != '(')
3137 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003138 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 goto end;
3140 }
3141
3142 /*
3143 * When skipping, evaluate the function once, to find the end of the
3144 * arguments.
3145 * When the function takes a range, this is discovered after the first
3146 * call, and the loop is broken.
3147 */
3148 if (eap->skip)
3149 {
3150 ++emsg_skip;
3151 lnum = eap->line2; /* do it once, also with an invalid range */
3152 }
3153 else
3154 lnum = eap->line1;
3155 for ( ; lnum <= eap->line2; ++lnum)
3156 {
3157 if (!eap->skip && eap->addr_count > 0)
3158 {
3159 curwin->w_cursor.lnum = lnum;
3160 curwin->w_cursor.col = 0;
3161 }
3162 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003164 eap->line1, eap->line2, &doesrange,
3165 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 failed = TRUE;
3168 break;
3169 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003170 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (doesrange || eap->skip)
3172 break;
3173 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003174 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003175 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003176 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (aborting())
3178 break;
3179 }
3180 if (eap->skip)
3181 --emsg_skip;
3182
3183 if (!failed)
3184 {
3185 /* Check for trailing illegal characters and a following command. */
3186 if (!ends_excmd(*arg))
3187 {
3188 emsg_severe = TRUE;
3189 EMSG(_(e_trailing));
3190 }
3191 else
3192 eap->nextcmd = check_nextcmd(arg);
3193 }
3194
3195end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003196 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003197 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198}
3199
3200/*
3201 * ":unlet[!] var1 ... " command.
3202 */
3203 void
3204ex_unlet(eap)
3205 exarg_T *eap;
3206{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003207 ex_unletlock(eap, eap->arg, 0);
3208}
3209
3210/*
3211 * ":lockvar" and ":unlockvar" commands
3212 */
3213 void
3214ex_lockvar(eap)
3215 exarg_T *eap;
3216{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 int deep = 2;
3219
3220 if (eap->forceit)
3221 deep = -1;
3222 else if (vim_isdigit(*arg))
3223 {
3224 deep = getdigits(&arg);
3225 arg = skipwhite(arg);
3226 }
3227
3228 ex_unletlock(eap, arg, deep);
3229}
3230
3231/*
3232 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3233 */
3234 static void
3235ex_unletlock(eap, argstart, deep)
3236 exarg_T *eap;
3237 char_u *argstart;
3238 int deep;
3239{
3240 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003243 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 do
3246 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003247 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003248 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3249 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 if (lv.ll_name == NULL)
3251 error = TRUE; /* error but continue parsing */
3252 if (name_end == NULL || (!vim_iswhite(*name_end)
3253 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 if (name_end != NULL)
3256 {
3257 emsg_severe = TRUE;
3258 EMSG(_(e_trailing));
3259 }
3260 if (!(eap->skip || error))
3261 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 break;
3263 }
3264
3265 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 {
3267 if (eap->cmdidx == CMD_unlet)
3268 {
3269 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3270 error = TRUE;
3271 }
3272 else
3273 {
3274 if (do_lock_var(&lv, name_end, deep,
3275 eap->cmdidx == CMD_lockvar) == FAIL)
3276 error = TRUE;
3277 }
3278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003280 if (!eap->skip)
3281 clear_lval(&lv);
3282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 arg = skipwhite(name_end);
3284 } while (!ends_excmd(*arg));
3285
3286 eap->nextcmd = check_nextcmd(arg);
3287}
3288
Bram Moolenaar8c711452005-01-14 21:53:12 +00003289 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003290do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003292 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003293 int forceit;
3294{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003295 int ret = OK;
3296 int cc;
3297
3298 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003300 cc = *name_end;
3301 *name_end = NUL;
3302
3303 /* Normal name or expanded name. */
3304 if (check_changedtick(lp->ll_name))
3305 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003306 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003307 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003308 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003310 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3311 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003312 else if (lp->ll_range)
3313 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003314 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003315
3316 /* Delete a range of List items. */
3317 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3318 {
3319 li = lp->ll_li->li_next;
3320 listitem_remove(lp->ll_list, lp->ll_li);
3321 lp->ll_li = li;
3322 ++lp->ll_n1;
3323 }
3324 }
3325 else
3326 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003327 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003328 /* unlet a List item. */
3329 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003330 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003331 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003332 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003333 }
3334
3335 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
3339 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003340 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 */
3342 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003343do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003345 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
Bram Moolenaar33570922005-01-25 22:26:29 +00003347 hashtab_T *ht;
3348 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003349 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 ht = find_var_ht(name, &varname);
3352 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 hi = hash_find(ht, varname);
3355 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003356 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003357 if (var_check_ro(HI2DI(hi)->di_flags, name))
3358 return FAIL;
3359 delete_var(ht, hi);
3360 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003363 if (forceit)
3364 return OK;
3365 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 return FAIL;
3367}
3368
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003369/*
3370 * Lock or unlock variable indicated by "lp".
3371 * "deep" is the levels to go (-1 for unlimited);
3372 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3373 */
3374 static int
3375do_lock_var(lp, name_end, deep, lock)
3376 lval_T *lp;
3377 char_u *name_end;
3378 int deep;
3379 int lock;
3380{
3381 int ret = OK;
3382 int cc;
3383 dictitem_T *di;
3384
3385 if (deep == 0) /* nothing to do */
3386 return OK;
3387
3388 if (lp->ll_tv == NULL)
3389 {
3390 cc = *name_end;
3391 *name_end = NUL;
3392
3393 /* Normal name or expanded name. */
3394 if (check_changedtick(lp->ll_name))
3395 ret = FAIL;
3396 else
3397 {
3398 di = find_var(lp->ll_name, NULL);
3399 if (di == NULL)
3400 ret = FAIL;
3401 else
3402 {
3403 if (lock)
3404 di->di_flags |= DI_FLAGS_LOCK;
3405 else
3406 di->di_flags &= ~DI_FLAGS_LOCK;
3407 item_lock(&di->di_tv, deep, lock);
3408 }
3409 }
3410 *name_end = cc;
3411 }
3412 else if (lp->ll_range)
3413 {
3414 listitem_T *li = lp->ll_li;
3415
3416 /* (un)lock a range of List items. */
3417 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3418 {
3419 item_lock(&li->li_tv, deep, lock);
3420 li = li->li_next;
3421 ++lp->ll_n1;
3422 }
3423 }
3424 else if (lp->ll_list != NULL)
3425 /* (un)lock a List item. */
3426 item_lock(&lp->ll_li->li_tv, deep, lock);
3427 else
3428 /* un(lock) a Dictionary item. */
3429 item_lock(&lp->ll_di->di_tv, deep, lock);
3430
3431 return ret;
3432}
3433
3434/*
3435 * Lock or unlock an item. "deep" is nr of levels to go.
3436 */
3437 static void
3438item_lock(tv, deep, lock)
3439 typval_T *tv;
3440 int deep;
3441 int lock;
3442{
3443 static int recurse = 0;
3444 list_T *l;
3445 listitem_T *li;
3446 dict_T *d;
3447 hashitem_T *hi;
3448 int todo;
3449
3450 if (recurse >= DICT_MAXNEST)
3451 {
3452 EMSG(_("E743: variable nested too deep for (un)lock"));
3453 return;
3454 }
3455 if (deep == 0)
3456 return;
3457 ++recurse;
3458
3459 /* lock/unlock the item itself */
3460 if (lock)
3461 tv->v_lock |= VAR_LOCKED;
3462 else
3463 tv->v_lock &= ~VAR_LOCKED;
3464
3465 switch (tv->v_type)
3466 {
3467 case VAR_LIST:
3468 if ((l = tv->vval.v_list) != NULL)
3469 {
3470 if (lock)
3471 l->lv_lock |= VAR_LOCKED;
3472 else
3473 l->lv_lock &= ~VAR_LOCKED;
3474 if (deep < 0 || deep > 1)
3475 /* recursive: lock/unlock the items the List contains */
3476 for (li = l->lv_first; li != NULL; li = li->li_next)
3477 item_lock(&li->li_tv, deep - 1, lock);
3478 }
3479 break;
3480 case VAR_DICT:
3481 if ((d = tv->vval.v_dict) != NULL)
3482 {
3483 if (lock)
3484 d->dv_lock |= VAR_LOCKED;
3485 else
3486 d->dv_lock &= ~VAR_LOCKED;
3487 if (deep < 0 || deep > 1)
3488 {
3489 /* recursive: lock/unlock the items the List contains */
3490 todo = d->dv_hashtab.ht_used;
3491 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3492 {
3493 if (!HASHITEM_EMPTY(hi))
3494 {
3495 --todo;
3496 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3497 }
3498 }
3499 }
3500 }
3501 }
3502 --recurse;
3503}
3504
Bram Moolenaara40058a2005-07-11 22:42:07 +00003505/*
3506 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3507 * it refers to a List or Dictionary that is locked.
3508 */
3509 static int
3510tv_islocked(tv)
3511 typval_T *tv;
3512{
3513 return (tv->v_lock & VAR_LOCKED)
3514 || (tv->v_type == VAR_LIST
3515 && tv->vval.v_list != NULL
3516 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3517 || (tv->v_type == VAR_DICT
3518 && tv->vval.v_dict != NULL
3519 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3520}
3521
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3523/*
3524 * Delete all "menutrans_" variables.
3525 */
3526 void
3527del_menutrans_vars()
3528{
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003530 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
Bram Moolenaar33570922005-01-25 22:26:29 +00003532 hash_lock(&globvarht);
3533 todo = globvarht.ht_used;
3534 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003535 {
3536 if (!HASHITEM_EMPTY(hi))
3537 {
3538 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3540 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003541 }
3542 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544}
3545#endif
3546
3547#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3548
3549/*
3550 * Local string buffer for the next two functions to store a variable name
3551 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3552 * get_user_var_name().
3553 */
3554
3555static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3556
3557static char_u *varnamebuf = NULL;
3558static int varnamebuflen = 0;
3559
3560/*
3561 * Function to concatenate a prefix and a variable name.
3562 */
3563 static char_u *
3564cat_prefix_varname(prefix, name)
3565 int prefix;
3566 char_u *name;
3567{
3568 int len;
3569
3570 len = (int)STRLEN(name) + 3;
3571 if (len > varnamebuflen)
3572 {
3573 vim_free(varnamebuf);
3574 len += 10; /* some additional space */
3575 varnamebuf = alloc(len);
3576 if (varnamebuf == NULL)
3577 {
3578 varnamebuflen = 0;
3579 return NULL;
3580 }
3581 varnamebuflen = len;
3582 }
3583 *varnamebuf = prefix;
3584 varnamebuf[1] = ':';
3585 STRCPY(varnamebuf + 2, name);
3586 return varnamebuf;
3587}
3588
3589/*
3590 * Function given to ExpandGeneric() to obtain the list of user defined
3591 * (global/buffer/window/built-in) variable names.
3592 */
3593/*ARGSUSED*/
3594 char_u *
3595get_user_var_name(xp, idx)
3596 expand_T *xp;
3597 int idx;
3598{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003599 static long_u gdone;
3600 static long_u bdone;
3601 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003602#ifdef FEAT_WINDOWS
3603 static long_u tdone;
3604#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003605 static int vidx;
3606 static hashitem_T *hi;
3607 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003610 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003611 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003612#ifdef FEAT_WINDOWS
3613 tdone = 0;
3614#endif
3615 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003616
3617 /* Global variables */
3618 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003620 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003622 else
3623 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003624 while (HASHITEM_EMPTY(hi))
3625 ++hi;
3626 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3627 return cat_prefix_varname('g', hi->hi_key);
3628 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003630
3631 /* b: variables */
3632 ht = &curbuf->b_vars.dv_hashtab;
3633 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003635 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003636 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003637 else
3638 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003639 while (HASHITEM_EMPTY(hi))
3640 ++hi;
3641 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003643 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003645 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 return (char_u *)"b:changedtick";
3647 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003648
3649 /* w: variables */
3650 ht = &curwin->w_vars.dv_hashtab;
3651 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003653 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003654 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003655 else
3656 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003657 while (HASHITEM_EMPTY(hi))
3658 ++hi;
3659 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003661
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003662#ifdef FEAT_WINDOWS
3663 /* t: variables */
3664 ht = &curtab->tp_vars.dv_hashtab;
3665 if (tdone < ht->ht_used)
3666 {
3667 if (tdone++ == 0)
3668 hi = ht->ht_array;
3669 else
3670 ++hi;
3671 while (HASHITEM_EMPTY(hi))
3672 ++hi;
3673 return cat_prefix_varname('t', hi->hi_key);
3674 }
3675#endif
3676
Bram Moolenaar33570922005-01-25 22:26:29 +00003677 /* v: variables */
3678 if (vidx < VV_LEN)
3679 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
3681 vim_free(varnamebuf);
3682 varnamebuf = NULL;
3683 varnamebuflen = 0;
3684 return NULL;
3685}
3686
3687#endif /* FEAT_CMDL_COMPL */
3688
3689/*
3690 * types for expressions.
3691 */
3692typedef enum
3693{
3694 TYPE_UNKNOWN = 0
3695 , TYPE_EQUAL /* == */
3696 , TYPE_NEQUAL /* != */
3697 , TYPE_GREATER /* > */
3698 , TYPE_GEQUAL /* >= */
3699 , TYPE_SMALLER /* < */
3700 , TYPE_SEQUAL /* <= */
3701 , TYPE_MATCH /* =~ */
3702 , TYPE_NOMATCH /* !~ */
3703} exptype_T;
3704
3705/*
3706 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003707 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3709 */
3710
3711/*
3712 * Handle zero level expression.
3713 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003714 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003715 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 * Return OK or FAIL.
3717 */
3718 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 char_u **nextcmd;
3723 int evaluate;
3724{
3725 int ret;
3726 char_u *p;
3727
3728 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (ret == FAIL || !ends_excmd(*p))
3731 {
3732 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003733 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 /*
3735 * Report the invalid expression unless the expression evaluation has
3736 * been cancelled due to an aborting error, an interrupt, or an
3737 * exception.
3738 */
3739 if (!aborting())
3740 EMSG2(_(e_invexpr2), arg);
3741 ret = FAIL;
3742 }
3743 if (nextcmd != NULL)
3744 *nextcmd = check_nextcmd(p);
3745
3746 return ret;
3747}
3748
3749/*
3750 * Handle top level expression:
3751 * expr1 ? expr0 : expr0
3752 *
3753 * "arg" must point to the first non-white of the expression.
3754 * "arg" is advanced to the next non-white after the recognized expression.
3755 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003756 * Note: "rettv.v_lock" is not set.
3757 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 * Return OK or FAIL.
3759 */
3760 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003761eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 int evaluate;
3765{
3766 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769 /*
3770 * Get the first variable.
3771 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 return FAIL;
3774
3775 if ((*arg)[0] == '?')
3776 {
3777 result = FALSE;
3778 if (evaluate)
3779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 int error = FALSE;
3781
3782 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003785 if (error)
3786 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788
3789 /*
3790 * Get the second variable.
3791 */
3792 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003793 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795
3796 /*
3797 * Check for the ":".
3798 */
3799 if ((*arg)[0] != ':')
3800 {
3801 EMSG(_("E109: Missing ':' after '?'"));
3802 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003803 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return FAIL;
3805 }
3806
3807 /*
3808 * Get the third variable.
3809 */
3810 *arg = skipwhite(*arg + 1);
3811 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3812 {
3813 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return FAIL;
3816 }
3817 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003818 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820
3821 return OK;
3822}
3823
3824/*
3825 * Handle first level expression:
3826 * expr2 || expr2 || expr2 logical OR
3827 *
3828 * "arg" must point to the first non-white of the expression.
3829 * "arg" is advanced to the next non-white after the recognized expression.
3830 *
3831 * Return OK or FAIL.
3832 */
3833 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 int evaluate;
3838{
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 long result;
3841 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003842 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
3844 /*
3845 * Get the first variable.
3846 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return FAIL;
3849
3850 /*
3851 * Repeat until there is no following "||".
3852 */
3853 first = TRUE;
3854 result = FALSE;
3855 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3856 {
3857 if (evaluate && first)
3858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003861 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 if (error)
3863 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 first = FALSE;
3865 }
3866
3867 /*
3868 * Get the second variable.
3869 */
3870 *arg = skipwhite(*arg + 2);
3871 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3872 return FAIL;
3873
3874 /*
3875 * Compute the result.
3876 */
3877 if (evaluate && !result)
3878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003879 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003882 if (error)
3883 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885 if (evaluate)
3886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 rettv->v_type = VAR_NUMBER;
3888 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890 }
3891
3892 return OK;
3893}
3894
3895/*
3896 * Handle second level expression:
3897 * expr3 && expr3 && expr3 logical AND
3898 *
3899 * "arg" must point to the first non-white of the expression.
3900 * "arg" is advanced to the next non-white after the recognized expression.
3901 *
3902 * Return OK or FAIL.
3903 */
3904 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 int evaluate;
3909{
Bram Moolenaar33570922005-01-25 22:26:29 +00003910 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 long result;
3912 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003913 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 /*
3916 * Get the first variable.
3917 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 return FAIL;
3920
3921 /*
3922 * Repeat until there is no following "&&".
3923 */
3924 first = TRUE;
3925 result = TRUE;
3926 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3927 {
3928 if (evaluate && first)
3929 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003930 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003932 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 if (error)
3934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 first = FALSE;
3936 }
3937
3938 /*
3939 * Get the second variable.
3940 */
3941 *arg = skipwhite(*arg + 2);
3942 if (eval4(arg, &var2, evaluate && result) == FAIL)
3943 return FAIL;
3944
3945 /*
3946 * Compute the result.
3947 */
3948 if (evaluate && result)
3949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003950 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003952 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003953 if (error)
3954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 if (evaluate)
3957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003958 rettv->v_type = VAR_NUMBER;
3959 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 }
3962
3963 return OK;
3964}
3965
3966/*
3967 * Handle third level expression:
3968 * var1 == var2
3969 * var1 =~ var2
3970 * var1 != var2
3971 * var1 !~ var2
3972 * var1 > var2
3973 * var1 >= var2
3974 * var1 < var2
3975 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003976 * var1 is var2
3977 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 *
3979 * "arg" must point to the first non-white of the expression.
3980 * "arg" is advanced to the next non-white after the recognized expression.
3981 *
3982 * Return OK or FAIL.
3983 */
3984 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003985eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int evaluate;
3989{
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 char_u *p;
3992 int i;
3993 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003994 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 int len = 2;
3996 long n1, n2;
3997 char_u *s1, *s2;
3998 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3999 regmatch_T regmatch;
4000 int ic;
4001 char_u *save_cpo;
4002
4003 /*
4004 * Get the first variable.
4005 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004006 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return FAIL;
4008
4009 p = *arg;
4010 switch (p[0])
4011 {
4012 case '=': if (p[1] == '=')
4013 type = TYPE_EQUAL;
4014 else if (p[1] == '~')
4015 type = TYPE_MATCH;
4016 break;
4017 case '!': if (p[1] == '=')
4018 type = TYPE_NEQUAL;
4019 else if (p[1] == '~')
4020 type = TYPE_NOMATCH;
4021 break;
4022 case '>': if (p[1] != '=')
4023 {
4024 type = TYPE_GREATER;
4025 len = 1;
4026 }
4027 else
4028 type = TYPE_GEQUAL;
4029 break;
4030 case '<': if (p[1] != '=')
4031 {
4032 type = TYPE_SMALLER;
4033 len = 1;
4034 }
4035 else
4036 type = TYPE_SEQUAL;
4037 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004038 case 'i': if (p[1] == 's')
4039 {
4040 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4041 len = 5;
4042 if (!vim_isIDc(p[len]))
4043 {
4044 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4045 type_is = TRUE;
4046 }
4047 }
4048 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050
4051 /*
4052 * If there is a comparitive operator, use it.
4053 */
4054 if (type != TYPE_UNKNOWN)
4055 {
4056 /* extra question mark appended: ignore case */
4057 if (p[len] == '?')
4058 {
4059 ic = TRUE;
4060 ++len;
4061 }
4062 /* extra '#' appended: match case */
4063 else if (p[len] == '#')
4064 {
4065 ic = FALSE;
4066 ++len;
4067 }
4068 /* nothing appened: use 'ignorecase' */
4069 else
4070 ic = p_ic;
4071
4072 /*
4073 * Get the second variable.
4074 */
4075 *arg = skipwhite(p + len);
4076 if (eval5(arg, &var2, evaluate) == FAIL)
4077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 return FAIL;
4080 }
4081
4082 if (evaluate)
4083 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004084 if (type_is && rettv->v_type != var2.v_type)
4085 {
4086 /* For "is" a different type always means FALSE, for "notis"
4087 * it means TRUE. */
4088 n1 = (type == TYPE_NEQUAL);
4089 }
4090 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4091 {
4092 if (type_is)
4093 {
4094 n1 = (rettv->v_type == var2.v_type
4095 && rettv->vval.v_list == var2.vval.v_list);
4096 if (type == TYPE_NEQUAL)
4097 n1 = !n1;
4098 }
4099 else if (rettv->v_type != var2.v_type
4100 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4101 {
4102 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004103 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004104 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004105 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004106 clear_tv(rettv);
4107 clear_tv(&var2);
4108 return FAIL;
4109 }
4110 else
4111 {
4112 /* Compare two Lists for being equal or unequal. */
4113 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4114 if (type == TYPE_NEQUAL)
4115 n1 = !n1;
4116 }
4117 }
4118
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004119 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4120 {
4121 if (type_is)
4122 {
4123 n1 = (rettv->v_type == var2.v_type
4124 && rettv->vval.v_dict == var2.vval.v_dict);
4125 if (type == TYPE_NEQUAL)
4126 n1 = !n1;
4127 }
4128 else if (rettv->v_type != var2.v_type
4129 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4130 {
4131 if (rettv->v_type != var2.v_type)
4132 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4133 else
4134 EMSG(_("E736: Invalid operation for Dictionary"));
4135 clear_tv(rettv);
4136 clear_tv(&var2);
4137 return FAIL;
4138 }
4139 else
4140 {
4141 /* Compare two Dictionaries for being equal or unequal. */
4142 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4143 if (type == TYPE_NEQUAL)
4144 n1 = !n1;
4145 }
4146 }
4147
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004148 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4149 {
4150 if (rettv->v_type != var2.v_type
4151 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4152 {
4153 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004154 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004155 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004156 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004157 clear_tv(rettv);
4158 clear_tv(&var2);
4159 return FAIL;
4160 }
4161 else
4162 {
4163 /* Compare two Funcrefs for being equal or unequal. */
4164 if (rettv->vval.v_string == NULL
4165 || var2.vval.v_string == NULL)
4166 n1 = FALSE;
4167 else
4168 n1 = STRCMP(rettv->vval.v_string,
4169 var2.vval.v_string) == 0;
4170 if (type == TYPE_NEQUAL)
4171 n1 = !n1;
4172 }
4173 }
4174
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 /*
4176 * If one of the two variables is a number, compare as a number.
4177 * When using "=~" or "!~", always compare as string.
4178 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004179 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 n1 = get_tv_number(rettv);
4183 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 switch (type)
4185 {
4186 case TYPE_EQUAL: n1 = (n1 == n2); break;
4187 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4188 case TYPE_GREATER: n1 = (n1 > n2); break;
4189 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4190 case TYPE_SMALLER: n1 = (n1 < n2); break;
4191 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4192 case TYPE_UNKNOWN:
4193 case TYPE_MATCH:
4194 case TYPE_NOMATCH: break; /* avoid gcc warning */
4195 }
4196 }
4197 else
4198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 s1 = get_tv_string_buf(rettv, buf1);
4200 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4202 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4203 else
4204 i = 0;
4205 n1 = FALSE;
4206 switch (type)
4207 {
4208 case TYPE_EQUAL: n1 = (i == 0); break;
4209 case TYPE_NEQUAL: n1 = (i != 0); break;
4210 case TYPE_GREATER: n1 = (i > 0); break;
4211 case TYPE_GEQUAL: n1 = (i >= 0); break;
4212 case TYPE_SMALLER: n1 = (i < 0); break;
4213 case TYPE_SEQUAL: n1 = (i <= 0); break;
4214
4215 case TYPE_MATCH:
4216 case TYPE_NOMATCH:
4217 /* avoid 'l' flag in 'cpoptions' */
4218 save_cpo = p_cpo;
4219 p_cpo = (char_u *)"";
4220 regmatch.regprog = vim_regcomp(s2,
4221 RE_MAGIC + RE_STRING);
4222 regmatch.rm_ic = ic;
4223 if (regmatch.regprog != NULL)
4224 {
4225 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4226 vim_free(regmatch.regprog);
4227 if (type == TYPE_NOMATCH)
4228 n1 = !n1;
4229 }
4230 p_cpo = save_cpo;
4231 break;
4232
4233 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4234 }
4235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 clear_tv(rettv);
4237 clear_tv(&var2);
4238 rettv->v_type = VAR_NUMBER;
4239 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 }
4242
4243 return OK;
4244}
4245
4246/*
4247 * Handle fourth level expression:
4248 * + number addition
4249 * - number subtraction
4250 * . string concatenation
4251 *
4252 * "arg" must point to the first non-white of the expression.
4253 * "arg" is advanced to the next non-white after the recognized expression.
4254 *
4255 * Return OK or FAIL.
4256 */
4257 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 int evaluate;
4262{
Bram Moolenaar33570922005-01-25 22:26:29 +00004263 typval_T var2;
4264 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 int op;
4266 long n1, n2;
4267 char_u *s1, *s2;
4268 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4269 char_u *p;
4270
4271 /*
4272 * Get the first variable.
4273 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 return FAIL;
4276
4277 /*
4278 * Repeat computing, until no '+', '-' or '.' is following.
4279 */
4280 for (;;)
4281 {
4282 op = **arg;
4283 if (op != '+' && op != '-' && op != '.')
4284 break;
4285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (op != '+' || rettv->v_type != VAR_LIST)
4287 {
4288 /* For "list + ...", an illegal use of the first operand as
4289 * a number cannot be determined before evaluating the 2nd
4290 * operand: if this is also a list, all is ok.
4291 * For "something . ...", "something - ..." or "non-list + ...",
4292 * we know that the first operand needs to be a string or number
4293 * without evaluating the 2nd operand. So check before to avoid
4294 * side effects after an error. */
4295 if (evaluate && get_tv_string_chk(rettv) == NULL)
4296 {
4297 clear_tv(rettv);
4298 return FAIL;
4299 }
4300 }
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 /*
4303 * Get the second variable.
4304 */
4305 *arg = skipwhite(*arg + 1);
4306 if (eval6(arg, &var2, evaluate) == FAIL)
4307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310 }
4311
4312 if (evaluate)
4313 {
4314 /*
4315 * Compute the result.
4316 */
4317 if (op == '.')
4318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4320 s2 = get_tv_string_buf_chk(&var2, buf2);
4321 if (s2 == NULL) /* type error ? */
4322 {
4323 clear_tv(rettv);
4324 clear_tv(&var2);
4325 return FAIL;
4326 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004327 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
4329 rettv->v_type = VAR_STRING;
4330 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004332 else if (op == '+' && rettv->v_type == VAR_LIST
4333 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004334 {
4335 /* concatenate Lists */
4336 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4337 &var3) == FAIL)
4338 {
4339 clear_tv(rettv);
4340 clear_tv(&var2);
4341 return FAIL;
4342 }
4343 clear_tv(rettv);
4344 *rettv = var3;
4345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 else
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 int error = FALSE;
4349
4350 n1 = get_tv_number_chk(rettv, &error);
4351 if (error)
4352 {
4353 /* This can only happen for "list + non-list".
4354 * For "non-list + ..." or "something - ...", we returned
4355 * before evaluating the 2nd operand. */
4356 clear_tv(rettv);
4357 return FAIL;
4358 }
4359 n2 = get_tv_number_chk(&var2, &error);
4360 if (error)
4361 {
4362 clear_tv(rettv);
4363 clear_tv(&var2);
4364 return FAIL;
4365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 if (op == '+')
4368 n1 = n1 + n2;
4369 else
4370 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 rettv->v_type = VAR_NUMBER;
4372 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 }
4377 return OK;
4378}
4379
4380/*
4381 * Handle fifth level expression:
4382 * * number multiplication
4383 * / number division
4384 * % number modulo
4385 *
4386 * "arg" must point to the first non-white of the expression.
4387 * "arg" is advanced to the next non-white after the recognized expression.
4388 *
4389 * Return OK or FAIL.
4390 */
4391 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 int evaluate;
4396{
Bram Moolenaar33570922005-01-25 22:26:29 +00004397 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 int op;
4399 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 /*
4403 * Get the first variable.
4404 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 return FAIL;
4407
4408 /*
4409 * Repeat computing, until no '*', '/' or '%' is following.
4410 */
4411 for (;;)
4412 {
4413 op = **arg;
4414 if (op != '*' && op != '/' && op != '%')
4415 break;
4416
4417 if (evaluate)
4418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004419 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 if (error)
4422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 else
4425 n1 = 0;
4426
4427 /*
4428 * Get the second variable.
4429 */
4430 *arg = skipwhite(*arg + 1);
4431 if (eval7(arg, &var2, evaluate) == FAIL)
4432 return FAIL;
4433
4434 if (evaluate)
4435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004436 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 if (error)
4439 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 /*
4442 * Compute the result.
4443 */
4444 if (op == '*')
4445 n1 = n1 * n2;
4446 else if (op == '/')
4447 {
4448 if (n2 == 0) /* give an error message? */
4449 n1 = 0x7fffffffL;
4450 else
4451 n1 = n1 / n2;
4452 }
4453 else
4454 {
4455 if (n2 == 0) /* give an error message? */
4456 n1 = 0;
4457 else
4458 n1 = n1 % n2;
4459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 rettv->v_type = VAR_NUMBER;
4461 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 }
4464
4465 return OK;
4466}
4467
4468/*
4469 * Handle sixth level expression:
4470 * number number constant
4471 * "string" string contstant
4472 * 'string' literal string contstant
4473 * &option-name option value
4474 * @r register contents
4475 * identifier variable value
4476 * function() function call
4477 * $VAR environment variable
4478 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004479 * [expr, expr] List
4480 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 *
4482 * Also handle:
4483 * ! in front logical NOT
4484 * - in front unary minus
4485 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 * trailing [] subscript in String or List
4487 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 *
4489 * "arg" must point to the first non-white of the expression.
4490 * "arg" is advanced to the next non-white after the recognized expression.
4491 *
4492 * Return OK or FAIL.
4493 */
4494 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004495eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 int evaluate;
4499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 long n;
4501 int len;
4502 char_u *s;
4503 int val;
4504 char_u *start_leader, *end_leader;
4505 int ret = OK;
4506 char_u *alias;
4507
4508 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004510 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
4514 /*
4515 * Skip '!' and '-' characters. They are handled later.
4516 */
4517 start_leader = *arg;
4518 while (**arg == '!' || **arg == '-' || **arg == '+')
4519 *arg = skipwhite(*arg + 1);
4520 end_leader = *arg;
4521
4522 switch (**arg)
4523 {
4524 /*
4525 * Number constant.
4526 */
4527 case '0':
4528 case '1':
4529 case '2':
4530 case '3':
4531 case '4':
4532 case '5':
4533 case '6':
4534 case '7':
4535 case '8':
4536 case '9':
4537 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4538 *arg += len;
4539 if (evaluate)
4540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541 rettv->v_type = VAR_NUMBER;
4542 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 break;
4545
4546 /*
4547 * String constant: "string".
4548 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 break;
4551
4552 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004556 break;
4557
4558 /*
4559 * List: [expr, expr]
4560 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 break;
4563
4564 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 * Dictionary: {key: val, key: val}
4566 */
4567 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4568 break;
4569
4570 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004571 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004573 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 break;
4575
4576 /*
4577 * Environment variable: $VAR.
4578 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 break;
4581
4582 /*
4583 * Register contents: @r.
4584 */
4585 case '@': ++*arg;
4586 if (evaluate)
4587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004589 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 if (**arg != NUL)
4592 ++*arg;
4593 break;
4594
4595 /*
4596 * nested expression: (expression).
4597 */
4598 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (**arg == ')')
4601 ++*arg;
4602 else if (ret == OK)
4603 {
4604 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 ret = FAIL;
4607 }
4608 break;
4609
Bram Moolenaar8c711452005-01-14 21:53:12 +00004610 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613
4614 if (ret == NOTDONE)
4615 {
4616 /*
4617 * Must be a variable or function name.
4618 * Can also be a curly-braces kind of name: {expr}.
4619 */
4620 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004621 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 if (alias != NULL)
4623 s = alias;
4624
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004625 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004626 ret = FAIL;
4627 else
4628 {
4629 if (**arg == '(') /* recursive! */
4630 {
4631 /* If "s" is the name of a variable of type VAR_FUNC
4632 * use its contents. */
4633 s = deref_func_name(s, &len);
4634
4635 /* Invoke the function. */
4636 ret = get_func_tv(s, len, rettv, arg,
4637 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004638 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 /* Stop the expression evaluation when immediately
4640 * aborting on error, or when an interrupt occurred or
4641 * an exception was thrown but not caught. */
4642 if (aborting())
4643 {
4644 if (ret == OK)
4645 clear_tv(rettv);
4646 ret = FAIL;
4647 }
4648 }
4649 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004650 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004651 else
4652 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004653 }
4654
4655 if (alias != NULL)
4656 vim_free(alias);
4657 }
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 *arg = skipwhite(*arg);
4660
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004661 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4662 * expr(expr). */
4663 if (ret == OK)
4664 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666 /*
4667 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4668 */
4669 if (ret == OK && evaluate && end_leader > start_leader)
4670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 int error = FALSE;
4672
4673 val = get_tv_number_chk(rettv, &error);
4674 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004676 clear_tv(rettv);
4677 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004679 else
4680 {
4681 while (end_leader > start_leader)
4682 {
4683 --end_leader;
4684 if (*end_leader == '!')
4685 val = !val;
4686 else if (*end_leader == '-')
4687 val = -val;
4688 }
4689 clear_tv(rettv);
4690 rettv->v_type = VAR_NUMBER;
4691 rettv->vval.v_number = val;
4692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694
4695 return ret;
4696}
4697
4698/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004699 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4700 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004701 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4702 */
4703 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004704eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004706 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004707 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004708 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709{
4710 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004711 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004713 long len = -1;
4714 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004715 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004720 if (verbose)
4721 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 return FAIL;
4723 }
4724
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727 /*
4728 * dict.name
4729 */
4730 key = *arg + 1;
4731 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4732 ;
4733 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 }
4737 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 /*
4740 * something[idx]
4741 *
4742 * Get the (first) variable from inside the [].
4743 */
4744 *arg = skipwhite(*arg + 1);
4745 if (**arg == ':')
4746 empty1 = TRUE;
4747 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4748 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4750 {
4751 /* not a number or string */
4752 clear_tv(&var1);
4753 return FAIL;
4754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004755
4756 /*
4757 * Get the second variable from inside the [:].
4758 */
4759 if (**arg == ':')
4760 {
4761 range = TRUE;
4762 *arg = skipwhite(*arg + 1);
4763 if (**arg == ']')
4764 empty2 = TRUE;
4765 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004767 if (!empty1)
4768 clear_tv(&var1);
4769 return FAIL;
4770 }
4771 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4772 {
4773 /* not a number or string */
4774 if (!empty1)
4775 clear_tv(&var1);
4776 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004777 return FAIL;
4778 }
4779 }
4780
4781 /* Check for the ']'. */
4782 if (**arg != ']')
4783 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004784 if (verbose)
4785 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004786 clear_tv(&var1);
4787 if (range)
4788 clear_tv(&var2);
4789 return FAIL;
4790 }
4791 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792 }
4793
4794 if (evaluate)
4795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 n1 = 0;
4797 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004799 n1 = get_tv_number(&var1);
4800 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 }
4802 if (range)
4803 {
4804 if (empty2)
4805 n2 = -1;
4806 else
4807 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004808 n2 = get_tv_number(&var2);
4809 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 }
4811 }
4812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 {
4815 case VAR_NUMBER:
4816 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004818 len = (long)STRLEN(s);
4819 if (range)
4820 {
4821 /* The resulting variable is a substring. If the indexes
4822 * are out of range the result is empty. */
4823 if (n1 < 0)
4824 {
4825 n1 = len + n1;
4826 if (n1 < 0)
4827 n1 = 0;
4828 }
4829 if (n2 < 0)
4830 n2 = len + n2;
4831 else if (n2 >= len)
4832 n2 = len;
4833 if (n1 >= len || n2 < 0 || n1 > n2)
4834 s = NULL;
4835 else
4836 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4837 }
4838 else
4839 {
4840 /* The resulting variable is a string of a single
4841 * character. If the index is too big or negative the
4842 * result is empty. */
4843 if (n1 >= len || n1 < 0)
4844 s = NULL;
4845 else
4846 s = vim_strnsave(s + n1, 1);
4847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(rettv);
4849 rettv->v_type = VAR_STRING;
4850 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004851 break;
4852
4853 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 if (n1 < 0)
4856 n1 = len + n1;
4857 if (!empty1 && (n1 < 0 || n1 >= len))
4858 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004859 if (verbose)
4860 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 return FAIL;
4862 }
4863 if (range)
4864 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004865 list_T *l;
4866 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004867
4868 if (n2 < 0)
4869 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004870 else if (n2 >= len)
4871 n2 = len - 1;
4872 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004874 if (verbose)
4875 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004876 return FAIL;
4877 }
4878 l = list_alloc();
4879 if (l == NULL)
4880 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 n1 <= n2; ++n1)
4883 {
4884 if (list_append_tv(l, &item->li_tv) == FAIL)
4885 {
4886 list_free(l);
4887 return FAIL;
4888 }
4889 item = item->li_next;
4890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 clear_tv(rettv);
4892 rettv->v_type = VAR_LIST;
4893 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004894 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004895 }
4896 else
4897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 clear_tv(rettv);
4901 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004902 }
4903 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004904
4905 case VAR_DICT:
4906 if (range)
4907 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004908 if (verbose)
4909 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004910 if (len == -1)
4911 clear_tv(&var1);
4912 return FAIL;
4913 }
4914 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004915 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916
4917 if (len == -1)
4918 {
4919 key = get_tv_string(&var1);
4920 if (*key == NUL)
4921 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004922 if (verbose)
4923 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 clear_tv(&var1);
4925 return FAIL;
4926 }
4927 }
4928
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004929 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004931 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004932 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004933 if (len == -1)
4934 clear_tv(&var1);
4935 if (item == NULL)
4936 return FAIL;
4937
4938 copy_tv(&item->di_tv, &var1);
4939 clear_tv(rettv);
4940 *rettv = var1;
4941 }
4942 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 }
4944 }
4945
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004946 return OK;
4947}
4948
4949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 * Get an option value.
4951 * "arg" points to the '&' or '+' before the option name.
4952 * "arg" is advanced to character after the option name.
4953 * Return OK or FAIL.
4954 */
4955 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004958 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 int evaluate;
4960{
4961 char_u *option_end;
4962 long numval;
4963 char_u *stringval;
4964 int opt_type;
4965 int c;
4966 int working = (**arg == '+'); /* has("+option") */
4967 int ret = OK;
4968 int opt_flags;
4969
4970 /*
4971 * Isolate the option name and find its value.
4972 */
4973 option_end = find_option_end(arg, &opt_flags);
4974 if (option_end == NULL)
4975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 EMSG2(_("E112: Option name missing: %s"), *arg);
4978 return FAIL;
4979 }
4980
4981 if (!evaluate)
4982 {
4983 *arg = option_end;
4984 return OK;
4985 }
4986
4987 c = *option_end;
4988 *option_end = NUL;
4989 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992 if (opt_type == -3) /* invalid name */
4993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 EMSG2(_("E113: Unknown option: %s"), *arg);
4996 ret = FAIL;
4997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
5000 if (opt_type == -2) /* hidden string option */
5001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 rettv->v_type = VAR_STRING;
5003 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005 else if (opt_type == -1) /* hidden number option */
5006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_NUMBER;
5008 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010 else if (opt_type == 1) /* number option */
5011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005012 rettv->v_type = VAR_NUMBER;
5013 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 else /* string option */
5016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017 rettv->v_type = VAR_STRING;
5018 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 }
5021 else if (working && (opt_type == -2 || opt_type == -1))
5022 ret = FAIL;
5023
5024 *option_end = c; /* put back for error messages */
5025 *arg = option_end;
5026
5027 return ret;
5028}
5029
5030/*
5031 * Allocate a variable for a string constant.
5032 * Return OK or FAIL.
5033 */
5034 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005035get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 int evaluate;
5039{
5040 char_u *p;
5041 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 int extra = 0;
5043
5044 /*
5045 * Find the end of the string, skipping backslashed characters.
5046 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
5049 if (*p == '\\' && p[1] != NUL)
5050 {
5051 ++p;
5052 /* A "\<x>" form occupies at least 4 characters, and produces up
5053 * to 6 characters: reserve space for 2 extra */
5054 if (*p == '<')
5055 extra += 2;
5056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058
5059 if (*p != '"')
5060 {
5061 EMSG2(_("E114: Missing quote: %s"), *arg);
5062 return FAIL;
5063 }
5064
5065 /* If only parsing, set *arg and return here */
5066 if (!evaluate)
5067 {
5068 *arg = p + 1;
5069 return OK;
5070 }
5071
5072 /*
5073 * Copy the string into allocated memory, handling backslashed
5074 * characters.
5075 */
5076 name = alloc((unsigned)(p - *arg + extra));
5077 if (name == NULL)
5078 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 rettv->v_type = VAR_STRING;
5080 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 if (*p == '\\')
5085 {
5086 switch (*++p)
5087 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 case 'b': *name++ = BS; ++p; break;
5089 case 'e': *name++ = ESC; ++p; break;
5090 case 'f': *name++ = FF; ++p; break;
5091 case 'n': *name++ = NL; ++p; break;
5092 case 'r': *name++ = CAR; ++p; break;
5093 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 case 'X': /* hex: "\x1", "\x12" */
5096 case 'x':
5097 case 'u': /* Unicode: "\u0023" */
5098 case 'U':
5099 if (vim_isxdigit(p[1]))
5100 {
5101 int n, nr;
5102 int c = toupper(*p);
5103
5104 if (c == 'X')
5105 n = 2;
5106 else
5107 n = 4;
5108 nr = 0;
5109 while (--n >= 0 && vim_isxdigit(p[1]))
5110 {
5111 ++p;
5112 nr = (nr << 4) + hex2nr(*p);
5113 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#ifdef FEAT_MBYTE
5116 /* For "\u" store the number according to
5117 * 'encoding'. */
5118 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 else
5121#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005122 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125
5126 /* octal: "\1", "\12", "\123" */
5127 case '0':
5128 case '1':
5129 case '2':
5130 case '3':
5131 case '4':
5132 case '5':
5133 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 case '7': *name = *p++ - '0';
5135 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 *name = (*name << 3) + *p++ - '0';
5138 if (*p >= '0' && *p <= '7')
5139 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 break;
5143
5144 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (extra != 0)
5147 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150 }
5151 /* FALLTHROUGH */
5152
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 break;
5155 }
5156 }
5157 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 *arg = p + 1;
5163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 return OK;
5165}
5166
5167/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 * Return OK or FAIL.
5170 */
5171 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005172get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 int evaluate;
5176{
5177 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005178 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005179 int reduce = 0;
5180
5181 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005183 */
5184 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5185 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005187 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005189 break;
5190 ++reduce;
5191 ++p;
5192 }
5193 }
5194
Bram Moolenaar8c711452005-01-14 21:53:12 +00005195 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005196 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005198 return FAIL;
5199 }
5200
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005202 if (!evaluate)
5203 {
5204 *arg = p + 1;
5205 return OK;
5206 }
5207
5208 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005210 */
5211 str = alloc((unsigned)((p - *arg) - reduce));
5212 if (str == NULL)
5213 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 rettv->v_type = VAR_STRING;
5215 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005216
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005218 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005220 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005222 break;
5223 ++p;
5224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005226 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005228 *arg = p + 1;
5229
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005230 return OK;
5231}
5232
5233/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234 * Allocate a variable for a List and fill it from "*arg".
5235 * Return OK or FAIL.
5236 */
5237 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005240 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 int evaluate;
5242{
Bram Moolenaar33570922005-01-25 22:26:29 +00005243 list_T *l = NULL;
5244 typval_T tv;
5245 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246
5247 if (evaluate)
5248 {
5249 l = list_alloc();
5250 if (l == NULL)
5251 return FAIL;
5252 }
5253
5254 *arg = skipwhite(*arg + 1);
5255 while (**arg != ']' && **arg != NUL)
5256 {
5257 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5258 goto failret;
5259 if (evaluate)
5260 {
5261 item = listitem_alloc();
5262 if (item != NULL)
5263 {
5264 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005265 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 list_append(l, item);
5267 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005268 else
5269 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 }
5271
5272 if (**arg == ']')
5273 break;
5274 if (**arg != ',')
5275 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 goto failret;
5278 }
5279 *arg = skipwhite(*arg + 1);
5280 }
5281
5282 if (**arg != ']')
5283 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285failret:
5286 if (evaluate)
5287 list_free(l);
5288 return FAIL;
5289 }
5290
5291 *arg = skipwhite(*arg + 1);
5292 if (evaluate)
5293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 rettv->v_type = VAR_LIST;
5295 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 ++l->lv_refcount;
5297 }
5298
5299 return OK;
5300}
5301
5302/*
5303 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005304 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005306 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307list_alloc()
5308{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005309 list_T *l;
5310
5311 l = (list_T *)alloc_clear(sizeof(list_T));
5312 if (l != NULL)
5313 {
5314 /* Prepend the list to the list of lists for garbage collection. */
5315 if (first_list != NULL)
5316 first_list->lv_used_prev = l;
5317 l->lv_used_prev = NULL;
5318 l->lv_used_next = first_list;
5319 first_list = l;
5320 }
5321 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322}
5323
5324/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005325 * Allocate an empty list for a return value.
5326 * Returns OK or FAIL.
5327 */
5328 static int
5329rettv_list_alloc(rettv)
5330 typval_T *rettv;
5331{
5332 list_T *l = list_alloc();
5333
5334 if (l == NULL)
5335 return FAIL;
5336
5337 rettv->vval.v_list = l;
5338 rettv->v_type = VAR_LIST;
5339 ++l->lv_refcount;
5340 return OK;
5341}
5342
5343/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 * Unreference a list: decrement the reference count and free it when it
5345 * becomes zero.
5346 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005347 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005349 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005351 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5352 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353}
5354
5355/*
5356 * Free a list, including all items it points to.
5357 * Ignores the reference count.
5358 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005359 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362{
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364
Bram Moolenaard9fba312005-06-26 22:34:35 +00005365 /* Avoid that recursive reference to the list frees us again. */
5366 l->lv_refcount = DEL_REFCOUNT;
5367
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005368 /* Remove the list from the list of lists for garbage collection. */
5369 if (l->lv_used_prev == NULL)
5370 first_list = l->lv_used_next;
5371 else
5372 l->lv_used_prev->lv_used_next = l->lv_used_next;
5373 if (l->lv_used_next != NULL)
5374 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5375
Bram Moolenaard9fba312005-06-26 22:34:35 +00005376 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005378 /* Remove the item before deleting it. */
5379 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 listitem_free(item);
5381 }
5382 vim_free(l);
5383}
5384
5385/*
5386 * Allocate a list item.
5387 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005388 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389listitem_alloc()
5390{
Bram Moolenaar33570922005-01-25 22:26:29 +00005391 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392}
5393
5394/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005395 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 */
5397 static void
5398listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 vim_free(item);
5403}
5404
5405/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005406 * Remove a list item from a List and free it. Also clears the value.
5407 */
5408 static void
5409listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005410 list_T *l;
5411 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005412{
5413 list_remove(l, item, item);
5414 listitem_free(item);
5415}
5416
5417/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 * Get the number of items in a list.
5419 */
5420 static long
5421list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005422 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 if (l == NULL)
5425 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005426 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427}
5428
5429/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005430 * Return TRUE when two lists have exactly the same values.
5431 */
5432 static int
5433list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005434 list_T *l1;
5435 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005436 int ic; /* ignore case for strings */
5437{
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005440 if (list_len(l1) != list_len(l2))
5441 return FALSE;
5442
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005443 for (item1 = l1->lv_first, item2 = l2->lv_first;
5444 item1 != NULL && item2 != NULL;
5445 item1 = item1->li_next, item2 = item2->li_next)
5446 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5447 return FALSE;
5448 return item1 == NULL && item2 == NULL;
5449}
5450
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005451#if defined(FEAT_PYTHON) || defined(PROTO)
5452/*
5453 * Return the dictitem that an entry in a hashtable points to.
5454 */
5455 dictitem_T *
5456dict_lookup(hi)
5457 hashitem_T *hi;
5458{
5459 return HI2DI(hi);
5460}
5461#endif
5462
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005463/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005464 * Return TRUE when two dictionaries have exactly the same key/values.
5465 */
5466 static int
5467dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005468 dict_T *d1;
5469 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005470 int ic; /* ignore case for strings */
5471{
Bram Moolenaar33570922005-01-25 22:26:29 +00005472 hashitem_T *hi;
5473 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005474 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005475
5476 if (dict_len(d1) != dict_len(d2))
5477 return FALSE;
5478
Bram Moolenaar33570922005-01-25 22:26:29 +00005479 todo = d1->dv_hashtab.ht_used;
5480 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005481 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005482 if (!HASHITEM_EMPTY(hi))
5483 {
5484 item2 = dict_find(d2, hi->hi_key, -1);
5485 if (item2 == NULL)
5486 return FALSE;
5487 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5488 return FALSE;
5489 --todo;
5490 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005491 }
5492 return TRUE;
5493}
5494
5495/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005496 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005497 * Compares the items just like "==" would compare them, but strings and
5498 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005499 */
5500 static int
5501tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 typval_T *tv1;
5503 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504 int ic; /* ignore case */
5505{
5506 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005507 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005508
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005509 if (tv1->v_type != tv2->v_type)
5510 return FALSE;
5511
5512 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005513 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005514 case VAR_LIST:
5515 /* recursive! */
5516 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5517
5518 case VAR_DICT:
5519 /* recursive! */
5520 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5521
5522 case VAR_FUNC:
5523 return (tv1->vval.v_string != NULL
5524 && tv2->vval.v_string != NULL
5525 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5526
5527 case VAR_NUMBER:
5528 return tv1->vval.v_number == tv2->vval.v_number;
5529
5530 case VAR_STRING:
5531 s1 = get_tv_string_buf(tv1, buf1);
5532 s2 = get_tv_string_buf(tv2, buf2);
5533 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005534 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005535
5536 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005537 return TRUE;
5538}
5539
5540/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 * Locate item with index "n" in list "l" and return it.
5542 * A negative index is counted from the end; -1 is the last item.
5543 * Returns NULL when "n" is out of range.
5544 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 long n;
5549{
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 long idx;
5552
5553 if (l == NULL)
5554 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005555
5556 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005558 n = l->lv_len + n;
5559
5560 /* Check for index out of range. */
5561 if (n < 0 || n >= l->lv_len)
5562 return NULL;
5563
5564 /* When there is a cached index may start search from there. */
5565 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005567 if (n < l->lv_idx / 2)
5568 {
5569 /* closest to the start of the list */
5570 item = l->lv_first;
5571 idx = 0;
5572 }
5573 else if (n > (l->lv_idx + l->lv_len) / 2)
5574 {
5575 /* closest to the end of the list */
5576 item = l->lv_last;
5577 idx = l->lv_len - 1;
5578 }
5579 else
5580 {
5581 /* closest to the cached index */
5582 item = l->lv_idx_item;
5583 idx = l->lv_idx;
5584 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 }
5586 else
5587 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005588 if (n < l->lv_len / 2)
5589 {
5590 /* closest to the start of the list */
5591 item = l->lv_first;
5592 idx = 0;
5593 }
5594 else
5595 {
5596 /* closest to the end of the list */
5597 item = l->lv_last;
5598 idx = l->lv_len - 1;
5599 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005601
5602 while (n > idx)
5603 {
5604 /* search forward */
5605 item = item->li_next;
5606 ++idx;
5607 }
5608 while (n < idx)
5609 {
5610 /* search backward */
5611 item = item->li_prev;
5612 --idx;
5613 }
5614
5615 /* cache the used index */
5616 l->lv_idx = idx;
5617 l->lv_idx_item = item;
5618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 return item;
5620}
5621
5622/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005623 * Get list item "l[idx]" as a number.
5624 */
5625 static long
5626list_find_nr(l, idx, errorp)
5627 list_T *l;
5628 long idx;
5629 int *errorp; /* set to TRUE when something wrong */
5630{
5631 listitem_T *li;
5632
5633 li = list_find(l, idx);
5634 if (li == NULL)
5635 {
5636 if (errorp != NULL)
5637 *errorp = TRUE;
5638 return -1L;
5639 }
5640 return get_tv_number_chk(&li->li_tv, errorp);
5641}
5642
5643/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005644 * Locate "item" list "l" and return its index.
5645 * Returns -1 when "item" is not in the list.
5646 */
5647 static long
5648list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 list_T *l;
5650 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005651{
5652 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005653 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005654
5655 if (l == NULL)
5656 return -1;
5657 idx = 0;
5658 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5659 ++idx;
5660 if (li == NULL)
5661 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005662 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005663}
5664
5665/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005666 * Append item "item" to the end of list "l".
5667 */
5668 static void
5669list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005670 list_T *l;
5671 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672{
5673 if (l->lv_last == NULL)
5674 {
5675 /* empty list */
5676 l->lv_first = item;
5677 l->lv_last = item;
5678 item->li_prev = NULL;
5679 }
5680 else
5681 {
5682 l->lv_last->li_next = item;
5683 item->li_prev = l->lv_last;
5684 l->lv_last = item;
5685 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005686 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 item->li_next = NULL;
5688}
5689
5690/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005691 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005692 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 */
5694 static int
5695list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005696 list_T *l;
5697 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005699 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700
Bram Moolenaar05159a02005-02-26 23:04:13 +00005701 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005703 copy_tv(tv, &li->li_tv);
5704 list_append(l, li);
5705 return OK;
5706}
5707
5708/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005709 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005710 * Return FAIL when out of memory.
5711 */
5712 int
5713list_append_dict(list, dict)
5714 list_T *list;
5715 dict_T *dict;
5716{
5717 listitem_T *li = listitem_alloc();
5718
5719 if (li == NULL)
5720 return FAIL;
5721 li->li_tv.v_type = VAR_DICT;
5722 li->li_tv.v_lock = 0;
5723 li->li_tv.vval.v_dict = dict;
5724 list_append(list, li);
5725 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 return OK;
5727}
5728
5729/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005730 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005731 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005732 * Returns FAIL when out of memory.
5733 */
5734 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005735list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005736 list_T *l;
5737 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005738 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005739{
5740 listitem_T *li = listitem_alloc();
5741
5742 if (li == NULL)
5743 return FAIL;
5744 list_append(l, li);
5745 li->li_tv.v_type = VAR_STRING;
5746 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005747 if (str == NULL)
5748 li->li_tv.vval.v_string = NULL;
5749 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005750 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005751 return FAIL;
5752 return OK;
5753}
5754
5755/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005756 * Append "n" to list "l".
5757 * Returns FAIL when out of memory.
5758 */
5759 static int
5760list_append_number(l, n)
5761 list_T *l;
5762 varnumber_T n;
5763{
5764 listitem_T *li;
5765
5766 li = listitem_alloc();
5767 if (li == NULL)
5768 return FAIL;
5769 li->li_tv.v_type = VAR_NUMBER;
5770 li->li_tv.v_lock = 0;
5771 li->li_tv.vval.v_number = n;
5772 list_append(l, li);
5773 return OK;
5774}
5775
5776/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005777 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005778 * If "item" is NULL append at the end.
5779 * Return FAIL when out of memory.
5780 */
5781 static int
5782list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005783 list_T *l;
5784 typval_T *tv;
5785 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005786{
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005788
5789 if (ni == NULL)
5790 return FAIL;
5791 copy_tv(tv, &ni->li_tv);
5792 if (item == NULL)
5793 /* Append new item at end of list. */
5794 list_append(l, ni);
5795 else
5796 {
5797 /* Insert new item before existing item. */
5798 ni->li_prev = item->li_prev;
5799 ni->li_next = item;
5800 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005801 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005802 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005803 ++l->lv_idx;
5804 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005805 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005806 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005807 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005808 l->lv_idx_item = NULL;
5809 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005810 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005811 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005812 }
5813 return OK;
5814}
5815
5816/*
5817 * Extend "l1" with "l2".
5818 * If "bef" is NULL append at the end, otherwise insert before this item.
5819 * Returns FAIL when out of memory.
5820 */
5821 static int
5822list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005823 list_T *l1;
5824 list_T *l2;
5825 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005826{
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005828
5829 for (item = l2->lv_first; item != NULL; item = item->li_next)
5830 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5831 return FAIL;
5832 return OK;
5833}
5834
5835/*
5836 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5837 * Return FAIL when out of memory.
5838 */
5839 static int
5840list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005841 list_T *l1;
5842 list_T *l2;
5843 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005844{
Bram Moolenaar33570922005-01-25 22:26:29 +00005845 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005846
5847 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005848 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005849 if (l == NULL)
5850 return FAIL;
5851 tv->v_type = VAR_LIST;
5852 tv->vval.v_list = l;
5853
5854 /* append all items from the second list */
5855 return list_extend(l, l2, NULL);
5856}
5857
5858/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005859 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005861 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 * Returns NULL when out of memory.
5863 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005864 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005865list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005866 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005868 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869{
Bram Moolenaar33570922005-01-25 22:26:29 +00005870 list_T *copy;
5871 listitem_T *item;
5872 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873
5874 if (orig == NULL)
5875 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876
5877 copy = list_alloc();
5878 if (copy != NULL)
5879 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880 if (copyID != 0)
5881 {
5882 /* Do this before adding the items, because one of the items may
5883 * refer back to this list. */
5884 orig->lv_copyID = copyID;
5885 orig->lv_copylist = copy;
5886 }
5887 for (item = orig->lv_first; item != NULL && !got_int;
5888 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 {
5890 ni = listitem_alloc();
5891 if (ni == NULL)
5892 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005893 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005894 {
5895 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5896 {
5897 vim_free(ni);
5898 break;
5899 }
5900 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005902 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 list_append(copy, ni);
5904 }
5905 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005906 if (item != NULL)
5907 {
5908 list_unref(copy);
5909 copy = NULL;
5910 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 }
5912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 return copy;
5914}
5915
5916/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005917 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005918 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005920 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005921list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 list_T *l;
5923 listitem_T *item;
5924 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 /* notify watchers */
5929 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005931 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005932 list_fix_watch(l, ip);
5933 if (ip == item2)
5934 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005936
5937 if (item2->li_next == NULL)
5938 l->lv_last = item->li_prev;
5939 else
5940 item2->li_next->li_prev = item->li_prev;
5941 if (item->li_prev == NULL)
5942 l->lv_first = item2->li_next;
5943 else
5944 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005945 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946}
5947
5948/*
5949 * Return an allocated string with the string representation of a list.
5950 * May return NULL.
5951 */
5952 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005953list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005955 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956{
5957 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958
5959 if (tv->vval.v_list == NULL)
5960 return NULL;
5961 ga_init2(&ga, (int)sizeof(char), 80);
5962 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005963 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005964 {
5965 vim_free(ga.ga_data);
5966 return NULL;
5967 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 ga_append(&ga, ']');
5969 ga_append(&ga, NUL);
5970 return (char_u *)ga.ga_data;
5971}
5972
5973/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005974 * Join list "l" into a string in "*gap", using separator "sep".
5975 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005976 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005978 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005979list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005980 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982 char_u *sep;
5983 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005984 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985{
5986 int first = TRUE;
5987 char_u *tofree;
5988 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005990 char_u *s;
5991
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 {
5994 if (first)
5995 first = FALSE;
5996 else
5997 ga_concat(gap, sep);
5998
5999 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006000 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006001 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006002 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006003 if (s != NULL)
6004 ga_concat(gap, s);
6005 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006006 if (s == NULL)
6007 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006008 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006009 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006010}
6011
6012/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006013 * Garbage collection for lists and dictionaries.
6014 *
6015 * We use reference counts to be able to free most items right away when they
6016 * are no longer used. But for composite items it's possible that it becomes
6017 * unused while the reference count is > 0: When there is a recursive
6018 * reference. Example:
6019 * :let l = [1, 2, 3]
6020 * :let d = {9: l}
6021 * :let l[1] = d
6022 *
6023 * Since this is quite unusual we handle this with garbage collection: every
6024 * once in a while find out which lists and dicts are not referenced from any
6025 * variable.
6026 *
6027 * Here is a good reference text about garbage collection (refers to Python
6028 * but it applies to all reference-counting mechanisms):
6029 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006030 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006031
6032/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006033 * Do garbage collection for lists and dicts.
6034 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006035 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006036 int
6037garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038{
6039 dict_T *dd;
6040 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 int copyID = ++current_copyID;
6042 buf_T *buf;
6043 win_T *wp;
6044 int i;
6045 funccall_T *fc;
6046 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006047#ifdef FEAT_WINDOWS
6048 tabpage_T *tp;
6049#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006050
6051 /*
6052 * 1. Go through all accessible variables and mark all lists and dicts
6053 * with copyID.
6054 */
6055 /* script-local variables */
6056 for (i = 1; i <= ga_scripts.ga_len; ++i)
6057 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6058
6059 /* buffer-local variables */
6060 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6061 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6062
6063 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006064 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006065 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6066
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006067#ifdef FEAT_WINDOWS
6068 /* tabpage-local variables */
6069 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6070 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6071#endif
6072
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006073 /* global variables */
6074 set_ref_in_ht(&globvarht, copyID);
6075
6076 /* function-local variables */
6077 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6078 {
6079 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6080 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6081 }
6082
6083 /*
6084 * 2. Go through the list of dicts and free items without the copyID.
6085 */
6086 for (dd = first_dict; dd != NULL; )
6087 if (dd->dv_copyID != copyID)
6088 {
6089 dict_free(dd);
6090 did_free = TRUE;
6091
6092 /* restart, next dict may also have been freed */
6093 dd = first_dict;
6094 }
6095 else
6096 dd = dd->dv_used_next;
6097
6098 /*
6099 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006100 * But don't free a list that has a watcher (used in a for loop), these
6101 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006102 */
6103 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006104 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006105 {
6106 list_free(ll);
6107 did_free = TRUE;
6108
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006109 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 ll = first_list;
6111 }
6112 else
6113 ll = ll->lv_used_next;
6114
6115 return did_free;
6116}
6117
6118/*
6119 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6120 */
6121 static void
6122set_ref_in_ht(ht, copyID)
6123 hashtab_T *ht;
6124 int copyID;
6125{
6126 int todo;
6127 hashitem_T *hi;
6128
6129 todo = ht->ht_used;
6130 for (hi = ht->ht_array; todo > 0; ++hi)
6131 if (!HASHITEM_EMPTY(hi))
6132 {
6133 --todo;
6134 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6135 }
6136}
6137
6138/*
6139 * Mark all lists and dicts referenced through list "l" with "copyID".
6140 */
6141 static void
6142set_ref_in_list(l, copyID)
6143 list_T *l;
6144 int copyID;
6145{
6146 listitem_T *li;
6147
6148 for (li = l->lv_first; li != NULL; li = li->li_next)
6149 set_ref_in_item(&li->li_tv, copyID);
6150}
6151
6152/*
6153 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6154 */
6155 static void
6156set_ref_in_item(tv, copyID)
6157 typval_T *tv;
6158 int copyID;
6159{
6160 dict_T *dd;
6161 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006162
6163 switch (tv->v_type)
6164 {
6165 case VAR_DICT:
6166 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006167 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006168 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006169 /* Didn't see this dict yet. */
6170 dd->dv_copyID = copyID;
6171 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006172 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006173 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006174
6175 case VAR_LIST:
6176 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006177 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006178 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006179 /* Didn't see this list yet. */
6180 ll->lv_copyID = copyID;
6181 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006182 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006183 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006184 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006185 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006186}
6187
6188/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006189 * Allocate an empty header for a dictionary.
6190 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006191 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006192dict_alloc()
6193{
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006195
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006198 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006199 /* Add the list to the hashtable for garbage collection. */
6200 if (first_dict != NULL)
6201 first_dict->dv_used_prev = d;
6202 d->dv_used_next = first_dict;
6203 d->dv_used_prev = NULL;
6204
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006206 d->dv_lock = 0;
6207 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006208 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006209 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006210 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006211}
6212
6213/*
6214 * Unreference a Dictionary: decrement the reference count and free it when it
6215 * becomes zero.
6216 */
6217 static void
6218dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006219 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006220{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006221 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6222 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223}
6224
6225/*
6226 * Free a Dictionary, including all items it contains.
6227 * Ignores the reference count.
6228 */
6229 static void
6230dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006231 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006232{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006233 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006235 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006236
Bram Moolenaard9fba312005-06-26 22:34:35 +00006237 /* Avoid that recursive reference to the dict frees us again. */
6238 d->dv_refcount = DEL_REFCOUNT;
6239
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006240 /* Remove the dict from the list of dicts for garbage collection. */
6241 if (d->dv_used_prev == NULL)
6242 first_dict = d->dv_used_next;
6243 else
6244 d->dv_used_prev->dv_used_next = d->dv_used_next;
6245 if (d->dv_used_next != NULL)
6246 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6247
6248 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006249 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 todo = d->dv_hashtab.ht_used;
6251 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253 if (!HASHITEM_EMPTY(hi))
6254 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006255 /* Remove the item before deleting it, just in case there is
6256 * something recursive causing trouble. */
6257 di = HI2DI(hi);
6258 hash_remove(&d->dv_hashtab, hi);
6259 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260 --todo;
6261 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006264 vim_free(d);
6265}
6266
6267/*
6268 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006269 * The "key" is copied to the new item.
6270 * Note that the value of the item "di_tv" still needs to be initialized!
6271 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006272 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274dictitem_alloc(key)
6275 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006276{
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006282 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006283 di->di_flags = 0;
6284 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006286}
6287
6288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289 * Make a copy of a Dictionary item.
6290 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006292dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294{
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006296
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006298 if (di != NULL)
6299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006300 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006301 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006302 copy_tv(&org->di_tv, &di->di_tv);
6303 }
6304 return di;
6305}
6306
6307/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006308 * Remove item "item" from Dictionary "dict" and free it.
6309 */
6310 static void
6311dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 dict_T *dict;
6313 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006314{
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006316
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006318 if (HASHITEM_EMPTY(hi))
6319 EMSG2(_(e_intern2), "dictitem_remove()");
6320 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006322 dictitem_free(item);
6323}
6324
6325/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006326 * Free a dict item. Also clears the value.
6327 */
6328 static void
6329dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006330 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006331{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006332 clear_tv(&item->di_tv);
6333 vim_free(item);
6334}
6335
6336/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006337 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6338 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006339 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006340 * Returns NULL when out of memory.
6341 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006343dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006344 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006345 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006346 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006347{
Bram Moolenaar33570922005-01-25 22:26:29 +00006348 dict_T *copy;
6349 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006350 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006352
6353 if (orig == NULL)
6354 return NULL;
6355
6356 copy = dict_alloc();
6357 if (copy != NULL)
6358 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006359 if (copyID != 0)
6360 {
6361 orig->dv_copyID = copyID;
6362 orig->dv_copydict = copy;
6363 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006365 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006367 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006369 --todo;
6370
6371 di = dictitem_alloc(hi->hi_key);
6372 if (di == NULL)
6373 break;
6374 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006375 {
6376 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6377 copyID) == FAIL)
6378 {
6379 vim_free(di);
6380 break;
6381 }
6382 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006383 else
6384 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6385 if (dict_add(copy, di) == FAIL)
6386 {
6387 dictitem_free(di);
6388 break;
6389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006392
Bram Moolenaare9a41262005-01-15 22:18:47 +00006393 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006394 if (todo > 0)
6395 {
6396 dict_unref(copy);
6397 copy = NULL;
6398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006399 }
6400
6401 return copy;
6402}
6403
6404/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006406 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006408 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 dict_T *d;
6411 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412{
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414}
6415
Bram Moolenaar8c711452005-01-14 21:53:12 +00006416/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 * Add a number or string entry to dictionary "d".
6418 * When "str" is NULL use number "nr", otherwise use "str".
6419 * Returns FAIL when out of memory and when key already exists.
6420 */
6421 int
6422dict_add_nr_str(d, key, nr, str)
6423 dict_T *d;
6424 char *key;
6425 long nr;
6426 char_u *str;
6427{
6428 dictitem_T *item;
6429
6430 item = dictitem_alloc((char_u *)key);
6431 if (item == NULL)
6432 return FAIL;
6433 item->di_tv.v_lock = 0;
6434 if (str == NULL)
6435 {
6436 item->di_tv.v_type = VAR_NUMBER;
6437 item->di_tv.vval.v_number = nr;
6438 }
6439 else
6440 {
6441 item->di_tv.v_type = VAR_STRING;
6442 item->di_tv.vval.v_string = vim_strsave(str);
6443 }
6444 if (dict_add(d, item) == FAIL)
6445 {
6446 dictitem_free(item);
6447 return FAIL;
6448 }
6449 return OK;
6450}
6451
6452/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006453 * Get the number of items in a Dictionary.
6454 */
6455 static long
6456dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006458{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006459 if (d == NULL)
6460 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006462}
6463
6464/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006465 * Find item "key[len]" in Dictionary "d".
6466 * If "len" is negative use strlen(key).
6467 * Returns NULL when not found.
6468 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006470dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006472 char_u *key;
6473 int len;
6474{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006475#define AKEYLEN 200
6476 char_u buf[AKEYLEN];
6477 char_u *akey;
6478 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006480
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006481 if (len < 0)
6482 akey = key;
6483 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006484 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006485 tofree = akey = vim_strnsave(key, len);
6486 if (akey == NULL)
6487 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006488 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006489 else
6490 {
6491 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006492 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006493 akey = buf;
6494 }
6495
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006497 vim_free(tofree);
6498 if (HASHITEM_EMPTY(hi))
6499 return NULL;
6500 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006501}
6502
6503/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006504 * Get a string item from a dictionary.
6505 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006506 * Returns NULL if the entry doesn't exist or out of memory.
6507 */
6508 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006509get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006510 dict_T *d;
6511 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006512 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006513{
6514 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006515 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006516
6517 di = dict_find(d, key, -1);
6518 if (di == NULL)
6519 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006520 s = get_tv_string(&di->di_tv);
6521 if (save && s != NULL)
6522 s = vim_strsave(s);
6523 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006524}
6525
6526/*
6527 * Get a number item from a dictionary.
6528 * Returns 0 if the entry doesn't exist or out of memory.
6529 */
6530 long
6531get_dict_number(d, key)
6532 dict_T *d;
6533 char_u *key;
6534{
6535 dictitem_T *di;
6536
6537 di = dict_find(d, key, -1);
6538 if (di == NULL)
6539 return 0;
6540 return get_tv_number(&di->di_tv);
6541}
6542
6543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006544 * Return an allocated string with the string representation of a Dictionary.
6545 * May return NULL.
6546 */
6547 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006548dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006550 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006551{
6552 garray_T ga;
6553 int first = TRUE;
6554 char_u *tofree;
6555 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006559 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006560
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006561 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 return NULL;
6563 ga_init2(&ga, (int)sizeof(char), 80);
6564 ga_append(&ga, '{');
6565
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006568 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006569 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006571 --todo;
6572
6573 if (first)
6574 first = FALSE;
6575 else
6576 ga_concat(&ga, (char_u *)", ");
6577
6578 tofree = string_quote(hi->hi_key, FALSE);
6579 if (tofree != NULL)
6580 {
6581 ga_concat(&ga, tofree);
6582 vim_free(tofree);
6583 }
6584 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006585 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006586 if (s != NULL)
6587 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006588 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 if (s == NULL)
6590 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006591 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 if (todo > 0)
6594 {
6595 vim_free(ga.ga_data);
6596 return NULL;
6597 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598
6599 ga_append(&ga, '}');
6600 ga_append(&ga, NUL);
6601 return (char_u *)ga.ga_data;
6602}
6603
6604/*
6605 * Allocate a variable for a Dictionary and fill it from "*arg".
6606 * Return OK or FAIL. Returns NOTDONE for {expr}.
6607 */
6608 static int
6609get_dict_tv(arg, rettv, evaluate)
6610 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006612 int evaluate;
6613{
Bram Moolenaar33570922005-01-25 22:26:29 +00006614 dict_T *d = NULL;
6615 typval_T tvkey;
6616 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006618 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006619 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006620 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006621
6622 /*
6623 * First check if it's not a curly-braces thing: {expr}.
6624 * Must do this without evaluating, otherwise a function may be called
6625 * twice. Unfortunately this means we need to call eval1() twice for the
6626 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006627 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006628 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006629 if (*start != '}')
6630 {
6631 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6632 return FAIL;
6633 if (*start == '}')
6634 return NOTDONE;
6635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636
6637 if (evaluate)
6638 {
6639 d = dict_alloc();
6640 if (d == NULL)
6641 return FAIL;
6642 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006643 tvkey.v_type = VAR_UNKNOWN;
6644 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006645
6646 *arg = skipwhite(*arg + 1);
6647 while (**arg != '}' && **arg != NUL)
6648 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006649 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006650 goto failret;
6651 if (**arg != ':')
6652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006653 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006654 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655 goto failret;
6656 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006657 key = get_tv_string_buf_chk(&tvkey, buf);
6658 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006660 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6661 if (key != NULL)
6662 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006663 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006664 goto failret;
6665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006666
6667 *arg = skipwhite(*arg + 1);
6668 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6669 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006670 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 goto failret;
6672 }
6673 if (evaluate)
6674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006675 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006676 if (item != NULL)
6677 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006678 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006679 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006680 clear_tv(&tv);
6681 goto failret;
6682 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006683 item = dictitem_alloc(key);
6684 clear_tv(&tvkey);
6685 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006687 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006688 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006689 if (dict_add(d, item) == FAIL)
6690 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691 }
6692 }
6693
6694 if (**arg == '}')
6695 break;
6696 if (**arg != ',')
6697 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006698 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006699 goto failret;
6700 }
6701 *arg = skipwhite(*arg + 1);
6702 }
6703
6704 if (**arg != '}')
6705 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006706 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006707failret:
6708 if (evaluate)
6709 dict_free(d);
6710 return FAIL;
6711 }
6712
6713 *arg = skipwhite(*arg + 1);
6714 if (evaluate)
6715 {
6716 rettv->v_type = VAR_DICT;
6717 rettv->vval.v_dict = d;
6718 ++d->dv_refcount;
6719 }
6720
6721 return OK;
6722}
6723
Bram Moolenaar8c711452005-01-14 21:53:12 +00006724/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006725 * Return a string with the string representation of a variable.
6726 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006727 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006728 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006729 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 * May return NULL;
6731 */
6732 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006733echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006734 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006736 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006737 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006738{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 static int recurse = 0;
6740 char_u *r = NULL;
6741
Bram Moolenaar33570922005-01-25 22:26:29 +00006742 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006744 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 *tofree = NULL;
6746 return NULL;
6747 }
6748 ++recurse;
6749
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750 switch (tv->v_type)
6751 {
6752 case VAR_FUNC:
6753 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006754 r = tv->vval.v_string;
6755 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006758 if (tv->vval.v_list == NULL)
6759 {
6760 *tofree = NULL;
6761 r = NULL;
6762 }
6763 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6764 {
6765 *tofree = NULL;
6766 r = (char_u *)"[...]";
6767 }
6768 else
6769 {
6770 tv->vval.v_list->lv_copyID = copyID;
6771 *tofree = list2string(tv, copyID);
6772 r = *tofree;
6773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006775
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006777 if (tv->vval.v_dict == NULL)
6778 {
6779 *tofree = NULL;
6780 r = NULL;
6781 }
6782 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6783 {
6784 *tofree = NULL;
6785 r = (char_u *)"{...}";
6786 }
6787 else
6788 {
6789 tv->vval.v_dict->dv_copyID = copyID;
6790 *tofree = dict2string(tv, copyID);
6791 r = *tofree;
6792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006793 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006794
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006795 case VAR_STRING:
6796 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006797 *tofree = NULL;
6798 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006799 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006800
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006801 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006802 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006803 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805
6806 --recurse;
6807 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808}
6809
6810/*
6811 * Return a string with the string representation of a variable.
6812 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6813 * "numbuf" is used for a number.
6814 * Puts quotes around strings, so that they can be parsed back by eval().
6815 * May return NULL;
6816 */
6817 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006818tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006819 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 char_u **tofree;
6821 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006822 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823{
6824 switch (tv->v_type)
6825 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 case VAR_FUNC:
6827 *tofree = string_quote(tv->vval.v_string, TRUE);
6828 return *tofree;
6829 case VAR_STRING:
6830 *tofree = string_quote(tv->vval.v_string, FALSE);
6831 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006833 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006834 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006836 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006839 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840}
6841
6842/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006843 * Return string "str" in ' quotes, doubling ' characters.
6844 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006845 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006846 */
6847 static char_u *
6848string_quote(str, function)
6849 char_u *str;
6850 int function;
6851{
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006853 char_u *p, *r, *s;
6854
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 len = (function ? 13 : 3);
6856 if (str != NULL)
6857 {
6858 len += STRLEN(str);
6859 for (p = str; *p != NUL; mb_ptr_adv(p))
6860 if (*p == '\'')
6861 ++len;
6862 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006863 s = r = alloc(len);
6864 if (r != NULL)
6865 {
6866 if (function)
6867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006868 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869 r += 10;
6870 }
6871 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006872 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006873 if (str != NULL)
6874 for (p = str; *p != NUL; )
6875 {
6876 if (*p == '\'')
6877 *r++ = '\'';
6878 MB_COPY_CHAR(p, r);
6879 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006881 if (function)
6882 *r++ = ')';
6883 *r++ = NUL;
6884 }
6885 return s;
6886}
6887
6888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 * Get the value of an environment variable.
6890 * "arg" is pointing to the '$'. It is advanced to after the name.
6891 * If the environment variable was not set, silently assume it is empty.
6892 * Always return OK.
6893 */
6894 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 int evaluate;
6899{
6900 char_u *string = NULL;
6901 int len;
6902 int cc;
6903 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006904 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 ++*arg;
6907 name = *arg;
6908 len = get_env_len(arg);
6909 if (evaluate)
6910 {
6911 if (len != 0)
6912 {
6913 cc = name[len];
6914 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006915 /* first try vim_getenv(), fast for normal environment vars */
6916 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006918 {
6919 if (!mustfree)
6920 string = vim_strsave(string);
6921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 else
6923 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006924 if (mustfree)
6925 vim_free(string);
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 /* next try expanding things like $VIM and ${HOME} */
6928 string = expand_env_save(name - 1);
6929 if (string != NULL && *string == '$')
6930 {
6931 vim_free(string);
6932 string = NULL;
6933 }
6934 }
6935 name[len] = cc;
6936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006937 rettv->v_type = VAR_STRING;
6938 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 }
6940
6941 return OK;
6942}
6943
6944/*
6945 * Array with names and number of arguments of all internal functions
6946 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6947 */
6948static struct fst
6949{
6950 char *f_name; /* function name */
6951 char f_min_argc; /* minimal number of arguments */
6952 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006954 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955} functions[] =
6956{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006957 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {"append", 2, 2, f_append},
6959 {"argc", 0, 0, f_argc},
6960 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00006961 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006963 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {"bufexists", 1, 1, f_bufexists},
6965 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6966 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6967 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6968 {"buflisted", 1, 1, f_buflisted},
6969 {"bufloaded", 1, 1, f_bufloaded},
6970 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006971 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {"bufwinnr", 1, 1, f_bufwinnr},
6973 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006974 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006975 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006976 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"char2nr", 1, 1, f_char2nr},
6978 {"cindent", 1, 1, f_cindent},
6979 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006980#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006981 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006982 {"complete_add", 1, 1, f_complete_add},
6983 {"complete_check", 0, 0, f_complete_check},
6984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006986 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006987 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006989 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006990 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {"delete", 1, 1, f_delete},
6992 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006993 {"diff_filler", 1, 1, f_diff_filler},
6994 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006995 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006997 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {"eventhandler", 0, 0, f_eventhandler},
6999 {"executable", 1, 1, f_executable},
7000 {"exists", 1, 1, f_exists},
7001 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007002 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7004 {"filereadable", 1, 1, f_filereadable},
7005 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007006 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007007 {"finddir", 1, 3, f_finddir},
7008 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {"fnamemodify", 2, 2, f_fnamemodify},
7010 {"foldclosed", 1, 1, f_foldclosed},
7011 {"foldclosedend", 1, 1, f_foldclosedend},
7012 {"foldlevel", 1, 1, f_foldlevel},
7013 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007014 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007016 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007018 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007019 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {"getbufvar", 2, 2, f_getbufvar},
7021 {"getchar", 0, 1, f_getchar},
7022 {"getcharmod", 0, 0, f_getcharmod},
7023 {"getcmdline", 0, 0, f_getcmdline},
7024 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007025 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007027 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007028 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {"getfsize", 1, 1, f_getfsize},
7030 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007031 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007032 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007033 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00007034 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007035 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007036 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {"getregtype", 0, 1, f_getregtype},
7038 {"getwinposx", 0, 0, f_getwinposx},
7039 {"getwinposy", 0, 0, f_getwinposy},
7040 {"getwinvar", 2, 2, f_getwinvar},
7041 {"glob", 1, 1, f_glob},
7042 {"globpath", 2, 2, f_globpath},
7043 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007044 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007045 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7047 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7048 {"histadd", 2, 2, f_histadd},
7049 {"histdel", 1, 2, f_histdel},
7050 {"histget", 1, 2, f_histget},
7051 {"histnr", 1, 1, f_histnr},
7052 {"hlID", 1, 1, f_hlID},
7053 {"hlexists", 1, 1, f_hlexists},
7054 {"hostname", 0, 0, f_hostname},
7055 {"iconv", 3, 3, f_iconv},
7056 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007057 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007058 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007060 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"inputrestore", 0, 0, f_inputrestore},
7062 {"inputsave", 0, 0, f_inputsave},
7063 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007064 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007066 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007067 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007068 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007069 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007071 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"libcall", 3, 3, f_libcall},
7073 {"libcallnr", 3, 3, f_libcallnr},
7074 {"line", 1, 1, f_line},
7075 {"line2byte", 1, 1, f_line2byte},
7076 {"lispindent", 1, 1, f_lispindent},
7077 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007078 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007079 {"maparg", 1, 3, f_maparg},
7080 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007081 {"match", 2, 4, f_match},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007082 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007083 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007084 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007085 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007086 {"max", 1, 1, f_max},
7087 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007088#ifdef vim_mkdir
7089 {"mkdir", 1, 3, f_mkdir},
7090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {"mode", 0, 0, f_mode},
7092 {"nextnonblank", 1, 1, f_nextnonblank},
7093 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007094 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007096 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007097 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007099 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007100 {"reltime", 0, 2, f_reltime},
7101 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {"remote_expr", 2, 3, f_remote_expr},
7103 {"remote_foreground", 1, 1, f_remote_foreground},
7104 {"remote_peek", 1, 2, f_remote_peek},
7105 {"remote_read", 1, 1, f_remote_read},
7106 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007107 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007109 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007111 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007112 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007113 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007114 {"searchpair", 3, 6, f_searchpair},
7115 {"searchpairpos", 3, 6, f_searchpairpos},
7116 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {"server2client", 2, 2, f_server2client},
7118 {"serverlist", 0, 0, f_serverlist},
7119 {"setbufvar", 3, 3, f_setbufvar},
7120 {"setcmdpos", 1, 1, f_setcmdpos},
7121 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007122 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007123 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007124 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {"setreg", 2, 3, f_setreg},
7126 {"setwinvar", 3, 3, f_setwinvar},
7127 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007128 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007129 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007130 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007131 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007132 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007133 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134#ifdef HAVE_STRFTIME
7135 {"strftime", 1, 2, f_strftime},
7136#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007138 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {"strlen", 1, 1, f_strlen},
7140 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007141 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {"strtrans", 1, 1, f_strtrans},
7143 {"submatch", 1, 1, f_submatch},
7144 {"substitute", 4, 4, f_substitute},
7145 {"synID", 3, 3, f_synID},
7146 {"synIDattr", 2, 3, f_synIDattr},
7147 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007148 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007149 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007150 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007152 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007153 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007155 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {"tolower", 1, 1, f_tolower},
7157 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007158 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007160 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"virtcol", 1, 1, f_virtcol},
7162 {"visualmode", 0, 1, f_visualmode},
7163 {"winbufnr", 1, 1, f_winbufnr},
7164 {"wincol", 0, 0, f_wincol},
7165 {"winheight", 1, 1, f_winheight},
7166 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007167 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007169 {"winrestview", 1, 1, f_winrestview},
7170 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007172 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173};
7174
7175#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7176
7177/*
7178 * Function given to ExpandGeneric() to obtain the list of internal
7179 * or user defined function names.
7180 */
7181 char_u *
7182get_function_name(xp, idx)
7183 expand_T *xp;
7184 int idx;
7185{
7186 static int intidx = -1;
7187 char_u *name;
7188
7189 if (idx == 0)
7190 intidx = -1;
7191 if (intidx < 0)
7192 {
7193 name = get_user_func_name(xp, idx);
7194 if (name != NULL)
7195 return name;
7196 }
7197 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7198 {
7199 STRCPY(IObuff, functions[intidx].f_name);
7200 STRCAT(IObuff, "(");
7201 if (functions[intidx].f_max_argc == 0)
7202 STRCAT(IObuff, ")");
7203 return IObuff;
7204 }
7205
7206 return NULL;
7207}
7208
7209/*
7210 * Function given to ExpandGeneric() to obtain the list of internal or
7211 * user defined variable or function names.
7212 */
7213/*ARGSUSED*/
7214 char_u *
7215get_expr_name(xp, idx)
7216 expand_T *xp;
7217 int idx;
7218{
7219 static int intidx = -1;
7220 char_u *name;
7221
7222 if (idx == 0)
7223 intidx = -1;
7224 if (intidx < 0)
7225 {
7226 name = get_function_name(xp, idx);
7227 if (name != NULL)
7228 return name;
7229 }
7230 return get_user_var_name(xp, ++intidx);
7231}
7232
7233#endif /* FEAT_CMDL_COMPL */
7234
7235/*
7236 * Find internal function in table above.
7237 * Return index, or -1 if not found
7238 */
7239 static int
7240find_internal_func(name)
7241 char_u *name; /* name of the function */
7242{
7243 int first = 0;
7244 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7245 int cmp;
7246 int x;
7247
7248 /*
7249 * Find the function name in the table. Binary search.
7250 */
7251 while (first <= last)
7252 {
7253 x = first + ((unsigned)(last - first) >> 1);
7254 cmp = STRCMP(name, functions[x].f_name);
7255 if (cmp < 0)
7256 last = x - 1;
7257 else if (cmp > 0)
7258 first = x + 1;
7259 else
7260 return x;
7261 }
7262 return -1;
7263}
7264
7265/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007266 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7267 * name it contains, otherwise return "name".
7268 */
7269 static char_u *
7270deref_func_name(name, lenp)
7271 char_u *name;
7272 int *lenp;
7273{
Bram Moolenaar33570922005-01-25 22:26:29 +00007274 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 int cc;
7276
7277 cc = name[*lenp];
7278 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007279 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007280 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007282 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 {
7285 *lenp = 0;
7286 return (char_u *)""; /* just in case */
7287 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 *lenp = STRLEN(v->di_tv.vval.v_string);
7289 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007290 }
7291
7292 return name;
7293}
7294
7295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 * Allocate a variable for the result of a function.
7297 * Return OK or FAIL.
7298 */
7299 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7301 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 char_u *name; /* name of the function */
7303 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 char_u **arg; /* argument, pointing to the '(' */
7306 linenr_T firstline; /* first line of range */
7307 linenr_T lastline; /* last line of range */
7308 int *doesrange; /* return: function handled range */
7309 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
7312 char_u *argp;
7313 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 int argcount = 0; /* number of arguments found */
7316
7317 /*
7318 * Get the arguments.
7319 */
7320 argp = *arg;
7321 while (argcount < MAX_FUNC_ARGS)
7322 {
7323 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7324 if (*argp == ')' || *argp == ',' || *argp == NUL)
7325 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7327 {
7328 ret = FAIL;
7329 break;
7330 }
7331 ++argcount;
7332 if (*argp != ',')
7333 break;
7334 }
7335 if (*argp == ')')
7336 ++argp;
7337 else
7338 ret = FAIL;
7339
7340 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007341 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 {
7345 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007346 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007348 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
7351 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007352 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354 *arg = skipwhite(argp);
7355 return ret;
7356}
7357
7358
7359/*
7360 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007361 * Return OK when the function can't be called, FAIL otherwise.
7362 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 */
7364 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007365call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 char_u *name; /* name of the function */
7368 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 linenr_T firstline; /* first line of range */
7373 linenr_T lastline; /* last line of range */
7374 int *doesrange; /* return: function handled range */
7375 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379#define ERROR_UNKNOWN 0
7380#define ERROR_TOOMANY 1
7381#define ERROR_TOOFEW 2
7382#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383#define ERROR_DICT 4
7384#define ERROR_NONE 5
7385#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 int error = ERROR_NONE;
7387 int i;
7388 int llen;
7389 ufunc_T *fp;
7390 int cc;
7391#define FLEN_FIXED 40
7392 char_u fname_buf[FLEN_FIXED + 1];
7393 char_u *fname;
7394
7395 /*
7396 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7397 * Change <SNR>123_name() to K_SNR 123_name().
7398 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7399 */
7400 cc = name[len];
7401 name[len] = NUL;
7402 llen = eval_fname_script(name);
7403 if (llen > 0)
7404 {
7405 fname_buf[0] = K_SPECIAL;
7406 fname_buf[1] = KS_EXTRA;
7407 fname_buf[2] = (int)KE_SNR;
7408 i = 3;
7409 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7410 {
7411 if (current_SID <= 0)
7412 error = ERROR_SCRIPT;
7413 else
7414 {
7415 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7416 i = (int)STRLEN(fname_buf);
7417 }
7418 }
7419 if (i + STRLEN(name + llen) < FLEN_FIXED)
7420 {
7421 STRCPY(fname_buf + i, name + llen);
7422 fname = fname_buf;
7423 }
7424 else
7425 {
7426 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7427 if (fname == NULL)
7428 error = ERROR_OTHER;
7429 else
7430 {
7431 mch_memmove(fname, fname_buf, (size_t)i);
7432 STRCPY(fname + i, name + llen);
7433 }
7434 }
7435 }
7436 else
7437 fname = name;
7438
7439 *doesrange = FALSE;
7440
7441
7442 /* execute the function if no errors detected and executing */
7443 if (evaluate && error == ERROR_NONE)
7444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007445 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 error = ERROR_UNKNOWN;
7447
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007448 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
7450 /*
7451 * User defined function.
7452 */
7453 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007456 /* Trigger FuncUndefined event, may load the function. */
7457 if (fp == NULL
7458 && apply_autocmds(EVENT_FUNCUNDEFINED,
7459 fname, fname, TRUE, NULL)
7460 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007462 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 fp = find_func(fname);
7464 }
7465#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007466 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007467 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007468 {
7469 /* loaded a package, search for the function again */
7470 fp = find_func(fname);
7471 }
7472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 if (fp != NULL)
7474 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007475 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007477 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007479 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007481 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 else
7484 {
7485 /*
7486 * Call the user function.
7487 * Save and restore search patterns, script variables and
7488 * redo buffer.
7489 */
7490 save_search_patterns();
7491 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007492 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007493 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007495 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7496 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7497 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007498 /* Function was unreferenced while being used, free it
7499 * now. */
7500 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 restoreRedobuff();
7502 restore_search_patterns();
7503 error = ERROR_NONE;
7504 }
7505 }
7506 }
7507 else
7508 {
7509 /*
7510 * Find the function name in the table, call its implementation.
7511 */
7512 i = find_internal_func(fname);
7513 if (i >= 0)
7514 {
7515 if (argcount < functions[i].f_min_argc)
7516 error = ERROR_TOOFEW;
7517 else if (argcount > functions[i].f_max_argc)
7518 error = ERROR_TOOMANY;
7519 else
7520 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007521 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007522 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 error = ERROR_NONE;
7524 }
7525 }
7526 }
7527 /*
7528 * The function call (or "FuncUndefined" autocommand sequence) might
7529 * have been aborted by an error, an interrupt, or an explicitly thrown
7530 * exception that has not been caught so far. This situation can be
7531 * tested for by calling aborting(). For an error in an internal
7532 * function or for the "E132" error in call_user_func(), however, the
7533 * throw point at which the "force_abort" flag (temporarily reset by
7534 * emsg()) is normally updated has not been reached yet. We need to
7535 * update that flag first to make aborting() reliable.
7536 */
7537 update_force_abort();
7538 }
7539 if (error == ERROR_NONE)
7540 ret = OK;
7541
7542 /*
7543 * Report an error unless the argument evaluation or function call has been
7544 * cancelled due to an aborting error, an interrupt, or an exception.
7545 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546 if (!aborting())
7547 {
7548 switch (error)
7549 {
7550 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007551 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 break;
7553 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007554 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 break;
7556 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007557 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 name);
7559 break;
7560 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007561 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 name);
7563 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007564 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007565 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007566 name);
7567 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 }
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570
7571 name[len] = cc;
7572 if (fname != name && fname != fname_buf)
7573 vim_free(fname);
7574
7575 return ret;
7576}
7577
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007578/*
7579 * Give an error message with a function name. Handle <SNR> things.
7580 */
7581 static void
7582emsg_funcname(msg, name)
7583 char *msg;
7584 char_u *name;
7585{
7586 char_u *p;
7587
7588 if (*name == K_SPECIAL)
7589 p = concat_str((char_u *)"<SNR>", name + 3);
7590 else
7591 p = name;
7592 EMSG2(_(msg), p);
7593 if (p != name)
7594 vim_free(p);
7595}
7596
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597/*********************************************
7598 * Implementation of the built-in functions
7599 */
7600
7601/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007602 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 */
7604 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007605f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 typval_T *argvars;
7607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007611 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007614 if ((l = argvars[0].vval.v_list) != NULL
7615 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7616 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007618 }
7619 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007620 EMSG(_(e_listreq));
7621}
7622
7623/*
7624 * "append(lnum, string/list)" function
7625 */
7626 static void
7627f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 typval_T *argvars;
7629 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007630{
7631 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007632 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007633 list_T *l = NULL;
7634 listitem_T *li = NULL;
7635 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007636 long added = 0;
7637
Bram Moolenaar0d660222005-01-07 21:51:51 +00007638 lnum = get_tv_lnum(argvars);
7639 if (lnum >= 0
7640 && lnum <= curbuf->b_ml.ml_line_count
7641 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007643 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007644 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645 l = argvars[1].vval.v_list;
7646 if (l == NULL)
7647 return;
7648 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007650 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007651 for (;;)
7652 {
7653 if (l == NULL)
7654 tv = &argvars[1]; /* append a string */
7655 else if (li == NULL)
7656 break; /* end of list */
7657 else
7658 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007659 line = get_tv_string_chk(tv);
7660 if (line == NULL) /* type error */
7661 {
7662 rettv->vval.v_number = 1; /* Failed */
7663 break;
7664 }
7665 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007666 ++added;
7667 if (l == NULL)
7668 break;
7669 li = li->li_next;
7670 }
7671
7672 appended_lines_mark(lnum, added);
7673 if (curwin->w_cursor.lnum > lnum)
7674 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007676 else
7677 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678}
7679
7680/*
7681 * "argc()" function
7682 */
7683/* ARGSUSED */
7684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 typval_T *argvars;
7687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690}
7691
7692/*
7693 * "argidx()" function
7694 */
7695/* ARGSUSED */
7696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 typval_T *argvars;
7699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007701 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702}
7703
7704/*
7705 * "argv(nr)" function
7706 */
7707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007708f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 typval_T *argvars;
7710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
7712 int idx;
7713
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007714 if (argvars[0].v_type != VAR_UNKNOWN)
7715 {
7716 idx = get_tv_number_chk(&argvars[0], NULL);
7717 if (idx >= 0 && idx < ARGCOUNT)
7718 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7719 else
7720 rettv->vval.v_string = NULL;
7721 rettv->v_type = VAR_STRING;
7722 }
7723 else if (rettv_list_alloc(rettv) == OK)
7724 for (idx = 0; idx < ARGCOUNT; ++idx)
7725 list_append_string(rettv->vval.v_list,
7726 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727}
7728
7729/*
7730 * "browse(save, title, initdir, default)" function
7731 */
7732/* ARGSUSED */
7733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T *argvars;
7736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
7738#ifdef FEAT_BROWSE
7739 int save;
7740 char_u *title;
7741 char_u *initdir;
7742 char_u *defname;
7743 char_u buf[NUMBUFLEN];
7744 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007745 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007747 save = get_tv_number_chk(&argvars[0], &error);
7748 title = get_tv_string_chk(&argvars[1]);
7749 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7750 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007752 if (error || title == NULL || initdir == NULL || defname == NULL)
7753 rettv->vval.v_string = NULL;
7754 else
7755 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007756 do_browse(save ? BROWSE_SAVE : 0,
7757 title, defname, NULL, initdir, NULL, curbuf);
7758#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007759 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007760#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007761 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007762}
7763
7764/*
7765 * "browsedir(title, initdir)" function
7766 */
7767/* ARGSUSED */
7768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 typval_T *argvars;
7771 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007772{
7773#ifdef FEAT_BROWSE
7774 char_u *title;
7775 char_u *initdir;
7776 char_u buf[NUMBUFLEN];
7777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007778 title = get_tv_string_chk(&argvars[0]);
7779 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007781 if (title == NULL || initdir == NULL)
7782 rettv->vval.v_string = NULL;
7783 else
7784 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007785 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007787 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007789 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
Bram Moolenaar33570922005-01-25 22:26:29 +00007792static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794/*
7795 * Find a buffer by number or exact name.
7796 */
7797 static buf_T *
7798find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007803 if (avar->v_type == VAR_NUMBER)
7804 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007805 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007807 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007808 if (buf == NULL)
7809 {
7810 /* No full path name match, try a match with a URL or a "nofile"
7811 * buffer, these don't use the full path. */
7812 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7813 if (buf->b_fname != NULL
7814 && (path_with_url(buf->b_fname)
7815#ifdef FEAT_QUICKFIX
7816 || bt_nofile(buf)
7817#endif
7818 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007819 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007820 break;
7821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 }
7823 return buf;
7824}
7825
7826/*
7827 * "bufexists(expr)" function
7828 */
7829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 typval_T *argvars;
7832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835}
7836
7837/*
7838 * "buflisted(expr)" function
7839 */
7840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007842 typval_T *argvars;
7843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845 buf_T *buf;
7846
7847 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849}
7850
7851/*
7852 * "bufloaded(expr)" function
7853 */
7854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859 buf_T *buf;
7860
7861 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
Bram Moolenaar33570922005-01-25 22:26:29 +00007865static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007866
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867/*
7868 * Get buffer by number or pattern.
7869 */
7870 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007872 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007874 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 int save_magic;
7876 char_u *save_cpo;
7877 buf_T *buf;
7878
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007879 if (tv->v_type == VAR_NUMBER)
7880 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007881 if (tv->v_type != VAR_STRING)
7882 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 if (name == NULL || *name == NUL)
7884 return curbuf;
7885 if (name[0] == '$' && name[1] == NUL)
7886 return lastbuf;
7887
7888 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7889 save_magic = p_magic;
7890 p_magic = TRUE;
7891 save_cpo = p_cpo;
7892 p_cpo = (char_u *)"";
7893
7894 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7895 TRUE, FALSE));
7896
7897 p_magic = save_magic;
7898 p_cpo = save_cpo;
7899
7900 /* If not found, try expanding the name, like done for bufexists(). */
7901 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007902 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
7904 return buf;
7905}
7906
7907/*
7908 * "bufname(expr)" function
7909 */
7910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 typval_T *argvars;
7913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 buf_T *buf;
7916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007917 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007919 buf = get_buf_tv(&argvars[0]);
7920 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007922 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 --emsg_off;
7926}
7927
7928/*
7929 * "bufnr(expr)" function
7930 */
7931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007932f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 typval_T *argvars;
7934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007937 int error = FALSE;
7938 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007940 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007943 --emsg_off;
7944
7945 /* If the buffer isn't found and the second argument is not zero create a
7946 * new buffer. */
7947 if (buf == NULL
7948 && argvars[1].v_type != VAR_UNKNOWN
7949 && get_tv_number_chk(&argvars[1], &error) != 0
7950 && !error
7951 && (name = get_tv_string_chk(&argvars[0])) != NULL
7952 && !error)
7953 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7954
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959}
7960
7961/*
7962 * "bufwinnr(nr)" function
7963 */
7964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 typval_T *argvars;
7967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968{
7969#ifdef FEAT_WINDOWS
7970 win_T *wp;
7971 int winnr = 0;
7972#endif
7973 buf_T *buf;
7974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007975 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978#ifdef FEAT_WINDOWS
7979 for (wp = firstwin; wp; wp = wp->w_next)
7980 {
7981 ++winnr;
7982 if (wp->w_buffer == buf)
7983 break;
7984 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007985 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007987 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988#endif
7989 --emsg_off;
7990}
7991
7992/*
7993 * "byte2line(byte)" function
7994 */
7995/*ARGSUSED*/
7996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007997f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 typval_T *argvars;
7999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003#else
8004 long boff = 0;
8005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008006 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 (linenr_T)0, &boff);
8012#endif
8013}
8014
8015/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008016 * "byteidx()" function
8017 */
8018/*ARGSUSED*/
8019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008020f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008021 typval_T *argvars;
8022 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008023{
8024#ifdef FEAT_MBYTE
8025 char_u *t;
8026#endif
8027 char_u *str;
8028 long idx;
8029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008030 str = get_tv_string_chk(&argvars[0]);
8031 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008033 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008034 return;
8035
8036#ifdef FEAT_MBYTE
8037 t = str;
8038 for ( ; idx > 0; idx--)
8039 {
8040 if (*t == NUL) /* EOL reached */
8041 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008042 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008043 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008044 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008045#else
8046 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008048#endif
8049}
8050
8051/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008052 * "call(func, arglist)" function
8053 */
8054 static void
8055f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 typval_T *argvars;
8057 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008058{
8059 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00008060 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008061 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008063 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008065
8066 rettv->vval.v_number = 0;
8067 if (argvars[1].v_type != VAR_LIST)
8068 {
8069 EMSG(_(e_listreq));
8070 return;
8071 }
8072 if (argvars[1].vval.v_list == NULL)
8073 return;
8074
8075 if (argvars[0].v_type == VAR_FUNC)
8076 func = argvars[0].vval.v_string;
8077 else
8078 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008079 if (*func == NUL)
8080 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008081
Bram Moolenaare9a41262005-01-15 22:18:47 +00008082 if (argvars[2].v_type != VAR_UNKNOWN)
8083 {
8084 if (argvars[2].v_type != VAR_DICT)
8085 {
8086 EMSG(_(e_dictreq));
8087 return;
8088 }
8089 selfdict = argvars[2].vval.v_dict;
8090 }
8091
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008092 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8093 item = item->li_next)
8094 {
8095 if (argc == MAX_FUNC_ARGS)
8096 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008097 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008098 break;
8099 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008100 /* Make a copy of each argument. This is needed to be able to set
8101 * v_lock to VAR_FIXED in the copy without changing the original list.
8102 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008103 copy_tv(&item->li_tv, &argv[argc++]);
8104 }
8105
8106 if (item == NULL)
8107 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008108 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8109 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008110
8111 /* Free the arguments. */
8112 while (argc > 0)
8113 clear_tv(&argv[--argc]);
8114}
8115
8116/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008117 * "changenr()" function
8118 */
8119/*ARGSUSED*/
8120 static void
8121f_changenr(argvars, rettv)
8122 typval_T *argvars;
8123 typval_T *rettv;
8124{
8125 rettv->vval.v_number = curbuf->b_u_seq_cur;
8126}
8127
8128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 * "char2nr(string)" function
8130 */
8131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T *argvars;
8134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136#ifdef FEAT_MBYTE
8137 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008138 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 else
8140#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142}
8143
8144/*
8145 * "cindent(lnum)" function
8146 */
8147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008148f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 typval_T *argvars;
8150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152#ifdef FEAT_CINDENT
8153 pos_T pos;
8154 linenr_T lnum;
8155
8156 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008157 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8159 {
8160 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008161 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 curwin->w_cursor = pos;
8163 }
8164 else
8165#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167}
8168
8169/*
8170 * "col(string)" function
8171 */
8172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008174 typval_T *argvars;
8175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177 colnr_T col = 0;
8178 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008179 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008181 fp = var2fpos(&argvars[0], FALSE, &fnum);
8182 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {
8184 if (fp->col == MAXCOL)
8185 {
8186 /* '> can be MAXCOL, get the length of the line then */
8187 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8188 col = STRLEN(ml_get(fp->lnum)) + 1;
8189 else
8190 col = MAXCOL;
8191 }
8192 else
8193 {
8194 col = fp->col + 1;
8195#ifdef FEAT_VIRTUALEDIT
8196 /* col(".") when the cursor is on the NUL at the end of the line
8197 * because of "coladd" can be seen as an extra column. */
8198 if (virtual_active() && fp == &curwin->w_cursor)
8199 {
8200 char_u *p = ml_get_cursor();
8201
8202 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8203 curwin->w_virtcol - curwin->w_cursor.coladd))
8204 {
8205# ifdef FEAT_MBYTE
8206 int l;
8207
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008208 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 col += l;
8210# else
8211 if (*p != NUL && p[1] == NUL)
8212 ++col;
8213# endif
8214 }
8215 }
8216#endif
8217 }
8218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008219 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220}
8221
Bram Moolenaar572cb562005-08-05 21:35:02 +00008222#if defined(FEAT_INS_EXPAND)
8223/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008224 * "complete()" function
8225 */
8226/*ARGSUSED*/
8227 static void
8228f_complete(argvars, rettv)
8229 typval_T *argvars;
8230 typval_T *rettv;
8231{
8232 int startcol;
8233
8234 if ((State & INSERT) == 0)
8235 {
8236 EMSG(_("E785: complete() can only be used in Insert mode"));
8237 return;
8238 }
8239 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8240 {
8241 EMSG(_(e_invarg));
8242 return;
8243 }
8244
8245 startcol = get_tv_number_chk(&argvars[0], NULL);
8246 if (startcol <= 0)
8247 return;
8248
8249 set_completion(startcol - 1, argvars[1].vval.v_list);
8250}
8251
8252/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008253 * "complete_add()" function
8254 */
8255/*ARGSUSED*/
8256 static void
8257f_complete_add(argvars, rettv)
8258 typval_T *argvars;
8259 typval_T *rettv;
8260{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008261 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008262}
8263
8264/*
8265 * "complete_check()" function
8266 */
8267/*ARGSUSED*/
8268 static void
8269f_complete_check(argvars, rettv)
8270 typval_T *argvars;
8271 typval_T *rettv;
8272{
8273 int saved = RedrawingDisabled;
8274
8275 RedrawingDisabled = 0;
8276 ins_compl_check_keys(0);
8277 rettv->vval.v_number = compl_interrupted;
8278 RedrawingDisabled = saved;
8279}
8280#endif
8281
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282/*
8283 * "confirm(message, buttons[, default [, type]])" function
8284 */
8285/*ARGSUSED*/
8286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008288 typval_T *argvars;
8289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
8291#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8292 char_u *message;
8293 char_u *buttons = NULL;
8294 char_u buf[NUMBUFLEN];
8295 char_u buf2[NUMBUFLEN];
8296 int def = 1;
8297 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008298 char_u *typestr;
8299 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008301 message = get_tv_string_chk(&argvars[0]);
8302 if (message == NULL)
8303 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008304 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008306 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8307 if (buttons == NULL)
8308 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008312 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008314 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8315 if (typestr == NULL)
8316 error = TRUE;
8317 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008319 switch (TOUPPER_ASC(*typestr))
8320 {
8321 case 'E': type = VIM_ERROR; break;
8322 case 'Q': type = VIM_QUESTION; break;
8323 case 'I': type = VIM_INFO; break;
8324 case 'W': type = VIM_WARNING; break;
8325 case 'G': type = VIM_GENERIC; break;
8326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 }
8329 }
8330 }
8331
8332 if (buttons == NULL || *buttons == NUL)
8333 buttons = (char_u *)_("&Ok");
8334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008335 if (error)
8336 rettv->vval.v_number = 0;
8337 else
8338 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 def, NULL);
8340#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342#endif
8343}
8344
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008345/*
8346 * "copy()" function
8347 */
8348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 typval_T *argvars;
8351 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008352{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008353 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
8356/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008357 * "count()" function
8358 */
8359 static void
8360f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 typval_T *argvars;
8362 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008363{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008364 long n = 0;
8365 int ic = FALSE;
8366
Bram Moolenaare9a41262005-01-15 22:18:47 +00008367 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008368 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 listitem_T *li;
8370 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008371 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008372
Bram Moolenaare9a41262005-01-15 22:18:47 +00008373 if ((l = argvars[0].vval.v_list) != NULL)
8374 {
8375 li = l->lv_first;
8376 if (argvars[2].v_type != VAR_UNKNOWN)
8377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008378 int error = FALSE;
8379
8380 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008381 if (argvars[3].v_type != VAR_UNKNOWN)
8382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008383 idx = get_tv_number_chk(&argvars[3], &error);
8384 if (!error)
8385 {
8386 li = list_find(l, idx);
8387 if (li == NULL)
8388 EMSGN(_(e_listidx), idx);
8389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008390 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008391 if (error)
8392 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008393 }
8394
8395 for ( ; li != NULL; li = li->li_next)
8396 if (tv_equal(&li->li_tv, &argvars[1], ic))
8397 ++n;
8398 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 else if (argvars[0].v_type == VAR_DICT)
8401 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 int todo;
8403 dict_T *d;
8404 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008405
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008406 if ((d = argvars[0].vval.v_dict) != NULL)
8407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008408 int error = FALSE;
8409
Bram Moolenaare9a41262005-01-15 22:18:47 +00008410 if (argvars[2].v_type != VAR_UNKNOWN)
8411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008413 if (argvars[3].v_type != VAR_UNKNOWN)
8414 EMSG(_(e_invarg));
8415 }
8416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008419 {
8420 if (!HASHITEM_EMPTY(hi))
8421 {
8422 --todo;
8423 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8424 ++n;
8425 }
8426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427 }
8428 }
8429 else
8430 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008431 rettv->vval.v_number = n;
8432}
8433
8434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8436 *
8437 * Checks the existence of a cscope connection.
8438 */
8439/*ARGSUSED*/
8440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008441f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 typval_T *argvars;
8443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
8445#ifdef FEAT_CSCOPE
8446 int num = 0;
8447 char_u *dbpath = NULL;
8448 char_u *prepend = NULL;
8449 char_u buf[NUMBUFLEN];
8450
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008451 if (argvars[0].v_type != VAR_UNKNOWN
8452 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 num = (int)get_tv_number(&argvars[0]);
8455 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008456 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 }
8459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008460 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#endif
8464}
8465
8466/*
8467 * "cursor(lnum, col)" function
8468 *
8469 * Moves the cursor to the specified line and column
8470 */
8471/*ARGSUSED*/
8472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008473f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 typval_T *argvars;
8475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
8477 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008478#ifdef FEAT_VIRTUALEDIT
8479 long coladd = 0;
8480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481
Bram Moolenaara5525202006-03-02 22:52:09 +00008482 if (argvars[1].v_type == VAR_UNKNOWN)
8483 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008484 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008485
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008486 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008487 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008488 line = pos.lnum;
8489 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008490#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008491 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008492#endif
8493 }
8494 else
8495 {
8496 line = get_tv_lnum(argvars);
8497 col = get_tv_number_chk(&argvars[1], NULL);
8498#ifdef FEAT_VIRTUALEDIT
8499 if (argvars[2].v_type != VAR_UNKNOWN)
8500 coladd = get_tv_number_chk(&argvars[2], NULL);
8501#endif
8502 }
8503 if (line < 0 || col < 0
8504#ifdef FEAT_VIRTUALEDIT
8505 || coladd < 0
8506#endif
8507 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008508 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 if (line > 0)
8510 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 if (col > 0)
8512 curwin->w_cursor.col = col - 1;
8513#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008514 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#endif
8516
8517 /* Make sure the cursor is in a valid position. */
8518 check_cursor();
8519#ifdef FEAT_MBYTE
8520 /* Correct cursor for multi-byte character. */
8521 if (has_mbyte)
8522 mb_adjust_cursor();
8523#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008524
8525 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526}
8527
8528/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 */
8531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008533 typval_T *argvars;
8534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008536 int noref = 0;
8537
8538 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008539 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008540 if (noref < 0 || noref > 1)
8541 EMSG(_(e_invarg));
8542 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008543 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544}
8545
8546/*
8547 * "delete()" function
8548 */
8549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008550f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008551 typval_T *argvars;
8552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008557 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560/*
8561 * "did_filetype()" function
8562 */
8563/*ARGSUSED*/
8564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 typval_T *argvars;
8567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568{
8569#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#endif
8574}
8575
8576/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008577 * "diff_filler()" function
8578 */
8579/*ARGSUSED*/
8580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *argvars;
8583 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008584{
8585#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008587#endif
8588}
8589
8590/*
8591 * "diff_hlID()" function
8592 */
8593/*ARGSUSED*/
8594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008595f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 typval_T *argvars;
8597 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008598{
8599#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008600 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008601 static linenr_T prev_lnum = 0;
8602 static int changedtick = 0;
8603 static int fnum = 0;
8604 static int change_start = 0;
8605 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008606 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008607 int filler_lines;
8608 int col;
8609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008610 if (lnum < 0) /* ignore type error in {lnum} arg */
8611 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008612 if (lnum != prev_lnum
8613 || changedtick != curbuf->b_changedtick
8614 || fnum != curbuf->b_fnum)
8615 {
8616 /* New line, buffer, change: need to get the values. */
8617 filler_lines = diff_check(curwin, lnum);
8618 if (filler_lines < 0)
8619 {
8620 if (filler_lines == -1)
8621 {
8622 change_start = MAXCOL;
8623 change_end = -1;
8624 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8625 hlID = HLF_ADD; /* added line */
8626 else
8627 hlID = HLF_CHD; /* changed line */
8628 }
8629 else
8630 hlID = HLF_ADD; /* added line */
8631 }
8632 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008633 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008634 prev_lnum = lnum;
8635 changedtick = curbuf->b_changedtick;
8636 fnum = curbuf->b_fnum;
8637 }
8638
8639 if (hlID == HLF_CHD || hlID == HLF_TXD)
8640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008642 if (col >= change_start && col <= change_end)
8643 hlID = HLF_TXD; /* changed text */
8644 else
8645 hlID = HLF_CHD; /* changed line */
8646 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008647 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008648#endif
8649}
8650
8651/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008652 * "empty({expr})" function
8653 */
8654 static void
8655f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 typval_T *argvars;
8657 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008658{
8659 int n;
8660
8661 switch (argvars[0].v_type)
8662 {
8663 case VAR_STRING:
8664 case VAR_FUNC:
8665 n = argvars[0].vval.v_string == NULL
8666 || *argvars[0].vval.v_string == NUL;
8667 break;
8668 case VAR_NUMBER:
8669 n = argvars[0].vval.v_number == 0;
8670 break;
8671 case VAR_LIST:
8672 n = argvars[0].vval.v_list == NULL
8673 || argvars[0].vval.v_list->lv_first == NULL;
8674 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008675 case VAR_DICT:
8676 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008678 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008679 default:
8680 EMSG2(_(e_intern2), "f_empty()");
8681 n = 0;
8682 }
8683
8684 rettv->vval.v_number = n;
8685}
8686
8687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 * "escape({string}, {chars})" function
8689 */
8690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008691f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 typval_T *argvars;
8693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
8695 char_u buf[NUMBUFLEN];
8696
Bram Moolenaar758711c2005-02-02 23:11:38 +00008697 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8698 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700}
8701
8702/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008703 * "eval()" function
8704 */
8705/*ARGSUSED*/
8706 static void
8707f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 typval_T *argvars;
8709 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008710{
8711 char_u *s;
8712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008713 s = get_tv_string_chk(&argvars[0]);
8714 if (s != NULL)
8715 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008717 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8718 {
8719 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008720 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008721 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008722 else if (*s != NUL)
8723 EMSG(_(e_trailing));
8724}
8725
8726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 * "eventhandler()" function
8728 */
8729/*ARGSUSED*/
8730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008731f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 typval_T *argvars;
8733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736}
8737
8738/*
8739 * "executable()" function
8740 */
8741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008743 typval_T *argvars;
8744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008746 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747}
8748
8749/*
8750 * "exists()" function
8751 */
8752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008754 typval_T *argvars;
8755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 char_u *p;
8758 char_u *name;
8759 int n = FALSE;
8760 int len = 0;
8761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 if (*p == '$') /* environment variable */
8764 {
8765 /* first try "normal" environment variables (fast) */
8766 if (mch_getenv(p + 1) != NULL)
8767 n = TRUE;
8768 else
8769 {
8770 /* try expanding things like $VIM and ${HOME} */
8771 p = expand_env_save(p);
8772 if (p != NULL && *p != '$')
8773 n = TRUE;
8774 vim_free(p);
8775 }
8776 }
8777 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008778 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 else if (*p == '*') /* internal or user defined function */
8780 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008781 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 }
8783 else if (*p == ':')
8784 {
8785 n = cmd_exists(p + 1);
8786 }
8787 else if (*p == '#')
8788 {
8789#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008790 if (p[1] == '#')
8791 n = autocmd_supported(p + 2);
8792 else
8793 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794#endif
8795 }
8796 else /* internal variable */
8797 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008798 char_u *tofree;
8799 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008801 /* get_name_len() takes care of expanding curly braces */
8802 name = p;
8803 len = get_name_len(&p, &tofree, TRUE, FALSE);
8804 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008806 if (tofree != NULL)
8807 name = tofree;
8808 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8809 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008811 /* handle d.key, l[idx], f(expr) */
8812 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8813 if (n)
8814 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 }
8816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008818 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
8820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822}
8823
8824/*
8825 * "expand()" function
8826 */
8827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008829 typval_T *argvars;
8830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832 char_u *s;
8833 int len;
8834 char_u *errormsg;
8835 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8836 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008837 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 rettv->v_type = VAR_STRING;
8840 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 if (*s == '%' || *s == '#' || *s == '<')
8842 {
8843 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 --emsg_off;
8846 }
8847 else
8848 {
8849 /* When the optional second argument is non-zero, don't remove matches
8850 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 if (argvars[1].v_type != VAR_UNKNOWN
8852 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854 if (!error)
8855 {
8856 ExpandInit(&xpc);
8857 xpc.xp_context = EXPAND_FILES;
8858 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8859 ExpandCleanup(&xpc);
8860 }
8861 else
8862 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 }
8864}
8865
8866/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008867 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008868 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008869 */
8870 static void
8871f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *argvars;
8873 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008874{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008875 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008877 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 list_T *l1, *l2;
8879 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008880 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008882
Bram Moolenaare9a41262005-01-15 22:18:47 +00008883 l1 = argvars[0].vval.v_list;
8884 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008885 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8886 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008887 {
8888 if (argvars[2].v_type != VAR_UNKNOWN)
8889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890 before = get_tv_number_chk(&argvars[2], &error);
8891 if (error)
8892 return; /* type error; errmsg already given */
8893
Bram Moolenaar758711c2005-02-02 23:11:38 +00008894 if (before == l1->lv_len)
8895 item = NULL;
8896 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008897 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008898 item = list_find(l1, before);
8899 if (item == NULL)
8900 {
8901 EMSGN(_(e_listidx), before);
8902 return;
8903 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008904 }
8905 }
8906 else
8907 item = NULL;
8908 list_extend(l1, l2, item);
8909
Bram Moolenaare9a41262005-01-15 22:18:47 +00008910 copy_tv(&argvars[0], rettv);
8911 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008912 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008913 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8914 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008915 dict_T *d1, *d2;
8916 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008917 char_u *action;
8918 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008919 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008920 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921
8922 d1 = argvars[0].vval.v_dict;
8923 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008924 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8925 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008926 {
8927 /* Check the third argument. */
8928 if (argvars[2].v_type != VAR_UNKNOWN)
8929 {
8930 static char *(av[]) = {"keep", "force", "error"};
8931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 action = get_tv_string_chk(&argvars[2]);
8933 if (action == NULL)
8934 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008935 for (i = 0; i < 3; ++i)
8936 if (STRCMP(action, av[i]) == 0)
8937 break;
8938 if (i == 3)
8939 {
8940 EMSGN(_(e_invarg2), action);
8941 return;
8942 }
8943 }
8944 else
8945 action = (char_u *)"force";
8946
8947 /* Go over all entries in the second dict and add them to the
8948 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008949 todo = d2->dv_hashtab.ht_used;
8950 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008952 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008954 --todo;
8955 di1 = dict_find(d1, hi2->hi_key, -1);
8956 if (di1 == NULL)
8957 {
8958 di1 = dictitem_copy(HI2DI(hi2));
8959 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8960 dictitem_free(di1);
8961 }
8962 else if (*action == 'e')
8963 {
8964 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8965 break;
8966 }
8967 else if (*action == 'f')
8968 {
8969 clear_tv(&di1->di_tv);
8970 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8971 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008972 }
8973 }
8974
Bram Moolenaare9a41262005-01-15 22:18:47 +00008975 copy_tv(&argvars[0], rettv);
8976 }
8977 }
8978 else
8979 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008980}
8981
8982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 * "filereadable()" function
8984 */
8985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 FILE *fd;
8991 char_u *p;
8992 int n;
8993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8996 {
8997 n = TRUE;
8998 fclose(fd);
8999 }
9000 else
9001 n = FALSE;
9002
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009003 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004}
9005
9006/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009007 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 * rights to write into.
9009 */
9010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *argvars;
9013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009015 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016}
9017
Bram Moolenaar33570922005-01-25 22:26:29 +00009018static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009019
9020 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009021findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009022 typval_T *argvars;
9023 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009024 int dir;
9025{
9026#ifdef FEAT_SEARCHPATH
9027 char_u *fname;
9028 char_u *fresult = NULL;
9029 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9030 char_u *p;
9031 char_u pathbuf[NUMBUFLEN];
9032 int count = 1;
9033 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009034 int error = FALSE;
9035#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009036
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009037 rettv->vval.v_string = NULL;
9038 rettv->v_type = VAR_STRING;
9039
9040#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009042
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009043 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009045 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9046 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009047 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 else
9049 {
9050 if (*p != NUL)
9051 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009053 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009054 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009056 }
9057
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009058 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9059 error = TRUE;
9060
9061 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009063 do
9064 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009065 if (rettv->v_type == VAR_STRING)
9066 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 fresult = find_file_in_path_option(first ? fname : NULL,
9068 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009069 0, first, path, dir, NULL,
9070 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009071 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009072
9073 if (fresult != NULL && rettv->v_type == VAR_LIST)
9074 list_append_string(rettv->vval.v_list, fresult, -1);
9075
9076 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009078
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009079 if (rettv->v_type == VAR_STRING)
9080 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009081#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009082}
9083
Bram Moolenaar33570922005-01-25 22:26:29 +00009084static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9085static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009086
9087/*
9088 * Implementation of map() and filter().
9089 */
9090 static void
9091filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009092 typval_T *argvars;
9093 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009094 int map;
9095{
9096 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009097 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 listitem_T *li, *nli;
9099 list_T *l = NULL;
9100 dictitem_T *di;
9101 hashtab_T *ht;
9102 hashitem_T *hi;
9103 dict_T *d = NULL;
9104 typval_T save_val;
9105 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009106 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009107 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009108 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009109 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009110
9111 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009112 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009113 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009114 if ((l = argvars[0].vval.v_list) == NULL
9115 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009116 return;
9117 }
9118 else if (argvars[0].v_type == VAR_DICT)
9119 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009120 if ((d = argvars[0].vval.v_dict) == NULL
9121 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009122 return;
9123 }
9124 else
9125 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009126 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009127 return;
9128 }
9129
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130 expr = get_tv_string_buf_chk(&argvars[1], buf);
9131 /* On type errors, the preceding call has already displayed an error
9132 * message. Avoid a misleading error message for an empty string that
9133 * was not passed as argument. */
9134 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 prepare_vimvar(VV_VAL, &save_val);
9137 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009138
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009139 /* We reset "did_emsg" to be able to detect whether an error
9140 * occurred during evaluation of the expression. */
9141 save_did_emsg = did_emsg;
9142 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 prepare_vimvar(VV_KEY, &save_key);
9147 vimvars[VV_KEY].vv_type = VAR_STRING;
9148
9149 ht = &d->dv_hashtab;
9150 hash_lock(ht);
9151 todo = ht->ht_used;
9152 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154 if (!HASHITEM_EMPTY(hi))
9155 {
9156 --todo;
9157 di = HI2DI(hi);
9158 if (tv_check_lock(di->di_tv.v_lock, msg))
9159 break;
9160 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009161 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009162 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009163 break;
9164 if (!map && rem)
9165 dictitem_remove(d, di);
9166 clear_tv(&vimvars[VV_KEY].vv_tv);
9167 }
9168 }
9169 hash_unlock(ht);
9170
9171 restore_vimvar(VV_KEY, &save_key);
9172 }
9173 else
9174 {
9175 for (li = l->lv_first; li != NULL; li = nli)
9176 {
9177 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009178 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009180 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009181 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009182 break;
9183 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009184 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009185 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009186 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009189
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009190 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009191 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009192
9193 copy_tv(&argvars[0], rettv);
9194}
9195
9196 static int
9197filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009198 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009199 char_u *expr;
9200 int map;
9201 int *remp;
9202{
Bram Moolenaar33570922005-01-25 22:26:29 +00009203 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009204 char_u *s;
9205
Bram Moolenaar33570922005-01-25 22:26:29 +00009206 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009207 s = expr;
9208 if (eval1(&s, &rettv, TRUE) == FAIL)
9209 return FAIL;
9210 if (*s != NUL) /* check for trailing chars after expr */
9211 {
9212 EMSG2(_(e_invexpr2), s);
9213 return FAIL;
9214 }
9215 if (map)
9216 {
9217 /* map(): replace the list item value */
9218 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009219 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009220 *tv = rettv;
9221 }
9222 else
9223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009224 int error = FALSE;
9225
Bram Moolenaare9a41262005-01-15 22:18:47 +00009226 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009228 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009229 /* On type error, nothing has been removed; return FAIL to stop the
9230 * loop. The error message was given by get_tv_number_chk(). */
9231 if (error)
9232 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009233 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009234 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009235 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009236}
9237
9238/*
9239 * "filter()" function
9240 */
9241 static void
9242f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009243 typval_T *argvars;
9244 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009245{
9246 filter_map(argvars, rettv, FALSE);
9247}
9248
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009249/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009250 * "finddir({fname}[, {path}[, {count}]])" function
9251 */
9252 static void
9253f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009254 typval_T *argvars;
9255 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009256{
9257 findfilendir(argvars, rettv, TRUE);
9258}
9259
9260/*
9261 * "findfile({fname}[, {path}[, {count}]])" function
9262 */
9263 static void
9264f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009265 typval_T *argvars;
9266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009267{
9268 findfilendir(argvars, rettv, FALSE);
9269}
9270
9271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 * "fnamemodify({fname}, {mods})" function
9273 */
9274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009276 typval_T *argvars;
9277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 char_u *fname;
9280 char_u *mods;
9281 int usedlen = 0;
9282 int len;
9283 char_u *fbuf = NULL;
9284 char_u buf[NUMBUFLEN];
9285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009286 fname = get_tv_string_chk(&argvars[0]);
9287 mods = get_tv_string_buf_chk(&argvars[1], buf);
9288 if (fname == NULL || mods == NULL)
9289 fname = NULL;
9290 else
9291 {
9292 len = (int)STRLEN(fname);
9293 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009296 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009300 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 vim_free(fbuf);
9302}
9303
Bram Moolenaar33570922005-01-25 22:26:29 +00009304static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305
9306/*
9307 * "foldclosed()" function
9308 */
9309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 typval_T *argvars;
9312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 int end;
9314{
9315#ifdef FEAT_FOLDING
9316 linenr_T lnum;
9317 linenr_T first, last;
9318
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9321 {
9322 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9323 {
9324 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 return;
9329 }
9330 }
9331#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333}
9334
9335/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336 * "foldclosed()" function
9337 */
9338 static void
9339f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009340 typval_T *argvars;
9341 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009342{
9343 foldclosed_both(argvars, rettv, FALSE);
9344}
9345
9346/*
9347 * "foldclosedend()" function
9348 */
9349 static void
9350f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009351 typval_T *argvars;
9352 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009353{
9354 foldclosed_both(argvars, rettv, TRUE);
9355}
9356
9357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 * "foldlevel()" function
9359 */
9360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009362 typval_T *argvars;
9363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364{
9365#ifdef FEAT_FOLDING
9366 linenr_T lnum;
9367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 else
9372#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009373 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374}
9375
9376/*
9377 * "foldtext()" function
9378 */
9379/*ARGSUSED*/
9380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 typval_T *argvars;
9383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385#ifdef FEAT_FOLDING
9386 linenr_T lnum;
9387 char_u *s;
9388 char_u *r;
9389 int len;
9390 char *txt;
9391#endif
9392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 rettv->v_type = VAR_STRING;
9394 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009396 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9397 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9398 <= curbuf->b_ml.ml_line_count
9399 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 {
9401 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9403 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 {
9405 if (!linewhite(lnum))
9406 break;
9407 ++lnum;
9408 }
9409
9410 /* Find interesting text in this line. */
9411 s = skipwhite(ml_get(lnum));
9412 /* skip C comment-start */
9413 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009416 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009417 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009418 {
9419 s = skipwhite(ml_get(lnum + 1));
9420 if (*s == '*')
9421 s = skipwhite(s + 1);
9422 }
9423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 txt = _("+-%s%3ld lines: ");
9425 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009426 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 + 20 /* for %3ld */
9428 + STRLEN(s))); /* concatenated */
9429 if (r != NULL)
9430 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009431 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9432 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9433 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 len = (int)STRLEN(r);
9435 STRCAT(r, s);
9436 /* remove 'foldmarker' and 'commentstring' */
9437 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 }
9440 }
9441#endif
9442}
9443
9444/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009445 * "foldtextresult(lnum)" function
9446 */
9447/*ARGSUSED*/
9448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009452{
9453#ifdef FEAT_FOLDING
9454 linenr_T lnum;
9455 char_u *text;
9456 char_u buf[51];
9457 foldinfo_T foldinfo;
9458 int fold_count;
9459#endif
9460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->v_type = VAR_STRING;
9462 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009463#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 /* treat illegal types and illegal string values for {lnum} the same */
9466 if (lnum < 0)
9467 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009468 fold_count = foldedCount(curwin, lnum, &foldinfo);
9469 if (fold_count > 0)
9470 {
9471 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9472 &foldinfo, buf);
9473 if (text == buf)
9474 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009476 }
9477#endif
9478}
9479
9480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 * "foreground()" function
9482 */
9483/*ARGSUSED*/
9484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009486 typval_T *argvars;
9487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490#ifdef FEAT_GUI
9491 if (gui.in_use)
9492 gui_mch_set_foreground();
9493#else
9494# ifdef WIN32
9495 win32_set_foreground();
9496# endif
9497#endif
9498}
9499
9500/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009501 * "function()" function
9502 */
9503/*ARGSUSED*/
9504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009505f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009506 typval_T *argvars;
9507 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009508{
9509 char_u *s;
9510
Bram Moolenaara7043832005-01-21 11:56:39 +00009511 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009513 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009514 EMSG2(_(e_invarg2), s);
9515 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009516 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 else
9518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_string = vim_strsave(s);
9520 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 }
9522}
9523
9524/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009525 * "garbagecollect()" function
9526 */
9527/*ARGSUSED*/
9528 static void
9529f_garbagecollect(argvars, rettv)
9530 typval_T *argvars;
9531 typval_T *rettv;
9532{
9533 garbage_collect();
9534}
9535
9536/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009537 * "get()" function
9538 */
9539 static void
9540f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009541 typval_T *argvars;
9542 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009543{
Bram Moolenaar33570922005-01-25 22:26:29 +00009544 listitem_T *li;
9545 list_T *l;
9546 dictitem_T *di;
9547 dict_T *d;
9548 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009549
Bram Moolenaare9a41262005-01-15 22:18:47 +00009550 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009551 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009552 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 int error = FALSE;
9555
9556 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9557 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009558 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009561 else if (argvars[0].v_type == VAR_DICT)
9562 {
9563 if ((d = argvars[0].vval.v_dict) != NULL)
9564 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009565 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009566 if (di != NULL)
9567 tv = &di->di_tv;
9568 }
9569 }
9570 else
9571 EMSG2(_(e_listdictarg), "get()");
9572
9573 if (tv == NULL)
9574 {
9575 if (argvars[2].v_type == VAR_UNKNOWN)
9576 rettv->vval.v_number = 0;
9577 else
9578 copy_tv(&argvars[2], rettv);
9579 }
9580 else
9581 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009582}
9583
Bram Moolenaar342337a2005-07-21 21:11:17 +00009584static 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 +00009585
9586/*
9587 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009588 * Return a range (from start to end) of lines in rettv from the specified
9589 * buffer.
9590 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009591 */
9592 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009593get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009594 buf_T *buf;
9595 linenr_T start;
9596 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009597 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009598 typval_T *rettv;
9599{
9600 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009601
Bram Moolenaar342337a2005-07-21 21:11:17 +00009602 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009603 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009604 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009605 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009606 }
9607 else
9608 rettv->vval.v_number = 0;
9609
9610 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9611 return;
9612
9613 if (!retlist)
9614 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009615 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9616 p = ml_get_buf(buf, start, FALSE);
9617 else
9618 p = (char_u *)"";
9619
9620 rettv->v_type = VAR_STRING;
9621 rettv->vval.v_string = vim_strsave(p);
9622 }
9623 else
9624 {
9625 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009626 return;
9627
9628 if (start < 1)
9629 start = 1;
9630 if (end > buf->b_ml.ml_line_count)
9631 end = buf->b_ml.ml_line_count;
9632 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009633 if (list_append_string(rettv->vval.v_list,
9634 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009635 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009636 }
9637}
9638
9639/*
9640 * "getbufline()" function
9641 */
9642 static void
9643f_getbufline(argvars, rettv)
9644 typval_T *argvars;
9645 typval_T *rettv;
9646{
9647 linenr_T lnum;
9648 linenr_T end;
9649 buf_T *buf;
9650
9651 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9652 ++emsg_off;
9653 buf = get_buf_tv(&argvars[0]);
9654 --emsg_off;
9655
Bram Moolenaar661b1822005-07-28 22:36:45 +00009656 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009657 if (argvars[2].v_type == VAR_UNKNOWN)
9658 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009659 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009660 end = get_tv_lnum_buf(&argvars[2], buf);
9661
Bram Moolenaar342337a2005-07-21 21:11:17 +00009662 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009663}
9664
Bram Moolenaar0d660222005-01-07 21:51:51 +00009665/*
9666 * "getbufvar()" function
9667 */
9668 static void
9669f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009672{
9673 buf_T *buf;
9674 buf_T *save_curbuf;
9675 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009676 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009678 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9679 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009680 ++emsg_off;
9681 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009682
9683 rettv->v_type = VAR_STRING;
9684 rettv->vval.v_string = NULL;
9685
9686 if (buf != NULL && varname != NULL)
9687 {
9688 if (*varname == '&') /* buffer-local-option */
9689 {
9690 /* set curbuf to be our buf, temporarily */
9691 save_curbuf = curbuf;
9692 curbuf = buf;
9693
9694 get_option_tv(&varname, rettv, TRUE);
9695
9696 /* restore previous notion of curbuf */
9697 curbuf = save_curbuf;
9698 }
9699 else
9700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009701 if (*varname == NUL)
9702 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9703 * scope prefix before the NUL byte is required by
9704 * find_var_in_ht(). */
9705 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009706 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009707 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009708 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009710 }
9711 }
9712
9713 --emsg_off;
9714}
9715
9716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 * "getchar()" function
9718 */
9719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 typval_T *argvars;
9722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009725 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
9727 ++no_mapping;
9728 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009729 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 /* getchar(): blocking wait. */
9731 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009732 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 /* getchar(1): only check if char avail */
9734 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009735 else if (error || vpeekc() == NUL)
9736 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 n = 0;
9738 else
9739 /* getchar(0) and char avail: return char */
9740 n = safe_vgetc();
9741 --no_mapping;
9742 --allow_keys;
9743
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (IS_SPECIAL(n) || mod_mask != 0)
9746 {
9747 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9748 int i = 0;
9749
9750 /* Turn a special key into three bytes, plus modifier. */
9751 if (mod_mask != 0)
9752 {
9753 temp[i++] = K_SPECIAL;
9754 temp[i++] = KS_MODIFIER;
9755 temp[i++] = mod_mask;
9756 }
9757 if (IS_SPECIAL(n))
9758 {
9759 temp[i++] = K_SPECIAL;
9760 temp[i++] = K_SECOND(n);
9761 temp[i++] = K_THIRD(n);
9762 }
9763#ifdef FEAT_MBYTE
9764 else if (has_mbyte)
9765 i += (*mb_char2bytes)(n, temp + i);
9766#endif
9767 else
9768 temp[i++] = n;
9769 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770 rettv->v_type = VAR_STRING;
9771 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 }
9773}
9774
9775/*
9776 * "getcharmod()" function
9777 */
9778/*ARGSUSED*/
9779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785}
9786
9787/*
9788 * "getcmdline()" function
9789 */
9790/*ARGSUSED*/
9791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->v_type = VAR_STRING;
9797 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800/*
9801 * "getcmdpos()" function
9802 */
9803/*ARGSUSED*/
9804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009806 typval_T *argvars;
9807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810}
9811
9812/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009813 * "getcmdtype()" function
9814 */
9815/*ARGSUSED*/
9816 static void
9817f_getcmdtype(argvars, rettv)
9818 typval_T *argvars;
9819 typval_T *rettv;
9820{
9821 rettv->v_type = VAR_STRING;
9822 rettv->vval.v_string = alloc(2);
9823 if (rettv->vval.v_string != NULL)
9824 {
9825 rettv->vval.v_string[0] = get_cmdline_type();
9826 rettv->vval.v_string[1] = NUL;
9827 }
9828}
9829
9830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 * "getcwd()" function
9832 */
9833/*ARGSUSED*/
9834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835f_getcwd(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{
9839 char_u cwd[MAXPATHL];
9840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 else
9845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009848 if (rettv->vval.v_string != NULL)
9849 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850#endif
9851 }
9852}
9853
9854/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009855 * "getfontname()" function
9856 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009857/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009860 typval_T *argvars;
9861 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863 rettv->v_type = VAR_STRING;
9864 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009865#ifdef FEAT_GUI
9866 if (gui.in_use)
9867 {
9868 GuiFont font;
9869 char_u *name = NULL;
9870
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009871 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009872 {
9873 /* Get the "Normal" font. Either the name saved by
9874 * hl_set_font_name() or from the font ID. */
9875 font = gui.norm_font;
9876 name = hl_get_font_name();
9877 }
9878 else
9879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009881 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9882 return;
9883 font = gui_mch_get_font(name, FALSE);
9884 if (font == NOFONT)
9885 return; /* Invalid font name, return empty string. */
9886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009888 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009889 gui_mch_free_font(font);
9890 }
9891#endif
9892}
9893
9894/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009895 * "getfperm({fname})" function
9896 */
9897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009899 typval_T *argvars;
9900 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009901{
9902 char_u *fname;
9903 struct stat st;
9904 char_u *perm = NULL;
9905 char_u flags[] = "rwx";
9906 int i;
9907
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009909
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009911 if (mch_stat((char *)fname, &st) >= 0)
9912 {
9913 perm = vim_strsave((char_u *)"---------");
9914 if (perm != NULL)
9915 {
9916 for (i = 0; i < 9; i++)
9917 {
9918 if (st.st_mode & (1 << (8 - i)))
9919 perm[i] = flags[i % 3];
9920 }
9921 }
9922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009924}
9925
9926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 * "getfsize({fname})" function
9928 */
9929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 typval_T *argvars;
9932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933{
9934 char_u *fname;
9935 struct stat st;
9936
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
9941 if (mch_stat((char *)fname, &st) >= 0)
9942 {
9943 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 }
9948 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950}
9951
9952/*
9953 * "getftime({fname})" function
9954 */
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960 char_u *fname;
9961 struct stat st;
9962
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009963 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
9965 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969}
9970
9971/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009972 * "getftype({fname})" function
9973 */
9974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *argvars;
9977 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009978{
9979 char_u *fname;
9980 struct stat st;
9981 char_u *type = NULL;
9982 char *t;
9983
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009985
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009987 if (mch_lstat((char *)fname, &st) >= 0)
9988 {
9989#ifdef S_ISREG
9990 if (S_ISREG(st.st_mode))
9991 t = "file";
9992 else if (S_ISDIR(st.st_mode))
9993 t = "dir";
9994# ifdef S_ISLNK
9995 else if (S_ISLNK(st.st_mode))
9996 t = "link";
9997# endif
9998# ifdef S_ISBLK
9999 else if (S_ISBLK(st.st_mode))
10000 t = "bdev";
10001# endif
10002# ifdef S_ISCHR
10003 else if (S_ISCHR(st.st_mode))
10004 t = "cdev";
10005# endif
10006# ifdef S_ISFIFO
10007 else if (S_ISFIFO(st.st_mode))
10008 t = "fifo";
10009# endif
10010# ifdef S_ISSOCK
10011 else if (S_ISSOCK(st.st_mode))
10012 t = "fifo";
10013# endif
10014 else
10015 t = "other";
10016#else
10017# ifdef S_IFMT
10018 switch (st.st_mode & S_IFMT)
10019 {
10020 case S_IFREG: t = "file"; break;
10021 case S_IFDIR: t = "dir"; break;
10022# ifdef S_IFLNK
10023 case S_IFLNK: t = "link"; break;
10024# endif
10025# ifdef S_IFBLK
10026 case S_IFBLK: t = "bdev"; break;
10027# endif
10028# ifdef S_IFCHR
10029 case S_IFCHR: t = "cdev"; break;
10030# endif
10031# ifdef S_IFIFO
10032 case S_IFIFO: t = "fifo"; break;
10033# endif
10034# ifdef S_IFSOCK
10035 case S_IFSOCK: t = "socket"; break;
10036# endif
10037 default: t = "other";
10038 }
10039# else
10040 if (mch_isdir(fname))
10041 t = "dir";
10042 else
10043 t = "file";
10044# endif
10045#endif
10046 type = vim_strsave((char_u *)t);
10047 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010049}
10050
10051/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010052 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010053 */
10054 static void
10055f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010056 typval_T *argvars;
10057 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010058{
10059 linenr_T lnum;
10060 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010061 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010062
10063 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010064 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010065 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010066 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010067 retlist = FALSE;
10068 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010069 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010070 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010071 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010072 retlist = TRUE;
10073 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010074
Bram Moolenaar342337a2005-07-21 21:11:17 +000010075 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010076}
10077
10078/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010079 * "getpos(string)" function
10080 */
10081 static void
10082f_getpos(argvars, rettv)
10083 typval_T *argvars;
10084 typval_T *rettv;
10085{
10086 pos_T *fp;
10087 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010088 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010089
10090 if (rettv_list_alloc(rettv) == OK)
10091 {
10092 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010093 fp = var2fpos(&argvars[0], TRUE, &fnum);
10094 if (fnum != -1)
10095 list_append_number(l, (varnumber_T)fnum);
10096 else
10097 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010098 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10099 : (varnumber_T)0);
10100 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10101 : (varnumber_T)0);
10102 list_append_number(l,
10103#ifdef FEAT_VIRTUALEDIT
10104 (fp != NULL) ? (varnumber_T)fp->coladd :
10105#endif
10106 (varnumber_T)0);
10107 }
10108 else
10109 rettv->vval.v_number = FALSE;
10110}
10111
10112/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010113 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010114 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010115/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010116 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010117f_getqflist(argvars, rettv)
10118 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010119 typval_T *rettv;
10120{
10121#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010122 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010123#endif
10124
10125 rettv->vval.v_number = FALSE;
10126#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010127 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010128 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010129 wp = NULL;
10130 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10131 {
10132 wp = find_win_by_nr(&argvars[0]);
10133 if (wp == NULL)
10134 return;
10135 }
10136
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010137 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010138 }
10139#endif
10140}
10141
10142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 * "getreg()" function
10144 */
10145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010147 typval_T *argvars;
10148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149{
10150 char_u *strregname;
10151 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010152 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010155 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 strregname = get_tv_string_chk(&argvars[0]);
10158 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010159 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010163 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 regname = (strregname == NULL ? '"' : *strregname);
10165 if (regname == 0)
10166 regname = '"';
10167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010169 rettv->vval.v_string = error ? NULL :
10170 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171}
10172
10173/*
10174 * "getregtype()" function
10175 */
10176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010178 typval_T *argvars;
10179 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
10181 char_u *strregname;
10182 int regname;
10183 char_u buf[NUMBUFLEN + 2];
10184 long reglen = 0;
10185
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010186 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 {
10188 strregname = get_tv_string_chk(&argvars[0]);
10189 if (strregname == NULL) /* type error; errmsg already given */
10190 {
10191 rettv->v_type = VAR_STRING;
10192 rettv->vval.v_string = NULL;
10193 return;
10194 }
10195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 else
10197 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199
10200 regname = (strregname == NULL ? '"' : *strregname);
10201 if (regname == 0)
10202 regname = '"';
10203
10204 buf[0] = NUL;
10205 buf[1] = NUL;
10206 switch (get_reg_type(regname, &reglen))
10207 {
10208 case MLINE: buf[0] = 'V'; break;
10209 case MCHAR: buf[0] = 'v'; break;
10210#ifdef FEAT_VISUAL
10211 case MBLOCK:
10212 buf[0] = Ctrl_V;
10213 sprintf((char *)buf + 1, "%ld", reglen + 1);
10214 break;
10215#endif
10216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 rettv->v_type = VAR_STRING;
10218 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219}
10220
10221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 * "getwinposx()" function
10223 */
10224/*ARGSUSED*/
10225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010227 typval_T *argvars;
10228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#ifdef FEAT_GUI
10232 if (gui.in_use)
10233 {
10234 int x, y;
10235
10236 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 }
10239#endif
10240}
10241
10242/*
10243 * "getwinposy()" function
10244 */
10245/*ARGSUSED*/
10246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010248 typval_T *argvars;
10249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252#ifdef FEAT_GUI
10253 if (gui.in_use)
10254 {
10255 int x, y;
10256
10257 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 }
10260#endif
10261}
10262
Bram Moolenaara40058a2005-07-11 22:42:07 +000010263 static win_T *
10264find_win_by_nr(vp)
10265 typval_T *vp;
10266{
10267#ifdef FEAT_WINDOWS
10268 win_T *wp;
10269#endif
10270 int nr;
10271
10272 nr = get_tv_number_chk(vp, NULL);
10273
10274#ifdef FEAT_WINDOWS
10275 if (nr < 0)
10276 return NULL;
10277 if (nr == 0)
10278 return curwin;
10279
10280 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10281 if (--nr <= 0)
10282 break;
10283 return wp;
10284#else
10285 if (nr == 0 || nr == 1)
10286 return curwin;
10287 return NULL;
10288#endif
10289}
10290
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291/*
10292 * "getwinvar()" function
10293 */
10294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010296 typval_T *argvars;
10297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299 win_T *win, *oldcurwin;
10300 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 varname = get_tv_string_chk(&argvars[1]);
10305 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010307 rettv->v_type = VAR_STRING;
10308 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309
10310 if (win != NULL && varname != NULL)
10311 {
10312 if (*varname == '&') /* window-local-option */
10313 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010314 /* Set curwin to be our win, temporarily. Also set curbuf, so
10315 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 oldcurwin = curwin;
10317 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010318 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321
10322 /* restore previous notion of curwin */
10323 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010324 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 }
10326 else
10327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 if (*varname == NUL)
10329 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10330 * scope prefix before the NUL byte is required by
10331 * find_var_in_ht(). */
10332 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010334 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 }
10338 }
10339
10340 --emsg_off;
10341}
10342
10343/*
10344 * "glob()" function
10345 */
10346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010348 typval_T *argvars;
10349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350{
10351 expand_T xpc;
10352
10353 ExpandInit(&xpc);
10354 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 rettv->v_type = VAR_STRING;
10356 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10358 ExpandCleanup(&xpc);
10359}
10360
10361/*
10362 * "globpath()" function
10363 */
10364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010365f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010366 typval_T *argvars;
10367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368{
10369 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010370 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010372 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373 if (file == NULL)
10374 rettv->vval.v_string = NULL;
10375 else
10376 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377}
10378
10379/*
10380 * "has()" function
10381 */
10382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010384 typval_T *argvars;
10385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386{
10387 int i;
10388 char_u *name;
10389 int n = FALSE;
10390 static char *(has_list[]) =
10391 {
10392#ifdef AMIGA
10393 "amiga",
10394# ifdef FEAT_ARP
10395 "arp",
10396# endif
10397#endif
10398#ifdef __BEOS__
10399 "beos",
10400#endif
10401#ifdef MSDOS
10402# ifdef DJGPP
10403 "dos32",
10404# else
10405 "dos16",
10406# endif
10407#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010408#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 "mac",
10410#endif
10411#if defined(MACOS_X_UNIX)
10412 "macunix",
10413#endif
10414#ifdef OS2
10415 "os2",
10416#endif
10417#ifdef __QNX__
10418 "qnx",
10419#endif
10420#ifdef RISCOS
10421 "riscos",
10422#endif
10423#ifdef UNIX
10424 "unix",
10425#endif
10426#ifdef VMS
10427 "vms",
10428#endif
10429#ifdef WIN16
10430 "win16",
10431#endif
10432#ifdef WIN32
10433 "win32",
10434#endif
10435#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10436 "win32unix",
10437#endif
10438#ifdef WIN64
10439 "win64",
10440#endif
10441#ifdef EBCDIC
10442 "ebcdic",
10443#endif
10444#ifndef CASE_INSENSITIVE_FILENAME
10445 "fname_case",
10446#endif
10447#ifdef FEAT_ARABIC
10448 "arabic",
10449#endif
10450#ifdef FEAT_AUTOCMD
10451 "autocmd",
10452#endif
10453#ifdef FEAT_BEVAL
10454 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010455# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10456 "balloon_multiline",
10457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458#endif
10459#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10460 "builtin_terms",
10461# ifdef ALL_BUILTIN_TCAPS
10462 "all_builtin_terms",
10463# endif
10464#endif
10465#ifdef FEAT_BYTEOFF
10466 "byte_offset",
10467#endif
10468#ifdef FEAT_CINDENT
10469 "cindent",
10470#endif
10471#ifdef FEAT_CLIENTSERVER
10472 "clientserver",
10473#endif
10474#ifdef FEAT_CLIPBOARD
10475 "clipboard",
10476#endif
10477#ifdef FEAT_CMDL_COMPL
10478 "cmdline_compl",
10479#endif
10480#ifdef FEAT_CMDHIST
10481 "cmdline_hist",
10482#endif
10483#ifdef FEAT_COMMENTS
10484 "comments",
10485#endif
10486#ifdef FEAT_CRYPT
10487 "cryptv",
10488#endif
10489#ifdef FEAT_CSCOPE
10490 "cscope",
10491#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010492#ifdef CURSOR_SHAPE
10493 "cursorshape",
10494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495#ifdef DEBUG
10496 "debug",
10497#endif
10498#ifdef FEAT_CON_DIALOG
10499 "dialog_con",
10500#endif
10501#ifdef FEAT_GUI_DIALOG
10502 "dialog_gui",
10503#endif
10504#ifdef FEAT_DIFF
10505 "diff",
10506#endif
10507#ifdef FEAT_DIGRAPHS
10508 "digraphs",
10509#endif
10510#ifdef FEAT_DND
10511 "dnd",
10512#endif
10513#ifdef FEAT_EMACS_TAGS
10514 "emacs_tags",
10515#endif
10516 "eval", /* always present, of course! */
10517#ifdef FEAT_EX_EXTRA
10518 "ex_extra",
10519#endif
10520#ifdef FEAT_SEARCH_EXTRA
10521 "extra_search",
10522#endif
10523#ifdef FEAT_FKMAP
10524 "farsi",
10525#endif
10526#ifdef FEAT_SEARCHPATH
10527 "file_in_path",
10528#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010529#if defined(UNIX) && !defined(USE_SYSTEM)
10530 "filterpipe",
10531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532#ifdef FEAT_FIND_ID
10533 "find_in_path",
10534#endif
10535#ifdef FEAT_FOLDING
10536 "folding",
10537#endif
10538#ifdef FEAT_FOOTER
10539 "footer",
10540#endif
10541#if !defined(USE_SYSTEM) && defined(UNIX)
10542 "fork",
10543#endif
10544#ifdef FEAT_GETTEXT
10545 "gettext",
10546#endif
10547#ifdef FEAT_GUI
10548 "gui",
10549#endif
10550#ifdef FEAT_GUI_ATHENA
10551# ifdef FEAT_GUI_NEXTAW
10552 "gui_neXtaw",
10553# else
10554 "gui_athena",
10555# endif
10556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557#ifdef FEAT_GUI_GTK
10558 "gui_gtk",
10559# ifdef HAVE_GTK2
10560 "gui_gtk2",
10561# endif
10562#endif
10563#ifdef FEAT_GUI_MAC
10564 "gui_mac",
10565#endif
10566#ifdef FEAT_GUI_MOTIF
10567 "gui_motif",
10568#endif
10569#ifdef FEAT_GUI_PHOTON
10570 "gui_photon",
10571#endif
10572#ifdef FEAT_GUI_W16
10573 "gui_win16",
10574#endif
10575#ifdef FEAT_GUI_W32
10576 "gui_win32",
10577#endif
10578#ifdef FEAT_HANGULIN
10579 "hangul_input",
10580#endif
10581#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10582 "iconv",
10583#endif
10584#ifdef FEAT_INS_EXPAND
10585 "insert_expand",
10586#endif
10587#ifdef FEAT_JUMPLIST
10588 "jumplist",
10589#endif
10590#ifdef FEAT_KEYMAP
10591 "keymap",
10592#endif
10593#ifdef FEAT_LANGMAP
10594 "langmap",
10595#endif
10596#ifdef FEAT_LIBCALL
10597 "libcall",
10598#endif
10599#ifdef FEAT_LINEBREAK
10600 "linebreak",
10601#endif
10602#ifdef FEAT_LISP
10603 "lispindent",
10604#endif
10605#ifdef FEAT_LISTCMDS
10606 "listcmds",
10607#endif
10608#ifdef FEAT_LOCALMAP
10609 "localmap",
10610#endif
10611#ifdef FEAT_MENU
10612 "menu",
10613#endif
10614#ifdef FEAT_SESSION
10615 "mksession",
10616#endif
10617#ifdef FEAT_MODIFY_FNAME
10618 "modify_fname",
10619#endif
10620#ifdef FEAT_MOUSE
10621 "mouse",
10622#endif
10623#ifdef FEAT_MOUSESHAPE
10624 "mouseshape",
10625#endif
10626#if defined(UNIX) || defined(VMS)
10627# ifdef FEAT_MOUSE_DEC
10628 "mouse_dec",
10629# endif
10630# ifdef FEAT_MOUSE_GPM
10631 "mouse_gpm",
10632# endif
10633# ifdef FEAT_MOUSE_JSB
10634 "mouse_jsbterm",
10635# endif
10636# ifdef FEAT_MOUSE_NET
10637 "mouse_netterm",
10638# endif
10639# ifdef FEAT_MOUSE_PTERM
10640 "mouse_pterm",
10641# endif
10642# ifdef FEAT_MOUSE_XTERM
10643 "mouse_xterm",
10644# endif
10645#endif
10646#ifdef FEAT_MBYTE
10647 "multi_byte",
10648#endif
10649#ifdef FEAT_MBYTE_IME
10650 "multi_byte_ime",
10651#endif
10652#ifdef FEAT_MULTI_LANG
10653 "multi_lang",
10654#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010655#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010656#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010657 "mzscheme",
10658#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660#ifdef FEAT_OLE
10661 "ole",
10662#endif
10663#ifdef FEAT_OSFILETYPE
10664 "osfiletype",
10665#endif
10666#ifdef FEAT_PATH_EXTRA
10667 "path_extra",
10668#endif
10669#ifdef FEAT_PERL
10670#ifndef DYNAMIC_PERL
10671 "perl",
10672#endif
10673#endif
10674#ifdef FEAT_PYTHON
10675#ifndef DYNAMIC_PYTHON
10676 "python",
10677#endif
10678#endif
10679#ifdef FEAT_POSTSCRIPT
10680 "postscript",
10681#endif
10682#ifdef FEAT_PRINTER
10683 "printer",
10684#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010685#ifdef FEAT_PROFILE
10686 "profile",
10687#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010688#ifdef FEAT_RELTIME
10689 "reltime",
10690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691#ifdef FEAT_QUICKFIX
10692 "quickfix",
10693#endif
10694#ifdef FEAT_RIGHTLEFT
10695 "rightleft",
10696#endif
10697#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10698 "ruby",
10699#endif
10700#ifdef FEAT_SCROLLBIND
10701 "scrollbind",
10702#endif
10703#ifdef FEAT_CMDL_INFO
10704 "showcmd",
10705 "cmdline_info",
10706#endif
10707#ifdef FEAT_SIGNS
10708 "signs",
10709#endif
10710#ifdef FEAT_SMARTINDENT
10711 "smartindent",
10712#endif
10713#ifdef FEAT_SNIFF
10714 "sniff",
10715#endif
10716#ifdef FEAT_STL_OPT
10717 "statusline",
10718#endif
10719#ifdef FEAT_SUN_WORKSHOP
10720 "sun_workshop",
10721#endif
10722#ifdef FEAT_NETBEANS_INTG
10723 "netbeans_intg",
10724#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010725#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010726 "spell",
10727#endif
10728#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 "syntax",
10730#endif
10731#if defined(USE_SYSTEM) || !defined(UNIX)
10732 "system",
10733#endif
10734#ifdef FEAT_TAG_BINS
10735 "tag_binary",
10736#endif
10737#ifdef FEAT_TAG_OLDSTATIC
10738 "tag_old_static",
10739#endif
10740#ifdef FEAT_TAG_ANYWHITE
10741 "tag_any_white",
10742#endif
10743#ifdef FEAT_TCL
10744# ifndef DYNAMIC_TCL
10745 "tcl",
10746# endif
10747#endif
10748#ifdef TERMINFO
10749 "terminfo",
10750#endif
10751#ifdef FEAT_TERMRESPONSE
10752 "termresponse",
10753#endif
10754#ifdef FEAT_TEXTOBJ
10755 "textobjects",
10756#endif
10757#ifdef HAVE_TGETENT
10758 "tgetent",
10759#endif
10760#ifdef FEAT_TITLE
10761 "title",
10762#endif
10763#ifdef FEAT_TOOLBAR
10764 "toolbar",
10765#endif
10766#ifdef FEAT_USR_CMDS
10767 "user-commands", /* was accidentally included in 5.4 */
10768 "user_commands",
10769#endif
10770#ifdef FEAT_VIMINFO
10771 "viminfo",
10772#endif
10773#ifdef FEAT_VERTSPLIT
10774 "vertsplit",
10775#endif
10776#ifdef FEAT_VIRTUALEDIT
10777 "virtualedit",
10778#endif
10779#ifdef FEAT_VISUAL
10780 "visual",
10781#endif
10782#ifdef FEAT_VISUALEXTRA
10783 "visualextra",
10784#endif
10785#ifdef FEAT_VREPLACE
10786 "vreplace",
10787#endif
10788#ifdef FEAT_WILDIGN
10789 "wildignore",
10790#endif
10791#ifdef FEAT_WILDMENU
10792 "wildmenu",
10793#endif
10794#ifdef FEAT_WINDOWS
10795 "windows",
10796#endif
10797#ifdef FEAT_WAK
10798 "winaltkeys",
10799#endif
10800#ifdef FEAT_WRITEBACKUP
10801 "writebackup",
10802#endif
10803#ifdef FEAT_XIM
10804 "xim",
10805#endif
10806#ifdef FEAT_XFONTSET
10807 "xfontset",
10808#endif
10809#ifdef USE_XSMP
10810 "xsmp",
10811#endif
10812#ifdef USE_XSMP_INTERACT
10813 "xsmp_interact",
10814#endif
10815#ifdef FEAT_XCLIPBOARD
10816 "xterm_clipboard",
10817#endif
10818#ifdef FEAT_XTERM_SAVE
10819 "xterm_save",
10820#endif
10821#if defined(UNIX) && defined(FEAT_X11)
10822 "X11",
10823#endif
10824 NULL
10825 };
10826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 for (i = 0; has_list[i] != NULL; ++i)
10829 if (STRICMP(name, has_list[i]) == 0)
10830 {
10831 n = TRUE;
10832 break;
10833 }
10834
10835 if (n == FALSE)
10836 {
10837 if (STRNICMP(name, "patch", 5) == 0)
10838 n = has_patch(atoi((char *)name + 5));
10839 else if (STRICMP(name, "vim_starting") == 0)
10840 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010841#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10842 else if (STRICMP(name, "balloon_multiline") == 0)
10843 n = multiline_balloon_available();
10844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845#ifdef DYNAMIC_TCL
10846 else if (STRICMP(name, "tcl") == 0)
10847 n = tcl_enabled(FALSE);
10848#endif
10849#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10850 else if (STRICMP(name, "iconv") == 0)
10851 n = iconv_enabled(FALSE);
10852#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010853#ifdef DYNAMIC_MZSCHEME
10854 else if (STRICMP(name, "mzscheme") == 0)
10855 n = mzscheme_enabled(FALSE);
10856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857#ifdef DYNAMIC_RUBY
10858 else if (STRICMP(name, "ruby") == 0)
10859 n = ruby_enabled(FALSE);
10860#endif
10861#ifdef DYNAMIC_PYTHON
10862 else if (STRICMP(name, "python") == 0)
10863 n = python_enabled(FALSE);
10864#endif
10865#ifdef DYNAMIC_PERL
10866 else if (STRICMP(name, "perl") == 0)
10867 n = perl_enabled(FALSE);
10868#endif
10869#ifdef FEAT_GUI
10870 else if (STRICMP(name, "gui_running") == 0)
10871 n = (gui.in_use || gui.starting);
10872# ifdef FEAT_GUI_W32
10873 else if (STRICMP(name, "gui_win32s") == 0)
10874 n = gui_is_win32s();
10875# endif
10876# ifdef FEAT_BROWSE
10877 else if (STRICMP(name, "browse") == 0)
10878 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10879# endif
10880#endif
10881#ifdef FEAT_SYN_HL
10882 else if (STRICMP(name, "syntax_items") == 0)
10883 n = syntax_present(curbuf);
10884#endif
10885#if defined(WIN3264)
10886 else if (STRICMP(name, "win95") == 0)
10887 n = mch_windows95();
10888#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010889#ifdef FEAT_NETBEANS_INTG
10890 else if (STRICMP(name, "netbeans_enabled") == 0)
10891 n = usingNetbeans;
10892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 }
10894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896}
10897
10898/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010899 * "has_key()" function
10900 */
10901 static void
10902f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *argvars;
10904 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905{
10906 rettv->vval.v_number = 0;
10907 if (argvars[0].v_type != VAR_DICT)
10908 {
10909 EMSG(_(e_dictreq));
10910 return;
10911 }
10912 if (argvars[0].vval.v_dict == NULL)
10913 return;
10914
10915 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010916 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010917}
10918
10919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 * "hasmapto()" function
10921 */
10922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *argvars;
10925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
10927 char_u *name;
10928 char_u *mode;
10929 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010930 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010933 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 mode = (char_u *)"nvo";
10935 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010938 if (argvars[2].v_type != VAR_UNKNOWN)
10939 abbr = get_tv_number(&argvars[2]);
10940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941
Bram Moolenaar2c932302006-03-18 21:42:09 +000010942 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010945 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946}
10947
10948/*
10949 * "histadd()" function
10950 */
10951/*ARGSUSED*/
10952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010954 typval_T *argvars;
10955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956{
10957#ifdef FEAT_CMDHIST
10958 int histype;
10959 char_u *str;
10960 char_u buf[NUMBUFLEN];
10961#endif
10962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 if (check_restricted() || check_secure())
10965 return;
10966#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10968 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 if (histype >= 0)
10970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 if (*str != NUL)
10973 {
10974 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 return;
10977 }
10978 }
10979#endif
10980}
10981
10982/*
10983 * "histdel()" function
10984 */
10985/*ARGSUSED*/
10986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
10991#ifdef FEAT_CMDHIST
10992 int n;
10993 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010994 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10997 if (str == NULL)
10998 n = 0;
10999 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011001 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011002 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011004 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 else
11007 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 get_tv_string_buf(&argvars[1], buf));
11010 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013#endif
11014}
11015
11016/*
11017 * "histget()" function
11018 */
11019/*ARGSUSED*/
11020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011022 typval_T *argvars;
11023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024{
11025#ifdef FEAT_CMDHIST
11026 int type;
11027 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011030 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11031 if (str == NULL)
11032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 {
11035 type = get_histtype(str);
11036 if (argvars[1].v_type == VAR_UNKNOWN)
11037 idx = get_history_idx(type);
11038 else
11039 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11040 /* -1 on type error */
11041 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047}
11048
11049/*
11050 * "histnr()" function
11051 */
11052/*ARGSUSED*/
11053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
11058 int i;
11059
11060#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 char_u *history = get_tv_string_chk(&argvars[0]);
11062
11063 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 if (i >= HIST_CMD && i < HIST_COUNT)
11065 i = get_history_idx(i);
11066 else
11067#endif
11068 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070}
11071
11072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 * "highlightID(name)" function
11074 */
11075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076f_hlID(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081}
11082
11083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084 * "highlight_exists()" function
11085 */
11086 static void
11087f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011088 typval_T *argvars;
11089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011090{
11091 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11092}
11093
11094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 * "hostname()" function
11096 */
11097/*ARGSUSED*/
11098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011100 typval_T *argvars;
11101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102{
11103 char_u hostname[256];
11104
11105 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 rettv->v_type = VAR_STRING;
11107 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108}
11109
11110/*
11111 * iconv() function
11112 */
11113/*ARGSUSED*/
11114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118{
11119#ifdef FEAT_MBYTE
11120 char_u buf1[NUMBUFLEN];
11121 char_u buf2[NUMBUFLEN];
11122 char_u *from, *to, *str;
11123 vimconv_T vimconv;
11124#endif
11125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->v_type = VAR_STRING;
11127 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
11129#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 str = get_tv_string(&argvars[0]);
11131 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11132 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 vimconv.vc_type = CONV_NONE;
11134 convert_setup(&vimconv, from, to);
11135
11136 /* If the encodings are equal, no conversion needed. */
11137 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
11142 convert_setup(&vimconv, NULL, NULL);
11143 vim_free(from);
11144 vim_free(to);
11145#endif
11146}
11147
11148/*
11149 * "indent()" function
11150 */
11151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011153 typval_T *argvars;
11154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155{
11156 linenr_T lnum;
11157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163}
11164
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011165/*
11166 * "index()" function
11167 */
11168 static void
11169f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011170 typval_T *argvars;
11171 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011172{
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 list_T *l;
11174 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011175 long idx = 0;
11176 int ic = FALSE;
11177
11178 rettv->vval.v_number = -1;
11179 if (argvars[0].v_type != VAR_LIST)
11180 {
11181 EMSG(_(e_listreq));
11182 return;
11183 }
11184 l = argvars[0].vval.v_list;
11185 if (l != NULL)
11186 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011187 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011188 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 int error = FALSE;
11191
Bram Moolenaar758711c2005-02-02 23:11:38 +000011192 /* Start at specified item. Use the cached index that list_find()
11193 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011195 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011196 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 ic = get_tv_number_chk(&argvars[3], &error);
11198 if (error)
11199 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011200 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011201
Bram Moolenaar758711c2005-02-02 23:11:38 +000011202 for ( ; item != NULL; item = item->li_next, ++idx)
11203 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011204 {
11205 rettv->vval.v_number = idx;
11206 break;
11207 }
11208 }
11209}
11210
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211static int inputsecret_flag = 0;
11212
11213/*
11214 * "input()" function
11215 * Also handles inputsecret() when inputsecret is set.
11216 */
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 typval_T *argvars;
11220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 char_u *p = NULL;
11224 int c;
11225 char_u buf[NUMBUFLEN];
11226 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011228 int xp_type = EXPAND_NOTHING;
11229 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011231 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232
11233#ifdef NO_CONSOLE_INPUT
11234 /* While starting up, there is no place to enter text. */
11235 if (no_console_input())
11236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011237 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 return;
11239 }
11240#endif
11241
11242 cmd_silent = FALSE; /* Want to see the prompt. */
11243 if (prompt != NULL)
11244 {
11245 /* Only the part of the message after the last NL is considered as
11246 * prompt for the command line */
11247 p = vim_strrchr(prompt, '\n');
11248 if (p == NULL)
11249 p = prompt;
11250 else
11251 {
11252 ++p;
11253 c = *p;
11254 *p = NUL;
11255 msg_start();
11256 msg_clr_eos();
11257 msg_puts_attr(prompt, echo_attr);
11258 msg_didout = FALSE;
11259 msg_starthere();
11260 *p = c;
11261 }
11262 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 if (argvars[1].v_type != VAR_UNKNOWN)
11265 {
11266 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11267 if (defstr != NULL)
11268 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269
Bram Moolenaar4463f292005-09-25 22:20:24 +000011270 if (argvars[2].v_type != VAR_UNKNOWN)
11271 {
11272 char_u *xp_name;
11273 int xp_namelen;
11274 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011275
Bram Moolenaar4463f292005-09-25 22:20:24 +000011276 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011277
Bram Moolenaar4463f292005-09-25 22:20:24 +000011278 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11279 if (xp_name == NULL)
11280 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011281
Bram Moolenaar4463f292005-09-25 22:20:24 +000011282 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011283
Bram Moolenaar4463f292005-09-25 22:20:24 +000011284 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11285 &xp_arg) == FAIL)
11286 return;
11287 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011288 }
11289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290 if (defstr != NULL)
11291 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011292 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11293 xp_type, xp_arg);
11294
11295 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297 /* since the user typed this, no need to wait for return */
11298 need_wait_return = FALSE;
11299 msg_didout = FALSE;
11300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 cmd_silent = cmd_silent_save;
11302}
11303
11304/*
11305 * "inputdialog()" function
11306 */
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311{
11312#if defined(FEAT_GUI_TEXTDIALOG)
11313 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11314 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11315 {
11316 char_u *message;
11317 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 message = get_tv_string_chk(&argvars[0]);
11321 if (argvars[1].v_type != VAR_UNKNOWN
11322 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011323 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 else
11325 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011326 if (message != NULL && defstr != NULL
11327 && do_dialog(VIM_QUESTION, NULL, message,
11328 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 else
11331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 if (message != NULL && defstr != NULL
11333 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011334 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->vval.v_string = vim_strsave(
11336 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 }
11342 else
11343#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345}
11346
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011347/*
11348 * "inputlist()" function
11349 */
11350 static void
11351f_inputlist(argvars, rettv)
11352 typval_T *argvars;
11353 typval_T *rettv;
11354{
11355 listitem_T *li;
11356 int selected;
11357 int mouse_used;
11358
11359 rettv->vval.v_number = 0;
11360#ifdef NO_CONSOLE_INPUT
11361 /* While starting up, there is no place to enter text. */
11362 if (no_console_input())
11363 return;
11364#endif
11365 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11366 {
11367 EMSG2(_(e_listarg), "inputlist()");
11368 return;
11369 }
11370
11371 msg_start();
11372 lines_left = Rows; /* avoid more prompt */
11373 msg_scroll = TRUE;
11374 msg_clr_eos();
11375
11376 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11377 {
11378 msg_puts(get_tv_string(&li->li_tv));
11379 msg_putchar('\n');
11380 }
11381
11382 /* Ask for choice. */
11383 selected = prompt_for_number(&mouse_used);
11384 if (mouse_used)
11385 selected -= lines_left;
11386
11387 rettv->vval.v_number = selected;
11388}
11389
11390
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11392
11393/*
11394 * "inputrestore()" function
11395 */
11396/*ARGSUSED*/
11397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *argvars;
11400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401{
11402 if (ga_userinput.ga_len > 0)
11403 {
11404 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11406 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 }
11409 else if (p_verbose > 1)
11410 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011411 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 }
11414}
11415
11416/*
11417 * "inputsave()" function
11418 */
11419/*ARGSUSED*/
11420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011422 typval_T *argvars;
11423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424{
11425 /* Add an entry to the stack of typehead storage. */
11426 if (ga_grow(&ga_userinput, 1) == OK)
11427 {
11428 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11429 + ga_userinput.ga_len);
11430 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435}
11436
11437/*
11438 * "inputsecret()" function
11439 */
11440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011442 typval_T *argvars;
11443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444{
11445 ++cmdline_star;
11446 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 --cmdline_star;
11449 --inputsecret_flag;
11450}
11451
11452/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011453 * "insert()" function
11454 */
11455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011457 typval_T *argvars;
11458 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011459{
11460 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 listitem_T *item;
11462 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011464
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011465 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011466 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011467 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011468 else if ((l = argvars[0].vval.v_list) != NULL
11469 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011470 {
11471 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472 before = get_tv_number_chk(&argvars[2], &error);
11473 if (error)
11474 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475
Bram Moolenaar758711c2005-02-02 23:11:38 +000011476 if (before == l->lv_len)
11477 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011478 else
11479 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011480 item = list_find(l, before);
11481 if (item == NULL)
11482 {
11483 EMSGN(_(e_listidx), before);
11484 l = NULL;
11485 }
11486 }
11487 if (l != NULL)
11488 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011489 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011490 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011491 }
11492 }
11493}
11494
11495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 * "isdirectory()" function
11497 */
11498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011500 typval_T *argvars;
11501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504}
11505
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011506/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011507 * "islocked()" function
11508 */
11509 static void
11510f_islocked(argvars, rettv)
11511 typval_T *argvars;
11512 typval_T *rettv;
11513{
11514 lval_T lv;
11515 char_u *end;
11516 dictitem_T *di;
11517
11518 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011519 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11520 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011521 if (end != NULL && lv.ll_name != NULL)
11522 {
11523 if (*end != NUL)
11524 EMSG(_(e_trailing));
11525 else
11526 {
11527 if (lv.ll_tv == NULL)
11528 {
11529 if (check_changedtick(lv.ll_name))
11530 rettv->vval.v_number = 1; /* always locked */
11531 else
11532 {
11533 di = find_var(lv.ll_name, NULL);
11534 if (di != NULL)
11535 {
11536 /* Consider a variable locked when:
11537 * 1. the variable itself is locked
11538 * 2. the value of the variable is locked.
11539 * 3. the List or Dict value is locked.
11540 */
11541 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11542 || tv_islocked(&di->di_tv));
11543 }
11544 }
11545 }
11546 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011547 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011548 else if (lv.ll_newkey != NULL)
11549 EMSG2(_(e_dictkey), lv.ll_newkey);
11550 else if (lv.ll_list != NULL)
11551 /* List item. */
11552 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11553 else
11554 /* Dictionary item. */
11555 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11556 }
11557 }
11558
11559 clear_lval(&lv);
11560}
11561
Bram Moolenaar33570922005-01-25 22:26:29 +000011562static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011563
11564/*
11565 * Turn a dict into a list:
11566 * "what" == 0: list of keys
11567 * "what" == 1: list of values
11568 * "what" == 2: list of items
11569 */
11570 static void
11571dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011572 typval_T *argvars;
11573 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011574 int what;
11575{
Bram Moolenaar33570922005-01-25 22:26:29 +000011576 list_T *l2;
11577 dictitem_T *di;
11578 hashitem_T *hi;
11579 listitem_T *li;
11580 listitem_T *li2;
11581 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011582 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011583
11584 rettv->vval.v_number = 0;
11585 if (argvars[0].v_type != VAR_DICT)
11586 {
11587 EMSG(_(e_dictreq));
11588 return;
11589 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011590 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011591 return;
11592
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011593 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011594 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011595
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 todo = d->dv_hashtab.ht_used;
11597 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011598 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011599 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011601 --todo;
11602 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011603
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011604 li = listitem_alloc();
11605 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011606 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011607 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011608
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011609 if (what == 0)
11610 {
11611 /* keys() */
11612 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011613 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011614 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11615 }
11616 else if (what == 1)
11617 {
11618 /* values() */
11619 copy_tv(&di->di_tv, &li->li_tv);
11620 }
11621 else
11622 {
11623 /* items() */
11624 l2 = list_alloc();
11625 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011626 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011627 li->li_tv.vval.v_list = l2;
11628 if (l2 == NULL)
11629 break;
11630 ++l2->lv_refcount;
11631
11632 li2 = listitem_alloc();
11633 if (li2 == NULL)
11634 break;
11635 list_append(l2, li2);
11636 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011637 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011638 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11639
11640 li2 = listitem_alloc();
11641 if (li2 == NULL)
11642 break;
11643 list_append(l2, li2);
11644 copy_tv(&di->di_tv, &li2->li_tv);
11645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011646 }
11647 }
11648}
11649
11650/*
11651 * "items(dict)" function
11652 */
11653 static void
11654f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011657{
11658 dict_list(argvars, rettv, 2);
11659}
11660
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011662 * "join()" function
11663 */
11664 static void
11665f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 typval_T *argvars;
11667 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011668{
11669 garray_T ga;
11670 char_u *sep;
11671
11672 rettv->vval.v_number = 0;
11673 if (argvars[0].v_type != VAR_LIST)
11674 {
11675 EMSG(_(e_listreq));
11676 return;
11677 }
11678 if (argvars[0].vval.v_list == NULL)
11679 return;
11680 if (argvars[1].v_type == VAR_UNKNOWN)
11681 sep = (char_u *)" ";
11682 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011684
11685 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686
11687 if (sep != NULL)
11688 {
11689 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011690 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 ga_append(&ga, NUL);
11692 rettv->vval.v_string = (char_u *)ga.ga_data;
11693 }
11694 else
11695 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011696}
11697
11698/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011699 * "keys()" function
11700 */
11701 static void
11702f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011703 typval_T *argvars;
11704 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011705{
11706 dict_list(argvars, rettv, 0);
11707}
11708
11709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 * "last_buffer_nr()" function.
11711 */
11712/*ARGSUSED*/
11713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011714f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011715 typval_T *argvars;
11716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717{
11718 int n = 0;
11719 buf_T *buf;
11720
11721 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11722 if (n < buf->b_fnum)
11723 n = buf->b_fnum;
11724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011726}
11727
11728/*
11729 * "len()" function
11730 */
11731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011732f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011733 typval_T *argvars;
11734 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011735{
11736 switch (argvars[0].v_type)
11737 {
11738 case VAR_STRING:
11739 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011740 rettv->vval.v_number = (varnumber_T)STRLEN(
11741 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011742 break;
11743 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011745 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 case VAR_DICT:
11747 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11748 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011749 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011750 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011751 break;
11752 }
11753}
11754
Bram Moolenaar33570922005-01-25 22:26:29 +000011755static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011756
11757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011758libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011759 typval_T *argvars;
11760 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011761 int type;
11762{
11763#ifdef FEAT_LIBCALL
11764 char_u *string_in;
11765 char_u **string_result;
11766 int nr_result;
11767#endif
11768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011770 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011774
11775 if (check_restricted() || check_secure())
11776 return;
11777
11778#ifdef FEAT_LIBCALL
11779 /* The first two args must be strings, otherwise its meaningless */
11780 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11781 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011782 string_in = NULL;
11783 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011784 string_in = argvars[2].vval.v_string;
11785 if (type == VAR_NUMBER)
11786 string_result = NULL;
11787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011789 if (mch_libcall(argvars[0].vval.v_string,
11790 argvars[1].vval.v_string,
11791 string_in,
11792 argvars[2].vval.v_number,
11793 string_result,
11794 &nr_result) == OK
11795 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011797 }
11798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799}
11800
11801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011802 * "libcall()" function
11803 */
11804 static void
11805f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011806 typval_T *argvars;
11807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011808{
11809 libcall_common(argvars, rettv, VAR_STRING);
11810}
11811
11812/*
11813 * "libcallnr()" function
11814 */
11815 static void
11816f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011817 typval_T *argvars;
11818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819{
11820 libcall_common(argvars, rettv, VAR_NUMBER);
11821}
11822
11823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 * "line(string)" function
11825 */
11826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011828 typval_T *argvars;
11829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830{
11831 linenr_T lnum = 0;
11832 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011833 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011835 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 if (fp != NULL)
11837 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839}
11840
11841/*
11842 * "line2byte(lnum)" function
11843 */
11844/*ARGSUSED*/
11845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *argvars;
11848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
11850#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852#else
11853 linenr_T lnum;
11854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11860 if (rettv->vval.v_number >= 0)
11861 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862#endif
11863}
11864
11865/*
11866 * "lispindent(lnum)" function
11867 */
11868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011870 typval_T *argvars;
11871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872{
11873#ifdef FEAT_LISP
11874 pos_T pos;
11875 linenr_T lnum;
11876
11877 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11880 {
11881 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 curwin->w_cursor = pos;
11884 }
11885 else
11886#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888}
11889
11890/*
11891 * "localtime()" function
11892 */
11893/*ARGSUSED*/
11894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 typval_T *argvars;
11897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900}
11901
Bram Moolenaar33570922005-01-25 22:26:29 +000011902static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903
11904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011906 typval_T *argvars;
11907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 int exact;
11909{
11910 char_u *keys;
11911 char_u *which;
11912 char_u buf[NUMBUFLEN];
11913 char_u *keys_buf = NULL;
11914 char_u *rhs;
11915 int mode;
11916 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011917 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918
11919 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 rettv->v_type = VAR_STRING;
11921 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011923 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 if (*keys == NUL)
11925 return;
11926
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011927 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011929 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011930 if (argvars[2].v_type != VAR_UNKNOWN)
11931 abbr = get_tv_number(&argvars[2]);
11932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 else
11934 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011935 if (which == NULL)
11936 return;
11937
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 mode = get_map_mode(&which, 0);
11939
11940 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011941 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 vim_free(keys_buf);
11943 if (rhs != NULL)
11944 {
11945 ga_init(&ga);
11946 ga.ga_itemsize = 1;
11947 ga.ga_growsize = 40;
11948
11949 while (*rhs != NUL)
11950 ga_concat(&ga, str2special(&rhs, FALSE));
11951
11952 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 }
11955}
11956
11957/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011958 * "map()" function
11959 */
11960 static void
11961f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011962 typval_T *argvars;
11963 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011964{
11965 filter_map(argvars, rettv, TRUE);
11966}
11967
11968/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011969 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 */
11971 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011972f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011973 typval_T *argvars;
11974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011976 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977}
11978
11979/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011980 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 */
11982 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011983f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011984 typval_T *argvars;
11985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011987 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988}
11989
Bram Moolenaar33570922005-01-25 22:26:29 +000011990static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991
11992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011994 typval_T *argvars;
11995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 int type;
11997{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011998 char_u *str = NULL;
11999 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 char_u *pat;
12001 regmatch_T regmatch;
12002 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012003 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 char_u *save_cpo;
12005 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012006 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012007 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012008 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 list_T *l = NULL;
12010 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012011 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012012 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013
12014 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12015 save_cpo = p_cpo;
12016 p_cpo = (char_u *)"";
12017
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012018 rettv->vval.v_number = -1;
12019 if (type == 3)
12020 {
12021 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012022 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012023 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012024 }
12025 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027 rettv->v_type = VAR_STRING;
12028 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012031 if (argvars[0].v_type == VAR_LIST)
12032 {
12033 if ((l = argvars[0].vval.v_list) == NULL)
12034 goto theend;
12035 li = l->lv_first;
12036 }
12037 else
12038 expr = str = get_tv_string(&argvars[0]);
12039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12041 if (pat == NULL)
12042 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012043
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012046 int error = FALSE;
12047
12048 start = get_tv_number_chk(&argvars[2], &error);
12049 if (error)
12050 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012051 if (l != NULL)
12052 {
12053 li = list_find(l, start);
12054 if (li == NULL)
12055 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012056 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012057 }
12058 else
12059 {
12060 if (start < 0)
12061 start = 0;
12062 if (start > (long)STRLEN(str))
12063 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012064 /* When "count" argument is there ignore matches before "start",
12065 * otherwise skip part of the string. Differs when pattern is "^"
12066 * or "\<". */
12067 if (argvars[3].v_type != VAR_UNKNOWN)
12068 startcol = start;
12069 else
12070 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012071 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012073 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 nth = get_tv_number_chk(&argvars[3], &error);
12075 if (error)
12076 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 }
12078
12079 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12080 if (regmatch.regprog != NULL)
12081 {
12082 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012083
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012084 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012085 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012086 if (l != NULL)
12087 {
12088 if (li == NULL)
12089 {
12090 match = FALSE;
12091 break;
12092 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012093 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012094 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012095 if (str == NULL)
12096 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012097 }
12098
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012099 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012100
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012101 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012102 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012103 if (l == NULL && !match)
12104 break;
12105
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012106 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012107 if (l != NULL)
12108 {
12109 li = li->li_next;
12110 ++idx;
12111 }
12112 else
12113 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012114#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012115 startcol = regmatch.startp[0]
12116 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012117#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012118 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012119#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012120 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012121 }
12122
12123 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012125 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012126 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012127 int i;
12128
12129 /* return list with matched string and submatches */
12130 for (i = 0; i < NSUBEXP; ++i)
12131 {
12132 if (regmatch.endp[i] == NULL)
12133 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012134 if (list_append_string(rettv->vval.v_list,
12135 regmatch.startp[i],
12136 (int)(regmatch.endp[i] - regmatch.startp[i]))
12137 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012138 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012139 }
12140 }
12141 else if (type == 2)
12142 {
12143 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012144 if (l != NULL)
12145 copy_tv(&li->li_tv, rettv);
12146 else
12147 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012149 }
12150 else if (l != NULL)
12151 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 else
12153 {
12154 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 (varnumber_T)(regmatch.startp[0] - str);
12157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 }
12162 }
12163 vim_free(regmatch.regprog);
12164 }
12165
12166theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012167 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 p_cpo = save_cpo;
12169}
12170
12171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172 * "match()" function
12173 */
12174 static void
12175f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012176 typval_T *argvars;
12177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012178{
12179 find_some_match(argvars, rettv, 1);
12180}
12181
12182/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012183 * "matcharg()" function
12184 */
12185 static void
12186f_matcharg(argvars, rettv)
12187 typval_T *argvars;
12188 typval_T *rettv;
12189{
12190 if (rettv_list_alloc(rettv) == OK)
12191 {
12192#ifdef FEAT_SEARCH_EXTRA
12193 int mi = get_tv_number(&argvars[0]);
12194
12195 if (mi >= 1 && mi <= 3)
12196 {
12197 list_append_string(rettv->vval.v_list,
12198 syn_id2name(curwin->w_match_id[mi - 1]), -1);
12199 list_append_string(rettv->vval.v_list,
12200 curwin->w_match_pat[mi - 1], -1);
12201 }
12202#endif
12203 }
12204}
12205
12206/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012207 * "matchend()" function
12208 */
12209 static void
12210f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012211 typval_T *argvars;
12212 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213{
12214 find_some_match(argvars, rettv, 0);
12215}
12216
12217/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012218 * "matchlist()" function
12219 */
12220 static void
12221f_matchlist(argvars, rettv)
12222 typval_T *argvars;
12223 typval_T *rettv;
12224{
12225 find_some_match(argvars, rettv, 3);
12226}
12227
12228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012229 * "matchstr()" function
12230 */
12231 static void
12232f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012233 typval_T *argvars;
12234 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012235{
12236 find_some_match(argvars, rettv, 2);
12237}
12238
Bram Moolenaar33570922005-01-25 22:26:29 +000012239static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012240
12241 static void
12242max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012243 typval_T *argvars;
12244 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012245 int domax;
12246{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012247 long n = 0;
12248 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012249 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012250
12251 if (argvars[0].v_type == VAR_LIST)
12252 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012253 list_T *l;
12254 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012255
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012256 l = argvars[0].vval.v_list;
12257 if (l != NULL)
12258 {
12259 li = l->lv_first;
12260 if (li != NULL)
12261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012262 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012263 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012264 {
12265 li = li->li_next;
12266 if (li == NULL)
12267 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012268 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012269 if (domax ? i > n : i < n)
12270 n = i;
12271 }
12272 }
12273 }
12274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012275 else if (argvars[0].v_type == VAR_DICT)
12276 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012277 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012278 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012279 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012280 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012281
12282 d = argvars[0].vval.v_dict;
12283 if (d != NULL)
12284 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 todo = d->dv_hashtab.ht_used;
12286 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012288 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012290 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012291 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012292 if (first)
12293 {
12294 n = i;
12295 first = FALSE;
12296 }
12297 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012298 n = i;
12299 }
12300 }
12301 }
12302 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012303 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012304 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012305 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012306}
12307
12308/*
12309 * "max()" function
12310 */
12311 static void
12312f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012313 typval_T *argvars;
12314 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012315{
12316 max_min(argvars, rettv, TRUE);
12317}
12318
12319/*
12320 * "min()" function
12321 */
12322 static void
12323f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012324 typval_T *argvars;
12325 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012326{
12327 max_min(argvars, rettv, FALSE);
12328}
12329
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012330static int mkdir_recurse __ARGS((char_u *dir, int prot));
12331
12332/*
12333 * Create the directory in which "dir" is located, and higher levels when
12334 * needed.
12335 */
12336 static int
12337mkdir_recurse(dir, prot)
12338 char_u *dir;
12339 int prot;
12340{
12341 char_u *p;
12342 char_u *updir;
12343 int r = FAIL;
12344
12345 /* Get end of directory name in "dir".
12346 * We're done when it's "/" or "c:/". */
12347 p = gettail_sep(dir);
12348 if (p <= get_past_head(dir))
12349 return OK;
12350
12351 /* If the directory exists we're done. Otherwise: create it.*/
12352 updir = vim_strnsave(dir, (int)(p - dir));
12353 if (updir == NULL)
12354 return FAIL;
12355 if (mch_isdir(updir))
12356 r = OK;
12357 else if (mkdir_recurse(updir, prot) == OK)
12358 r = vim_mkdir_emsg(updir, prot);
12359 vim_free(updir);
12360 return r;
12361}
12362
12363#ifdef vim_mkdir
12364/*
12365 * "mkdir()" function
12366 */
12367 static void
12368f_mkdir(argvars, rettv)
12369 typval_T *argvars;
12370 typval_T *rettv;
12371{
12372 char_u *dir;
12373 char_u buf[NUMBUFLEN];
12374 int prot = 0755;
12375
12376 rettv->vval.v_number = FAIL;
12377 if (check_restricted() || check_secure())
12378 return;
12379
12380 dir = get_tv_string_buf(&argvars[0], buf);
12381 if (argvars[1].v_type != VAR_UNKNOWN)
12382 {
12383 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 prot = get_tv_number_chk(&argvars[2], NULL);
12385 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012386 mkdir_recurse(dir, prot);
12387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012389}
12390#endif
12391
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 * "mode()" function
12394 */
12395/*ARGSUSED*/
12396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012398 typval_T *argvars;
12399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
12401 char_u buf[2];
12402
12403#ifdef FEAT_VISUAL
12404 if (VIsual_active)
12405 {
12406 if (VIsual_select)
12407 buf[0] = VIsual_mode + 's' - 'v';
12408 else
12409 buf[0] = VIsual_mode;
12410 }
12411 else
12412#endif
12413 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12414 buf[0] = 'r';
12415 else if (State & INSERT)
12416 {
12417 if (State & REPLACE_FLAG)
12418 buf[0] = 'R';
12419 else
12420 buf[0] = 'i';
12421 }
12422 else if (State & CMDLINE)
12423 buf[0] = 'c';
12424 else
12425 buf[0] = 'n';
12426
12427 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428 rettv->vval.v_string = vim_strsave(buf);
12429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430}
12431
12432/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012433 * "nextnonblank()" function
12434 */
12435 static void
12436f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012437 typval_T *argvars;
12438 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012439{
12440 linenr_T lnum;
12441
12442 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012444 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012445 {
12446 lnum = 0;
12447 break;
12448 }
12449 if (*skipwhite(ml_get(lnum)) != NUL)
12450 break;
12451 }
12452 rettv->vval.v_number = lnum;
12453}
12454
12455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456 * "nr2char()" function
12457 */
12458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012459f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012460 typval_T *argvars;
12461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462{
12463 char_u buf[NUMBUFLEN];
12464
12465#ifdef FEAT_MBYTE
12466 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468 else
12469#endif
12470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 buf[1] = NUL;
12473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 rettv->v_type = VAR_STRING;
12475 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476}
12477
12478/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012479 * "pathshorten()" function
12480 */
12481 static void
12482f_pathshorten(argvars, rettv)
12483 typval_T *argvars;
12484 typval_T *rettv;
12485{
12486 char_u *p;
12487
12488 rettv->v_type = VAR_STRING;
12489 p = get_tv_string_chk(&argvars[0]);
12490 if (p == NULL)
12491 rettv->vval.v_string = NULL;
12492 else
12493 {
12494 p = vim_strsave(p);
12495 rettv->vval.v_string = p;
12496 if (p != NULL)
12497 shorten_dir(p);
12498 }
12499}
12500
12501/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012502 * "prevnonblank()" function
12503 */
12504 static void
12505f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012506 typval_T *argvars;
12507 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508{
12509 linenr_T lnum;
12510
12511 lnum = get_tv_lnum(argvars);
12512 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12513 lnum = 0;
12514 else
12515 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12516 --lnum;
12517 rettv->vval.v_number = lnum;
12518}
12519
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012520#ifdef HAVE_STDARG_H
12521/* This dummy va_list is here because:
12522 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12523 * - locally in the function results in a "used before set" warning
12524 * - using va_start() to initialize it gives "function with fixed args" error */
12525static va_list ap;
12526#endif
12527
Bram Moolenaar8c711452005-01-14 21:53:12 +000012528/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012529 * "printf()" function
12530 */
12531 static void
12532f_printf(argvars, rettv)
12533 typval_T *argvars;
12534 typval_T *rettv;
12535{
12536 rettv->v_type = VAR_STRING;
12537 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012538#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012539 {
12540 char_u buf[NUMBUFLEN];
12541 int len;
12542 char_u *s;
12543 int saved_did_emsg = did_emsg;
12544 char *fmt;
12545
12546 /* Get the required length, allocate the buffer and do it for real. */
12547 did_emsg = FALSE;
12548 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012549 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012550 if (!did_emsg)
12551 {
12552 s = alloc(len + 1);
12553 if (s != NULL)
12554 {
12555 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012556 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012557 }
12558 }
12559 did_emsg |= saved_did_emsg;
12560 }
12561#endif
12562}
12563
12564/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012565 * "pumvisible()" function
12566 */
12567/*ARGSUSED*/
12568 static void
12569f_pumvisible(argvars, rettv)
12570 typval_T *argvars;
12571 typval_T *rettv;
12572{
12573 rettv->vval.v_number = 0;
12574#ifdef FEAT_INS_EXPAND
12575 if (pum_visible())
12576 rettv->vval.v_number = 1;
12577#endif
12578}
12579
12580/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012581 * "range()" function
12582 */
12583 static void
12584f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012587{
12588 long start;
12589 long end;
12590 long stride = 1;
12591 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012592 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012594 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012595 if (argvars[1].v_type == VAR_UNKNOWN)
12596 {
12597 end = start - 1;
12598 start = 0;
12599 }
12600 else
12601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012603 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012605 }
12606
12607 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012608 if (error)
12609 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012610 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012611 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012612 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012613 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012614 else
12615 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012616 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012617 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012618 if (list_append_number(rettv->vval.v_list,
12619 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012620 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012621 }
12622}
12623
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012624/*
12625 * "readfile()" function
12626 */
12627 static void
12628f_readfile(argvars, rettv)
12629 typval_T *argvars;
12630 typval_T *rettv;
12631{
12632 int binary = FALSE;
12633 char_u *fname;
12634 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012635 listitem_T *li;
12636#define FREAD_SIZE 200 /* optimized for text lines */
12637 char_u buf[FREAD_SIZE];
12638 int readlen; /* size of last fread() */
12639 int buflen; /* nr of valid chars in buf[] */
12640 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12641 int tolist; /* first byte in buf[] still to be put in list */
12642 int chop; /* how many CR to chop off */
12643 char_u *prev = NULL; /* previously read bytes, if any */
12644 int prevlen = 0; /* length of "prev" if not NULL */
12645 char_u *s;
12646 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012647 long maxline = MAXLNUM;
12648 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012649
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012650 if (argvars[1].v_type != VAR_UNKNOWN)
12651 {
12652 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12653 binary = TRUE;
12654 if (argvars[2].v_type != VAR_UNKNOWN)
12655 maxline = get_tv_number(&argvars[2]);
12656 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012657
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012658 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012659 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012660
12661 /* Always open the file in binary mode, library functions have a mind of
12662 * their own about CR-LF conversion. */
12663 fname = get_tv_string(&argvars[0]);
12664 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12665 {
12666 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12667 return;
12668 }
12669
12670 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012671 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012672 {
12673 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12674 buflen = filtd + readlen;
12675 tolist = 0;
12676 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12677 {
12678 if (buf[filtd] == '\n' || readlen <= 0)
12679 {
12680 /* Only when in binary mode add an empty list item when the
12681 * last line ends in a '\n'. */
12682 if (!binary && readlen == 0 && filtd == 0)
12683 break;
12684
12685 /* Found end-of-line or end-of-file: add a text line to the
12686 * list. */
12687 chop = 0;
12688 if (!binary)
12689 while (filtd - chop - 1 >= tolist
12690 && buf[filtd - chop - 1] == '\r')
12691 ++chop;
12692 len = filtd - tolist - chop;
12693 if (prev == NULL)
12694 s = vim_strnsave(buf + tolist, len);
12695 else
12696 {
12697 s = alloc((unsigned)(prevlen + len + 1));
12698 if (s != NULL)
12699 {
12700 mch_memmove(s, prev, prevlen);
12701 vim_free(prev);
12702 prev = NULL;
12703 mch_memmove(s + prevlen, buf + tolist, len);
12704 s[prevlen + len] = NUL;
12705 }
12706 }
12707 tolist = filtd + 1;
12708
12709 li = listitem_alloc();
12710 if (li == NULL)
12711 {
12712 vim_free(s);
12713 break;
12714 }
12715 li->li_tv.v_type = VAR_STRING;
12716 li->li_tv.v_lock = 0;
12717 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012718 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012719
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012720 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012721 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012722 if (readlen <= 0)
12723 break;
12724 }
12725 else if (buf[filtd] == NUL)
12726 buf[filtd] = '\n';
12727 }
12728 if (readlen <= 0)
12729 break;
12730
12731 if (tolist == 0)
12732 {
12733 /* "buf" is full, need to move text to an allocated buffer */
12734 if (prev == NULL)
12735 {
12736 prev = vim_strnsave(buf, buflen);
12737 prevlen = buflen;
12738 }
12739 else
12740 {
12741 s = alloc((unsigned)(prevlen + buflen));
12742 if (s != NULL)
12743 {
12744 mch_memmove(s, prev, prevlen);
12745 mch_memmove(s + prevlen, buf, buflen);
12746 vim_free(prev);
12747 prev = s;
12748 prevlen += buflen;
12749 }
12750 }
12751 filtd = 0;
12752 }
12753 else
12754 {
12755 mch_memmove(buf, buf + tolist, buflen - tolist);
12756 filtd -= tolist;
12757 }
12758 }
12759
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012760 /*
12761 * For a negative line count use only the lines at the end of the file,
12762 * free the rest.
12763 */
12764 if (maxline < 0)
12765 while (cnt > -maxline)
12766 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012767 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012768 --cnt;
12769 }
12770
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012771 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012772 fclose(fd);
12773}
12774
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012775#if defined(FEAT_RELTIME)
12776static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12777
12778/*
12779 * Convert a List to proftime_T.
12780 * Return FAIL when there is something wrong.
12781 */
12782 static int
12783list2proftime(arg, tm)
12784 typval_T *arg;
12785 proftime_T *tm;
12786{
12787 long n1, n2;
12788 int error = FALSE;
12789
12790 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12791 || arg->vval.v_list->lv_len != 2)
12792 return FAIL;
12793 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12794 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12795# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012796 tm->HighPart = n1;
12797 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012798# else
12799 tm->tv_sec = n1;
12800 tm->tv_usec = n2;
12801# endif
12802 return error ? FAIL : OK;
12803}
12804#endif /* FEAT_RELTIME */
12805
12806/*
12807 * "reltime()" function
12808 */
12809 static void
12810f_reltime(argvars, rettv)
12811 typval_T *argvars;
12812 typval_T *rettv;
12813{
12814#ifdef FEAT_RELTIME
12815 proftime_T res;
12816 proftime_T start;
12817
12818 if (argvars[0].v_type == VAR_UNKNOWN)
12819 {
12820 /* No arguments: get current time. */
12821 profile_start(&res);
12822 }
12823 else if (argvars[1].v_type == VAR_UNKNOWN)
12824 {
12825 if (list2proftime(&argvars[0], &res) == FAIL)
12826 return;
12827 profile_end(&res);
12828 }
12829 else
12830 {
12831 /* Two arguments: compute the difference. */
12832 if (list2proftime(&argvars[0], &start) == FAIL
12833 || list2proftime(&argvars[1], &res) == FAIL)
12834 return;
12835 profile_sub(&res, &start);
12836 }
12837
12838 if (rettv_list_alloc(rettv) == OK)
12839 {
12840 long n1, n2;
12841
12842# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012843 n1 = res.HighPart;
12844 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012845# else
12846 n1 = res.tv_sec;
12847 n2 = res.tv_usec;
12848# endif
12849 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12850 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12851 }
12852#endif
12853}
12854
12855/*
12856 * "reltimestr()" function
12857 */
12858 static void
12859f_reltimestr(argvars, rettv)
12860 typval_T *argvars;
12861 typval_T *rettv;
12862{
12863#ifdef FEAT_RELTIME
12864 proftime_T tm;
12865#endif
12866
12867 rettv->v_type = VAR_STRING;
12868 rettv->vval.v_string = NULL;
12869#ifdef FEAT_RELTIME
12870 if (list2proftime(&argvars[0], &tm) == OK)
12871 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12872#endif
12873}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012874
Bram Moolenaar0d660222005-01-07 21:51:51 +000012875#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12876static void make_connection __ARGS((void));
12877static int check_connection __ARGS((void));
12878
12879 static void
12880make_connection()
12881{
12882 if (X_DISPLAY == NULL
12883# ifdef FEAT_GUI
12884 && !gui.in_use
12885# endif
12886 )
12887 {
12888 x_force_connect = TRUE;
12889 setup_term_clip();
12890 x_force_connect = FALSE;
12891 }
12892}
12893
12894 static int
12895check_connection()
12896{
12897 make_connection();
12898 if (X_DISPLAY == NULL)
12899 {
12900 EMSG(_("E240: No connection to Vim server"));
12901 return FAIL;
12902 }
12903 return OK;
12904}
12905#endif
12906
12907#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012908static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012909
12910 static void
12911remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012912 typval_T *argvars;
12913 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012914 int expr;
12915{
12916 char_u *server_name;
12917 char_u *keys;
12918 char_u *r = NULL;
12919 char_u buf[NUMBUFLEN];
12920# ifdef WIN32
12921 HWND w;
12922# else
12923 Window w;
12924# endif
12925
12926 if (check_restricted() || check_secure())
12927 return;
12928
12929# ifdef FEAT_X11
12930 if (check_connection() == FAIL)
12931 return;
12932# endif
12933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 server_name = get_tv_string_chk(&argvars[0]);
12935 if (server_name == NULL)
12936 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012937 keys = get_tv_string_buf(&argvars[1], buf);
12938# ifdef WIN32
12939 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12940# else
12941 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12942 < 0)
12943# endif
12944 {
12945 if (r != NULL)
12946 EMSG(r); /* sending worked but evaluation failed */
12947 else
12948 EMSG2(_("E241: Unable to send to %s"), server_name);
12949 return;
12950 }
12951
12952 rettv->vval.v_string = r;
12953
12954 if (argvars[2].v_type != VAR_UNKNOWN)
12955 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012957 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012958 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012959
12960 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 v.di_tv.v_type = VAR_STRING;
12962 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012963 idvar = get_tv_string_chk(&argvars[2]);
12964 if (idvar != NULL)
12965 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012966 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012967 }
12968}
12969#endif
12970
12971/*
12972 * "remote_expr()" function
12973 */
12974/*ARGSUSED*/
12975 static void
12976f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012977 typval_T *argvars;
12978 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979{
12980 rettv->v_type = VAR_STRING;
12981 rettv->vval.v_string = NULL;
12982#ifdef FEAT_CLIENTSERVER
12983 remote_common(argvars, rettv, TRUE);
12984#endif
12985}
12986
12987/*
12988 * "remote_foreground()" function
12989 */
12990/*ARGSUSED*/
12991 static void
12992f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012993 typval_T *argvars;
12994 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012995{
12996 rettv->vval.v_number = 0;
12997#ifdef FEAT_CLIENTSERVER
12998# ifdef WIN32
12999 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013000 {
13001 char_u *server_name = get_tv_string_chk(&argvars[0]);
13002
13003 if (server_name != NULL)
13004 serverForeground(server_name);
13005 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013006# else
13007 /* Send a foreground() expression to the server. */
13008 argvars[1].v_type = VAR_STRING;
13009 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13010 argvars[2].v_type = VAR_UNKNOWN;
13011 remote_common(argvars, rettv, TRUE);
13012 vim_free(argvars[1].vval.v_string);
13013# endif
13014#endif
13015}
13016
13017/*ARGSUSED*/
13018 static void
13019f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013020 typval_T *argvars;
13021 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013022{
13023#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013024 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013025 char_u *s = NULL;
13026# ifdef WIN32
13027 int n = 0;
13028# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013030
13031 if (check_restricted() || check_secure())
13032 {
13033 rettv->vval.v_number = -1;
13034 return;
13035 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013036 serverid = get_tv_string_chk(&argvars[0]);
13037 if (serverid == NULL)
13038 {
13039 rettv->vval.v_number = -1;
13040 return; /* type error; errmsg already given */
13041 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013042# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013044 if (n == 0)
13045 rettv->vval.v_number = -1;
13046 else
13047 {
13048 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13049 rettv->vval.v_number = (s != NULL);
13050 }
13051# else
13052 rettv->vval.v_number = 0;
13053 if (check_connection() == FAIL)
13054 return;
13055
13056 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013058# endif
13059
13060 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 char_u *retvar;
13063
Bram Moolenaar33570922005-01-25 22:26:29 +000013064 v.di_tv.v_type = VAR_STRING;
13065 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 retvar = get_tv_string_chk(&argvars[1]);
13067 if (retvar != NULL)
13068 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013069 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013070 }
13071#else
13072 rettv->vval.v_number = -1;
13073#endif
13074}
13075
13076/*ARGSUSED*/
13077 static void
13078f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013079 typval_T *argvars;
13080 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013081{
13082 char_u *r = NULL;
13083
13084#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013085 char_u *serverid = get_tv_string_chk(&argvars[0]);
13086
13087 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013088 {
13089# ifdef WIN32
13090 /* The server's HWND is encoded in the 'id' parameter */
13091 int n = 0;
13092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013094 if (n != 0)
13095 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13096 if (r == NULL)
13097# else
13098 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013100# endif
13101 EMSG(_("E277: Unable to read a server reply"));
13102 }
13103#endif
13104 rettv->v_type = VAR_STRING;
13105 rettv->vval.v_string = r;
13106}
13107
13108/*
13109 * "remote_send()" function
13110 */
13111/*ARGSUSED*/
13112 static void
13113f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013114 typval_T *argvars;
13115 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013116{
13117 rettv->v_type = VAR_STRING;
13118 rettv->vval.v_string = NULL;
13119#ifdef FEAT_CLIENTSERVER
13120 remote_common(argvars, rettv, FALSE);
13121#endif
13122}
13123
13124/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013125 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013126 */
13127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013131{
Bram Moolenaar33570922005-01-25 22:26:29 +000013132 list_T *l;
13133 listitem_T *item, *item2;
13134 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013135 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013136 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013137 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013138 dict_T *d;
13139 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013140
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013141 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013142 if (argvars[0].v_type == VAR_DICT)
13143 {
13144 if (argvars[2].v_type != VAR_UNKNOWN)
13145 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013146 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013147 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013149 key = get_tv_string_chk(&argvars[1]);
13150 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013152 di = dict_find(d, key, -1);
13153 if (di == NULL)
13154 EMSG2(_(e_dictkey), key);
13155 else
13156 {
13157 *rettv = di->di_tv;
13158 init_tv(&di->di_tv);
13159 dictitem_remove(d, di);
13160 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013161 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013162 }
13163 }
13164 else if (argvars[0].v_type != VAR_LIST)
13165 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013166 else if ((l = argvars[0].vval.v_list) != NULL
13167 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013169 int error = FALSE;
13170
13171 idx = get_tv_number_chk(&argvars[1], &error);
13172 if (error)
13173 ; /* type error: do nothing, errmsg already given */
13174 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013175 EMSGN(_(e_listidx), idx);
13176 else
13177 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013178 if (argvars[2].v_type == VAR_UNKNOWN)
13179 {
13180 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013181 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013182 *rettv = item->li_tv;
13183 vim_free(item);
13184 }
13185 else
13186 {
13187 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013188 end = get_tv_number_chk(&argvars[2], &error);
13189 if (error)
13190 ; /* type error: do nothing */
13191 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013192 EMSGN(_(e_listidx), end);
13193 else
13194 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013195 int cnt = 0;
13196
13197 for (li = item; li != NULL; li = li->li_next)
13198 {
13199 ++cnt;
13200 if (li == item2)
13201 break;
13202 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013203 if (li == NULL) /* didn't find "item2" after "item" */
13204 EMSG(_(e_invrange));
13205 else
13206 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013207 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013208 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013209 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013210 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013211 l->lv_first = item;
13212 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013213 item->li_prev = NULL;
13214 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013215 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013216 }
13217 }
13218 }
13219 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013220 }
13221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222}
13223
13224/*
13225 * "rename({from}, {to})" function
13226 */
13227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 typval_T *argvars;
13230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231{
13232 char_u buf[NUMBUFLEN];
13233
13234 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13238 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239}
13240
13241/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013242 * "repeat()" function
13243 */
13244/*ARGSUSED*/
13245 static void
13246f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013247 typval_T *argvars;
13248 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013249{
13250 char_u *p;
13251 int n;
13252 int slen;
13253 int len;
13254 char_u *r;
13255 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013256
13257 n = get_tv_number(&argvars[1]);
13258 if (argvars[0].v_type == VAR_LIST)
13259 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013260 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013261 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013262 if (list_extend(rettv->vval.v_list,
13263 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013264 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013265 }
13266 else
13267 {
13268 p = get_tv_string(&argvars[0]);
13269 rettv->v_type = VAR_STRING;
13270 rettv->vval.v_string = NULL;
13271
13272 slen = (int)STRLEN(p);
13273 len = slen * n;
13274 if (len <= 0)
13275 return;
13276
13277 r = alloc(len + 1);
13278 if (r != NULL)
13279 {
13280 for (i = 0; i < n; i++)
13281 mch_memmove(r + i * slen, p, (size_t)slen);
13282 r[len] = NUL;
13283 }
13284
13285 rettv->vval.v_string = r;
13286 }
13287}
13288
13289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 * "resolve()" function
13291 */
13292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013294 typval_T *argvars;
13295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296{
13297 char_u *p;
13298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013299 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300#ifdef FEAT_SHORTCUT
13301 {
13302 char_u *v = NULL;
13303
13304 v = mch_resolve_shortcut(p);
13305 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 }
13310#else
13311# ifdef HAVE_READLINK
13312 {
13313 char_u buf[MAXPATHL + 1];
13314 char_u *cpy;
13315 int len;
13316 char_u *remain = NULL;
13317 char_u *q;
13318 int is_relative_to_current = FALSE;
13319 int has_trailing_pathsep = FALSE;
13320 int limit = 100;
13321
13322 p = vim_strsave(p);
13323
13324 if (p[0] == '.' && (vim_ispathsep(p[1])
13325 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13326 is_relative_to_current = TRUE;
13327
13328 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013329 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 has_trailing_pathsep = TRUE;
13331
13332 q = getnextcomp(p);
13333 if (*q != NUL)
13334 {
13335 /* Separate the first path component in "p", and keep the
13336 * remainder (beginning with the path separator). */
13337 remain = vim_strsave(q - 1);
13338 q[-1] = NUL;
13339 }
13340
13341 for (;;)
13342 {
13343 for (;;)
13344 {
13345 len = readlink((char *)p, (char *)buf, MAXPATHL);
13346 if (len <= 0)
13347 break;
13348 buf[len] = NUL;
13349
13350 if (limit-- == 0)
13351 {
13352 vim_free(p);
13353 vim_free(remain);
13354 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 goto fail;
13357 }
13358
13359 /* Ensure that the result will have a trailing path separator
13360 * if the argument has one. */
13361 if (remain == NULL && has_trailing_pathsep)
13362 add_pathsep(buf);
13363
13364 /* Separate the first path component in the link value and
13365 * concatenate the remainders. */
13366 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13367 if (*q != NUL)
13368 {
13369 if (remain == NULL)
13370 remain = vim_strsave(q - 1);
13371 else
13372 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013373 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 if (cpy != NULL)
13375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376 vim_free(remain);
13377 remain = cpy;
13378 }
13379 }
13380 q[-1] = NUL;
13381 }
13382
13383 q = gettail(p);
13384 if (q > p && *q == NUL)
13385 {
13386 /* Ignore trailing path separator. */
13387 q[-1] = NUL;
13388 q = gettail(p);
13389 }
13390 if (q > p && !mch_isFullName(buf))
13391 {
13392 /* symlink is relative to directory of argument */
13393 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13394 if (cpy != NULL)
13395 {
13396 STRCPY(cpy, p);
13397 STRCPY(gettail(cpy), buf);
13398 vim_free(p);
13399 p = cpy;
13400 }
13401 }
13402 else
13403 {
13404 vim_free(p);
13405 p = vim_strsave(buf);
13406 }
13407 }
13408
13409 if (remain == NULL)
13410 break;
13411
13412 /* Append the first path component of "remain" to "p". */
13413 q = getnextcomp(remain + 1);
13414 len = q - remain - (*q != NUL);
13415 cpy = vim_strnsave(p, STRLEN(p) + len);
13416 if (cpy != NULL)
13417 {
13418 STRNCAT(cpy, remain, len);
13419 vim_free(p);
13420 p = cpy;
13421 }
13422 /* Shorten "remain". */
13423 if (*q != NUL)
13424 STRCPY(remain, q - 1);
13425 else
13426 {
13427 vim_free(remain);
13428 remain = NULL;
13429 }
13430 }
13431
13432 /* If the result is a relative path name, make it explicitly relative to
13433 * the current directory if and only if the argument had this form. */
13434 if (!vim_ispathsep(*p))
13435 {
13436 if (is_relative_to_current
13437 && *p != NUL
13438 && !(p[0] == '.'
13439 && (p[1] == NUL
13440 || vim_ispathsep(p[1])
13441 || (p[1] == '.'
13442 && (p[2] == NUL
13443 || vim_ispathsep(p[2]))))))
13444 {
13445 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013446 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 if (cpy != NULL)
13448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 vim_free(p);
13450 p = cpy;
13451 }
13452 }
13453 else if (!is_relative_to_current)
13454 {
13455 /* Strip leading "./". */
13456 q = p;
13457 while (q[0] == '.' && vim_ispathsep(q[1]))
13458 q += 2;
13459 if (q > p)
13460 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13461 }
13462 }
13463
13464 /* Ensure that the result will have no trailing path separator
13465 * if the argument had none. But keep "/" or "//". */
13466 if (!has_trailing_pathsep)
13467 {
13468 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013469 if (after_pathsep(p, q))
13470 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013471 }
13472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 }
13475# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477# endif
13478#endif
13479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481
13482#ifdef HAVE_READLINK
13483fail:
13484#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486}
13487
13488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013489 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 */
13491 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013492f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *argvars;
13494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495{
Bram Moolenaar33570922005-01-25 22:26:29 +000013496 list_T *l;
13497 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498
Bram Moolenaar0d660222005-01-07 21:51:51 +000013499 rettv->vval.v_number = 0;
13500 if (argvars[0].v_type != VAR_LIST)
13501 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013502 else if ((l = argvars[0].vval.v_list) != NULL
13503 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013504 {
13505 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013506 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013507 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013508 while (li != NULL)
13509 {
13510 ni = li->li_prev;
13511 list_append(l, li);
13512 li = ni;
13513 }
13514 rettv->vval.v_list = l;
13515 rettv->v_type = VAR_LIST;
13516 ++l->lv_refcount;
13517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518}
13519
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013520#define SP_NOMOVE 0x01 /* don't move cursor */
13521#define SP_REPEAT 0x02 /* repeat to find outer pair */
13522#define SP_RETCOUNT 0x04 /* return matchcount */
13523#define SP_SETPCMARK 0x08 /* set previous context mark */
13524#define SP_START 0x10 /* accept match at start position */
13525#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13526#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013527
Bram Moolenaar33570922005-01-25 22:26:29 +000013528static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013529
13530/*
13531 * Get flags for a search function.
13532 * Possibly sets "p_ws".
13533 * Returns BACKWARD, FORWARD or zero (for an error).
13534 */
13535 static int
13536get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013538 int *flagsp;
13539{
13540 int dir = FORWARD;
13541 char_u *flags;
13542 char_u nbuf[NUMBUFLEN];
13543 int mask;
13544
13545 if (varp->v_type != VAR_UNKNOWN)
13546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013547 flags = get_tv_string_buf_chk(varp, nbuf);
13548 if (flags == NULL)
13549 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013550 while (*flags != NUL)
13551 {
13552 switch (*flags)
13553 {
13554 case 'b': dir = BACKWARD; break;
13555 case 'w': p_ws = TRUE; break;
13556 case 'W': p_ws = FALSE; break;
13557 default: mask = 0;
13558 if (flagsp != NULL)
13559 switch (*flags)
13560 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013561 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013562 case 'e': mask = SP_END; break;
13563 case 'm': mask = SP_RETCOUNT; break;
13564 case 'n': mask = SP_NOMOVE; break;
13565 case 'p': mask = SP_SUBPAT; break;
13566 case 'r': mask = SP_REPEAT; break;
13567 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013568 }
13569 if (mask == 0)
13570 {
13571 EMSG2(_(e_invarg2), flags);
13572 dir = 0;
13573 }
13574 else
13575 *flagsp |= mask;
13576 }
13577 if (dir == 0)
13578 break;
13579 ++flags;
13580 }
13581 }
13582 return dir;
13583}
13584
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013586 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013588 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013589search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013591 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013592 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013594 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595 char_u *pat;
13596 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013597 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 int save_p_ws = p_ws;
13599 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013600 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013601 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013602 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013603 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013606 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013607 if (dir == 0)
13608 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013609 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013610 if (flags & SP_START)
13611 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013612 if (flags & SP_END)
13613 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013614
13615 /* Optional extra argument: line number to stop searching. */
13616 if (argvars[1].v_type != VAR_UNKNOWN
13617 && argvars[2].v_type != VAR_UNKNOWN)
13618 {
13619 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13620 if (lnum_stop < 0)
13621 goto theend;
13622 }
13623
Bram Moolenaar231334e2005-07-25 20:46:57 +000013624 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013625 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013626 * Check to make sure only those flags are set.
13627 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13628 * flags cannot be set. Check for that condition also.
13629 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013630 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013631 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013634 goto theend;
13635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013637 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013638 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13639 options, RE_SEARCH, (linenr_T)lnum_stop);
13640 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013642 if (flags & SP_SUBPAT)
13643 retval = subpatnum;
13644 else
13645 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013646 if (flags & SP_SETPCMARK)
13647 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013649 if (match_pos != NULL)
13650 {
13651 /* Store the match cursor position */
13652 match_pos->lnum = pos.lnum;
13653 match_pos->col = pos.col + 1;
13654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 /* "/$" will put the cursor after the end of the line, may need to
13656 * correct that here */
13657 check_cursor();
13658 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013659
13660 /* If 'n' flag is used: restore cursor position. */
13661 if (flags & SP_NOMOVE)
13662 curwin->w_cursor = save_cursor;
13663theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013665
13666 return retval;
13667}
13668
13669/*
13670 * "search()" function
13671 */
13672 static void
13673f_search(argvars, rettv)
13674 typval_T *argvars;
13675 typval_T *rettv;
13676{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013677 int flags = 0;
13678
13679 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680}
13681
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013683 * "searchdecl()" function
13684 */
13685 static void
13686f_searchdecl(argvars, rettv)
13687 typval_T *argvars;
13688 typval_T *rettv;
13689{
13690 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013691 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013692 int error = FALSE;
13693 char_u *name;
13694
13695 rettv->vval.v_number = 1; /* default: FAIL */
13696
13697 name = get_tv_string_chk(&argvars[0]);
13698 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013699 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013700 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013701 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13702 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13703 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013704 if (!error && name != NULL)
13705 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013706 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013707}
13708
13709/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013710 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013712 static int
13713searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013715 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716{
13717 char_u *spat, *mpat, *epat;
13718 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 int dir;
13721 int flags = 0;
13722 char_u nbuf1[NUMBUFLEN];
13723 char_u nbuf2[NUMBUFLEN];
13724 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013725 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013726 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729 spat = get_tv_string_chk(&argvars[0]);
13730 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13731 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13732 if (spat == NULL || mpat == NULL || epat == NULL)
13733 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 /* Handle the optional fourth argument: flags */
13736 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013737 if (dir == 0)
13738 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013739
13740 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013741 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13742 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013743 if ((flags & (SP_END | SP_SUBPAT)) != 0
13744 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013745 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013746 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013747 goto theend;
13748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013750 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013751 if (argvars[3].v_type == VAR_UNKNOWN
13752 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 skip = (char_u *)"";
13754 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013756 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013757 if (argvars[5].v_type != VAR_UNKNOWN)
13758 {
13759 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13760 if (lnum_stop < 0)
13761 goto theend;
13762 }
13763 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 if (skip == NULL)
13765 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013767 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13768 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013769
13770theend:
13771 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013772
13773 return retval;
13774}
13775
13776/*
13777 * "searchpair()" function
13778 */
13779 static void
13780f_searchpair(argvars, rettv)
13781 typval_T *argvars;
13782 typval_T *rettv;
13783{
13784 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13785}
13786
13787/*
13788 * "searchpairpos()" function
13789 */
13790 static void
13791f_searchpairpos(argvars, rettv)
13792 typval_T *argvars;
13793 typval_T *rettv;
13794{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013795 pos_T match_pos;
13796 int lnum = 0;
13797 int col = 0;
13798
13799 rettv->vval.v_number = 0;
13800
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013801 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013802 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013803
13804 if (searchpair_cmn(argvars, &match_pos) > 0)
13805 {
13806 lnum = match_pos.lnum;
13807 col = match_pos.col;
13808 }
13809
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013810 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13811 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013812}
13813
13814/*
13815 * Search for a start/middle/end thing.
13816 * Used by searchpair(), see its documentation for the details.
13817 * Returns 0 or -1 for no match,
13818 */
13819 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013820do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013821 char_u *spat; /* start pattern */
13822 char_u *mpat; /* middle pattern */
13823 char_u *epat; /* end pattern */
13824 int dir; /* BACKWARD or FORWARD */
13825 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013826 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013827 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013828 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013829{
13830 char_u *save_cpo;
13831 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13832 long retval = 0;
13833 pos_T pos;
13834 pos_T firstpos;
13835 pos_T foundpos;
13836 pos_T save_cursor;
13837 pos_T save_pos;
13838 int n;
13839 int r;
13840 int nest = 1;
13841 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013842 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013843
13844 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13845 save_cpo = p_cpo;
13846 p_cpo = (char_u *)"";
13847
13848 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13849 * start/middle/end (pat3, for the top pair). */
13850 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13851 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13852 if (pat2 == NULL || pat3 == NULL)
13853 goto theend;
13854 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13855 if (*mpat == NUL)
13856 STRCPY(pat3, pat2);
13857 else
13858 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13859 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013860 if (flags & SP_START)
13861 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013862
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 save_cursor = curwin->w_cursor;
13864 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013865 clearpos(&firstpos);
13866 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 pat = pat3;
13868 for (;;)
13869 {
13870 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013871 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13873 /* didn't find it or found the first match again: FAIL */
13874 break;
13875
13876 if (firstpos.lnum == 0)
13877 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013878 if (equalpos(pos, foundpos))
13879 {
13880 /* Found the same position again. Can happen with a pattern that
13881 * has "\zs" at the end and searching backwards. Advance one
13882 * character and try again. */
13883 if (dir == BACKWARD)
13884 decl(&pos);
13885 else
13886 incl(&pos);
13887 }
13888 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889
13890 /* If the skip pattern matches, ignore this match. */
13891 if (*skip != NUL)
13892 {
13893 save_pos = curwin->w_cursor;
13894 curwin->w_cursor = pos;
13895 r = eval_to_bool(skip, &err, NULL, FALSE);
13896 curwin->w_cursor = save_pos;
13897 if (err)
13898 {
13899 /* Evaluating {skip} caused an error, break here. */
13900 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013901 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 break;
13903 }
13904 if (r)
13905 continue;
13906 }
13907
13908 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13909 {
13910 /* Found end when searching backwards or start when searching
13911 * forward: nested pair. */
13912 ++nest;
13913 pat = pat2; /* nested, don't search for middle */
13914 }
13915 else
13916 {
13917 /* Found end when searching forward or start when searching
13918 * backward: end of (nested) pair; or found middle in outer pair. */
13919 if (--nest == 1)
13920 pat = pat3; /* outer level, search for middle */
13921 }
13922
13923 if (nest == 0)
13924 {
13925 /* Found the match: return matchcount or line number. */
13926 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013927 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013929 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013930 if (flags & SP_SETPCMARK)
13931 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 curwin->w_cursor = pos;
13933 if (!(flags & SP_REPEAT))
13934 break;
13935 nest = 1; /* search for next unmatched */
13936 }
13937 }
13938
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013939 if (match_pos != NULL)
13940 {
13941 /* Store the match cursor position */
13942 match_pos->lnum = curwin->w_cursor.lnum;
13943 match_pos->col = curwin->w_cursor.col + 1;
13944 }
13945
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013947 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 curwin->w_cursor = save_cursor;
13949
13950theend:
13951 vim_free(pat2);
13952 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013954
13955 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956}
13957
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013958/*
13959 * "searchpos()" function
13960 */
13961 static void
13962f_searchpos(argvars, rettv)
13963 typval_T *argvars;
13964 typval_T *rettv;
13965{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013966 pos_T match_pos;
13967 int lnum = 0;
13968 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013969 int n;
13970 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013971
13972 rettv->vval.v_number = 0;
13973
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013974 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013975 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013976
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013977 n = search_cmn(argvars, &match_pos, &flags);
13978 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013979 {
13980 lnum = match_pos.lnum;
13981 col = match_pos.col;
13982 }
13983
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013984 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13985 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013986 if (flags & SP_SUBPAT)
13987 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013988}
13989
13990
Bram Moolenaar0d660222005-01-07 21:51:51 +000013991/*ARGSUSED*/
13992 static void
13993f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013994 typval_T *argvars;
13995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013997#ifdef FEAT_CLIENTSERVER
13998 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013999 char_u *server = get_tv_string_chk(&argvars[0]);
14000 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001
Bram Moolenaar0d660222005-01-07 21:51:51 +000014002 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 if (server == NULL || reply == NULL)
14004 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014005 if (check_restricted() || check_secure())
14006 return;
14007# ifdef FEAT_X11
14008 if (check_connection() == FAIL)
14009 return;
14010# endif
14011
14012 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014014 EMSG(_("E258: Unable to send to client"));
14015 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014017 rettv->vval.v_number = 0;
14018#else
14019 rettv->vval.v_number = -1;
14020#endif
14021}
14022
14023/*ARGSUSED*/
14024 static void
14025f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014026 typval_T *argvars;
14027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028{
14029 char_u *r = NULL;
14030
14031#ifdef FEAT_CLIENTSERVER
14032# ifdef WIN32
14033 r = serverGetVimNames();
14034# else
14035 make_connection();
14036 if (X_DISPLAY != NULL)
14037 r = serverGetVimNames(X_DISPLAY);
14038# endif
14039#endif
14040 rettv->v_type = VAR_STRING;
14041 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042}
14043
14044/*
14045 * "setbufvar()" function
14046 */
14047/*ARGSUSED*/
14048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014049f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014050 typval_T *argvars;
14051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052{
14053 buf_T *buf;
14054#ifdef FEAT_AUTOCMD
14055 aco_save_T aco;
14056#else
14057 buf_T *save_curbuf;
14058#endif
14059 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014060 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 char_u nbuf[NUMBUFLEN];
14062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014063 rettv->vval.v_number = 0;
14064
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 if (check_restricted() || check_secure())
14066 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014067 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14068 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 varp = &argvars[2];
14071
14072 if (buf != NULL && varname != NULL && varp != NULL)
14073 {
14074 /* set curbuf to be our buf, temporarily */
14075#ifdef FEAT_AUTOCMD
14076 aucmd_prepbuf(&aco, buf);
14077#else
14078 save_curbuf = curbuf;
14079 curbuf = buf;
14080#endif
14081
14082 if (*varname == '&')
14083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 long numval;
14085 char_u *strval;
14086 int error = FALSE;
14087
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 numval = get_tv_number_chk(varp, &error);
14090 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 if (!error && strval != NULL)
14092 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 }
14094 else
14095 {
14096 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14097 if (bufvarname != NULL)
14098 {
14099 STRCPY(bufvarname, "b:");
14100 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014101 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 vim_free(bufvarname);
14103 }
14104 }
14105
14106 /* reset notion of buffer */
14107#ifdef FEAT_AUTOCMD
14108 aucmd_restbuf(&aco);
14109#else
14110 curbuf = save_curbuf;
14111#endif
14112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113}
14114
14115/*
14116 * "setcmdpos()" function
14117 */
14118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 typval_T *argvars;
14121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014123 int pos = (int)get_tv_number(&argvars[0]) - 1;
14124
14125 if (pos >= 0)
14126 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127}
14128
14129/*
14130 * "setline()" function
14131 */
14132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014133f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136{
14137 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014138 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014139 list_T *l = NULL;
14140 listitem_T *li = NULL;
14141 long added = 0;
14142 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014144 lnum = get_tv_lnum(&argvars[0]);
14145 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014147 l = argvars[1].vval.v_list;
14148 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014150 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014152
14153 rettv->vval.v_number = 0; /* OK */
14154 for (;;)
14155 {
14156 if (l != NULL)
14157 {
14158 /* list argument, get next string */
14159 if (li == NULL)
14160 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014161 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014162 li = li->li_next;
14163 }
14164
14165 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014167 break;
14168 if (lnum <= curbuf->b_ml.ml_line_count)
14169 {
14170 /* existing line, replace it */
14171 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14172 {
14173 changed_bytes(lnum, 0);
14174 check_cursor_col();
14175 rettv->vval.v_number = 0; /* OK */
14176 }
14177 }
14178 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14179 {
14180 /* lnum is one past the last line, append the line */
14181 ++added;
14182 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14183 rettv->vval.v_number = 0; /* OK */
14184 }
14185
14186 if (l == NULL) /* only one string argument */
14187 break;
14188 ++lnum;
14189 }
14190
14191 if (added > 0)
14192 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193}
14194
14195/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014196 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014197 */
14198/*ARGSUSED*/
14199 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014200set_qf_ll_list(wp, list_arg, action_arg, rettv)
14201 win_T *wp;
14202 typval_T *list_arg;
14203 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014204 typval_T *rettv;
14205{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014206#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014207 char_u *act;
14208 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014209#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014210
Bram Moolenaar2641f772005-03-25 21:58:17 +000014211 rettv->vval.v_number = -1;
14212
14213#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014214 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014215 EMSG(_(e_listreq));
14216 else
14217 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014218 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014219
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014220 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014221 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014222 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 if (act == NULL)
14224 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014225 if (*act == 'a' || *act == 'r')
14226 action = *act;
14227 }
14228
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014229 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014230 rettv->vval.v_number = 0;
14231 }
14232#endif
14233}
14234
14235/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014236 * "setloclist()" function
14237 */
14238/*ARGSUSED*/
14239 static void
14240f_setloclist(argvars, rettv)
14241 typval_T *argvars;
14242 typval_T *rettv;
14243{
14244 win_T *win;
14245
14246 rettv->vval.v_number = -1;
14247
14248 win = find_win_by_nr(&argvars[0]);
14249 if (win != NULL)
14250 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14251}
14252
14253/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014254 * "setpos()" function
14255 */
14256/*ARGSUSED*/
14257 static void
14258f_setpos(argvars, rettv)
14259 typval_T *argvars;
14260 typval_T *rettv;
14261{
14262 pos_T pos;
14263 int fnum;
14264 char_u *name;
14265
14266 name = get_tv_string_chk(argvars);
14267 if (name != NULL)
14268 {
14269 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14270 {
14271 --pos.col;
14272 if (name[0] == '.') /* cursor */
14273 {
14274 if (fnum == curbuf->b_fnum)
14275 {
14276 curwin->w_cursor = pos;
14277 check_cursor();
14278 }
14279 else
14280 EMSG(_(e_invarg));
14281 }
14282 else if (name[0] == '\'') /* mark */
14283 (void)setmark_pos(name[1], &pos, fnum);
14284 else
14285 EMSG(_(e_invarg));
14286 }
14287 }
14288}
14289
14290/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014291 * "setqflist()" function
14292 */
14293/*ARGSUSED*/
14294 static void
14295f_setqflist(argvars, rettv)
14296 typval_T *argvars;
14297 typval_T *rettv;
14298{
14299 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14300}
14301
14302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 * "setreg()" function
14304 */
14305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014307 typval_T *argvars;
14308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309{
14310 int regname;
14311 char_u *strregname;
14312 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 int append;
14315 char_u yank_type;
14316 long block_len;
14317
14318 block_len = -1;
14319 yank_type = MAUTO;
14320 append = FALSE;
14321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014322 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014323 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325 if (strregname == NULL)
14326 return; /* type error; errmsg already given */
14327 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 if (regname == 0 || regname == '@')
14329 regname = '"';
14330 else if (regname == '=')
14331 return;
14332
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014333 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 stropt = get_tv_string_chk(&argvars[2]);
14336 if (stropt == NULL)
14337 return; /* type error */
14338 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 switch (*stropt)
14340 {
14341 case 'a': case 'A': /* append */
14342 append = TRUE;
14343 break;
14344 case 'v': case 'c': /* character-wise selection */
14345 yank_type = MCHAR;
14346 break;
14347 case 'V': case 'l': /* line-wise selection */
14348 yank_type = MLINE;
14349 break;
14350#ifdef FEAT_VISUAL
14351 case 'b': case Ctrl_V: /* block-wise selection */
14352 yank_type = MBLOCK;
14353 if (VIM_ISDIGIT(stropt[1]))
14354 {
14355 ++stropt;
14356 block_len = getdigits(&stropt) - 1;
14357 --stropt;
14358 }
14359 break;
14360#endif
14361 }
14362 }
14363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 strval = get_tv_string_chk(&argvars[1]);
14365 if (strval != NULL)
14366 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369}
14370
14371
14372/*
14373 * "setwinvar(expr)" function
14374 */
14375/*ARGSUSED*/
14376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014378 typval_T *argvars;
14379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380{
14381 win_T *win;
14382#ifdef FEAT_WINDOWS
14383 win_T *save_curwin;
14384#endif
14385 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014386 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 char_u nbuf[NUMBUFLEN];
14388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 rettv->vval.v_number = 0;
14390
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 if (check_restricted() || check_secure())
14392 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014394 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 varp = &argvars[2];
14396
14397 if (win != NULL && varname != NULL && varp != NULL)
14398 {
14399#ifdef FEAT_WINDOWS
14400 /* set curwin to be our win, temporarily */
14401 save_curwin = curwin;
14402 curwin = win;
14403 curbuf = curwin->w_buffer;
14404#endif
14405
14406 if (*varname == '&')
14407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 long numval;
14409 char_u *strval;
14410 int error = FALSE;
14411
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 numval = get_tv_number_chk(varp, &error);
14414 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014415 if (!error && strval != NULL)
14416 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 }
14418 else
14419 {
14420 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14421 if (winvarname != NULL)
14422 {
14423 STRCPY(winvarname, "w:");
14424 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014425 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 vim_free(winvarname);
14427 }
14428 }
14429
14430#ifdef FEAT_WINDOWS
14431 /* Restore current window, if it's still valid (autocomands can make
14432 * it invalid). */
14433 if (win_valid(save_curwin))
14434 {
14435 curwin = save_curwin;
14436 curbuf = curwin->w_buffer;
14437 }
14438#endif
14439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440}
14441
14442/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014443 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 */
14445 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014446f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014447 typval_T *argvars;
14448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014450 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451
Bram Moolenaar0d660222005-01-07 21:51:51 +000014452 p = get_tv_string(&argvars[0]);
14453 rettv->vval.v_string = vim_strsave(p);
14454 simplify_filename(rettv->vval.v_string); /* simplify in place */
14455 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456}
14457
Bram Moolenaar0d660222005-01-07 21:51:51 +000014458static int
14459#ifdef __BORLANDC__
14460 _RTLENTRYF
14461#endif
14462 item_compare __ARGS((const void *s1, const void *s2));
14463static int
14464#ifdef __BORLANDC__
14465 _RTLENTRYF
14466#endif
14467 item_compare2 __ARGS((const void *s1, const void *s2));
14468
14469static int item_compare_ic;
14470static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014472#define ITEM_COMPARE_FAIL 999
14473
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014475 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477 static int
14478#ifdef __BORLANDC__
14479_RTLENTRYF
14480#endif
14481item_compare(s1, s2)
14482 const void *s1;
14483 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014485 char_u *p1, *p2;
14486 char_u *tofree1, *tofree2;
14487 int res;
14488 char_u numbuf1[NUMBUFLEN];
14489 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014491 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14492 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493 if (item_compare_ic)
14494 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 res = STRCMP(p1, p2);
14497 vim_free(tofree1);
14498 vim_free(tofree2);
14499 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500}
14501
14502 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014503#ifdef __BORLANDC__
14504_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014506item_compare2(s1, s2)
14507 const void *s1;
14508 const void *s2;
14509{
14510 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014511 typval_T rettv;
14512 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014513 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 /* shortcut after failure in previous call; compare all items equal */
14516 if (item_compare_func_err)
14517 return 0;
14518
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014519 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14520 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14522 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014523
14524 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14525 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014526 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014527 clear_tv(&argv[0]);
14528 clear_tv(&argv[1]);
14529
14530 if (res == FAIL)
14531 res = ITEM_COMPARE_FAIL;
14532 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 /* return value has wrong type */
14534 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14535 if (item_compare_func_err)
14536 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014537 clear_tv(&rettv);
14538 return res;
14539}
14540
14541/*
14542 * "sort({list})" function
14543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 typval_T *argvars;
14547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548{
Bram Moolenaar33570922005-01-25 22:26:29 +000014549 list_T *l;
14550 listitem_T *li;
14551 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014552 long len;
14553 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555 rettv->vval.v_number = 0;
14556 if (argvars[0].v_type != VAR_LIST)
14557 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 else
14559 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014561 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014562 return;
14563 rettv->vval.v_list = l;
14564 rettv->v_type = VAR_LIST;
14565 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 len = list_len(l);
14568 if (len <= 1)
14569 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 item_compare_ic = FALSE;
14572 item_compare_func = NULL;
14573 if (argvars[1].v_type != VAR_UNKNOWN)
14574 {
14575 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014576 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014577 else
14578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014579 int error = FALSE;
14580
14581 i = get_tv_number_chk(&argvars[1], &error);
14582 if (error)
14583 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584 if (i == 1)
14585 item_compare_ic = TRUE;
14586 else
14587 item_compare_func = get_tv_string(&argvars[1]);
14588 }
14589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590
Bram Moolenaar0d660222005-01-07 21:51:51 +000014591 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014592 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 if (ptrs == NULL)
14594 return;
14595 i = 0;
14596 for (li = l->lv_first; li != NULL; li = li->li_next)
14597 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014599 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600 /* test the compare function */
14601 if (item_compare_func != NULL
14602 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14603 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014604 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014606 {
14607 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014608 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014609 item_compare_func == NULL ? item_compare : item_compare2);
14610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 if (!item_compare_func_err)
14612 {
14613 /* Clear the List and append the items in the sorted order. */
14614 l->lv_first = l->lv_last = NULL;
14615 l->lv_len = 0;
14616 for (i = 0; i < len; ++i)
14617 list_append(l, ptrs[i]);
14618 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619 }
14620
14621 vim_free(ptrs);
14622 }
14623}
14624
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014625/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014626 * "soundfold({word})" function
14627 */
14628 static void
14629f_soundfold(argvars, rettv)
14630 typval_T *argvars;
14631 typval_T *rettv;
14632{
14633 char_u *s;
14634
14635 rettv->v_type = VAR_STRING;
14636 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014637#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014638 rettv->vval.v_string = eval_soundfold(s);
14639#else
14640 rettv->vval.v_string = vim_strsave(s);
14641#endif
14642}
14643
14644/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014645 * "spellbadword()" function
14646 */
14647/* ARGSUSED */
14648 static void
14649f_spellbadword(argvars, rettv)
14650 typval_T *argvars;
14651 typval_T *rettv;
14652{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014653 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014654 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014655 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014656
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014657 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014658 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014659
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014660#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014661 if (argvars[0].v_type == VAR_UNKNOWN)
14662 {
14663 /* Find the start and length of the badly spelled word. */
14664 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14665 if (len != 0)
14666 word = ml_get_cursor();
14667 }
14668 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14669 {
14670 char_u *str = get_tv_string_chk(&argvars[0]);
14671 int capcol = -1;
14672
14673 if (str != NULL)
14674 {
14675 /* Check the argument for spelling. */
14676 while (*str != NUL)
14677 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014678 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014679 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014680 {
14681 word = str;
14682 break;
14683 }
14684 str += len;
14685 }
14686 }
14687 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014688#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014689
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014690 list_append_string(rettv->vval.v_list, word, len);
14691 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014692 attr == HLF_SPB ? "bad" :
14693 attr == HLF_SPR ? "rare" :
14694 attr == HLF_SPL ? "local" :
14695 attr == HLF_SPC ? "caps" :
14696 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014697}
14698
14699/*
14700 * "spellsuggest()" function
14701 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014702/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014703 static void
14704f_spellsuggest(argvars, rettv)
14705 typval_T *argvars;
14706 typval_T *rettv;
14707{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014708#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014709 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014710 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014711 int maxcount;
14712 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014713 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014714 listitem_T *li;
14715 int need_capital = FALSE;
14716#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014717
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014718 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014719 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014720
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014721#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014722 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14723 {
14724 str = get_tv_string(&argvars[0]);
14725 if (argvars[1].v_type != VAR_UNKNOWN)
14726 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014727 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014728 if (maxcount <= 0)
14729 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014730 if (argvars[2].v_type != VAR_UNKNOWN)
14731 {
14732 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14733 if (typeerr)
14734 return;
14735 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014736 }
14737 else
14738 maxcount = 25;
14739
Bram Moolenaar4770d092006-01-12 23:22:24 +000014740 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014741
14742 for (i = 0; i < ga.ga_len; ++i)
14743 {
14744 str = ((char_u **)ga.ga_data)[i];
14745
14746 li = listitem_alloc();
14747 if (li == NULL)
14748 vim_free(str);
14749 else
14750 {
14751 li->li_tv.v_type = VAR_STRING;
14752 li->li_tv.v_lock = 0;
14753 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014754 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014755 }
14756 }
14757 ga_clear(&ga);
14758 }
14759#endif
14760}
14761
Bram Moolenaar0d660222005-01-07 21:51:51 +000014762 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014763f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014764 typval_T *argvars;
14765 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014766{
14767 char_u *str;
14768 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014769 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014770 regmatch_T regmatch;
14771 char_u patbuf[NUMBUFLEN];
14772 char_u *save_cpo;
14773 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014774 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014775 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014777
14778 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14779 save_cpo = p_cpo;
14780 p_cpo = (char_u *)"";
14781
14782 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014783 if (argvars[1].v_type != VAR_UNKNOWN)
14784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14786 if (pat == NULL)
14787 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014788 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014790 }
14791 if (pat == NULL || *pat == NUL)
14792 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014793
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014794 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 if (typeerr)
14797 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798
Bram Moolenaar0d660222005-01-07 21:51:51 +000014799 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14800 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014802 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014803 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014804 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014805 if (*str == NUL)
14806 match = FALSE; /* empty item at the end */
14807 else
14808 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014809 if (match)
14810 end = regmatch.startp[0];
14811 else
14812 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014813 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14814 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014815 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014816 if (list_append_string(rettv->vval.v_list, str,
14817 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014818 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014819 }
14820 if (!match)
14821 break;
14822 /* Advance to just after the match. */
14823 if (regmatch.endp[0] > str)
14824 col = 0;
14825 else
14826 {
14827 /* Don't get stuck at the same match. */
14828#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014829 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014830#else
14831 col = 1;
14832#endif
14833 }
14834 str = regmatch.endp[0];
14835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836
Bram Moolenaar0d660222005-01-07 21:51:51 +000014837 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839
Bram Moolenaar0d660222005-01-07 21:51:51 +000014840 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841}
14842
Bram Moolenaar2c932302006-03-18 21:42:09 +000014843/*
14844 * "str2nr()" function
14845 */
14846 static void
14847f_str2nr(argvars, rettv)
14848 typval_T *argvars;
14849 typval_T *rettv;
14850{
14851 int base = 10;
14852 char_u *p;
14853 long n;
14854
14855 if (argvars[1].v_type != VAR_UNKNOWN)
14856 {
14857 base = get_tv_number(&argvars[1]);
14858 if (base != 8 && base != 10 && base != 16)
14859 {
14860 EMSG(_(e_invarg));
14861 return;
14862 }
14863 }
14864
14865 p = skipwhite(get_tv_string(&argvars[0]));
14866 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14867 rettv->vval.v_number = n;
14868}
14869
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870#ifdef HAVE_STRFTIME
14871/*
14872 * "strftime({format}[, {time}])" function
14873 */
14874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014875f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014876 typval_T *argvars;
14877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878{
14879 char_u result_buf[256];
14880 struct tm *curtime;
14881 time_t seconds;
14882 char_u *p;
14883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014886 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014887 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 seconds = time(NULL);
14889 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014890 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 curtime = localtime(&seconds);
14892 /* MSVC returns NULL for an invalid value of seconds. */
14893 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 else
14896 {
14897# ifdef FEAT_MBYTE
14898 vimconv_T conv;
14899 char_u *enc;
14900
14901 conv.vc_type = CONV_NONE;
14902 enc = enc_locale();
14903 convert_setup(&conv, p_enc, enc);
14904 if (conv.vc_type != CONV_NONE)
14905 p = string_convert(&conv, p, NULL);
14906# endif
14907 if (p != NULL)
14908 (void)strftime((char *)result_buf, sizeof(result_buf),
14909 (char *)p, curtime);
14910 else
14911 result_buf[0] = NUL;
14912
14913# ifdef FEAT_MBYTE
14914 if (conv.vc_type != CONV_NONE)
14915 vim_free(p);
14916 convert_setup(&conv, enc, p_enc);
14917 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014918 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 else
14920# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014921 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922
14923# ifdef FEAT_MBYTE
14924 /* Release conversion descriptors */
14925 convert_setup(&conv, NULL, NULL);
14926 vim_free(enc);
14927# endif
14928 }
14929}
14930#endif
14931
14932/*
14933 * "stridx()" function
14934 */
14935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014937 typval_T *argvars;
14938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939{
14940 char_u buf[NUMBUFLEN];
14941 char_u *needle;
14942 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014943 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014947 needle = get_tv_string_chk(&argvars[1]);
14948 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014949 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014950 if (needle == NULL || haystack == NULL)
14951 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952
Bram Moolenaar33570922005-01-25 22:26:29 +000014953 if (argvars[2].v_type != VAR_UNKNOWN)
14954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955 int error = FALSE;
14956
14957 start_idx = get_tv_number_chk(&argvars[2], &error);
14958 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014959 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014960 if (start_idx >= 0)
14961 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014962 }
14963
14964 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14965 if (pos != NULL)
14966 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967}
14968
14969/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014970 * "string()" function
14971 */
14972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014973f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014974 typval_T *argvars;
14975 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014976{
14977 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014978 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014980 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014981 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014982 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984}
14985
14986/*
14987 * "strlen()" function
14988 */
14989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014991 typval_T *argvars;
14992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 rettv->vval.v_number = (varnumber_T)(STRLEN(
14995 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014996}
14997
14998/*
14999 * "strpart()" function
15000 */
15001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 typval_T *argvars;
15004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005{
15006 char_u *p;
15007 int n;
15008 int len;
15009 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015010 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 slen = (int)STRLEN(p);
15014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 n = get_tv_number_chk(&argvars[1], &error);
15016 if (error)
15017 len = 0;
15018 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 else
15021 len = slen - n; /* default len: all bytes that are available. */
15022
15023 /*
15024 * Only return the overlap between the specified part and the actual
15025 * string.
15026 */
15027 if (n < 0)
15028 {
15029 len += n;
15030 n = 0;
15031 }
15032 else if (n > slen)
15033 n = slen;
15034 if (len < 0)
15035 len = 0;
15036 else if (n + len > slen)
15037 len = slen - n;
15038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->v_type = VAR_STRING;
15040 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041}
15042
15043/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044 * "strridx()" function
15045 */
15046 static void
15047f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015048 typval_T *argvars;
15049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050{
15051 char_u buf[NUMBUFLEN];
15052 char_u *needle;
15053 char_u *haystack;
15054 char_u *rest;
15055 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015056 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015058 needle = get_tv_string_chk(&argvars[1]);
15059 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015060 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061
15062 rettv->vval.v_number = -1;
15063 if (needle == NULL || haystack == NULL)
15064 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015065 if (argvars[2].v_type != VAR_UNKNOWN)
15066 {
15067 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015069 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015071 }
15072 else
15073 end_idx = haystack_len;
15074
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015076 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015078 lastmatch = haystack + end_idx;
15079 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015081 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 for (rest = haystack; *rest != '\0'; ++rest)
15083 {
15084 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015085 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015086 break;
15087 lastmatch = rest;
15088 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015089 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090
15091 if (lastmatch == NULL)
15092 rettv->vval.v_number = -1;
15093 else
15094 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15095}
15096
15097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 * "strtrans()" function
15099 */
15100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015101f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 typval_T *argvars;
15103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015104{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015105 rettv->v_type = VAR_STRING;
15106 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107}
15108
15109/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110 * "submatch()" function
15111 */
15112 static void
15113f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015114 typval_T *argvars;
15115 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116{
15117 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015118 rettv->vval.v_string =
15119 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120}
15121
15122/*
15123 * "substitute()" function
15124 */
15125 static void
15126f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 typval_T *argvars;
15128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129{
15130 char_u patbuf[NUMBUFLEN];
15131 char_u subbuf[NUMBUFLEN];
15132 char_u flagsbuf[NUMBUFLEN];
15133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 char_u *str = get_tv_string_chk(&argvars[0]);
15135 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15136 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15137 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15138
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015140 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15141 rettv->vval.v_string = NULL;
15142 else
15143 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144}
15145
15146/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015147 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 */
15149/*ARGSUSED*/
15150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015151f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015152 typval_T *argvars;
15153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154{
15155 int id = 0;
15156#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015157 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 long col;
15159 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015160 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015162 lnum = get_tv_lnum(argvars); /* -1 on type error */
15163 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15164 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015167 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015168 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169#endif
15170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015171 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172}
15173
15174/*
15175 * "synIDattr(id, what [, mode])" function
15176 */
15177/*ARGSUSED*/
15178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015179f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015180 typval_T *argvars;
15181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182{
15183 char_u *p = NULL;
15184#ifdef FEAT_SYN_HL
15185 int id;
15186 char_u *what;
15187 char_u *mode;
15188 char_u modebuf[NUMBUFLEN];
15189 int modec;
15190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015191 id = get_tv_number(&argvars[0]);
15192 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015193 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015195 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 modec = TOLOWER_ASC(mode[0]);
15197 if (modec != 't' && modec != 'c'
15198#ifdef FEAT_GUI
15199 && modec != 'g'
15200#endif
15201 )
15202 modec = 0; /* replace invalid with current */
15203 }
15204 else
15205 {
15206#ifdef FEAT_GUI
15207 if (gui.in_use)
15208 modec = 'g';
15209 else
15210#endif
15211 if (t_colors > 1)
15212 modec = 'c';
15213 else
15214 modec = 't';
15215 }
15216
15217
15218 switch (TOLOWER_ASC(what[0]))
15219 {
15220 case 'b':
15221 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15222 p = highlight_color(id, what, modec);
15223 else /* bold */
15224 p = highlight_has_attr(id, HL_BOLD, modec);
15225 break;
15226
15227 case 'f': /* fg[#] */
15228 p = highlight_color(id, what, modec);
15229 break;
15230
15231 case 'i':
15232 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15233 p = highlight_has_attr(id, HL_INVERSE, modec);
15234 else /* italic */
15235 p = highlight_has_attr(id, HL_ITALIC, modec);
15236 break;
15237
15238 case 'n': /* name */
15239 p = get_highlight_name(NULL, id - 1);
15240 break;
15241
15242 case 'r': /* reverse */
15243 p = highlight_has_attr(id, HL_INVERSE, modec);
15244 break;
15245
15246 case 's': /* standout */
15247 p = highlight_has_attr(id, HL_STANDOUT, modec);
15248 break;
15249
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015250 case 'u':
15251 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15252 /* underline */
15253 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15254 else
15255 /* undercurl */
15256 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 break;
15258 }
15259
15260 if (p != NULL)
15261 p = vim_strsave(p);
15262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015263 rettv->v_type = VAR_STRING;
15264 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265}
15266
15267/*
15268 * "synIDtrans(id)" function
15269 */
15270/*ARGSUSED*/
15271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015272f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 typval_T *argvars;
15274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275{
15276 int id;
15277
15278#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015279 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015280
15281 if (id > 0)
15282 id = syn_get_final_id(id);
15283 else
15284#endif
15285 id = 0;
15286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288}
15289
15290/*
15291 * "system()" function
15292 */
15293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015295 typval_T *argvars;
15296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015298 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015300 char_u *infile = NULL;
15301 char_u buf[NUMBUFLEN];
15302 int err = FALSE;
15303 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015305 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015306 {
15307 /*
15308 * Write the string to a temp file, to be used for input of the shell
15309 * command.
15310 */
15311 if ((infile = vim_tempname('i')) == NULL)
15312 {
15313 EMSG(_(e_notmp));
15314 return;
15315 }
15316
15317 fd = mch_fopen((char *)infile, WRITEBIN);
15318 if (fd == NULL)
15319 {
15320 EMSG2(_(e_notopen), infile);
15321 goto done;
15322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015323 p = get_tv_string_buf_chk(&argvars[1], buf);
15324 if (p == NULL)
15325 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015326 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15327 err = TRUE;
15328 if (fclose(fd) != 0)
15329 err = TRUE;
15330 if (err)
15331 {
15332 EMSG(_("E677: Error writing temp file"));
15333 goto done;
15334 }
15335 }
15336
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015337 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15338 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015339
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340#ifdef USE_CR
15341 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015342 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 {
15344 char_u *s;
15345
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015346 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 {
15348 if (*s == CAR)
15349 *s = NL;
15350 }
15351 }
15352#else
15353# ifdef USE_CRNL
15354 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015355 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 {
15357 char_u *s, *d;
15358
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015359 d = res;
15360 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 {
15362 if (s[0] == CAR && s[1] == NL)
15363 ++s;
15364 *d++ = *s;
15365 }
15366 *d = NUL;
15367 }
15368# endif
15369#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015370
15371done:
15372 if (infile != NULL)
15373 {
15374 mch_remove(infile);
15375 vim_free(infile);
15376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015377 rettv->v_type = VAR_STRING;
15378 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379}
15380
15381/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015382 * "tabpagebuflist()" function
15383 */
15384/* ARGSUSED */
15385 static void
15386f_tabpagebuflist(argvars, rettv)
15387 typval_T *argvars;
15388 typval_T *rettv;
15389{
15390#ifndef FEAT_WINDOWS
15391 rettv->vval.v_number = 0;
15392#else
15393 tabpage_T *tp;
15394 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015395
15396 if (argvars[0].v_type == VAR_UNKNOWN)
15397 wp = firstwin;
15398 else
15399 {
15400 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15401 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015402 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015403 }
15404 if (wp == NULL)
15405 rettv->vval.v_number = 0;
15406 else
15407 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015408 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015409 rettv->vval.v_number = 0;
15410 else
15411 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015412 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015413 if (list_append_number(rettv->vval.v_list,
15414 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015415 break;
15416 }
15417 }
15418#endif
15419}
15420
15421
15422/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015423 * "tabpagenr()" function
15424 */
15425/* ARGSUSED */
15426 static void
15427f_tabpagenr(argvars, rettv)
15428 typval_T *argvars;
15429 typval_T *rettv;
15430{
15431 int nr = 1;
15432#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015433 char_u *arg;
15434
15435 if (argvars[0].v_type != VAR_UNKNOWN)
15436 {
15437 arg = get_tv_string_chk(&argvars[0]);
15438 nr = 0;
15439 if (arg != NULL)
15440 {
15441 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015442 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015443 else
15444 EMSG2(_(e_invexpr2), arg);
15445 }
15446 }
15447 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015448 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015449#endif
15450 rettv->vval.v_number = nr;
15451}
15452
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015453
15454#ifdef FEAT_WINDOWS
15455static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15456
15457/*
15458 * Common code for tabpagewinnr() and winnr().
15459 */
15460 static int
15461get_winnr(tp, argvar)
15462 tabpage_T *tp;
15463 typval_T *argvar;
15464{
15465 win_T *twin;
15466 int nr = 1;
15467 win_T *wp;
15468 char_u *arg;
15469
15470 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15471 if (argvar->v_type != VAR_UNKNOWN)
15472 {
15473 arg = get_tv_string_chk(argvar);
15474 if (arg == NULL)
15475 nr = 0; /* type error; errmsg already given */
15476 else if (STRCMP(arg, "$") == 0)
15477 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15478 else if (STRCMP(arg, "#") == 0)
15479 {
15480 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15481 if (twin == NULL)
15482 nr = 0;
15483 }
15484 else
15485 {
15486 EMSG2(_(e_invexpr2), arg);
15487 nr = 0;
15488 }
15489 }
15490
15491 if (nr > 0)
15492 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15493 wp != twin; wp = wp->w_next)
15494 ++nr;
15495 return nr;
15496}
15497#endif
15498
15499/*
15500 * "tabpagewinnr()" function
15501 */
15502/* ARGSUSED */
15503 static void
15504f_tabpagewinnr(argvars, rettv)
15505 typval_T *argvars;
15506 typval_T *rettv;
15507{
15508 int nr = 1;
15509#ifdef FEAT_WINDOWS
15510 tabpage_T *tp;
15511
15512 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15513 if (tp == NULL)
15514 nr = 0;
15515 else
15516 nr = get_winnr(tp, &argvars[1]);
15517#endif
15518 rettv->vval.v_number = nr;
15519}
15520
15521
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015522/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015523 * "tagfiles()" function
15524 */
15525/*ARGSUSED*/
15526 static void
15527f_tagfiles(argvars, rettv)
15528 typval_T *argvars;
15529 typval_T *rettv;
15530{
15531 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015532 tagname_T tn;
15533 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015534
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015535 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015536 {
15537 rettv->vval.v_number = 0;
15538 return;
15539 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015540
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015541 for (first = TRUE; ; first = FALSE)
15542 if (get_tagfname(&tn, first, fname) == FAIL
15543 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015544 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015545 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015546}
15547
15548/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015549 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015550 */
15551 static void
15552f_taglist(argvars, rettv)
15553 typval_T *argvars;
15554 typval_T *rettv;
15555{
15556 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015557
15558 tag_pattern = get_tv_string(&argvars[0]);
15559
15560 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015561 if (*tag_pattern == NUL)
15562 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015564 if (rettv_list_alloc(rettv) == OK)
15565 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015566}
15567
15568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569 * "tempname()" function
15570 */
15571/*ARGSUSED*/
15572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015574 typval_T *argvars;
15575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576{
15577 static int x = 'A';
15578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 rettv->v_type = VAR_STRING;
15580 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
15582 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15583 * names. Skip 'I' and 'O', they are used for shell redirection. */
15584 do
15585 {
15586 if (x == 'Z')
15587 x = '0';
15588 else if (x == '9')
15589 x = 'A';
15590 else
15591 {
15592#ifdef EBCDIC
15593 if (x == 'I')
15594 x = 'J';
15595 else if (x == 'R')
15596 x = 'S';
15597 else
15598#endif
15599 ++x;
15600 }
15601 } while (x == 'I' || x == 'O');
15602}
15603
15604/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015605 * "test(list)" function: Just checking the walls...
15606 */
15607/*ARGSUSED*/
15608 static void
15609f_test(argvars, rettv)
15610 typval_T *argvars;
15611 typval_T *rettv;
15612{
15613 /* Used for unit testing. Change the code below to your liking. */
15614#if 0
15615 listitem_T *li;
15616 list_T *l;
15617 char_u *bad, *good;
15618
15619 if (argvars[0].v_type != VAR_LIST)
15620 return;
15621 l = argvars[0].vval.v_list;
15622 if (l == NULL)
15623 return;
15624 li = l->lv_first;
15625 if (li == NULL)
15626 return;
15627 bad = get_tv_string(&li->li_tv);
15628 li = li->li_next;
15629 if (li == NULL)
15630 return;
15631 good = get_tv_string(&li->li_tv);
15632 rettv->vval.v_number = test_edit_score(bad, good);
15633#endif
15634}
15635
15636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 * "tolower(string)" function
15638 */
15639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015640f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015641 typval_T *argvars;
15642 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643{
15644 char_u *p;
15645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015646 p = vim_strsave(get_tv_string(&argvars[0]));
15647 rettv->v_type = VAR_STRING;
15648 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649
15650 if (p != NULL)
15651 while (*p != NUL)
15652 {
15653#ifdef FEAT_MBYTE
15654 int l;
15655
15656 if (enc_utf8)
15657 {
15658 int c, lc;
15659
15660 c = utf_ptr2char(p);
15661 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015662 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 /* TODO: reallocate string when byte count changes. */
15664 if (utf_char2len(lc) == l)
15665 utf_char2bytes(lc, p);
15666 p += l;
15667 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015668 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 p += l; /* skip multi-byte character */
15670 else
15671#endif
15672 {
15673 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15674 ++p;
15675 }
15676 }
15677}
15678
15679/*
15680 * "toupper(string)" function
15681 */
15682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015683f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015684 typval_T *argvars;
15685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015687 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015688 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
15691/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015692 * "tr(string, fromstr, tostr)" function
15693 */
15694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 typval_T *argvars;
15697 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015698{
15699 char_u *instr;
15700 char_u *fromstr;
15701 char_u *tostr;
15702 char_u *p;
15703#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015704 int inlen;
15705 int fromlen;
15706 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015707 int idx;
15708 char_u *cpstr;
15709 int cplen;
15710 int first = TRUE;
15711#endif
15712 char_u buf[NUMBUFLEN];
15713 char_u buf2[NUMBUFLEN];
15714 garray_T ga;
15715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015717 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15718 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015719
15720 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721 rettv->v_type = VAR_STRING;
15722 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015723 if (fromstr == NULL || tostr == NULL)
15724 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015725 ga_init2(&ga, (int)sizeof(char), 80);
15726
15727#ifdef FEAT_MBYTE
15728 if (!has_mbyte)
15729#endif
15730 /* not multi-byte: fromstr and tostr must be the same length */
15731 if (STRLEN(fromstr) != STRLEN(tostr))
15732 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015733#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015734error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015735#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015736 EMSG2(_(e_invarg2), fromstr);
15737 ga_clear(&ga);
15738 return;
15739 }
15740
15741 /* fromstr and tostr have to contain the same number of chars */
15742 while (*instr != NUL)
15743 {
15744#ifdef FEAT_MBYTE
15745 if (has_mbyte)
15746 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015747 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015748 cpstr = instr;
15749 cplen = inlen;
15750 idx = 0;
15751 for (p = fromstr; *p != NUL; p += fromlen)
15752 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015753 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015754 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15755 {
15756 for (p = tostr; *p != NUL; p += tolen)
15757 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015758 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015759 if (idx-- == 0)
15760 {
15761 cplen = tolen;
15762 cpstr = p;
15763 break;
15764 }
15765 }
15766 if (*p == NUL) /* tostr is shorter than fromstr */
15767 goto error;
15768 break;
15769 }
15770 ++idx;
15771 }
15772
15773 if (first && cpstr == instr)
15774 {
15775 /* Check that fromstr and tostr have the same number of
15776 * (multi-byte) characters. Done only once when a character
15777 * of instr doesn't appear in fromstr. */
15778 first = FALSE;
15779 for (p = tostr; *p != NUL; p += tolen)
15780 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015781 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015782 --idx;
15783 }
15784 if (idx != 0)
15785 goto error;
15786 }
15787
15788 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015789 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015790 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015791
15792 instr += inlen;
15793 }
15794 else
15795#endif
15796 {
15797 /* When not using multi-byte chars we can do it faster. */
15798 p = vim_strchr(fromstr, *instr);
15799 if (p != NULL)
15800 ga_append(&ga, tostr[p - fromstr]);
15801 else
15802 ga_append(&ga, *instr);
15803 ++instr;
15804 }
15805 }
15806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015807 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015808}
15809
15810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 * "type(expr)" function
15812 */
15813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015814f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015815 typval_T *argvars;
15816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015818 int n;
15819
15820 switch (argvars[0].v_type)
15821 {
15822 case VAR_NUMBER: n = 0; break;
15823 case VAR_STRING: n = 1; break;
15824 case VAR_FUNC: n = 2; break;
15825 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015826 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015827 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15828 }
15829 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830}
15831
15832/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015833 * "values(dict)" function
15834 */
15835 static void
15836f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015837 typval_T *argvars;
15838 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015839{
15840 dict_list(argvars, rettv, 1);
15841}
15842
15843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 * "virtcol(string)" function
15845 */
15846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015847f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015848 typval_T *argvars;
15849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850{
15851 colnr_T vcol = 0;
15852 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015853 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015855 fp = var2fpos(&argvars[0], FALSE, &fnum);
15856 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15857 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 {
15859 getvvcol(curwin, fp, NULL, NULL, &vcol);
15860 ++vcol;
15861 }
15862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015863 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864}
15865
15866/*
15867 * "visualmode()" function
15868 */
15869/*ARGSUSED*/
15870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015871f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015872 typval_T *argvars;
15873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874{
15875#ifdef FEAT_VISUAL
15876 char_u str[2];
15877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015878 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 str[0] = curbuf->b_visual_mode_eval;
15880 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015881 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882
15883 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015884 if ((argvars[0].v_type == VAR_NUMBER
15885 && argvars[0].vval.v_number != 0)
15886 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015887 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 curbuf->b_visual_mode_eval = NUL;
15889#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015890 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891#endif
15892}
15893
15894/*
15895 * "winbufnr(nr)" function
15896 */
15897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015898f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 typval_T *argvars;
15900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015901{
15902 win_T *wp;
15903
15904 wp = find_win_by_nr(&argvars[0]);
15905 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909}
15910
15911/*
15912 * "wincol()" function
15913 */
15914/*ARGSUSED*/
15915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015916f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015917 typval_T *argvars;
15918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919{
15920 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015921 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922}
15923
15924/*
15925 * "winheight(nr)" function
15926 */
15927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015928f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015929 typval_T *argvars;
15930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931{
15932 win_T *wp;
15933
15934 wp = find_win_by_nr(&argvars[0]);
15935 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015936 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015938 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939}
15940
15941/*
15942 * "winline()" function
15943 */
15944/*ARGSUSED*/
15945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015946f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015947 typval_T *argvars;
15948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949{
15950 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015951 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952}
15953
15954/*
15955 * "winnr()" function
15956 */
15957/* ARGSUSED */
15958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015959f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015960 typval_T *argvars;
15961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015962{
15963 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015964
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015966 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015968 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969}
15970
15971/*
15972 * "winrestcmd()" function
15973 */
15974/* ARGSUSED */
15975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015976f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015977 typval_T *argvars;
15978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979{
15980#ifdef FEAT_WINDOWS
15981 win_T *wp;
15982 int winnr = 1;
15983 garray_T ga;
15984 char_u buf[50];
15985
15986 ga_init2(&ga, (int)sizeof(char), 70);
15987 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15988 {
15989 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15990 ga_concat(&ga, buf);
15991# ifdef FEAT_VERTSPLIT
15992 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15993 ga_concat(&ga, buf);
15994# endif
15995 ++winnr;
15996 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015997 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015999 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016001 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004}
16005
16006/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016007 * "winrestview()" function
16008 */
16009/* ARGSUSED */
16010 static void
16011f_winrestview(argvars, rettv)
16012 typval_T *argvars;
16013 typval_T *rettv;
16014{
16015 dict_T *dict;
16016
16017 if (argvars[0].v_type != VAR_DICT
16018 || (dict = argvars[0].vval.v_dict) == NULL)
16019 EMSG(_(e_invarg));
16020 else
16021 {
16022 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16023 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16024#ifdef FEAT_VIRTUALEDIT
16025 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16026#endif
16027 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016028 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016029
16030 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
16031#ifdef FEAT_DIFF
16032 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16033#endif
16034 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16035 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16036
16037 check_cursor();
16038 changed_cline_bef_curs();
16039 invalidate_botline();
16040 redraw_later(VALID);
16041
16042 if (curwin->w_topline == 0)
16043 curwin->w_topline = 1;
16044 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16045 curwin->w_topline = curbuf->b_ml.ml_line_count;
16046#ifdef FEAT_DIFF
16047 check_topfill(curwin, TRUE);
16048#endif
16049 }
16050}
16051
16052/*
16053 * "winsaveview()" function
16054 */
16055/* ARGSUSED */
16056 static void
16057f_winsaveview(argvars, rettv)
16058 typval_T *argvars;
16059 typval_T *rettv;
16060{
16061 dict_T *dict;
16062
16063 dict = dict_alloc();
16064 if (dict == NULL)
16065 return;
16066 rettv->v_type = VAR_DICT;
16067 rettv->vval.v_dict = dict;
16068 ++dict->dv_refcount;
16069
16070 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16071 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16072#ifdef FEAT_VIRTUALEDIT
16073 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16074#endif
16075 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16076
16077 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16078#ifdef FEAT_DIFF
16079 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16080#endif
16081 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16082 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16083}
16084
16085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 * "winwidth(nr)" function
16087 */
16088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016089f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 typval_T *argvars;
16091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092{
16093 win_T *wp;
16094
16095 wp = find_win_by_nr(&argvars[0]);
16096 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 else
16099#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016100 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016102 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103#endif
16104}
16105
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016107 * "writefile()" function
16108 */
16109 static void
16110f_writefile(argvars, rettv)
16111 typval_T *argvars;
16112 typval_T *rettv;
16113{
16114 int binary = FALSE;
16115 char_u *fname;
16116 FILE *fd;
16117 listitem_T *li;
16118 char_u *s;
16119 int ret = 0;
16120 int c;
16121
16122 if (argvars[0].v_type != VAR_LIST)
16123 {
16124 EMSG2(_(e_listarg), "writefile()");
16125 return;
16126 }
16127 if (argvars[0].vval.v_list == NULL)
16128 return;
16129
16130 if (argvars[2].v_type != VAR_UNKNOWN
16131 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16132 binary = TRUE;
16133
16134 /* Always open the file in binary mode, library functions have a mind of
16135 * their own about CR-LF conversion. */
16136 fname = get_tv_string(&argvars[1]);
16137 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16138 {
16139 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16140 ret = -1;
16141 }
16142 else
16143 {
16144 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16145 li = li->li_next)
16146 {
16147 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16148 {
16149 if (*s == '\n')
16150 c = putc(NUL, fd);
16151 else
16152 c = putc(*s, fd);
16153 if (c == EOF)
16154 {
16155 ret = -1;
16156 break;
16157 }
16158 }
16159 if (!binary || li->li_next != NULL)
16160 if (putc('\n', fd) == EOF)
16161 {
16162 ret = -1;
16163 break;
16164 }
16165 if (ret < 0)
16166 {
16167 EMSG(_(e_write));
16168 break;
16169 }
16170 }
16171 fclose(fd);
16172 }
16173
16174 rettv->vval.v_number = ret;
16175}
16176
16177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016179 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 */
16181 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016182var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016183 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016185 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016187 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016189 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190
Bram Moolenaara5525202006-03-02 22:52:09 +000016191 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016192 if (varp->v_type == VAR_LIST)
16193 {
16194 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016195 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016196 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016197
16198 l = varp->vval.v_list;
16199 if (l == NULL)
16200 return NULL;
16201
16202 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016203 pos.lnum = list_find_nr(l, 0L, &error);
16204 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016205 return NULL; /* invalid line number */
16206
16207 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016208 pos.col = list_find_nr(l, 1L, &error);
16209 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016210 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016211 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016212 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016213 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016214 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016215 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016216
Bram Moolenaara5525202006-03-02 22:52:09 +000016217#ifdef FEAT_VIRTUALEDIT
16218 /* Get the virtual offset. Defaults to zero. */
16219 pos.coladd = list_find_nr(l, 2L, &error);
16220 if (error)
16221 pos.coladd = 0;
16222#endif
16223
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016224 return &pos;
16225 }
16226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 name = get_tv_string_chk(varp);
16228 if (name == NULL)
16229 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 if (name[0] == '.') /* cursor */
16231 return &curwin->w_cursor;
16232 if (name[0] == '\'') /* mark */
16233 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016234 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16236 return NULL;
16237 return pp;
16238 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016239
16240#ifdef FEAT_VIRTUALEDIT
16241 pos.coladd = 0;
16242#endif
16243
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016244 if (name[0] == 'w' && lnum)
16245 {
16246 pos.col = 0;
16247 if (name[1] == '0') /* "w0": first visible line */
16248 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016249 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016250 pos.lnum = curwin->w_topline;
16251 return &pos;
16252 }
16253 else if (name[1] == '$') /* "w$": last visible line */
16254 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016255 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016256 pos.lnum = curwin->w_botline - 1;
16257 return &pos;
16258 }
16259 }
16260 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 {
16262 if (lnum)
16263 {
16264 pos.lnum = curbuf->b_ml.ml_line_count;
16265 pos.col = 0;
16266 }
16267 else
16268 {
16269 pos.lnum = curwin->w_cursor.lnum;
16270 pos.col = (colnr_T)STRLEN(ml_get_curline());
16271 }
16272 return &pos;
16273 }
16274 return NULL;
16275}
16276
16277/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016278 * Convert list in "arg" into a position and optional file number.
16279 * When "fnump" is NULL there is no file number, only 3 items.
16280 * Note that the column is passed on as-is, the caller may want to decrement
16281 * it to use 1 for the first column.
16282 * Return FAIL when conversion is not possible, doesn't check the position for
16283 * validity.
16284 */
16285 static int
16286list2fpos(arg, posp, fnump)
16287 typval_T *arg;
16288 pos_T *posp;
16289 int *fnump;
16290{
16291 list_T *l = arg->vval.v_list;
16292 long i = 0;
16293 long n;
16294
16295 /* List must be: [fnum, lnum, col, coladd] */
16296 if (arg->v_type != VAR_LIST || l == NULL
16297 || l->lv_len != (fnump == NULL ? 3 : 4))
16298 return FAIL;
16299
16300 if (fnump != NULL)
16301 {
16302 n = list_find_nr(l, i++, NULL); /* fnum */
16303 if (n < 0)
16304 return FAIL;
16305 if (n == 0)
16306 n = curbuf->b_fnum; /* current buffer */
16307 *fnump = n;
16308 }
16309
16310 n = list_find_nr(l, i++, NULL); /* lnum */
16311 if (n < 0)
16312 return FAIL;
16313 posp->lnum = n;
16314
16315 n = list_find_nr(l, i++, NULL); /* col */
16316 if (n < 0)
16317 return FAIL;
16318 posp->col = n;
16319
16320#ifdef FEAT_VIRTUALEDIT
16321 n = list_find_nr(l, i, NULL);
16322 if (n < 0)
16323 return FAIL;
16324 posp->coladd = n;
16325#endif
16326
16327 return OK;
16328}
16329
16330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 * Get the length of an environment variable name.
16332 * Advance "arg" to the first character after the name.
16333 * Return 0 for error.
16334 */
16335 static int
16336get_env_len(arg)
16337 char_u **arg;
16338{
16339 char_u *p;
16340 int len;
16341
16342 for (p = *arg; vim_isIDc(*p); ++p)
16343 ;
16344 if (p == *arg) /* no name found */
16345 return 0;
16346
16347 len = (int)(p - *arg);
16348 *arg = p;
16349 return len;
16350}
16351
16352/*
16353 * Get the length of the name of a function or internal variable.
16354 * "arg" is advanced to the first non-white character after the name.
16355 * Return 0 if something is wrong.
16356 */
16357 static int
16358get_id_len(arg)
16359 char_u **arg;
16360{
16361 char_u *p;
16362 int len;
16363
16364 /* Find the end of the name. */
16365 for (p = *arg; eval_isnamec(*p); ++p)
16366 ;
16367 if (p == *arg) /* no name found */
16368 return 0;
16369
16370 len = (int)(p - *arg);
16371 *arg = skipwhite(p);
16372
16373 return len;
16374}
16375
16376/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016377 * Get the length of the name of a variable or function.
16378 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016380 * Return -1 if curly braces expansion failed.
16381 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 * If the name contains 'magic' {}'s, expand them and return the
16383 * expanded name in an allocated string via 'alias' - caller must free.
16384 */
16385 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016386get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 char_u **arg;
16388 char_u **alias;
16389 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016390 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391{
16392 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 char_u *p;
16394 char_u *expr_start;
16395 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396
16397 *alias = NULL; /* default to no alias */
16398
16399 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16400 && (*arg)[2] == (int)KE_SNR)
16401 {
16402 /* hard coded <SNR>, already translated */
16403 *arg += 3;
16404 return get_id_len(arg) + 3;
16405 }
16406 len = eval_fname_script(*arg);
16407 if (len > 0)
16408 {
16409 /* literal "<SID>", "s:" or "<SNR>" */
16410 *arg += len;
16411 }
16412
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016414 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016416 p = find_name_end(*arg, &expr_start, &expr_end,
16417 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 if (expr_start != NULL)
16419 {
16420 char_u *temp_string;
16421
16422 if (!evaluate)
16423 {
16424 len += (int)(p - *arg);
16425 *arg = skipwhite(p);
16426 return len;
16427 }
16428
16429 /*
16430 * Include any <SID> etc in the expanded string:
16431 * Thus the -len here.
16432 */
16433 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16434 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016435 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 *alias = temp_string;
16437 *arg = skipwhite(p);
16438 return (int)STRLEN(temp_string);
16439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440
16441 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016442 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 EMSG2(_(e_invexpr2), *arg);
16444
16445 return len;
16446}
16447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016448/*
16449 * Find the end of a variable or function name, taking care of magic braces.
16450 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16451 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016452 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016453 * Return a pointer to just after the name. Equal to "arg" if there is no
16454 * valid name.
16455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016457find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 char_u *arg;
16459 char_u **expr_start;
16460 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016461 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016463 int mb_nest = 0;
16464 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 char_u *p;
16466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016467 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469 *expr_start = NULL;
16470 *expr_end = NULL;
16471 }
16472
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016473 /* Quick check for valid starting character. */
16474 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16475 return arg;
16476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016477 for (p = arg; *p != NUL
16478 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016479 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016480 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016481 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016482 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016483 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016484 if (*p == '\'')
16485 {
16486 /* skip over 'string' to avoid counting [ and ] inside it. */
16487 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16488 ;
16489 if (*p == NUL)
16490 break;
16491 }
16492 else if (*p == '"')
16493 {
16494 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16495 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16496 if (*p == '\\' && p[1] != NUL)
16497 ++p;
16498 if (*p == NUL)
16499 break;
16500 }
16501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016504 if (*p == '[')
16505 ++br_nest;
16506 else if (*p == ']')
16507 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016512 if (*p == '{')
16513 {
16514 mb_nest++;
16515 if (expr_start != NULL && *expr_start == NULL)
16516 *expr_start = p;
16517 }
16518 else if (*p == '}')
16519 {
16520 mb_nest--;
16521 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16522 *expr_end = p;
16523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 }
16526
16527 return p;
16528}
16529
16530/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016531 * Expands out the 'magic' {}'s in a variable/function name.
16532 * Note that this can call itself recursively, to deal with
16533 * constructs like foo{bar}{baz}{bam}
16534 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16535 * "in_start" ^
16536 * "expr_start" ^
16537 * "expr_end" ^
16538 * "in_end" ^
16539 *
16540 * Returns a new allocated string, which the caller must free.
16541 * Returns NULL for failure.
16542 */
16543 static char_u *
16544make_expanded_name(in_start, expr_start, expr_end, in_end)
16545 char_u *in_start;
16546 char_u *expr_start;
16547 char_u *expr_end;
16548 char_u *in_end;
16549{
16550 char_u c1;
16551 char_u *retval = NULL;
16552 char_u *temp_result;
16553 char_u *nextcmd = NULL;
16554
16555 if (expr_end == NULL || in_end == NULL)
16556 return NULL;
16557 *expr_start = NUL;
16558 *expr_end = NUL;
16559 c1 = *in_end;
16560 *in_end = NUL;
16561
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016562 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016563 if (temp_result != NULL && nextcmd == NULL)
16564 {
16565 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16566 + (in_end - expr_end) + 1));
16567 if (retval != NULL)
16568 {
16569 STRCPY(retval, in_start);
16570 STRCAT(retval, temp_result);
16571 STRCAT(retval, expr_end + 1);
16572 }
16573 }
16574 vim_free(temp_result);
16575
16576 *in_end = c1; /* put char back for error messages */
16577 *expr_start = '{';
16578 *expr_end = '}';
16579
16580 if (retval != NULL)
16581 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016582 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016583 if (expr_start != NULL)
16584 {
16585 /* Further expansion! */
16586 temp_result = make_expanded_name(retval, expr_start,
16587 expr_end, temp_result);
16588 vim_free(retval);
16589 retval = temp_result;
16590 }
16591 }
16592
16593 return retval;
16594}
16595
16596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016598 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 */
16600 static int
16601eval_isnamec(c)
16602 int c;
16603{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016604 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16605}
16606
16607/*
16608 * Return TRUE if character "c" can be used as the first character in a
16609 * variable or function name (excluding '{' and '}').
16610 */
16611 static int
16612eval_isnamec1(c)
16613 int c;
16614{
16615 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016616}
16617
16618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 * Set number v: variable to "val".
16620 */
16621 void
16622set_vim_var_nr(idx, val)
16623 int idx;
16624 long val;
16625{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016626 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627}
16628
16629/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016630 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 */
16632 long
16633get_vim_var_nr(idx)
16634 int idx;
16635{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016636 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637}
16638
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016639#if defined(FEAT_AUTOCMD) || defined(PROTO)
16640/*
16641 * Get string v: variable value. Uses a static buffer, can only be used once.
16642 */
16643 char_u *
16644get_vim_var_str(idx)
16645 int idx;
16646{
16647 return get_tv_string(&vimvars[idx].vv_tv);
16648}
16649#endif
16650
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651/*
16652 * Set v:count, v:count1 and v:prevcount.
16653 */
16654 void
16655set_vcount(count, count1)
16656 long count;
16657 long count1;
16658{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016659 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16660 vimvars[VV_COUNT].vv_nr = count;
16661 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662}
16663
16664/*
16665 * Set string v: variable to a copy of "val".
16666 */
16667 void
16668set_vim_var_string(idx, val, len)
16669 int idx;
16670 char_u *val;
16671 int len; /* length of "val" to use or -1 (whole string) */
16672{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016673 /* Need to do this (at least) once, since we can't initialize a union.
16674 * Will always be invoked when "v:progname" is set. */
16675 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16676
Bram Moolenaare9a41262005-01-15 22:18:47 +000016677 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016679 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016681 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016683 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684}
16685
16686/*
16687 * Set v:register if needed.
16688 */
16689 void
16690set_reg_var(c)
16691 int c;
16692{
16693 char_u regname;
16694
16695 if (c == 0 || c == ' ')
16696 regname = '"';
16697 else
16698 regname = c;
16699 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016700 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 set_vim_var_string(VV_REG, &regname, 1);
16702}
16703
16704/*
16705 * Get or set v:exception. If "oldval" == NULL, return the current value.
16706 * Otherwise, restore the value to "oldval" and return NULL.
16707 * Must always be called in pairs to save and restore v:exception! Does not
16708 * take care of memory allocations.
16709 */
16710 char_u *
16711v_exception(oldval)
16712 char_u *oldval;
16713{
16714 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016715 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716
Bram Moolenaare9a41262005-01-15 22:18:47 +000016717 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 return NULL;
16719}
16720
16721/*
16722 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16723 * Otherwise, restore the value to "oldval" and return NULL.
16724 * Must always be called in pairs to save and restore v:throwpoint! Does not
16725 * take care of memory allocations.
16726 */
16727 char_u *
16728v_throwpoint(oldval)
16729 char_u *oldval;
16730{
16731 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016732 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733
Bram Moolenaare9a41262005-01-15 22:18:47 +000016734 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735 return NULL;
16736}
16737
16738#if defined(FEAT_AUTOCMD) || defined(PROTO)
16739/*
16740 * Set v:cmdarg.
16741 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16742 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16743 * Must always be called in pairs!
16744 */
16745 char_u *
16746set_cmdarg(eap, oldarg)
16747 exarg_T *eap;
16748 char_u *oldarg;
16749{
16750 char_u *oldval;
16751 char_u *newval;
16752 unsigned len;
16753
Bram Moolenaare9a41262005-01-15 22:18:47 +000016754 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016755 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016757 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016758 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016759 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 }
16761
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016762 if (eap->force_bin == FORCE_BIN)
16763 len = 6;
16764 else if (eap->force_bin == FORCE_NOBIN)
16765 len = 8;
16766 else
16767 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016768
16769 if (eap->read_edit)
16770 len += 7;
16771
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016772 if (eap->force_ff != 0)
16773 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16774# ifdef FEAT_MBYTE
16775 if (eap->force_enc != 0)
16776 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016777 if (eap->bad_char != 0)
16778 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016779# endif
16780
16781 newval = alloc(len + 1);
16782 if (newval == NULL)
16783 return NULL;
16784
16785 if (eap->force_bin == FORCE_BIN)
16786 sprintf((char *)newval, " ++bin");
16787 else if (eap->force_bin == FORCE_NOBIN)
16788 sprintf((char *)newval, " ++nobin");
16789 else
16790 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016791
16792 if (eap->read_edit)
16793 STRCAT(newval, " ++edit");
16794
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016795 if (eap->force_ff != 0)
16796 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16797 eap->cmd + eap->force_ff);
16798# ifdef FEAT_MBYTE
16799 if (eap->force_enc != 0)
16800 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16801 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016802 if (eap->bad_char != 0)
16803 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16804 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016805# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016806 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016807 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808}
16809#endif
16810
16811/*
16812 * Get the value of internal variable "name".
16813 * Return OK or FAIL.
16814 */
16815 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016816get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 char_u *name;
16818 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016819 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016820 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821{
16822 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016823 typval_T *tv = NULL;
16824 typval_T atv;
16825 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827
16828 /* truncate the name, so that we can use strcmp() */
16829 cc = name[len];
16830 name[len] = NUL;
16831
16832 /*
16833 * Check for "b:changedtick".
16834 */
16835 if (STRCMP(name, "b:changedtick") == 0)
16836 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016837 atv.v_type = VAR_NUMBER;
16838 atv.vval.v_number = curbuf->b_changedtick;
16839 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 }
16841
16842 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 * Check for user-defined variables.
16844 */
16845 else
16846 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016847 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016849 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 }
16851
Bram Moolenaare9a41262005-01-15 22:18:47 +000016852 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016854 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016855 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856 ret = FAIL;
16857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016858 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016859 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860
16861 name[len] = cc;
16862
16863 return ret;
16864}
16865
16866/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016867 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16868 * Also handle function call with Funcref variable: func(expr)
16869 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16870 */
16871 static int
16872handle_subscript(arg, rettv, evaluate, verbose)
16873 char_u **arg;
16874 typval_T *rettv;
16875 int evaluate; /* do more than finding the end */
16876 int verbose; /* give error messages */
16877{
16878 int ret = OK;
16879 dict_T *selfdict = NULL;
16880 char_u *s;
16881 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016882 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016883
16884 while (ret == OK
16885 && (**arg == '['
16886 || (**arg == '.' && rettv->v_type == VAR_DICT)
16887 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16888 && !vim_iswhite(*(*arg - 1)))
16889 {
16890 if (**arg == '(')
16891 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016892 /* need to copy the funcref so that we can clear rettv */
16893 functv = *rettv;
16894 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016895
16896 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016897 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016898 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016899 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16900 &len, evaluate, selfdict);
16901
16902 /* Clear the funcref afterwards, so that deleting it while
16903 * evaluating the arguments is possible (see test55). */
16904 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016905
16906 /* Stop the expression evaluation when immediately aborting on
16907 * error, or when an interrupt occurred or an exception was thrown
16908 * but not caught. */
16909 if (aborting())
16910 {
16911 if (ret == OK)
16912 clear_tv(rettv);
16913 ret = FAIL;
16914 }
16915 dict_unref(selfdict);
16916 selfdict = NULL;
16917 }
16918 else /* **arg == '[' || **arg == '.' */
16919 {
16920 dict_unref(selfdict);
16921 if (rettv->v_type == VAR_DICT)
16922 {
16923 selfdict = rettv->vval.v_dict;
16924 if (selfdict != NULL)
16925 ++selfdict->dv_refcount;
16926 }
16927 else
16928 selfdict = NULL;
16929 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16930 {
16931 clear_tv(rettv);
16932 ret = FAIL;
16933 }
16934 }
16935 }
16936 dict_unref(selfdict);
16937 return ret;
16938}
16939
16940/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016941 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16942 * value).
16943 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016944 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016945alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016946{
Bram Moolenaar33570922005-01-25 22:26:29 +000016947 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016948}
16949
16950/*
16951 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 * The string "s" must have been allocated, it is consumed.
16953 * Return NULL for out of memory, the variable otherwise.
16954 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016955 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016956alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 char_u *s;
16958{
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016961 rettv = alloc_tv();
16962 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016964 rettv->v_type = VAR_STRING;
16965 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 }
16967 else
16968 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016969 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970}
16971
16972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016973 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016975 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016976free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978{
16979 if (varp != NULL)
16980 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016981 switch (varp->v_type)
16982 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016983 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016984 func_unref(varp->vval.v_string);
16985 /*FALLTHROUGH*/
16986 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016987 vim_free(varp->vval.v_string);
16988 break;
16989 case VAR_LIST:
16990 list_unref(varp->vval.v_list);
16991 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016992 case VAR_DICT:
16993 dict_unref(varp->vval.v_dict);
16994 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016995 case VAR_NUMBER:
16996 case VAR_UNKNOWN:
16997 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016998 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016999 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017000 break;
17001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 vim_free(varp);
17003 }
17004}
17005
17006/*
17007 * Free the memory for a variable value and set the value to NULL or 0.
17008 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017009 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017010clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017011 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012{
17013 if (varp != NULL)
17014 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017015 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017017 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017018 func_unref(varp->vval.v_string);
17019 /*FALLTHROUGH*/
17020 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017021 vim_free(varp->vval.v_string);
17022 varp->vval.v_string = NULL;
17023 break;
17024 case VAR_LIST:
17025 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017026 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017027 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017028 case VAR_DICT:
17029 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017030 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017031 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017032 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017033 varp->vval.v_number = 0;
17034 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017035 case VAR_UNKNOWN:
17036 break;
17037 default:
17038 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017040 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 }
17042}
17043
17044/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045 * Set the value of a variable to NULL without freeing items.
17046 */
17047 static void
17048init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017049 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050{
17051 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017052 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017053}
17054
17055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 * Get the number value of a variable.
17057 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 * For incompatible types, return 0.
17059 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17060 * caller of incompatible types: it sets *denote to TRUE if "denote"
17061 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 */
17063 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017064get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 int error = FALSE;
17068
17069 return get_tv_number_chk(varp, &error); /* return 0L on error */
17070}
17071
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017072 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073get_tv_number_chk(varp, denote)
17074 typval_T *varp;
17075 int *denote;
17076{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017077 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017079 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017081 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017083 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017084 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017085 break;
17086 case VAR_STRING:
17087 if (varp->vval.v_string != NULL)
17088 vim_str2nr(varp->vval.v_string, NULL, NULL,
17089 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017090 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017091 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017092 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017093 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017094 case VAR_DICT:
17095 EMSG(_("E728: Using a Dictionary as a number"));
17096 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017097 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017098 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017099 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017101 if (denote == NULL) /* useful for values that must be unsigned */
17102 n = -1;
17103 else
17104 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017105 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106}
17107
17108/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017109 * Get the lnum from the first argument.
17110 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017111 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112 */
17113 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017114get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116{
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 linenr_T lnum;
17119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 if (lnum == 0) /* no valid number, try using line() */
17122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017123 rettv.v_type = VAR_NUMBER;
17124 f_line(argvars, &rettv);
17125 lnum = rettv.vval.v_number;
17126 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 }
17128 return lnum;
17129}
17130
17131/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017132 * Get the lnum from the first argument.
17133 * Also accepts "$", then "buf" is used.
17134 * Returns 0 on error.
17135 */
17136 static linenr_T
17137get_tv_lnum_buf(argvars, buf)
17138 typval_T *argvars;
17139 buf_T *buf;
17140{
17141 if (argvars[0].v_type == VAR_STRING
17142 && argvars[0].vval.v_string != NULL
17143 && argvars[0].vval.v_string[0] == '$'
17144 && buf != NULL)
17145 return buf->b_ml.ml_line_count;
17146 return get_tv_number_chk(&argvars[0], NULL);
17147}
17148
17149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150 * Get the string value of a variable.
17151 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017152 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17153 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 * If the String variable has never been set, return an empty string.
17155 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17157 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 */
17159 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017160get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017161 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162{
17163 static char_u mybuf[NUMBUFLEN];
17164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166}
17167
17168 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017170 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017171 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017173 char_u *res = get_tv_string_buf_chk(varp, buf);
17174
17175 return res != NULL ? res : (char_u *)"";
17176}
17177
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017178 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179get_tv_string_chk(varp)
17180 typval_T *varp;
17181{
17182 static char_u mybuf[NUMBUFLEN];
17183
17184 return get_tv_string_buf_chk(varp, mybuf);
17185}
17186
17187 static char_u *
17188get_tv_string_buf_chk(varp, buf)
17189 typval_T *varp;
17190 char_u *buf;
17191{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017192 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194 case VAR_NUMBER:
17195 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17196 return buf;
17197 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017198 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017199 break;
17200 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017201 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017202 break;
17203 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017204 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017205 break;
17206 case VAR_STRING:
17207 if (varp->vval.v_string != NULL)
17208 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017209 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017210 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017211 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215}
17216
17217/*
17218 * Find variable "name" in the list of variables.
17219 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017220 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017221 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017222 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017224 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017225find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017230 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231
Bram Moolenaara7043832005-01-21 11:56:39 +000017232 ht = find_var_ht(name, &varname);
17233 if (htp != NULL)
17234 *htp = ht;
17235 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017237 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238}
17239
17240/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017241 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017242 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017244 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017245find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017246 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017247 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017248 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017249{
Bram Moolenaar33570922005-01-25 22:26:29 +000017250 hashitem_T *hi;
17251
17252 if (*varname == NUL)
17253 {
17254 /* Must be something like "s:", otherwise "ht" would be NULL. */
17255 switch (varname[-2])
17256 {
17257 case 's': return &SCRIPT_SV(current_SID).sv_var;
17258 case 'g': return &globvars_var;
17259 case 'v': return &vimvars_var;
17260 case 'b': return &curbuf->b_bufvar;
17261 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017262#ifdef FEAT_WINDOWS
17263 case 't': return &curtab->tp_winvar;
17264#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017265 case 'l': return current_funccal == NULL
17266 ? NULL : &current_funccal->l_vars_var;
17267 case 'a': return current_funccal == NULL
17268 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017269 }
17270 return NULL;
17271 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017272
17273 hi = hash_find(ht, varname);
17274 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017275 {
17276 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017277 * worked find the variable again. Don't auto-load a script if it was
17278 * loaded already, otherwise it would be loaded every time when
17279 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017280 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017281 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017282 hi = hash_find(ht, varname);
17283 if (HASHITEM_EMPTY(hi))
17284 return NULL;
17285 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017286 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017287}
17288
17289/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017290 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017291 * Set "varname" to the start of name without ':'.
17292 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017293 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017294find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 char_u *name;
17296 char_u **varname;
17297{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017298 hashitem_T *hi;
17299
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 if (name[1] != ':')
17301 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017302 /* The name must not start with a colon or #. */
17303 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 return NULL;
17305 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017306
17307 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017308 hi = hash_find(&compat_hashtab, name);
17309 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017310 return &compat_hashtab;
17311
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017313 return &globvarht; /* global variable */
17314 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 }
17316 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017317 if (*name == 'g') /* global variable */
17318 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017319 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17320 */
17321 if (vim_strchr(name + 2, ':') != NULL
17322 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017323 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017325 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017327 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017328#ifdef FEAT_WINDOWS
17329 if (*name == 't') /* tab page variable */
17330 return &curtab->tp_vars.dv_hashtab;
17331#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017332 if (*name == 'v') /* v: variable */
17333 return &vimvarht;
17334 if (*name == 'a' && current_funccal != NULL) /* function argument */
17335 return &current_funccal->l_avars.dv_hashtab;
17336 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17337 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 if (*name == 's' /* script variable */
17339 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17340 return &SCRIPT_VARS(current_SID);
17341 return NULL;
17342}
17343
17344/*
17345 * Get the string value of a (global/local) variable.
17346 * Returns NULL when it doesn't exist.
17347 */
17348 char_u *
17349get_var_value(name)
17350 char_u *name;
17351{
Bram Moolenaar33570922005-01-25 22:26:29 +000017352 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaara7043832005-01-21 11:56:39 +000017354 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 if (v == NULL)
17356 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358}
17359
17360/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 * sourcing this script and when executing functions defined in the script.
17363 */
17364 void
17365new_script_vars(id)
17366 scid_T id;
17367{
Bram Moolenaara7043832005-01-21 11:56:39 +000017368 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017369 hashtab_T *ht;
17370 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017371
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17373 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017374 /* Re-allocating ga_data means that an ht_array pointing to
17375 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017377 for (i = 1; i <= ga_scripts.ga_len; ++i)
17378 {
17379 ht = &SCRIPT_VARS(i);
17380 if (ht->ht_mask == HT_INIT_SIZE - 1)
17381 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017382 sv = &SCRIPT_SV(i);
17383 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017384 }
17385
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 while (ga_scripts.ga_len < id)
17387 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17389 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 }
17392 }
17393}
17394
17395/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017396 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17397 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398 */
17399 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017400init_var_dict(dict, dict_var)
17401 dict_T *dict;
17402 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403{
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 hash_init(&dict->dv_hashtab);
17405 dict->dv_refcount = 99999;
17406 dict_var->di_tv.vval.v_dict = dict;
17407 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017408 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17410 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411}
17412
17413/*
17414 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017415 * Frees all allocated variables and the value they contain.
17416 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 */
17418 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017419vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017420 hashtab_T *ht;
17421{
17422 vars_clear_ext(ht, TRUE);
17423}
17424
17425/*
17426 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17427 */
17428 static void
17429vars_clear_ext(ht, free_val)
17430 hashtab_T *ht;
17431 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432{
Bram Moolenaara7043832005-01-21 11:56:39 +000017433 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017434 hashitem_T *hi;
17435 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436
Bram Moolenaar33570922005-01-25 22:26:29 +000017437 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017438 todo = ht->ht_used;
17439 for (hi = ht->ht_array; todo > 0; ++hi)
17440 {
17441 if (!HASHITEM_EMPTY(hi))
17442 {
17443 --todo;
17444
Bram Moolenaar33570922005-01-25 22:26:29 +000017445 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017446 * ht_array might change then. hash_clear() takes care of it
17447 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017448 v = HI2DI(hi);
17449 if (free_val)
17450 clear_tv(&v->di_tv);
17451 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17452 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017453 }
17454 }
17455 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017456 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457}
17458
Bram Moolenaara7043832005-01-21 11:56:39 +000017459/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017460 * Delete a variable from hashtab "ht" at item "hi".
17461 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017464delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 hashtab_T *ht;
17466 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467{
Bram Moolenaar33570922005-01-25 22:26:29 +000017468 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017469
17470 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017471 clear_tv(&di->di_tv);
17472 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473}
17474
17475/*
17476 * List the value of one internal variable.
17477 */
17478 static void
17479list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 char_u *prefix;
17482{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017483 char_u *tofree;
17484 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017486
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017487 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017488 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017489 s == NULL ? (char_u *)"" : s);
17490 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491}
17492
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 static void
17494list_one_var_a(prefix, name, type, string)
17495 char_u *prefix;
17496 char_u *name;
17497 int type;
17498 char_u *string;
17499{
17500 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17501 if (name != NULL) /* "a:" vars don't have a name stored */
17502 msg_puts(name);
17503 msg_putchar(' ');
17504 msg_advance(22);
17505 if (type == VAR_NUMBER)
17506 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017507 else if (type == VAR_FUNC)
17508 msg_putchar('*');
17509 else if (type == VAR_LIST)
17510 {
17511 msg_putchar('[');
17512 if (*string == '[')
17513 ++string;
17514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017515 else if (type == VAR_DICT)
17516 {
17517 msg_putchar('{');
17518 if (*string == '{')
17519 ++string;
17520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 else
17522 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017523
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017525
17526 if (type == VAR_FUNC)
17527 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528}
17529
17530/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017531 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532 * If the variable already exists, the value is updated.
17533 * Otherwise the variable is created.
17534 */
17535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017536set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017538 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540{
Bram Moolenaar33570922005-01-25 22:26:29 +000017541 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017544 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017547 {
17548 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17549 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17550 ? name[2] : name[0]))
17551 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017552 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017553 return;
17554 }
17555 if (function_exists(name))
17556 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017557 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017558 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017559 return;
17560 }
17561 }
17562
Bram Moolenaara7043832005-01-21 11:56:39 +000017563 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017565 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017566 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017567 return;
17568 }
17569
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017570 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017571 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017573 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017574 if (var_check_ro(v->di_flags, name)
17575 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 return;
17577 if (v->di_tv.v_type != tv->v_type
17578 && !((v->di_tv.v_type == VAR_STRING
17579 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580 && (tv->v_type == VAR_STRING
17581 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017582 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017583 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017584 return;
17585 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017586
17587 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017588 * Handle setting internal v: variables separately: we don't change
17589 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017590 */
17591 if (ht == &vimvarht)
17592 {
17593 if (v->di_tv.v_type == VAR_STRING)
17594 {
17595 vim_free(v->di_tv.vval.v_string);
17596 if (copy || tv->v_type != VAR_STRING)
17597 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17598 else
17599 {
17600 /* Take over the string to avoid an extra alloc/free. */
17601 v->di_tv.vval.v_string = tv->vval.v_string;
17602 tv->vval.v_string = NULL;
17603 }
17604 }
17605 else if (v->di_tv.v_type != VAR_NUMBER)
17606 EMSG2(_(e_intern2), "set_var()");
17607 else
17608 v->di_tv.vval.v_number = get_tv_number(tv);
17609 return;
17610 }
17611
17612 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 }
17614 else /* add a new variable */
17615 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017616 /* Make sure the variable name is valid. */
17617 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017618 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17619 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017620 {
17621 EMSG2(_(e_illvar), varname);
17622 return;
17623 }
17624
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017625 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17626 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017627 if (v == NULL)
17628 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017629 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017630 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017632 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 return;
17634 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017635 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017638 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017640 else
17641 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017643 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017644 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646}
17647
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017648/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017649 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17650 * Also give an error message.
17651 */
17652 static int
17653var_check_ro(flags, name)
17654 int flags;
17655 char_u *name;
17656{
17657 if (flags & DI_FLAGS_RO)
17658 {
17659 EMSG2(_(e_readonlyvar), name);
17660 return TRUE;
17661 }
17662 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17663 {
17664 EMSG2(_(e_readonlysbx), name);
17665 return TRUE;
17666 }
17667 return FALSE;
17668}
17669
17670/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017671 * Return TRUE if typeval "tv" is set to be locked (immutable).
17672 * Also give an error message, using "name".
17673 */
17674 static int
17675tv_check_lock(lock, name)
17676 int lock;
17677 char_u *name;
17678{
17679 if (lock & VAR_LOCKED)
17680 {
17681 EMSG2(_("E741: Value is locked: %s"),
17682 name == NULL ? (char_u *)_("Unknown") : name);
17683 return TRUE;
17684 }
17685 if (lock & VAR_FIXED)
17686 {
17687 EMSG2(_("E742: Cannot change value of %s"),
17688 name == NULL ? (char_u *)_("Unknown") : name);
17689 return TRUE;
17690 }
17691 return FALSE;
17692}
17693
17694/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017696 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017697 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017701 typval_T *from;
17702 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017704 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017705 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017706 switch (from->v_type)
17707 {
17708 case VAR_NUMBER:
17709 to->vval.v_number = from->vval.v_number;
17710 break;
17711 case VAR_STRING:
17712 case VAR_FUNC:
17713 if (from->vval.v_string == NULL)
17714 to->vval.v_string = NULL;
17715 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017717 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017718 if (from->v_type == VAR_FUNC)
17719 func_ref(to->vval.v_string);
17720 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017721 break;
17722 case VAR_LIST:
17723 if (from->vval.v_list == NULL)
17724 to->vval.v_list = NULL;
17725 else
17726 {
17727 to->vval.v_list = from->vval.v_list;
17728 ++to->vval.v_list->lv_refcount;
17729 }
17730 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017731 case VAR_DICT:
17732 if (from->vval.v_dict == NULL)
17733 to->vval.v_dict = NULL;
17734 else
17735 {
17736 to->vval.v_dict = from->vval.v_dict;
17737 ++to->vval.v_dict->dv_refcount;
17738 }
17739 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017740 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017741 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017742 break;
17743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744}
17745
17746/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017747 * Make a copy of an item.
17748 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017749 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17750 * reference to an already copied list/dict can be used.
17751 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017752 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017753 static int
17754item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 typval_T *from;
17756 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017757 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017758 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017759{
17760 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017761 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017762
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017764 {
17765 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017766 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017767 }
17768 ++recurse;
17769
17770 switch (from->v_type)
17771 {
17772 case VAR_NUMBER:
17773 case VAR_STRING:
17774 case VAR_FUNC:
17775 copy_tv(from, to);
17776 break;
17777 case VAR_LIST:
17778 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017779 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017780 if (from->vval.v_list == NULL)
17781 to->vval.v_list = NULL;
17782 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17783 {
17784 /* use the copy made earlier */
17785 to->vval.v_list = from->vval.v_list->lv_copylist;
17786 ++to->vval.v_list->lv_refcount;
17787 }
17788 else
17789 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17790 if (to->vval.v_list == NULL)
17791 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017792 break;
17793 case VAR_DICT:
17794 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017795 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017796 if (from->vval.v_dict == NULL)
17797 to->vval.v_dict = NULL;
17798 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17799 {
17800 /* use the copy made earlier */
17801 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17802 ++to->vval.v_dict->dv_refcount;
17803 }
17804 else
17805 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17806 if (to->vval.v_dict == NULL)
17807 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017808 break;
17809 default:
17810 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017811 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017812 }
17813 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017814 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017815}
17816
17817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 * ":echo expr1 ..." print each argument separated with a space, add a
17819 * newline at the end.
17820 * ":echon expr1 ..." print each argument plain.
17821 */
17822 void
17823ex_echo(eap)
17824 exarg_T *eap;
17825{
17826 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017827 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017828 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 char_u *p;
17830 int needclr = TRUE;
17831 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017832 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833
17834 if (eap->skip)
17835 ++emsg_skip;
17836 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17837 {
17838 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 {
17841 /*
17842 * Report the invalid expression unless the expression evaluation
17843 * has been cancelled due to an aborting error, an interrupt, or an
17844 * exception.
17845 */
17846 if (!aborting())
17847 EMSG2(_(e_invexpr2), p);
17848 break;
17849 }
17850 if (!eap->skip)
17851 {
17852 if (atstart)
17853 {
17854 atstart = FALSE;
17855 /* Call msg_start() after eval1(), evaluating the expression
17856 * may cause a message to appear. */
17857 if (eap->cmdidx == CMD_echo)
17858 msg_start();
17859 }
17860 else if (eap->cmdidx == CMD_echo)
17861 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017862 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017863 if (p != NULL)
17864 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017866 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017868 if (*p != TAB && needclr)
17869 {
17870 /* remove any text still there from the command */
17871 msg_clr_eos();
17872 needclr = FALSE;
17873 }
17874 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 }
17876 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017877 {
17878#ifdef FEAT_MBYTE
17879 if (has_mbyte)
17880 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017881 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017882
17883 (void)msg_outtrans_len_attr(p, i, echo_attr);
17884 p += i - 1;
17885 }
17886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017888 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017891 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017893 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 arg = skipwhite(arg);
17895 }
17896 eap->nextcmd = check_nextcmd(arg);
17897
17898 if (eap->skip)
17899 --emsg_skip;
17900 else
17901 {
17902 /* remove text that may still be there from the command */
17903 if (needclr)
17904 msg_clr_eos();
17905 if (eap->cmdidx == CMD_echo)
17906 msg_end();
17907 }
17908}
17909
17910/*
17911 * ":echohl {name}".
17912 */
17913 void
17914ex_echohl(eap)
17915 exarg_T *eap;
17916{
17917 int id;
17918
17919 id = syn_name2id(eap->arg);
17920 if (id == 0)
17921 echo_attr = 0;
17922 else
17923 echo_attr = syn_id2attr(id);
17924}
17925
17926/*
17927 * ":execute expr1 ..." execute the result of an expression.
17928 * ":echomsg expr1 ..." Print a message
17929 * ":echoerr expr1 ..." Print an error
17930 * Each gets spaces around each argument and a newline at the end for
17931 * echo commands
17932 */
17933 void
17934ex_execute(eap)
17935 exarg_T *eap;
17936{
17937 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017938 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 int ret = OK;
17940 char_u *p;
17941 garray_T ga;
17942 int len;
17943 int save_did_emsg;
17944
17945 ga_init2(&ga, 1, 80);
17946
17947 if (eap->skip)
17948 ++emsg_skip;
17949 while (*arg != NUL && *arg != '|' && *arg != '\n')
17950 {
17951 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017952 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 {
17954 /*
17955 * Report the invalid expression unless the expression evaluation
17956 * has been cancelled due to an aborting error, an interrupt, or an
17957 * exception.
17958 */
17959 if (!aborting())
17960 EMSG2(_(e_invexpr2), p);
17961 ret = FAIL;
17962 break;
17963 }
17964
17965 if (!eap->skip)
17966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017967 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 len = (int)STRLEN(p);
17969 if (ga_grow(&ga, len + 2) == FAIL)
17970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017971 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 ret = FAIL;
17973 break;
17974 }
17975 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 ga.ga_len += len;
17979 }
17980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 arg = skipwhite(arg);
17983 }
17984
17985 if (ret != FAIL && ga.ga_data != NULL)
17986 {
17987 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017990 out_flush();
17991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 else if (eap->cmdidx == CMD_echoerr)
17993 {
17994 /* We don't want to abort following commands, restore did_emsg. */
17995 save_did_emsg = did_emsg;
17996 EMSG((char_u *)ga.ga_data);
17997 if (!force_abort)
17998 did_emsg = save_did_emsg;
17999 }
18000 else if (eap->cmdidx == CMD_execute)
18001 do_cmdline((char_u *)ga.ga_data,
18002 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18003 }
18004
18005 ga_clear(&ga);
18006
18007 if (eap->skip)
18008 --emsg_skip;
18009
18010 eap->nextcmd = check_nextcmd(arg);
18011}
18012
18013/*
18014 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18015 * "arg" points to the "&" or '+' when called, to "option" when returning.
18016 * Returns NULL when no option name found. Otherwise pointer to the char
18017 * after the option name.
18018 */
18019 static char_u *
18020find_option_end(arg, opt_flags)
18021 char_u **arg;
18022 int *opt_flags;
18023{
18024 char_u *p = *arg;
18025
18026 ++p;
18027 if (*p == 'g' && p[1] == ':')
18028 {
18029 *opt_flags = OPT_GLOBAL;
18030 p += 2;
18031 }
18032 else if (*p == 'l' && p[1] == ':')
18033 {
18034 *opt_flags = OPT_LOCAL;
18035 p += 2;
18036 }
18037 else
18038 *opt_flags = 0;
18039
18040 if (!ASCII_ISALPHA(*p))
18041 return NULL;
18042 *arg = p;
18043
18044 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18045 p += 4; /* termcap option */
18046 else
18047 while (ASCII_ISALPHA(*p))
18048 ++p;
18049 return p;
18050}
18051
18052/*
18053 * ":function"
18054 */
18055 void
18056ex_function(eap)
18057 exarg_T *eap;
18058{
18059 char_u *theline;
18060 int j;
18061 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 char_u *name = NULL;
18064 char_u *p;
18065 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018066 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 garray_T newargs;
18068 garray_T newlines;
18069 int varargs = FALSE;
18070 int mustend = FALSE;
18071 int flags = 0;
18072 ufunc_T *fp;
18073 int indent;
18074 int nesting;
18075 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018076 dictitem_T *v;
18077 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018078 static int func_nr = 0; /* number for nameless function */
18079 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018080 hashtab_T *ht;
18081 int todo;
18082 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018083 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
18085 /*
18086 * ":function" without argument: list functions.
18087 */
18088 if (ends_excmd(*eap->arg))
18089 {
18090 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018091 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018092 todo = func_hashtab.ht_used;
18093 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018094 {
18095 if (!HASHITEM_EMPTY(hi))
18096 {
18097 --todo;
18098 fp = HI2UF(hi);
18099 if (!isdigit(*fp->uf_name))
18100 list_func_head(fp, FALSE);
18101 }
18102 }
18103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 eap->nextcmd = check_nextcmd(eap->arg);
18105 return;
18106 }
18107
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018108 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018109 * ":function /pat": list functions matching pattern.
18110 */
18111 if (*eap->arg == '/')
18112 {
18113 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18114 if (!eap->skip)
18115 {
18116 regmatch_T regmatch;
18117
18118 c = *p;
18119 *p = NUL;
18120 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18121 *p = c;
18122 if (regmatch.regprog != NULL)
18123 {
18124 regmatch.rm_ic = p_ic;
18125
18126 todo = func_hashtab.ht_used;
18127 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18128 {
18129 if (!HASHITEM_EMPTY(hi))
18130 {
18131 --todo;
18132 fp = HI2UF(hi);
18133 if (!isdigit(*fp->uf_name)
18134 && vim_regexec(&regmatch, fp->uf_name, 0))
18135 list_func_head(fp, FALSE);
18136 }
18137 }
18138 }
18139 }
18140 if (*p == '/')
18141 ++p;
18142 eap->nextcmd = check_nextcmd(p);
18143 return;
18144 }
18145
18146 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018147 * Get the function name. There are these situations:
18148 * func normal function name
18149 * "name" == func, "fudi.fd_dict" == NULL
18150 * dict.func new dictionary entry
18151 * "name" == NULL, "fudi.fd_dict" set,
18152 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18153 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018154 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018155 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18156 * dict.func existing dict entry that's not a Funcref
18157 * "name" == NULL, "fudi.fd_dict" set,
18158 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018161 name = trans_function_name(&p, eap->skip, 0, &fudi);
18162 paren = (vim_strchr(p, '(') != NULL);
18163 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 {
18165 /*
18166 * Return on an invalid expression in braces, unless the expression
18167 * evaluation has been cancelled due to an aborting error, an
18168 * interrupt, or an exception.
18169 */
18170 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018172 if (!eap->skip && fudi.fd_newkey != NULL)
18173 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018174 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 else
18178 eap->skip = TRUE;
18179 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018180
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 /* An error in a function call during evaluation of an expression in magic
18182 * braces should not cause the function not to be defined. */
18183 saved_did_emsg = did_emsg;
18184 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185
18186 /*
18187 * ":function func" with only function name: list function.
18188 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018189 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 {
18191 if (!ends_excmd(*skipwhite(p)))
18192 {
18193 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018194 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 }
18196 eap->nextcmd = check_nextcmd(p);
18197 if (eap->nextcmd != NULL)
18198 *p = NUL;
18199 if (!eap->skip && !got_int)
18200 {
18201 fp = find_func(name);
18202 if (fp != NULL)
18203 {
18204 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018205 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018207 if (FUNCLINE(fp, j) == NULL)
18208 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 msg_putchar('\n');
18210 msg_outnum((long)(j + 1));
18211 if (j < 9)
18212 msg_putchar(' ');
18213 if (j < 99)
18214 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018215 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 out_flush(); /* show a line at a time */
18217 ui_breakcheck();
18218 }
18219 if (!got_int)
18220 {
18221 msg_putchar('\n');
18222 msg_puts((char_u *)" endfunction");
18223 }
18224 }
18225 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018226 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018228 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 }
18230
18231 /*
18232 * ":function name(arg1, arg2)" Define function.
18233 */
18234 p = skipwhite(p);
18235 if (*p != '(')
18236 {
18237 if (!eap->skip)
18238 {
18239 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018240 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 }
18242 /* attempt to continue by skipping some text */
18243 if (vim_strchr(p, '(') != NULL)
18244 p = vim_strchr(p, '(');
18245 }
18246 p = skipwhite(p + 1);
18247
18248 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18249 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18250
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018251 if (!eap->skip)
18252 {
18253 /* Check the name of the function. */
18254 if (name != NULL)
18255 arg = name;
18256 else
18257 arg = fudi.fd_newkey;
18258 if (arg != NULL)
18259 {
18260 if (*arg == K_SPECIAL)
18261 j = 3;
18262 else
18263 j = 0;
18264 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18265 : eval_isnamec(arg[j])))
18266 ++j;
18267 if (arg[j] != NUL)
18268 emsg_funcname(_(e_invarg2), arg);
18269 }
18270 }
18271
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 /*
18273 * Isolate the arguments: "arg1, arg2, ...)"
18274 */
18275 while (*p != ')')
18276 {
18277 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18278 {
18279 varargs = TRUE;
18280 p += 3;
18281 mustend = TRUE;
18282 }
18283 else
18284 {
18285 arg = p;
18286 while (ASCII_ISALNUM(*p) || *p == '_')
18287 ++p;
18288 if (arg == p || isdigit(*arg)
18289 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18290 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18291 {
18292 if (!eap->skip)
18293 EMSG2(_("E125: Illegal argument: %s"), arg);
18294 break;
18295 }
18296 if (ga_grow(&newargs, 1) == FAIL)
18297 goto erret;
18298 c = *p;
18299 *p = NUL;
18300 arg = vim_strsave(arg);
18301 if (arg == NULL)
18302 goto erret;
18303 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18304 *p = c;
18305 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 if (*p == ',')
18307 ++p;
18308 else
18309 mustend = TRUE;
18310 }
18311 p = skipwhite(p);
18312 if (mustend && *p != ')')
18313 {
18314 if (!eap->skip)
18315 EMSG2(_(e_invarg2), eap->arg);
18316 break;
18317 }
18318 }
18319 ++p; /* skip the ')' */
18320
Bram Moolenaare9a41262005-01-15 22:18:47 +000018321 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 for (;;)
18323 {
18324 p = skipwhite(p);
18325 if (STRNCMP(p, "range", 5) == 0)
18326 {
18327 flags |= FC_RANGE;
18328 p += 5;
18329 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018330 else if (STRNCMP(p, "dict", 4) == 0)
18331 {
18332 flags |= FC_DICT;
18333 p += 4;
18334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 else if (STRNCMP(p, "abort", 5) == 0)
18336 {
18337 flags |= FC_ABORT;
18338 p += 5;
18339 }
18340 else
18341 break;
18342 }
18343
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018344 /* When there is a line break use what follows for the function body.
18345 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18346 if (*p == '\n')
18347 line_arg = p + 1;
18348 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 EMSG(_(e_trailing));
18350
18351 /*
18352 * Read the body of the function, until ":endfunction" is found.
18353 */
18354 if (KeyTyped)
18355 {
18356 /* Check if the function already exists, don't let the user type the
18357 * whole function before telling him it doesn't work! For a script we
18358 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018359 if (!eap->skip && !eap->forceit)
18360 {
18361 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18362 EMSG(_(e_funcdict));
18363 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018364 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018367 if (!eap->skip && did_emsg)
18368 goto erret;
18369
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 msg_putchar('\n'); /* don't overwrite the function name */
18371 cmdline_row = msg_row;
18372 }
18373
18374 indent = 2;
18375 nesting = 0;
18376 for (;;)
18377 {
18378 msg_scroll = TRUE;
18379 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018380 sourcing_lnum_off = sourcing_lnum;
18381
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018382 if (line_arg != NULL)
18383 {
18384 /* Use eap->arg, split up in parts by line breaks. */
18385 theline = line_arg;
18386 p = vim_strchr(theline, '\n');
18387 if (p == NULL)
18388 line_arg += STRLEN(line_arg);
18389 else
18390 {
18391 *p = NUL;
18392 line_arg = p + 1;
18393 }
18394 }
18395 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 theline = getcmdline(':', 0L, indent);
18397 else
18398 theline = eap->getline(':', eap->cookie, indent);
18399 if (KeyTyped)
18400 lines_left = Rows - 1;
18401 if (theline == NULL)
18402 {
18403 EMSG(_("E126: Missing :endfunction"));
18404 goto erret;
18405 }
18406
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018407 /* Detect line continuation: sourcing_lnum increased more than one. */
18408 if (sourcing_lnum > sourcing_lnum_off + 1)
18409 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18410 else
18411 sourcing_lnum_off = 0;
18412
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 if (skip_until != NULL)
18414 {
18415 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18416 * don't check for ":endfunc". */
18417 if (STRCMP(theline, skip_until) == 0)
18418 {
18419 vim_free(skip_until);
18420 skip_until = NULL;
18421 }
18422 }
18423 else
18424 {
18425 /* skip ':' and blanks*/
18426 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18427 ;
18428
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018429 /* Check for "endfunction". */
18430 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018432 if (line_arg == NULL)
18433 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 break;
18435 }
18436
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018437 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 * at "end". */
18439 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18440 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018441 else if (STRNCMP(p, "if", 2) == 0
18442 || STRNCMP(p, "wh", 2) == 0
18443 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444 || STRNCMP(p, "try", 3) == 0)
18445 indent += 2;
18446
18447 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018448 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018450 if (*p == '!')
18451 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 p += eval_fname_script(p);
18453 if (ASCII_ISALPHA(*p))
18454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018455 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 if (*skipwhite(p) == '(')
18457 {
18458 ++nesting;
18459 indent += 2;
18460 }
18461 }
18462 }
18463
18464 /* Check for ":append" or ":insert". */
18465 p = skip_range(p, NULL);
18466 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18467 || (p[0] == 'i'
18468 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18469 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18470 skip_until = vim_strsave((char_u *)".");
18471
18472 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18473 arg = skipwhite(skiptowhite(p));
18474 if (arg[0] == '<' && arg[1] =='<'
18475 && ((p[0] == 'p' && p[1] == 'y'
18476 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18477 || (p[0] == 'p' && p[1] == 'e'
18478 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18479 || (p[0] == 't' && p[1] == 'c'
18480 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18481 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18482 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018483 || (p[0] == 'm' && p[1] == 'z'
18484 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 ))
18486 {
18487 /* ":python <<" continues until a dot, like ":append" */
18488 p = skipwhite(arg + 2);
18489 if (*p == NUL)
18490 skip_until = vim_strsave((char_u *)".");
18491 else
18492 skip_until = vim_strsave(p);
18493 }
18494 }
18495
18496 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018497 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018498 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018499 if (line_arg == NULL)
18500 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018502 }
18503
18504 /* Copy the line to newly allocated memory. get_one_sourceline()
18505 * allocates 250 bytes per line, this saves 80% on average. The cost
18506 * is an extra alloc/free. */
18507 p = vim_strsave(theline);
18508 if (p != NULL)
18509 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018510 if (line_arg == NULL)
18511 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018512 theline = p;
18513 }
18514
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018515 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18516
18517 /* Add NULL lines for continuation lines, so that the line count is
18518 * equal to the index in the growarray. */
18519 while (sourcing_lnum_off-- > 0)
18520 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018521
18522 /* Check for end of eap->arg. */
18523 if (line_arg != NULL && *line_arg == NUL)
18524 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018525 }
18526
18527 /* Don't define the function when skipping commands or when an error was
18528 * detected. */
18529 if (eap->skip || did_emsg)
18530 goto erret;
18531
18532 /*
18533 * If there are no errors, add the function
18534 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018535 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018536 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018537 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018538 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018539 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018540 emsg_funcname("E707: Function name conflicts with variable: %s",
18541 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018542 goto erret;
18543 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018544
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018545 fp = find_func(name);
18546 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018548 if (!eap->forceit)
18549 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018550 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018551 goto erret;
18552 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018553 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018554 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018555 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018556 name);
18557 goto erret;
18558 }
18559 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018560 ga_clear_strings(&(fp->uf_args));
18561 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018562 vim_free(name);
18563 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 }
18566 else
18567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018568 char numbuf[20];
18569
18570 fp = NULL;
18571 if (fudi.fd_newkey == NULL && !eap->forceit)
18572 {
18573 EMSG(_(e_funcdict));
18574 goto erret;
18575 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018576 if (fudi.fd_di == NULL)
18577 {
18578 /* Can't add a function to a locked dictionary */
18579 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18580 goto erret;
18581 }
18582 /* Can't change an existing function if it is locked */
18583 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18584 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018585
18586 /* Give the function a sequential number. Can only be used with a
18587 * Funcref! */
18588 vim_free(name);
18589 sprintf(numbuf, "%d", ++func_nr);
18590 name = vim_strsave((char_u *)numbuf);
18591 if (name == NULL)
18592 goto erret;
18593 }
18594
18595 if (fp == NULL)
18596 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018597 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018598 {
18599 int slen, plen;
18600 char_u *scriptname;
18601
18602 /* Check that the autoload name matches the script name. */
18603 j = FAIL;
18604 if (sourcing_name != NULL)
18605 {
18606 scriptname = autoload_name(name);
18607 if (scriptname != NULL)
18608 {
18609 p = vim_strchr(scriptname, '/');
18610 plen = STRLEN(p);
18611 slen = STRLEN(sourcing_name);
18612 if (slen > plen && fnamecmp(p,
18613 sourcing_name + slen - plen) == 0)
18614 j = OK;
18615 vim_free(scriptname);
18616 }
18617 }
18618 if (j == FAIL)
18619 {
18620 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18621 goto erret;
18622 }
18623 }
18624
18625 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 if (fp == NULL)
18627 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018628
18629 if (fudi.fd_dict != NULL)
18630 {
18631 if (fudi.fd_di == NULL)
18632 {
18633 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018634 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018635 if (fudi.fd_di == NULL)
18636 {
18637 vim_free(fp);
18638 goto erret;
18639 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018640 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18641 {
18642 vim_free(fudi.fd_di);
18643 goto erret;
18644 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018645 }
18646 else
18647 /* overwrite existing dict entry */
18648 clear_tv(&fudi.fd_di->di_tv);
18649 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018650 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018651 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018652 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018653
18654 /* behave like "dict" was used */
18655 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018656 }
18657
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018659 STRCPY(fp->uf_name, name);
18660 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018662 fp->uf_args = newargs;
18663 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018664#ifdef FEAT_PROFILE
18665 fp->uf_tml_count = NULL;
18666 fp->uf_tml_total = NULL;
18667 fp->uf_tml_self = NULL;
18668 fp->uf_profiling = FALSE;
18669 if (prof_def_func())
18670 func_do_profile(fp);
18671#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018672 fp->uf_varargs = varargs;
18673 fp->uf_flags = flags;
18674 fp->uf_calls = 0;
18675 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018676 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677
18678erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 ga_clear_strings(&newargs);
18680 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018681ret_free:
18682 vim_free(skip_until);
18683 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686}
18687
18688/*
18689 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018690 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018692 * flags:
18693 * TFN_INT: internal function name OK
18694 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 * Advances "pp" to just after the function name (if no error).
18696 */
18697 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018698trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 char_u **pp;
18700 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018701 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018702 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018704 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 char_u *start;
18706 char_u *end;
18707 int lead;
18708 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018710 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018711
18712 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018713 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018715
18716 /* Check for hard coded <SNR>: already translated function ID (from a user
18717 * command). */
18718 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18719 && (*pp)[2] == (int)KE_SNR)
18720 {
18721 *pp += 3;
18722 len = get_id_len(pp) + 3;
18723 return vim_strnsave(start, len);
18724 }
18725
18726 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18727 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018729 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018731
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018732 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18733 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018734 if (end == start)
18735 {
18736 if (!skip)
18737 EMSG(_("E129: Function name required"));
18738 goto theend;
18739 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018740 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018741 {
18742 /*
18743 * Report an invalid expression in braces, unless the expression
18744 * evaluation has been cancelled due to an aborting error, an
18745 * interrupt, or an exception.
18746 */
18747 if (!aborting())
18748 {
18749 if (end != NULL)
18750 EMSG2(_(e_invarg2), start);
18751 }
18752 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018753 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018754 goto theend;
18755 }
18756
18757 if (lv.ll_tv != NULL)
18758 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018759 if (fdp != NULL)
18760 {
18761 fdp->fd_dict = lv.ll_dict;
18762 fdp->fd_newkey = lv.ll_newkey;
18763 lv.ll_newkey = NULL;
18764 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018765 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018766 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18767 {
18768 name = vim_strsave(lv.ll_tv->vval.v_string);
18769 *pp = end;
18770 }
18771 else
18772 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018773 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18774 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018775 EMSG(_(e_funcref));
18776 else
18777 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018778 name = NULL;
18779 }
18780 goto theend;
18781 }
18782
18783 if (lv.ll_name == NULL)
18784 {
18785 /* Error found, but continue after the function name. */
18786 *pp = end;
18787 goto theend;
18788 }
18789
18790 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018792 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018793 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18794 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18795 {
18796 /* When there was "s:" already or the name expanded to get a
18797 * leading "s:" then remove it. */
18798 lv.ll_name += 2;
18799 len -= 2;
18800 lead = 2;
18801 }
18802 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018803 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018804 {
18805 if (lead == 2) /* skip over "s:" */
18806 lv.ll_name += 2;
18807 len = (int)(end - lv.ll_name);
18808 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018809
18810 /*
18811 * Copy the function name to allocated memory.
18812 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18813 * Accept <SNR>123_name() outside a script.
18814 */
18815 if (skip)
18816 lead = 0; /* do nothing */
18817 else if (lead > 0)
18818 {
18819 lead = 3;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018820 if (eval_fname_sid(lv.ll_exp_name != NULL ? lv.ll_exp_name : *pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018821 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000018822 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018823 if (current_SID <= 0)
18824 {
18825 EMSG(_(e_usingsid));
18826 goto theend;
18827 }
18828 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18829 lead += (int)STRLEN(sid_buf);
18830 }
18831 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018832 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018833 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018834 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018835 goto theend;
18836 }
18837 name = alloc((unsigned)(len + lead + 1));
18838 if (name != NULL)
18839 {
18840 if (lead > 0)
18841 {
18842 name[0] = K_SPECIAL;
18843 name[1] = KS_EXTRA;
18844 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018845 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018846 STRCPY(name + 3, sid_buf);
18847 }
18848 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18849 name[len + lead] = NUL;
18850 }
18851 *pp = end;
18852
18853theend:
18854 clear_lval(&lv);
18855 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856}
18857
18858/*
18859 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18860 * Return 2 if "p" starts with "s:".
18861 * Return 0 otherwise.
18862 */
18863 static int
18864eval_fname_script(p)
18865 char_u *p;
18866{
18867 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18868 || STRNICMP(p + 1, "SNR>", 4) == 0))
18869 return 5;
18870 if (p[0] == 's' && p[1] == ':')
18871 return 2;
18872 return 0;
18873}
18874
18875/*
18876 * Return TRUE if "p" starts with "<SID>" or "s:".
18877 * Only works if eval_fname_script() returned non-zero for "p"!
18878 */
18879 static int
18880eval_fname_sid(p)
18881 char_u *p;
18882{
18883 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18884}
18885
18886/*
18887 * List the head of the function: "name(arg1, arg2)".
18888 */
18889 static void
18890list_func_head(fp, indent)
18891 ufunc_T *fp;
18892 int indent;
18893{
18894 int j;
18895
18896 msg_start();
18897 if (indent)
18898 MSG_PUTS(" ");
18899 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018900 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 {
18902 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018903 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 }
18905 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018906 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018908 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 {
18910 if (j)
18911 MSG_PUTS(", ");
18912 msg_puts(FUNCARG(fp, j));
18913 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018914 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 {
18916 if (j)
18917 MSG_PUTS(", ");
18918 MSG_PUTS("...");
18919 }
18920 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018921 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018922 if (p_verbose > 0)
18923 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924}
18925
18926/*
18927 * Find a function by name, return pointer to it in ufuncs.
18928 * Return NULL for unknown function.
18929 */
18930 static ufunc_T *
18931find_func(name)
18932 char_u *name;
18933{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018934 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018936 hi = hash_find(&func_hashtab, name);
18937 if (!HASHITEM_EMPTY(hi))
18938 return HI2UF(hi);
18939 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940}
18941
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018942#if defined(EXITFREE) || defined(PROTO)
18943 void
18944free_all_functions()
18945{
18946 hashitem_T *hi;
18947
18948 /* Need to start all over every time, because func_free() may change the
18949 * hash table. */
18950 while (func_hashtab.ht_used > 0)
18951 for (hi = func_hashtab.ht_array; ; ++hi)
18952 if (!HASHITEM_EMPTY(hi))
18953 {
18954 func_free(HI2UF(hi));
18955 break;
18956 }
18957}
18958#endif
18959
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018960/*
18961 * Return TRUE if a function "name" exists.
18962 */
18963 static int
18964function_exists(name)
18965 char_u *name;
18966{
18967 char_u *p = name;
18968 int n = FALSE;
18969
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018970 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018971 if (p != NULL)
18972 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018973 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018974 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018975 else
18976 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018977 vim_free(p);
18978 }
18979 return n;
18980}
18981
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018982/*
18983 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018984 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018985 */
18986 static int
18987builtin_function(name)
18988 char_u *name;
18989{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018990 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18991 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018992}
18993
Bram Moolenaar05159a02005-02-26 23:04:13 +000018994#if defined(FEAT_PROFILE) || defined(PROTO)
18995/*
18996 * Start profiling function "fp".
18997 */
18998 static void
18999func_do_profile(fp)
19000 ufunc_T *fp;
19001{
19002 fp->uf_tm_count = 0;
19003 profile_zero(&fp->uf_tm_self);
19004 profile_zero(&fp->uf_tm_total);
19005 if (fp->uf_tml_count == NULL)
19006 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19007 (sizeof(int) * fp->uf_lines.ga_len));
19008 if (fp->uf_tml_total == NULL)
19009 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19010 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19011 if (fp->uf_tml_self == NULL)
19012 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19013 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19014 fp->uf_tml_idx = -1;
19015 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19016 || fp->uf_tml_self == NULL)
19017 return; /* out of memory */
19018
19019 fp->uf_profiling = TRUE;
19020}
19021
19022/*
19023 * Dump the profiling results for all functions in file "fd".
19024 */
19025 void
19026func_dump_profile(fd)
19027 FILE *fd;
19028{
19029 hashitem_T *hi;
19030 int todo;
19031 ufunc_T *fp;
19032 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019033 ufunc_T **sorttab;
19034 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019035
19036 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019037 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19038
Bram Moolenaar05159a02005-02-26 23:04:13 +000019039 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19040 {
19041 if (!HASHITEM_EMPTY(hi))
19042 {
19043 --todo;
19044 fp = HI2UF(hi);
19045 if (fp->uf_profiling)
19046 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019047 if (sorttab != NULL)
19048 sorttab[st_len++] = fp;
19049
Bram Moolenaar05159a02005-02-26 23:04:13 +000019050 if (fp->uf_name[0] == K_SPECIAL)
19051 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19052 else
19053 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19054 if (fp->uf_tm_count == 1)
19055 fprintf(fd, "Called 1 time\n");
19056 else
19057 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19058 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19059 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19060 fprintf(fd, "\n");
19061 fprintf(fd, "count total (s) self (s)\n");
19062
19063 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19064 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019065 if (FUNCLINE(fp, i) == NULL)
19066 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019067 prof_func_line(fd, fp->uf_tml_count[i],
19068 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019069 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19070 }
19071 fprintf(fd, "\n");
19072 }
19073 }
19074 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019075
19076 if (sorttab != NULL && st_len > 0)
19077 {
19078 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19079 prof_total_cmp);
19080 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19081 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19082 prof_self_cmp);
19083 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19084 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019085}
Bram Moolenaar73830342005-02-28 22:48:19 +000019086
19087 static void
19088prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19089 FILE *fd;
19090 ufunc_T **sorttab;
19091 int st_len;
19092 char *title;
19093 int prefer_self; /* when equal print only self time */
19094{
19095 int i;
19096 ufunc_T *fp;
19097
19098 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19099 fprintf(fd, "count total (s) self (s) function\n");
19100 for (i = 0; i < 20 && i < st_len; ++i)
19101 {
19102 fp = sorttab[i];
19103 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19104 prefer_self);
19105 if (fp->uf_name[0] == K_SPECIAL)
19106 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19107 else
19108 fprintf(fd, " %s()\n", fp->uf_name);
19109 }
19110 fprintf(fd, "\n");
19111}
19112
19113/*
19114 * Print the count and times for one function or function line.
19115 */
19116 static void
19117prof_func_line(fd, count, total, self, prefer_self)
19118 FILE *fd;
19119 int count;
19120 proftime_T *total;
19121 proftime_T *self;
19122 int prefer_self; /* when equal print only self time */
19123{
19124 if (count > 0)
19125 {
19126 fprintf(fd, "%5d ", count);
19127 if (prefer_self && profile_equal(total, self))
19128 fprintf(fd, " ");
19129 else
19130 fprintf(fd, "%s ", profile_msg(total));
19131 if (!prefer_self && profile_equal(total, self))
19132 fprintf(fd, " ");
19133 else
19134 fprintf(fd, "%s ", profile_msg(self));
19135 }
19136 else
19137 fprintf(fd, " ");
19138}
19139
19140/*
19141 * Compare function for total time sorting.
19142 */
19143 static int
19144#ifdef __BORLANDC__
19145_RTLENTRYF
19146#endif
19147prof_total_cmp(s1, s2)
19148 const void *s1;
19149 const void *s2;
19150{
19151 ufunc_T *p1, *p2;
19152
19153 p1 = *(ufunc_T **)s1;
19154 p2 = *(ufunc_T **)s2;
19155 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19156}
19157
19158/*
19159 * Compare function for self time sorting.
19160 */
19161 static int
19162#ifdef __BORLANDC__
19163_RTLENTRYF
19164#endif
19165prof_self_cmp(s1, s2)
19166 const void *s1;
19167 const void *s2;
19168{
19169 ufunc_T *p1, *p2;
19170
19171 p1 = *(ufunc_T **)s1;
19172 p2 = *(ufunc_T **)s2;
19173 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19174}
19175
Bram Moolenaar05159a02005-02-26 23:04:13 +000019176#endif
19177
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019178/* The names of packages that once were loaded is remembered. */
19179static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19180
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019181/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019182 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019183 * Return TRUE if a package was loaded.
19184 */
19185 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019186script_autoload(name, reload)
19187 char_u *name;
19188 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019189{
19190 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019191 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019192 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019193 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019194
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019195 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019196 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019197 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019198 return FALSE;
19199
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019200 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019201
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019202 /* Find the name in the list of previously loaded package names. Skip
19203 * "autoload/", it's always the same. */
19204 for (i = 0; i < ga_loaded.ga_len; ++i)
19205 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19206 break;
19207 if (!reload && i < ga_loaded.ga_len)
19208 ret = FALSE; /* was loaded already */
19209 else
19210 {
19211 /* Remember the name if it wasn't loaded already. */
19212 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19213 {
19214 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19215 tofree = NULL;
19216 }
19217
19218 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019219 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019220 ret = TRUE;
19221 }
19222
19223 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019224 return ret;
19225}
19226
19227/*
19228 * Return the autoload script name for a function or variable name.
19229 * Returns NULL when out of memory.
19230 */
19231 static char_u *
19232autoload_name(name)
19233 char_u *name;
19234{
19235 char_u *p;
19236 char_u *scriptname;
19237
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019238 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019239 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19240 if (scriptname == NULL)
19241 return FALSE;
19242 STRCPY(scriptname, "autoload/");
19243 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019244 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019245 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019246 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019247 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019248 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019249}
19250
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19252
19253/*
19254 * Function given to ExpandGeneric() to obtain the list of user defined
19255 * function names.
19256 */
19257 char_u *
19258get_user_func_name(xp, idx)
19259 expand_T *xp;
19260 int idx;
19261{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019262 static long_u done;
19263 static hashitem_T *hi;
19264 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265
19266 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019268 done = 0;
19269 hi = func_hashtab.ht_array;
19270 }
19271 if (done < func_hashtab.ht_used)
19272 {
19273 if (done++ > 0)
19274 ++hi;
19275 while (HASHITEM_EMPTY(hi))
19276 ++hi;
19277 fp = HI2UF(hi);
19278
19279 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19280 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281
19282 cat_func_name(IObuff, fp);
19283 if (xp->xp_context != EXPAND_USER_FUNC)
19284 {
19285 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019286 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 STRCAT(IObuff, ")");
19288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 return IObuff;
19290 }
19291 return NULL;
19292}
19293
19294#endif /* FEAT_CMDL_COMPL */
19295
19296/*
19297 * Copy the function name of "fp" to buffer "buf".
19298 * "buf" must be able to hold the function name plus three bytes.
19299 * Takes care of script-local function names.
19300 */
19301 static void
19302cat_func_name(buf, fp)
19303 char_u *buf;
19304 ufunc_T *fp;
19305{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019306 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 {
19308 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019309 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 }
19311 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019312 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313}
19314
19315/*
19316 * ":delfunction {name}"
19317 */
19318 void
19319ex_delfunction(eap)
19320 exarg_T *eap;
19321{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019322 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 char_u *p;
19324 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019325 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326
19327 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019328 name = trans_function_name(&p, eap->skip, 0, &fudi);
19329 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019331 {
19332 if (fudi.fd_dict != NULL && !eap->skip)
19333 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 if (!ends_excmd(*skipwhite(p)))
19337 {
19338 vim_free(name);
19339 EMSG(_(e_trailing));
19340 return;
19341 }
19342 eap->nextcmd = check_nextcmd(p);
19343 if (eap->nextcmd != NULL)
19344 *p = NUL;
19345
19346 if (!eap->skip)
19347 fp = find_func(name);
19348 vim_free(name);
19349
19350 if (!eap->skip)
19351 {
19352 if (fp == NULL)
19353 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019354 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 return;
19356 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019357 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 {
19359 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19360 return;
19361 }
19362
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019363 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019365 /* Delete the dict item that refers to the function, it will
19366 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019367 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019369 else
19370 func_free(fp);
19371 }
19372}
19373
19374/*
19375 * Free a function and remove it from the list of functions.
19376 */
19377 static void
19378func_free(fp)
19379 ufunc_T *fp;
19380{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019381 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019382
19383 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019384 ga_clear_strings(&(fp->uf_args));
19385 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019386#ifdef FEAT_PROFILE
19387 vim_free(fp->uf_tml_count);
19388 vim_free(fp->uf_tml_total);
19389 vim_free(fp->uf_tml_self);
19390#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019391
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019392 /* remove the function from the function hashtable */
19393 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19394 if (HASHITEM_EMPTY(hi))
19395 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019396 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019397 hash_remove(&func_hashtab, hi);
19398
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019399 vim_free(fp);
19400}
19401
19402/*
19403 * Unreference a Function: decrement the reference count and free it when it
19404 * becomes zero. Only for numbered functions.
19405 */
19406 static void
19407func_unref(name)
19408 char_u *name;
19409{
19410 ufunc_T *fp;
19411
19412 if (name != NULL && isdigit(*name))
19413 {
19414 fp = find_func(name);
19415 if (fp == NULL)
19416 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019417 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019418 {
19419 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019420 * when "uf_calls" becomes zero. */
19421 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019422 func_free(fp);
19423 }
19424 }
19425}
19426
19427/*
19428 * Count a reference to a Function.
19429 */
19430 static void
19431func_ref(name)
19432 char_u *name;
19433{
19434 ufunc_T *fp;
19435
19436 if (name != NULL && isdigit(*name))
19437 {
19438 fp = find_func(name);
19439 if (fp == NULL)
19440 EMSG2(_(e_intern2), "func_ref()");
19441 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019442 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 }
19444}
19445
19446/*
19447 * Call a user function.
19448 */
19449 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019450call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 ufunc_T *fp; /* pointer to function */
19452 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019453 typval_T *argvars; /* arguments */
19454 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 linenr_T firstline; /* first line of range */
19456 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458{
Bram Moolenaar33570922005-01-25 22:26:29 +000019459 char_u *save_sourcing_name;
19460 linenr_T save_sourcing_lnum;
19461 scid_T save_current_SID;
19462 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 int save_did_emsg;
19464 static int depth = 0;
19465 dictitem_T *v;
19466 int fixvar_idx = 0; /* index in fixvar[] */
19467 int i;
19468 int ai;
19469 char_u numbuf[NUMBUFLEN];
19470 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019471#ifdef FEAT_PROFILE
19472 proftime_T wait_start;
19473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474
19475 /* If depth of calling is getting too high, don't execute the function */
19476 if (depth >= p_mfd)
19477 {
19478 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019479 rettv->v_type = VAR_NUMBER;
19480 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 return;
19482 }
19483 ++depth;
19484
19485 line_breakcheck(); /* check for CTRL-C hit */
19486
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019487 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019488 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019490 fc.rettv = rettv;
19491 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 fc.linenr = 0;
19493 fc.returned = FALSE;
19494 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019496 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 fc.dbg_tick = debug_tick;
19498
Bram Moolenaar33570922005-01-25 22:26:29 +000019499 /*
19500 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19501 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19502 * each argument variable and saves a lot of time.
19503 */
19504 /*
19505 * Init l: variables.
19506 */
19507 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019508 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019509 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019510 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19511 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019513 name = v->di_key;
19514 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019515 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19516 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19517 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019518 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019519 v->di_tv.vval.v_dict = selfdict;
19520 ++selfdict->dv_refcount;
19521 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019522
Bram Moolenaar33570922005-01-25 22:26:29 +000019523 /*
19524 * Init a: variables.
19525 * Set a:0 to "argcount".
19526 * Set a:000 to a list with room for the "..." arguments.
19527 */
19528 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19529 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019530 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019531 v = &fc.fixvar[fixvar_idx++].var;
19532 STRCPY(v->di_key, "000");
19533 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19534 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19535 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019536 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 v->di_tv.vval.v_list = &fc.l_varlist;
19538 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19539 fc.l_varlist.lv_refcount = 99999;
19540
19541 /*
19542 * Set a:firstline to "firstline" and a:lastline to "lastline".
19543 * Set a:name to named arguments.
19544 * Set a:N to the "..." arguments.
19545 */
19546 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19547 (varnumber_T)firstline);
19548 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19549 (varnumber_T)lastline);
19550 for (i = 0; i < argcount; ++i)
19551 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019552 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019553 if (ai < 0)
19554 /* named argument a:name */
19555 name = FUNCARG(fp, i);
19556 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019557 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019558 /* "..." argument a:1, a:2, etc. */
19559 sprintf((char *)numbuf, "%d", ai + 1);
19560 name = numbuf;
19561 }
19562 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19563 {
19564 v = &fc.fixvar[fixvar_idx++].var;
19565 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19566 }
19567 else
19568 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019569 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19570 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019571 if (v == NULL)
19572 break;
19573 v->di_flags = DI_FLAGS_RO;
19574 }
19575 STRCPY(v->di_key, name);
19576 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19577
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019578 /* Note: the values are copied directly to avoid alloc/free.
19579 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019580 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019581 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019582
19583 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19584 {
19585 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19586 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019587 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019588 }
19589 }
19590
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591 /* Don't redraw while executing the function. */
19592 ++RedrawingDisabled;
19593 save_sourcing_name = sourcing_name;
19594 save_sourcing_lnum = sourcing_lnum;
19595 sourcing_lnum = 1;
19596 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019597 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 if (sourcing_name != NULL)
19599 {
19600 if (save_sourcing_name != NULL
19601 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19602 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19603 else
19604 STRCPY(sourcing_name, "function ");
19605 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19606
19607 if (p_verbose >= 12)
19608 {
19609 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019610 verbose_enter_scroll();
19611
Bram Moolenaar555b2802005-05-19 21:08:39 +000019612 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 if (p_verbose >= 14)
19614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019616 char_u numbuf[NUMBUFLEN];
19617 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618
19619 msg_puts((char_u *)"(");
19620 for (i = 0; i < argcount; ++i)
19621 {
19622 if (i > 0)
19623 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019624 if (argvars[i].v_type == VAR_NUMBER)
19625 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 else
19627 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019628 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019629 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019631 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 }
19633 }
19634 msg_puts((char_u *)")");
19635 }
19636 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019637
19638 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 --no_wait_return;
19640 }
19641 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019642#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019643 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019644 {
19645 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19646 func_do_profile(fp);
19647 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019648 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019649 {
19650 ++fp->uf_tm_count;
19651 profile_start(&fp->uf_tm_start);
19652 profile_zero(&fp->uf_tm_children);
19653 }
19654 script_prof_save(&wait_start);
19655 }
19656#endif
19657
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019659 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 save_did_emsg = did_emsg;
19661 did_emsg = FALSE;
19662
19663 /* call do_cmdline() to execute the lines */
19664 do_cmdline(NULL, get_func_line, (void *)&fc,
19665 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19666
19667 --RedrawingDisabled;
19668
19669 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019670 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672 clear_tv(rettv);
19673 rettv->v_type = VAR_NUMBER;
19674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 }
19676
Bram Moolenaar05159a02005-02-26 23:04:13 +000019677#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019678 if (do_profiling == PROF_YES && (fp->uf_profiling
19679 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019680 {
19681 profile_end(&fp->uf_tm_start);
19682 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19683 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019684 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019685 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019686 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019687 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19688 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019689 }
19690 }
19691#endif
19692
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 /* when being verbose, mention the return value */
19694 if (p_verbose >= 12)
19695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019697 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019700 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019701 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019702 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19703 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019706 char_u buf[MSG_BUF_LEN];
19707 char_u numbuf[NUMBUFLEN];
19708 char_u *tofree;
19709
Bram Moolenaar555b2802005-05-19 21:08:39 +000019710 /* The value may be very long. Skip the middle part, so that we
19711 * have some idea how it starts and ends. smsg() would always
19712 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019713 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019714 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019715 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019716 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 }
19718 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019719
19720 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 --no_wait_return;
19722 }
19723
19724 vim_free(sourcing_name);
19725 sourcing_name = save_sourcing_name;
19726 sourcing_lnum = save_sourcing_lnum;
19727 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019728#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019729 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019730 script_prof_restore(&wait_start);
19731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732
19733 if (p_verbose >= 12 && sourcing_name != NULL)
19734 {
19735 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019736 verbose_enter_scroll();
19737
Bram Moolenaar555b2802005-05-19 21:08:39 +000019738 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019740
19741 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 --no_wait_return;
19743 }
19744
19745 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019746 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747
Bram Moolenaar33570922005-01-25 22:26:29 +000019748 /* The a: variables typevals were not alloced, only free the allocated
19749 * variables. */
19750 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19751
19752 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 --depth;
19754}
19755
19756/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 * Add a number variable "name" to dict "dp" with value "nr".
19758 */
19759 static void
19760add_nr_var(dp, v, name, nr)
19761 dict_T *dp;
19762 dictitem_T *v;
19763 char *name;
19764 varnumber_T nr;
19765{
19766 STRCPY(v->di_key, name);
19767 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19768 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19769 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019770 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019771 v->di_tv.vval.v_number = nr;
19772}
19773
19774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775 * ":return [expr]"
19776 */
19777 void
19778ex_return(eap)
19779 exarg_T *eap;
19780{
19781 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 int returning = FALSE;
19784
19785 if (current_funccal == NULL)
19786 {
19787 EMSG(_("E133: :return not inside a function"));
19788 return;
19789 }
19790
19791 if (eap->skip)
19792 ++emsg_skip;
19793
19794 eap->nextcmd = NULL;
19795 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019796 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 {
19798 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 }
19803 /* It's safer to return also on error. */
19804 else if (!eap->skip)
19805 {
19806 /*
19807 * Return unless the expression evaluation has been cancelled due to an
19808 * aborting error, an interrupt, or an exception.
19809 */
19810 if (!aborting())
19811 returning = do_return(eap, FALSE, TRUE, NULL);
19812 }
19813
19814 /* When skipping or the return gets pending, advance to the next command
19815 * in this line (!returning). Otherwise, ignore the rest of the line.
19816 * Following lines will be ignored by get_func_line(). */
19817 if (returning)
19818 eap->nextcmd = NULL;
19819 else if (eap->nextcmd == NULL) /* no argument */
19820 eap->nextcmd = check_nextcmd(arg);
19821
19822 if (eap->skip)
19823 --emsg_skip;
19824}
19825
19826/*
19827 * Return from a function. Possibly makes the return pending. Also called
19828 * for a pending return at the ":endtry" or after returning from an extra
19829 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019830 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 * FALSE when the return gets pending.
19833 */
19834 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019835do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 exarg_T *eap;
19837 int reanimate;
19838 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019839 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840{
19841 int idx;
19842 struct condstack *cstack = eap->cstack;
19843
19844 if (reanimate)
19845 /* Undo the return. */
19846 current_funccal->returned = FALSE;
19847
19848 /*
19849 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19850 * not in its finally clause (which then is to be executed next) is found.
19851 * In this case, make the ":return" pending for execution at the ":endtry".
19852 * Otherwise, return normally.
19853 */
19854 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19855 if (idx >= 0)
19856 {
19857 cstack->cs_pending[idx] = CSTP_RETURN;
19858
19859 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019860 /* A pending return again gets pending. "rettv" points to an
19861 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019863 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864 else
19865 {
19866 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019867 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019871 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 {
19873 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019874 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019875 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 else
19877 EMSG(_(e_outofmem));
19878 }
19879 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019880 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881
19882 if (reanimate)
19883 {
19884 /* The pending return value could be overwritten by a ":return"
19885 * without argument in a finally clause; reset the default
19886 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019887 current_funccal->rettv->v_type = VAR_NUMBER;
19888 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 }
19890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019891 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 }
19893 else
19894 {
19895 current_funccal->returned = TRUE;
19896
19897 /* If the return is carried out now, store the return value. For
19898 * a return immediately after reanimation, the value is already
19899 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019900 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019902 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019903 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019905 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 }
19907 }
19908
19909 return idx < 0;
19910}
19911
19912/*
19913 * Free the variable with a pending return value.
19914 */
19915 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019916discard_pending_return(rettv)
19917 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918{
Bram Moolenaar33570922005-01-25 22:26:29 +000019919 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920}
19921
19922/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 * is an allocated string. Used by report_pending() for verbose messages.
19925 */
19926 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927get_return_cmd(rettv)
19928 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019930 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019931 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019932 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019934 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019935 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019936 if (s == NULL)
19937 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019938
19939 STRCPY(IObuff, ":return ");
19940 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19941 if (STRLEN(s) + 8 >= IOSIZE)
19942 STRCPY(IObuff + IOSIZE - 4, "...");
19943 vim_free(tofree);
19944 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945}
19946
19947/*
19948 * Get next function line.
19949 * Called by do_cmdline() to get the next line.
19950 * Returns allocated string, or NULL for end of function.
19951 */
19952/* ARGSUSED */
19953 char_u *
19954get_func_line(c, cookie, indent)
19955 int c; /* not used */
19956 void *cookie;
19957 int indent; /* not used */
19958{
Bram Moolenaar33570922005-01-25 22:26:29 +000019959 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019960 ufunc_T *fp = fcp->func;
19961 char_u *retval;
19962 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963
19964 /* If breakpoints have been added/deleted need to check for it. */
19965 if (fcp->dbg_tick != debug_tick)
19966 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019967 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 sourcing_lnum);
19969 fcp->dbg_tick = debug_tick;
19970 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019971#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019972 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019973 func_line_end(cookie);
19974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975
Bram Moolenaar05159a02005-02-26 23:04:13 +000019976 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019977 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19978 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 retval = NULL;
19980 else
19981 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019982 /* Skip NULL lines (continuation lines). */
19983 while (fcp->linenr < gap->ga_len
19984 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19985 ++fcp->linenr;
19986 if (fcp->linenr >= gap->ga_len)
19987 retval = NULL;
19988 else
19989 {
19990 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19991 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019992#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019993 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019994 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019995#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 }
19998
19999 /* Did we encounter a breakpoint? */
20000 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20001 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020002 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020004 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 sourcing_lnum);
20006 fcp->dbg_tick = debug_tick;
20007 }
20008
20009 return retval;
20010}
20011
Bram Moolenaar05159a02005-02-26 23:04:13 +000020012#if defined(FEAT_PROFILE) || defined(PROTO)
20013/*
20014 * Called when starting to read a function line.
20015 * "sourcing_lnum" must be correct!
20016 * When skipping lines it may not actually be executed, but we won't find out
20017 * until later and we need to store the time now.
20018 */
20019 void
20020func_line_start(cookie)
20021 void *cookie;
20022{
20023 funccall_T *fcp = (funccall_T *)cookie;
20024 ufunc_T *fp = fcp->func;
20025
20026 if (fp->uf_profiling && sourcing_lnum >= 1
20027 && sourcing_lnum <= fp->uf_lines.ga_len)
20028 {
20029 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020030 /* Skip continuation lines. */
20031 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20032 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020033 fp->uf_tml_execed = FALSE;
20034 profile_start(&fp->uf_tml_start);
20035 profile_zero(&fp->uf_tml_children);
20036 profile_get_wait(&fp->uf_tml_wait);
20037 }
20038}
20039
20040/*
20041 * Called when actually executing a function line.
20042 */
20043 void
20044func_line_exec(cookie)
20045 void *cookie;
20046{
20047 funccall_T *fcp = (funccall_T *)cookie;
20048 ufunc_T *fp = fcp->func;
20049
20050 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20051 fp->uf_tml_execed = TRUE;
20052}
20053
20054/*
20055 * Called when done with a function line.
20056 */
20057 void
20058func_line_end(cookie)
20059 void *cookie;
20060{
20061 funccall_T *fcp = (funccall_T *)cookie;
20062 ufunc_T *fp = fcp->func;
20063
20064 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20065 {
20066 if (fp->uf_tml_execed)
20067 {
20068 ++fp->uf_tml_count[fp->uf_tml_idx];
20069 profile_end(&fp->uf_tml_start);
20070 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020071 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020072 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20073 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020074 }
20075 fp->uf_tml_idx = -1;
20076 }
20077}
20078#endif
20079
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080/*
20081 * Return TRUE if the currently active function should be ended, because a
20082 * return was encountered or an error occured. Used inside a ":while".
20083 */
20084 int
20085func_has_ended(cookie)
20086 void *cookie;
20087{
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
20090 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20091 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020092 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 || fcp->returned);
20094}
20095
20096/*
20097 * return TRUE if cookie indicates a function which "abort"s on errors.
20098 */
20099 int
20100func_has_abort(cookie)
20101 void *cookie;
20102{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020103 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104}
20105
20106#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20107typedef enum
20108{
20109 VAR_FLAVOUR_DEFAULT,
20110 VAR_FLAVOUR_SESSION,
20111 VAR_FLAVOUR_VIMINFO
20112} var_flavour_T;
20113
20114static var_flavour_T var_flavour __ARGS((char_u *varname));
20115
20116 static var_flavour_T
20117var_flavour(varname)
20118 char_u *varname;
20119{
20120 char_u *p = varname;
20121
20122 if (ASCII_ISUPPER(*p))
20123 {
20124 while (*(++p))
20125 if (ASCII_ISLOWER(*p))
20126 return VAR_FLAVOUR_SESSION;
20127 return VAR_FLAVOUR_VIMINFO;
20128 }
20129 else
20130 return VAR_FLAVOUR_DEFAULT;
20131}
20132#endif
20133
20134#if defined(FEAT_VIMINFO) || defined(PROTO)
20135/*
20136 * Restore global vars that start with a capital from the viminfo file
20137 */
20138 int
20139read_viminfo_varlist(virp, writing)
20140 vir_T *virp;
20141 int writing;
20142{
20143 char_u *tab;
20144 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146
20147 if (!writing && (find_viminfo_parameter('!') != NULL))
20148 {
20149 tab = vim_strchr(virp->vir_line + 1, '\t');
20150 if (tab != NULL)
20151 {
20152 *tab++ = '\0'; /* isolate the variable name */
20153 if (*tab == 'S') /* string var */
20154 is_string = TRUE;
20155
20156 tab = vim_strchr(tab, '\t');
20157 if (tab != NULL)
20158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 if (is_string)
20160 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020161 tv.v_type = VAR_STRING;
20162 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 }
20165 else
20166 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020167 tv.v_type = VAR_NUMBER;
20168 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020170 set_var(virp->vir_line + 1, &tv, FALSE);
20171 if (is_string)
20172 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 }
20174 }
20175 }
20176
20177 return viminfo_readline(virp);
20178}
20179
20180/*
20181 * Write global vars that start with a capital to the viminfo file
20182 */
20183 void
20184write_viminfo_varlist(fp)
20185 FILE *fp;
20186{
Bram Moolenaar33570922005-01-25 22:26:29 +000020187 hashitem_T *hi;
20188 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020189 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020191 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020192 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020193 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020194
20195 if (find_viminfo_parameter('!') == NULL)
20196 return;
20197
20198 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020199
Bram Moolenaar33570922005-01-25 22:26:29 +000020200 todo = globvarht.ht_used;
20201 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020203 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020205 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 this_var = HI2DI(hi);
20207 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020210 {
20211 case VAR_STRING: s = "STR"; break;
20212 case VAR_NUMBER: s = "NUM"; break;
20213 default: continue;
20214 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020215 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020216 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020217 if (p != NULL)
20218 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020219 vim_free(tofree);
20220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 }
20222 }
20223}
20224#endif
20225
20226#if defined(FEAT_SESSION) || defined(PROTO)
20227 int
20228store_session_globals(fd)
20229 FILE *fd;
20230{
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 hashitem_T *hi;
20232 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020233 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 char_u *p, *t;
20235
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 todo = globvarht.ht_used;
20237 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020239 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020241 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 this_var = HI2DI(hi);
20243 if ((this_var->di_tv.v_type == VAR_NUMBER
20244 || this_var->di_tv.v_type == VAR_STRING)
20245 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020246 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020247 /* Escape special characters with a backslash. Turn a LF and
20248 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020249 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020250 (char_u *)"\\\"\n\r");
20251 if (p == NULL) /* out of memory */
20252 break;
20253 for (t = p; *t != NUL; ++t)
20254 if (*t == '\n')
20255 *t = 'n';
20256 else if (*t == '\r')
20257 *t = 'r';
20258 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 this_var->di_key,
20260 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20261 : ' ',
20262 p,
20263 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20264 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020265 || put_eol(fd) == FAIL)
20266 {
20267 vim_free(p);
20268 return FAIL;
20269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 vim_free(p);
20271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 }
20273 }
20274 return OK;
20275}
20276#endif
20277
Bram Moolenaar661b1822005-07-28 22:36:45 +000020278/*
20279 * Display script name where an item was last set.
20280 * Should only be invoked when 'verbose' is non-zero.
20281 */
20282 void
20283last_set_msg(scriptID)
20284 scid_T scriptID;
20285{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020286 char_u *p;
20287
Bram Moolenaar661b1822005-07-28 22:36:45 +000020288 if (scriptID != 0)
20289 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020290 p = home_replace_save(NULL, get_scriptname(scriptID));
20291 if (p != NULL)
20292 {
20293 verbose_enter();
20294 MSG_PUTS(_("\n\tLast set from "));
20295 MSG_PUTS(p);
20296 vim_free(p);
20297 verbose_leave();
20298 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020299 }
20300}
20301
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302#endif /* FEAT_EVAL */
20303
20304#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20305
20306
20307#ifdef WIN3264
20308/*
20309 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20310 */
20311static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20312static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20313static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20314
20315/*
20316 * Get the short pathname of a file.
20317 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20318 */
20319 static int
20320get_short_pathname(fnamep, bufp, fnamelen)
20321 char_u **fnamep;
20322 char_u **bufp;
20323 int *fnamelen;
20324{
20325 int l,len;
20326 char_u *newbuf;
20327
20328 len = *fnamelen;
20329
20330 l = GetShortPathName(*fnamep, *fnamep, len);
20331 if (l > len - 1)
20332 {
20333 /* If that doesn't work (not enough space), then save the string
20334 * and try again with a new buffer big enough
20335 */
20336 newbuf = vim_strnsave(*fnamep, l);
20337 if (newbuf == NULL)
20338 return 0;
20339
20340 vim_free(*bufp);
20341 *fnamep = *bufp = newbuf;
20342
20343 l = GetShortPathName(*fnamep,*fnamep,l+1);
20344
20345 /* Really should always succeed, as the buffer is big enough */
20346 }
20347
20348 *fnamelen = l;
20349 return 1;
20350}
20351
20352/*
20353 * Create a short path name. Returns the length of the buffer it needs.
20354 * Doesn't copy over the end of the buffer passed in.
20355 */
20356 static int
20357shortpath_for_invalid_fname(fname, bufp, fnamelen)
20358 char_u **fname;
20359 char_u **bufp;
20360 int *fnamelen;
20361{
20362 char_u *s, *p, *pbuf2, *pbuf3;
20363 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020364 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365
20366 /* Make a copy */
20367 len2 = *fnamelen;
20368 pbuf2 = vim_strnsave(*fname, len2);
20369 pbuf3 = NULL;
20370
20371 s = pbuf2 + len2 - 1; /* Find the end */
20372 slen = 1;
20373 plen = len2;
20374
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020375 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 {
20377 --s;
20378 ++slen;
20379 --plen;
20380 }
20381
20382 do
20383 {
20384 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020385 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 {
20387 --s;
20388 ++slen;
20389 --plen;
20390 }
20391 if (s <= pbuf2)
20392 break;
20393
20394 /* Remeber the character that is about to be blatted */
20395 ch = *s;
20396 *s = 0; /* get_short_pathname requires a null-terminated string */
20397
20398 /* Try it in situ */
20399 p = pbuf2;
20400 if (!get_short_pathname(&p, &pbuf3, &plen))
20401 {
20402 vim_free(pbuf2);
20403 return -1;
20404 }
20405 *s = ch; /* Preserve the string */
20406 } while (plen == 0);
20407
20408 if (plen > 0)
20409 {
20410 /* Remeber the length of the new string. */
20411 *fnamelen = len = plen + slen;
20412 vim_free(*bufp);
20413 if (len > len2)
20414 {
20415 /* If there's not enough space in the currently allocated string,
20416 * then copy it to a buffer big enough.
20417 */
20418 *fname= *bufp = vim_strnsave(p, len);
20419 if (*fname == NULL)
20420 return -1;
20421 }
20422 else
20423 {
20424 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20425 *fname = *bufp = pbuf2;
20426 if (p != pbuf2)
20427 strncpy(*fname, p, plen);
20428 pbuf2 = NULL;
20429 }
20430 /* Concat the next bit */
20431 strncpy(*fname + plen, s, slen);
20432 (*fname)[len] = '\0';
20433 }
20434 vim_free(pbuf3);
20435 vim_free(pbuf2);
20436 return 0;
20437}
20438
20439/*
20440 * Get a pathname for a partial path.
20441 */
20442 static int
20443shortpath_for_partial(fnamep, bufp, fnamelen)
20444 char_u **fnamep;
20445 char_u **bufp;
20446 int *fnamelen;
20447{
20448 int sepcount, len, tflen;
20449 char_u *p;
20450 char_u *pbuf, *tfname;
20451 int hasTilde;
20452
20453 /* Count up the path seperators from the RHS.. so we know which part
20454 * of the path to return.
20455 */
20456 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020457 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 if (vim_ispathsep(*p))
20459 ++sepcount;
20460
20461 /* Need full path first (use expand_env() to remove a "~/") */
20462 hasTilde = (**fnamep == '~');
20463 if (hasTilde)
20464 pbuf = tfname = expand_env_save(*fnamep);
20465 else
20466 pbuf = tfname = FullName_save(*fnamep, FALSE);
20467
20468 len = tflen = STRLEN(tfname);
20469
20470 if (!get_short_pathname(&tfname, &pbuf, &len))
20471 return -1;
20472
20473 if (len == 0)
20474 {
20475 /* Don't have a valid filename, so shorten the rest of the
20476 * path if we can. This CAN give us invalid 8.3 filenames, but
20477 * there's not a lot of point in guessing what it might be.
20478 */
20479 len = tflen;
20480 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20481 return -1;
20482 }
20483
20484 /* Count the paths backward to find the beginning of the desired string. */
20485 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020486 {
20487#ifdef FEAT_MBYTE
20488 if (has_mbyte)
20489 p -= mb_head_off(tfname, p);
20490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 if (vim_ispathsep(*p))
20492 {
20493 if (sepcount == 0 || (hasTilde && sepcount == 1))
20494 break;
20495 else
20496 sepcount --;
20497 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 if (hasTilde)
20500 {
20501 --p;
20502 if (p >= tfname)
20503 *p = '~';
20504 else
20505 return -1;
20506 }
20507 else
20508 ++p;
20509
20510 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20511 vim_free(*bufp);
20512 *fnamelen = (int)STRLEN(p);
20513 *bufp = pbuf;
20514 *fnamep = p;
20515
20516 return 0;
20517}
20518#endif /* WIN3264 */
20519
20520/*
20521 * Adjust a filename, according to a string of modifiers.
20522 * *fnamep must be NUL terminated when called. When returning, the length is
20523 * determined by *fnamelen.
20524 * Returns valid flags.
20525 * When there is an error, *fnamep is set to NULL.
20526 */
20527 int
20528modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20529 char_u *src; /* string with modifiers */
20530 int *usedlen; /* characters after src that are used */
20531 char_u **fnamep; /* file name so far */
20532 char_u **bufp; /* buffer for allocated file name or NULL */
20533 int *fnamelen; /* length of fnamep */
20534{
20535 int valid = 0;
20536 char_u *tail;
20537 char_u *s, *p, *pbuf;
20538 char_u dirname[MAXPATHL];
20539 int c;
20540 int has_fullname = 0;
20541#ifdef WIN3264
20542 int has_shortname = 0;
20543#endif
20544
20545repeat:
20546 /* ":p" - full path/file_name */
20547 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20548 {
20549 has_fullname = 1;
20550
20551 valid |= VALID_PATH;
20552 *usedlen += 2;
20553
20554 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20555 if ((*fnamep)[0] == '~'
20556#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20557 && ((*fnamep)[1] == '/'
20558# ifdef BACKSLASH_IN_FILENAME
20559 || (*fnamep)[1] == '\\'
20560# endif
20561 || (*fnamep)[1] == NUL)
20562
20563#endif
20564 )
20565 {
20566 *fnamep = expand_env_save(*fnamep);
20567 vim_free(*bufp); /* free any allocated file name */
20568 *bufp = *fnamep;
20569 if (*fnamep == NULL)
20570 return -1;
20571 }
20572
20573 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020574 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 {
20576 if (vim_ispathsep(*p)
20577 && p[1] == '.'
20578 && (p[2] == NUL
20579 || vim_ispathsep(p[2])
20580 || (p[2] == '.'
20581 && (p[3] == NUL || vim_ispathsep(p[3])))))
20582 break;
20583 }
20584
20585 /* FullName_save() is slow, don't use it when not needed. */
20586 if (*p != NUL || !vim_isAbsName(*fnamep))
20587 {
20588 *fnamep = FullName_save(*fnamep, *p != NUL);
20589 vim_free(*bufp); /* free any allocated file name */
20590 *bufp = *fnamep;
20591 if (*fnamep == NULL)
20592 return -1;
20593 }
20594
20595 /* Append a path separator to a directory. */
20596 if (mch_isdir(*fnamep))
20597 {
20598 /* Make room for one or two extra characters. */
20599 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20600 vim_free(*bufp); /* free any allocated file name */
20601 *bufp = *fnamep;
20602 if (*fnamep == NULL)
20603 return -1;
20604 add_pathsep(*fnamep);
20605 }
20606 }
20607
20608 /* ":." - path relative to the current directory */
20609 /* ":~" - path relative to the home directory */
20610 /* ":8" - shortname path - postponed till after */
20611 while (src[*usedlen] == ':'
20612 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20613 {
20614 *usedlen += 2;
20615 if (c == '8')
20616 {
20617#ifdef WIN3264
20618 has_shortname = 1; /* Postpone this. */
20619#endif
20620 continue;
20621 }
20622 pbuf = NULL;
20623 /* Need full path first (use expand_env() to remove a "~/") */
20624 if (!has_fullname)
20625 {
20626 if (c == '.' && **fnamep == '~')
20627 p = pbuf = expand_env_save(*fnamep);
20628 else
20629 p = pbuf = FullName_save(*fnamep, FALSE);
20630 }
20631 else
20632 p = *fnamep;
20633
20634 has_fullname = 0;
20635
20636 if (p != NULL)
20637 {
20638 if (c == '.')
20639 {
20640 mch_dirname(dirname, MAXPATHL);
20641 s = shorten_fname(p, dirname);
20642 if (s != NULL)
20643 {
20644 *fnamep = s;
20645 if (pbuf != NULL)
20646 {
20647 vim_free(*bufp); /* free any allocated file name */
20648 *bufp = pbuf;
20649 pbuf = NULL;
20650 }
20651 }
20652 }
20653 else
20654 {
20655 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20656 /* Only replace it when it starts with '~' */
20657 if (*dirname == '~')
20658 {
20659 s = vim_strsave(dirname);
20660 if (s != NULL)
20661 {
20662 *fnamep = s;
20663 vim_free(*bufp);
20664 *bufp = s;
20665 }
20666 }
20667 }
20668 vim_free(pbuf);
20669 }
20670 }
20671
20672 tail = gettail(*fnamep);
20673 *fnamelen = (int)STRLEN(*fnamep);
20674
20675 /* ":h" - head, remove "/file_name", can be repeated */
20676 /* Don't remove the first "/" or "c:\" */
20677 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20678 {
20679 valid |= VALID_HEAD;
20680 *usedlen += 2;
20681 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020682 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 --tail;
20684 *fnamelen = (int)(tail - *fnamep);
20685#ifdef VMS
20686 if (*fnamelen > 0)
20687 *fnamelen += 1; /* the path separator is part of the path */
20688#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020689 while (tail > s && !after_pathsep(s, tail))
20690 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691 }
20692
20693 /* ":8" - shortname */
20694 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20695 {
20696 *usedlen += 2;
20697#ifdef WIN3264
20698 has_shortname = 1;
20699#endif
20700 }
20701
20702#ifdef WIN3264
20703 /* Check shortname after we have done 'heads' and before we do 'tails'
20704 */
20705 if (has_shortname)
20706 {
20707 pbuf = NULL;
20708 /* Copy the string if it is shortened by :h */
20709 if (*fnamelen < (int)STRLEN(*fnamep))
20710 {
20711 p = vim_strnsave(*fnamep, *fnamelen);
20712 if (p == 0)
20713 return -1;
20714 vim_free(*bufp);
20715 *bufp = *fnamep = p;
20716 }
20717
20718 /* Split into two implementations - makes it easier. First is where
20719 * there isn't a full name already, second is where there is.
20720 */
20721 if (!has_fullname && !vim_isAbsName(*fnamep))
20722 {
20723 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20724 return -1;
20725 }
20726 else
20727 {
20728 int l;
20729
20730 /* Simple case, already have the full-name
20731 * Nearly always shorter, so try first time. */
20732 l = *fnamelen;
20733 if (!get_short_pathname(fnamep, bufp, &l))
20734 return -1;
20735
20736 if (l == 0)
20737 {
20738 /* Couldn't find the filename.. search the paths.
20739 */
20740 l = *fnamelen;
20741 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20742 return -1;
20743 }
20744 *fnamelen = l;
20745 }
20746 }
20747#endif /* WIN3264 */
20748
20749 /* ":t" - tail, just the basename */
20750 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20751 {
20752 *usedlen += 2;
20753 *fnamelen -= (int)(tail - *fnamep);
20754 *fnamep = tail;
20755 }
20756
20757 /* ":e" - extension, can be repeated */
20758 /* ":r" - root, without extension, can be repeated */
20759 while (src[*usedlen] == ':'
20760 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20761 {
20762 /* find a '.' in the tail:
20763 * - for second :e: before the current fname
20764 * - otherwise: The last '.'
20765 */
20766 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20767 s = *fnamep - 2;
20768 else
20769 s = *fnamep + *fnamelen - 1;
20770 for ( ; s > tail; --s)
20771 if (s[0] == '.')
20772 break;
20773 if (src[*usedlen + 1] == 'e') /* :e */
20774 {
20775 if (s > tail)
20776 {
20777 *fnamelen += (int)(*fnamep - (s + 1));
20778 *fnamep = s + 1;
20779#ifdef VMS
20780 /* cut version from the extension */
20781 s = *fnamep + *fnamelen - 1;
20782 for ( ; s > *fnamep; --s)
20783 if (s[0] == ';')
20784 break;
20785 if (s > *fnamep)
20786 *fnamelen = s - *fnamep;
20787#endif
20788 }
20789 else if (*fnamep <= tail)
20790 *fnamelen = 0;
20791 }
20792 else /* :r */
20793 {
20794 if (s > tail) /* remove one extension */
20795 *fnamelen = (int)(s - *fnamep);
20796 }
20797 *usedlen += 2;
20798 }
20799
20800 /* ":s?pat?foo?" - substitute */
20801 /* ":gs?pat?foo?" - global substitute */
20802 if (src[*usedlen] == ':'
20803 && (src[*usedlen + 1] == 's'
20804 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20805 {
20806 char_u *str;
20807 char_u *pat;
20808 char_u *sub;
20809 int sep;
20810 char_u *flags;
20811 int didit = FALSE;
20812
20813 flags = (char_u *)"";
20814 s = src + *usedlen + 2;
20815 if (src[*usedlen + 1] == 'g')
20816 {
20817 flags = (char_u *)"g";
20818 ++s;
20819 }
20820
20821 sep = *s++;
20822 if (sep)
20823 {
20824 /* find end of pattern */
20825 p = vim_strchr(s, sep);
20826 if (p != NULL)
20827 {
20828 pat = vim_strnsave(s, (int)(p - s));
20829 if (pat != NULL)
20830 {
20831 s = p + 1;
20832 /* find end of substitution */
20833 p = vim_strchr(s, sep);
20834 if (p != NULL)
20835 {
20836 sub = vim_strnsave(s, (int)(p - s));
20837 str = vim_strnsave(*fnamep, *fnamelen);
20838 if (sub != NULL && str != NULL)
20839 {
20840 *usedlen = (int)(p + 1 - src);
20841 s = do_string_sub(str, pat, sub, flags);
20842 if (s != NULL)
20843 {
20844 *fnamep = s;
20845 *fnamelen = (int)STRLEN(s);
20846 vim_free(*bufp);
20847 *bufp = s;
20848 didit = TRUE;
20849 }
20850 }
20851 vim_free(sub);
20852 vim_free(str);
20853 }
20854 vim_free(pat);
20855 }
20856 }
20857 /* after using ":s", repeat all the modifiers */
20858 if (didit)
20859 goto repeat;
20860 }
20861 }
20862
20863 return valid;
20864}
20865
20866/*
20867 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20868 * "flags" can be "g" to do a global substitute.
20869 * Returns an allocated string, NULL for error.
20870 */
20871 char_u *
20872do_string_sub(str, pat, sub, flags)
20873 char_u *str;
20874 char_u *pat;
20875 char_u *sub;
20876 char_u *flags;
20877{
20878 int sublen;
20879 regmatch_T regmatch;
20880 int i;
20881 int do_all;
20882 char_u *tail;
20883 garray_T ga;
20884 char_u *ret;
20885 char_u *save_cpo;
20886
20887 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20888 save_cpo = p_cpo;
20889 p_cpo = (char_u *)"";
20890
20891 ga_init2(&ga, 1, 200);
20892
20893 do_all = (flags[0] == 'g');
20894
20895 regmatch.rm_ic = p_ic;
20896 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20897 if (regmatch.regprog != NULL)
20898 {
20899 tail = str;
20900 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20901 {
20902 /*
20903 * Get some space for a temporary buffer to do the substitution
20904 * into. It will contain:
20905 * - The text up to where the match is.
20906 * - The substituted text.
20907 * - The text after the match.
20908 */
20909 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20910 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20911 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20912 {
20913 ga_clear(&ga);
20914 break;
20915 }
20916
20917 /* copy the text up to where the match is */
20918 i = (int)(regmatch.startp[0] - tail);
20919 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20920 /* add the substituted text */
20921 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20922 + ga.ga_len + i, TRUE, TRUE, FALSE);
20923 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 /* avoid getting stuck on a match with an empty string */
20925 if (tail == regmatch.endp[0])
20926 {
20927 if (*tail == NUL)
20928 break;
20929 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20930 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 }
20932 else
20933 {
20934 tail = regmatch.endp[0];
20935 if (*tail == NUL)
20936 break;
20937 }
20938 if (!do_all)
20939 break;
20940 }
20941
20942 if (ga.ga_data != NULL)
20943 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20944
20945 vim_free(regmatch.regprog);
20946 }
20947
20948 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20949 ga_clear(&ga);
20950 p_cpo = save_cpo;
20951
20952 return ret;
20953}
20954
20955#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */