blob: 8d275dc8ba3ef3b1a0b2e77f3192961bb2c76a51 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000344};
345
346/* shorthand */
347#define vv_type vv_di.di_tv.v_type
348#define vv_nr vv_di.di_tv.vval.v_number
349#define vv_str vv_di.di_tv.vval.v_string
350#define vv_tv vv_di.di_tv
351
352/*
353 * The v: variables are stored in dictionary "vimvardict".
354 * "vimvars_var" is the variable that is used for the "l:" scope.
355 */
356static dict_T vimvardict;
357static dictitem_T vimvars_var;
358#define vimvarht vimvardict.dv_hashtab
359
Bram Moolenaara40058a2005-07-11 22:42:07 +0000360static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
361static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
362#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
363static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
364#endif
365static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
366static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
367static char_u *skip_var_one __ARGS((char_u *arg));
368static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
369static void list_glob_vars __ARGS((void));
370static void list_buf_vars __ARGS((void));
371static void list_win_vars __ARGS((void));
372static void list_vim_vars __ARGS((void));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000373static void list_script_vars __ARGS((void));
374static void list_func_vars __ARGS((void));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000375static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
376static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
377static int check_changedtick __ARGS((char_u *arg));
378static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
379static void clear_lval __ARGS((lval_T *lp));
380static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
381static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
382static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
383static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
384static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
385static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
386static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
387static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
388static void item_lock __ARGS((typval_T *tv, int deep, int lock));
389static int tv_islocked __ARGS((typval_T *tv));
390
Bram Moolenaar33570922005-01-25 22:26:29 +0000391static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
392static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000400static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000401static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000405static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static listitem_T *listitem_alloc __ARGS((void));
407static void listitem_free __ARGS((listitem_T *item));
408static void listitem_remove __ARGS((list_T *l, listitem_T *item));
409static long list_len __ARGS((list_T *l));
410static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
411static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
412static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000414static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_append __ARGS((list_T *l, listitem_T *item));
417static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000418static int list_append_string __ARGS((list_T *l, char_u *str, int len));
419static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
421static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
422static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000423static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
426static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000427static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
428static void set_ref_in_list __ARGS((list_T *l, int copyID));
429static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static void dict_unref __ARGS((dict_T *d));
431static void dict_free __ARGS((dict_T *d));
432static dictitem_T *dictitem_alloc __ARGS((char_u *key));
433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
435static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000436static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int dict_add __ARGS((dict_T *d, dictitem_T *item));
438static long dict_len __ARGS((dict_T *d));
439static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
449static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000450static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451
452static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000468static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000472#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000473static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000474static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
476#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
482static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000508static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000510static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000516static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000524static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000525static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000548static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000554static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000571static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000575#ifdef vim_mkdir
576static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
577#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000582static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000583static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000585static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000597static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000599static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000606static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000607static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000608static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000613static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000614static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000617static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618#ifdef HAVE_STRFTIME
619static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
621static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000633static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000634static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000635static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000636static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000637static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000639static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000653static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000656static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000658static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
659static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static int get_env_len __ARGS((char_u **arg));
661static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000662static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000663static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
664#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
665#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
666 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000667static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000669static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000670static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
671static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static typval_T *alloc_tv __ARGS((void));
673static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void init_tv __ARGS((typval_T *varp));
675static long get_tv_number __ARGS((typval_T *varp));
676static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000677static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static char_u *get_tv_string __ARGS((typval_T *varp));
679static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000682static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
684static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
685static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
686static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
687static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
688static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
689static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000690static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000692static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
694static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
695static int eval_fname_script __ARGS((char_u *p));
696static int eval_fname_sid __ARGS((char_u *p));
697static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static ufunc_T *find_func __ARGS((char_u *name));
699static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000700static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701#ifdef FEAT_PROFILE
702static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000703static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
704static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
705static int
706# ifdef __BORLANDC__
707 _RTLENTRYF
708# endif
709 prof_total_cmp __ARGS((const void *s1, const void *s2));
710static int
711# ifdef __BORLANDC__
712 _RTLENTRYF
713# endif
714 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000715#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000716static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000717static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000718static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void func_free __ARGS((ufunc_T *fp));
720static void func_unref __ARGS((char_u *name));
721static void func_ref __ARGS((char_u *name));
722static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
723static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000724static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000725static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000726static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000728/* Character used as separated in autoload function/variable names. */
729#define AUTOLOAD_CHAR '#'
730
Bram Moolenaar33570922005-01-25 22:26:29 +0000731/*
732 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000733 */
734 void
735eval_init()
736{
Bram Moolenaar33570922005-01-25 22:26:29 +0000737 int i;
738 struct vimvar *p;
739
740 init_var_dict(&globvardict, &globvars_var);
741 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000742 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000743 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000744
745 for (i = 0; i < VV_LEN; ++i)
746 {
747 p = &vimvars[i];
748 STRCPY(p->vv_di.di_key, p->vv_name);
749 if (p->vv_flags & VV_RO)
750 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
751 else if (p->vv_flags & VV_RO_SBX)
752 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
753 else
754 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755
756 /* add to v: scope dict, unless the value is not always available */
757 if (p->vv_type != VAR_UNKNOWN)
758 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000759 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000760 /* add to compat scope dict */
761 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000763}
764
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765#if defined(EXITFREE) || defined(PROTO)
766 void
767eval_clear()
768{
769 int i;
770 struct vimvar *p;
771
772 for (i = 0; i < VV_LEN; ++i)
773 {
774 p = &vimvars[i];
775 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000776 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000777 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000778 p->vv_di.di_tv.vval.v_string = NULL;
779 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000780 }
781 hash_clear(&vimvarht);
782 hash_clear(&compat_hashtab);
783
784 /* script-local variables */
785 for (i = 1; i <= ga_scripts.ga_len; ++i)
786 vars_clear(&SCRIPT_VARS(i));
787 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000788 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000789
790 /* global variables */
791 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000792
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000793 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000795 hash_clear(&func_hashtab);
796
797 /* unreferenced lists and dicts */
798 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000799}
800#endif
801
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * Return the name of the executed function.
804 */
805 char_u *
806func_name(cookie)
807 void *cookie;
808{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000809 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/*
813 * Return the address holding the next breakpoint line for a funccall cookie.
814 */
815 linenr_T *
816func_breakpoint(cookie)
817 void *cookie;
818{
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820}
821
822/*
823 * Return the address holding the debug tick for a funccall cookie.
824 */
825 int *
826func_dbg_tick(cookie)
827 void *cookie;
828{
Bram Moolenaar33570922005-01-25 22:26:29 +0000829 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
832/*
833 * Return the nesting level for a funccall cookie.
834 */
835 int
836func_level(cookie)
837 void *cookie;
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840}
841
842/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000843funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845/*
846 * Return TRUE when a function was ended by a ":return" command.
847 */
848 int
849current_func_returned()
850{
851 return current_funccal->returned;
852}
853
854
855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 * Set an internal variable to a string value. Creates the variable if it does
857 * not already exist.
858 */
859 void
860set_internal_string_var(name, value)
861 char_u *name;
862 char_u *value;
863{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000864 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 val = vim_strsave(value);
868 if (val != NULL)
869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000870 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000871 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000873 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876 }
877}
878
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000879static lval_T *redir_lval = NULL;
880static char_u *redir_endp = NULL;
881static char_u *redir_varname = NULL;
882
883/*
884 * Start recording command output to a variable
885 * Returns OK if successfully completed the setup. FAIL otherwise.
886 */
887 int
888var_redir_start(name, append)
889 char_u *name;
890 int append; /* append to an existing variable */
891{
892 int save_emsg;
893 int err;
894 typval_T tv;
895
896 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000897 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898 {
899 EMSG(_(e_invarg));
900 return FAIL;
901 }
902
903 redir_varname = vim_strsave(name);
904 if (redir_varname == NULL)
905 return FAIL;
906
907 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
908 if (redir_lval == NULL)
909 {
910 var_redir_stop();
911 return FAIL;
912 }
913
914 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000915 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
916 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
918 {
919 if (redir_endp != NULL && *redir_endp != NUL)
920 /* Trailing characters are present after the variable name */
921 EMSG(_(e_trailing));
922 else
923 EMSG(_(e_invarg));
924 var_redir_stop();
925 return FAIL;
926 }
927
928 /* check if we can write to the variable: set it to or append an empty
929 * string */
930 save_emsg = did_emsg;
931 did_emsg = FALSE;
932 tv.v_type = VAR_STRING;
933 tv.vval.v_string = (char_u *)"";
934 if (append)
935 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
936 else
937 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
938 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000939 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 if (err)
941 {
942 var_redir_stop();
943 return FAIL;
944 }
945 if (redir_lval->ll_newkey != NULL)
946 {
947 /* Dictionary item was created, don't do it again. */
948 vim_free(redir_lval->ll_newkey);
949 redir_lval->ll_newkey = NULL;
950 }
951
952 return OK;
953}
954
955/*
956 * Append "value[len]" to the variable set by var_redir_start().
957 */
958 void
959var_redir_str(value, len)
960 char_u *value;
961 int len;
962{
963 char_u *val;
964 typval_T tv;
965 int save_emsg;
966 int err;
967
968 if (redir_lval == NULL)
969 return;
970
971 if (len == -1)
972 /* Append the entire string */
973 val = vim_strsave(value);
974 else
975 /* Append only the specified number of characters */
976 val = vim_strnsave(value, len);
977 if (val == NULL)
978 return;
979
980 tv.v_type = VAR_STRING;
981 tv.vval.v_string = val;
982
983 save_emsg = did_emsg;
984 did_emsg = FALSE;
985 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
986 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000987 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000988 if (err)
989 var_redir_stop();
990
991 vim_free(tv.vval.v_string);
992}
993
994/*
995 * Stop redirecting command output to a variable.
996 */
997 void
998var_redir_stop()
999{
1000 if (redir_lval != NULL)
1001 {
1002 clear_lval(redir_lval);
1003 vim_free(redir_lval);
1004 redir_lval = NULL;
1005 }
1006 vim_free(redir_varname);
1007 redir_varname = NULL;
1008}
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010# if defined(FEAT_MBYTE) || defined(PROTO)
1011 int
1012eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1013 char_u *enc_from;
1014 char_u *enc_to;
1015 char_u *fname_from;
1016 char_u *fname_to;
1017{
1018 int err = FALSE;
1019
1020 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1021 set_vim_var_string(VV_CC_TO, enc_to, -1);
1022 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1023 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1024 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1025 err = TRUE;
1026 set_vim_var_string(VV_CC_FROM, NULL, -1);
1027 set_vim_var_string(VV_CC_TO, NULL, -1);
1028 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1029 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1030
1031 if (err)
1032 return FAIL;
1033 return OK;
1034}
1035# endif
1036
1037# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1038 int
1039eval_printexpr(fname, args)
1040 char_u *fname;
1041 char_u *args;
1042{
1043 int err = FALSE;
1044
1045 set_vim_var_string(VV_FNAME_IN, fname, -1);
1046 set_vim_var_string(VV_CMDARG, args, -1);
1047 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1048 err = TRUE;
1049 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1050 set_vim_var_string(VV_CMDARG, NULL, -1);
1051
1052 if (err)
1053 {
1054 mch_remove(fname);
1055 return FAIL;
1056 }
1057 return OK;
1058}
1059# endif
1060
1061# if defined(FEAT_DIFF) || defined(PROTO)
1062 void
1063eval_diff(origfile, newfile, outfile)
1064 char_u *origfile;
1065 char_u *newfile;
1066 char_u *outfile;
1067{
1068 int err = FALSE;
1069
1070 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1071 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1072 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1073 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1074 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1075 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1076 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1077}
1078
1079 void
1080eval_patch(origfile, difffile, outfile)
1081 char_u *origfile;
1082 char_u *difffile;
1083 char_u *outfile;
1084{
1085 int err;
1086
1087 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1088 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1089 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1090 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1091 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1092 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1093 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1094}
1095# endif
1096
1097/*
1098 * Top level evaluation function, returning a boolean.
1099 * Sets "error" to TRUE if there was an error.
1100 * Return TRUE or FALSE.
1101 */
1102 int
1103eval_to_bool(arg, error, nextcmd, skip)
1104 char_u *arg;
1105 int *error;
1106 char_u **nextcmd;
1107 int skip; /* only parse, don't execute */
1108{
Bram Moolenaar33570922005-01-25 22:26:29 +00001109 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 int retval = FALSE;
1111
1112 if (skip)
1113 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 else
1117 {
1118 *error = FALSE;
1119 if (!skip)
1120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001121 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
1124 }
1125 if (skip)
1126 --emsg_skip;
1127
1128 return retval;
1129}
1130
1131/*
1132 * Top level evaluation function, returning a string. If "skip" is TRUE,
1133 * only parsing to "nextcmd" is done, without reporting errors. Return
1134 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1135 */
1136 char_u *
1137eval_to_string_skip(arg, nextcmd, skip)
1138 char_u *arg;
1139 char_u **nextcmd;
1140 int skip; /* only parse, don't execute */
1141{
Bram Moolenaar33570922005-01-25 22:26:29 +00001142 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 char_u *retval;
1144
1145 if (skip)
1146 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001147 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 retval = NULL;
1149 else
1150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 retval = vim_strsave(get_tv_string(&tv));
1152 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154 if (skip)
1155 --emsg_skip;
1156
1157 return retval;
1158}
1159
1160/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001161 * Skip over an expression at "*pp".
1162 * Return FAIL for an error, OK otherwise.
1163 */
1164 int
1165skip_expr(pp)
1166 char_u **pp;
1167{
Bram Moolenaar33570922005-01-25 22:26:29 +00001168 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001169
1170 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001171 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001172}
1173
1174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * Top level evaluation function, returning a string.
1176 * Return pointer to allocated memory, or NULL for failure.
1177 */
1178 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001179eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 char_u *arg;
1181 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001182 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
Bram Moolenaar33570922005-01-25 22:26:29 +00001184 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001186 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001188 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 retval = NULL;
1190 else
1191 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001192 if (dolist && tv.v_type == VAR_LIST)
1193 {
1194 ga_init2(&ga, (int)sizeof(char), 80);
1195 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1196 ga_append(&ga, NUL);
1197 retval = (char_u *)ga.ga_data;
1198 }
1199 else
1200 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001201 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203
1204 return retval;
1205}
1206
1207/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001208 * Call eval_to_string() without using current local variables and using
1209 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
1211 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001212eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 char_u *arg;
1214 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001215 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
1217 char_u *retval;
1218 void *save_funccalp;
1219
1220 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001221 if (use_sandbox)
1222 ++sandbox;
1223 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001224 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001225 if (use_sandbox)
1226 --sandbox;
1227 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 restore_funccal(save_funccalp);
1229 return retval;
1230}
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/*
1233 * Top level evaluation function, returning a number.
1234 * Evaluates "expr" silently.
1235 * Returns -1 for an error.
1236 */
1237 int
1238eval_to_number(expr)
1239 char_u *expr;
1240{
Bram Moolenaar33570922005-01-25 22:26:29 +00001241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001243 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 ++emsg_off;
1246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001247 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 retval = -1;
1249 else
1250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001251 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001252 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 --emsg_off;
1255
1256 return retval;
1257}
1258
Bram Moolenaara40058a2005-07-11 22:42:07 +00001259/*
1260 * Prepare v: variable "idx" to be used.
1261 * Save the current typeval in "save_tv".
1262 * When not used yet add the variable to the v: hashtable.
1263 */
1264 static void
1265prepare_vimvar(idx, save_tv)
1266 int idx;
1267 typval_T *save_tv;
1268{
1269 *save_tv = vimvars[idx].vv_tv;
1270 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1271 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1272}
1273
1274/*
1275 * Restore v: variable "idx" to typeval "save_tv".
1276 * When no longer defined, remove the variable from the v: hashtable.
1277 */
1278 static void
1279restore_vimvar(idx, save_tv)
1280 int idx;
1281 typval_T *save_tv;
1282{
1283 hashitem_T *hi;
1284
1285 clear_tv(&vimvars[idx].vv_tv);
1286 vimvars[idx].vv_tv = *save_tv;
1287 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1288 {
1289 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1290 if (HASHITEM_EMPTY(hi))
1291 EMSG2(_(e_intern2), "restore_vimvar()");
1292 else
1293 hash_remove(&vimvarht, hi);
1294 }
1295}
1296
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001297#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001298/*
1299 * Evaluate an expression to a list with suggestions.
1300 * For the "expr:" part of 'spellsuggest'.
1301 */
1302 list_T *
1303eval_spell_expr(badword, expr)
1304 char_u *badword;
1305 char_u *expr;
1306{
1307 typval_T save_val;
1308 typval_T rettv;
1309 list_T *list = NULL;
1310 char_u *p = skipwhite(expr);
1311
1312 /* Set "v:val" to the bad word. */
1313 prepare_vimvar(VV_VAL, &save_val);
1314 vimvars[VV_VAL].vv_type = VAR_STRING;
1315 vimvars[VV_VAL].vv_str = badword;
1316 if (p_verbose == 0)
1317 ++emsg_off;
1318
1319 if (eval1(&p, &rettv, TRUE) == OK)
1320 {
1321 if (rettv.v_type != VAR_LIST)
1322 clear_tv(&rettv);
1323 else
1324 list = rettv.vval.v_list;
1325 }
1326
1327 if (p_verbose == 0)
1328 --emsg_off;
1329 vimvars[VV_VAL].vv_str = NULL;
1330 restore_vimvar(VV_VAL, &save_val);
1331
1332 return list;
1333}
1334
1335/*
1336 * "list" is supposed to contain two items: a word and a number. Return the
1337 * word in "pp" and the number as the return value.
1338 * Return -1 if anything isn't right.
1339 * Used to get the good word and score from the eval_spell_expr() result.
1340 */
1341 int
1342get_spellword(list, pp)
1343 list_T *list;
1344 char_u **pp;
1345{
1346 listitem_T *li;
1347
1348 li = list->lv_first;
1349 if (li == NULL)
1350 return -1;
1351 *pp = get_tv_string(&li->li_tv);
1352
1353 li = li->li_next;
1354 if (li == NULL)
1355 return -1;
1356 return get_tv_number(&li->li_tv);
1357}
1358#endif
1359
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001360/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001361 * Top level evaluation function.
1362 * Returns an allocated typval_T with the result.
1363 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001364 */
1365 typval_T *
1366eval_expr(arg, nextcmd)
1367 char_u *arg;
1368 char_u **nextcmd;
1369{
1370 typval_T *tv;
1371
1372 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001373 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001374 {
1375 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001376 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001377 }
1378
1379 return tv;
1380}
1381
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1384/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001385 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001387 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001389 static int
1390call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 char_u *func;
1392 int argc;
1393 char_u **argv;
1394 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 long n;
1399 int len;
1400 int i;
1401 int doesrange;
1402 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001403 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar33570922005-01-25 22:26:29 +00001405 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 for (i = 0; i < argc; i++)
1410 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001411 /* Pass a NULL or empty argument as an empty string */
1412 if (argv[i] == NULL || *argv[i] == NUL)
1413 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001414 argvars[i].v_type = VAR_STRING;
1415 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001416 continue;
1417 }
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 /* Recognize a number argument, the others must be strings. */
1420 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1421 if (len != 0 && len == (int)STRLEN(argv[i]))
1422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001423 argvars[i].v_type = VAR_NUMBER;
1424 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 else
1427 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001428 argvars[i].v_type = VAR_STRING;
1429 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 }
1432
1433 if (safe)
1434 {
1435 save_funccalp = save_funccal();
1436 ++sandbox;
1437 }
1438
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001439 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1440 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001442 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (safe)
1444 {
1445 --sandbox;
1446 restore_funccal(save_funccalp);
1447 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001448 vim_free(argvars);
1449
1450 if (ret == FAIL)
1451 clear_tv(rettv);
1452
1453 return ret;
1454}
1455
1456/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001457 * Call vimL function "func" and return the result as a string.
1458 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001459 * Uses argv[argc] for the function arguments.
1460 */
1461 void *
1462call_func_retstr(func, argc, argv, safe)
1463 char_u *func;
1464 int argc;
1465 char_u **argv;
1466 int safe; /* use the sandbox */
1467{
1468 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001469 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470
1471 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1472 return NULL;
1473
1474 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 return retval;
1477}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001479#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001481 * Call vimL function "func" and return the result as a number.
1482 * Returns -1 when calling the function fails.
1483 * Uses argv[argc] for the function arguments.
1484 */
1485 long
1486call_func_retnr(func, argc, argv, safe)
1487 char_u *func;
1488 int argc;
1489 char_u **argv;
1490 int safe; /* use the sandbox */
1491{
1492 typval_T rettv;
1493 long retval;
1494
1495 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1496 return -1;
1497
1498 retval = get_tv_number_chk(&rettv, NULL);
1499 clear_tv(&rettv);
1500 return retval;
1501}
1502#endif
1503
1504/*
1505 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001506 * Uses argv[argc] for the function arguments.
1507 */
1508 void *
1509call_func_retlist(func, argc, argv, safe)
1510 char_u *func;
1511 int argc;
1512 char_u **argv;
1513 int safe; /* use the sandbox */
1514{
1515 typval_T rettv;
1516
1517 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1518 return NULL;
1519
1520 if (rettv.v_type != VAR_LIST)
1521 {
1522 clear_tv(&rettv);
1523 return NULL;
1524 }
1525
1526 return rettv.vval.v_list;
1527}
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529#endif
1530
1531/*
1532 * Save the current function call pointer, and set it to NULL.
1533 * Used when executing autocommands and for ":source".
1534 */
1535 void *
1536save_funccal()
1537{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001538 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 current_funccal = NULL;
1541 return (void *)fc;
1542}
1543
1544 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001545restore_funccal(vfc)
1546 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001548 funccall_T *fc = (funccall_T *)vfc;
1549
1550 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551}
1552
Bram Moolenaar05159a02005-02-26 23:04:13 +00001553#if defined(FEAT_PROFILE) || defined(PROTO)
1554/*
1555 * Prepare profiling for entering a child or something else that is not
1556 * counted for the script/function itself.
1557 * Should always be called in pair with prof_child_exit().
1558 */
1559 void
1560prof_child_enter(tm)
1561 proftime_T *tm; /* place to store waittime */
1562{
1563 funccall_T *fc = current_funccal;
1564
1565 if (fc != NULL && fc->func->uf_profiling)
1566 profile_start(&fc->prof_child);
1567 script_prof_save(tm);
1568}
1569
1570/*
1571 * Take care of time spent in a child.
1572 * Should always be called after prof_child_enter().
1573 */
1574 void
1575prof_child_exit(tm)
1576 proftime_T *tm; /* where waittime was stored */
1577{
1578 funccall_T *fc = current_funccal;
1579
1580 if (fc != NULL && fc->func->uf_profiling)
1581 {
1582 profile_end(&fc->prof_child);
1583 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1584 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1585 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1586 }
1587 script_prof_restore(tm);
1588}
1589#endif
1590
1591
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592#ifdef FEAT_FOLDING
1593/*
1594 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1595 * it in "*cp". Doesn't give error messages.
1596 */
1597 int
1598eval_foldexpr(arg, cp)
1599 char_u *arg;
1600 int *cp;
1601{
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 int retval;
1604 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001605 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1606 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001609 if (use_sandbox)
1610 ++sandbox;
1611 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 retval = 0;
1615 else
1616 {
1617 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001618 if (tv.v_type == VAR_NUMBER)
1619 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001620 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 retval = 0;
1622 else
1623 {
1624 /* If the result is a string, check if there is a non-digit before
1625 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 if (!VIM_ISDIGIT(*s) && *s != '-')
1628 *cp = *s++;
1629 retval = atol((char *)s);
1630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001631 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
1633 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001634 if (use_sandbox)
1635 --sandbox;
1636 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 return retval;
1639}
1640#endif
1641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001643 * ":let" list all variable values
1644 * ":let var1 var2" list variable values
1645 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 * ":let var += expr" assignment command.
1647 * ":let var -= expr" assignment command.
1648 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 */
1651 void
1652ex_let(eap)
1653 exarg_T *eap;
1654{
1655 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001657 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001659 int var_count = 0;
1660 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 expr = skip_var_list(arg, &var_count, &semicolon);
1664 if (expr == NULL)
1665 return;
1666 expr = vim_strchr(expr, '=');
1667 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001669 /*
1670 * ":let" without "=": list variables
1671 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 if (*arg == '[')
1673 EMSG(_(e_invarg));
1674 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 /* ":let var1 var2" */
1676 arg = list_arg_vars(eap, arg);
1677 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001680 list_glob_vars();
1681 list_buf_vars();
1682 list_win_vars();
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001683 list_script_vars();
1684 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001685 list_vim_vars();
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 eap->nextcmd = check_nextcmd(arg);
1688 }
1689 else
1690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001691 op[0] = '=';
1692 op[1] = NUL;
1693 if (expr > arg)
1694 {
1695 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1696 op[0] = expr[-1]; /* +=, -= or .= */
1697 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if (eap->skip)
1701 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (eap->skip)
1704 {
1705 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001706 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 --emsg_skip;
1708 }
1709 else if (i != FAIL)
1710 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 }
1716}
1717
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001718/*
1719 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1720 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 * When "nextchars" is not NULL it points to a string with characters that
1722 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1723 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 * Returns OK or FAIL;
1725 */
1726 static int
1727ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1728 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001729 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 int copy; /* copy values from "tv", don't move */
1731 int semicolon; /* from skip_var_list() */
1732 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001733 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734{
1735 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001736 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001737 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001738 listitem_T *item;
1739 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740
1741 if (*arg != '[')
1742 {
1743 /*
1744 * ":let var = expr" or ":for var in list"
1745 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001746 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 return FAIL;
1748 return OK;
1749 }
1750
1751 /*
1752 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1753 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001754 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755 {
1756 EMSG(_(e_listreq));
1757 return FAIL;
1758 }
1759
1760 i = list_len(l);
1761 if (semicolon == 0 && var_count < i)
1762 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001763 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001764 return FAIL;
1765 }
1766 if (var_count - semicolon > i)
1767 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001768 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 return FAIL;
1770 }
1771
1772 item = l->lv_first;
1773 while (*arg != ']')
1774 {
1775 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 item = item->li_next;
1778 if (arg == NULL)
1779 return FAIL;
1780
1781 arg = skipwhite(arg);
1782 if (*arg == ';')
1783 {
1784 /* Put the rest of the list (may be empty) in the var after ';'.
1785 * Create a new list for this. */
1786 l = list_alloc();
1787 if (l == NULL)
1788 return FAIL;
1789 while (item != NULL)
1790 {
1791 list_append_tv(l, &item->li_tv);
1792 item = item->li_next;
1793 }
1794
1795 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001796 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797 ltv.vval.v_list = l;
1798 l->lv_refcount = 1;
1799
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001800 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1801 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 clear_tv(&ltv);
1803 if (arg == NULL)
1804 return FAIL;
1805 break;
1806 }
1807 else if (*arg != ',' && *arg != ']')
1808 {
1809 EMSG2(_(e_intern2), "ex_let_vars()");
1810 return FAIL;
1811 }
1812 }
1813
1814 return OK;
1815}
1816
1817/*
1818 * Skip over assignable variable "var" or list of variables "[var, var]".
1819 * Used for ":let varvar = expr" and ":for varvar in expr".
1820 * For "[var, var]" increment "*var_count" for each variable.
1821 * for "[var, var; var]" set "semicolon".
1822 * Return NULL for an error.
1823 */
1824 static char_u *
1825skip_var_list(arg, var_count, semicolon)
1826 char_u *arg;
1827 int *var_count;
1828 int *semicolon;
1829{
1830 char_u *p, *s;
1831
1832 if (*arg == '[')
1833 {
1834 /* "[var, var]": find the matching ']'. */
1835 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001836 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 {
1838 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1839 s = skip_var_one(p);
1840 if (s == p)
1841 {
1842 EMSG2(_(e_invarg2), p);
1843 return NULL;
1844 }
1845 ++*var_count;
1846
1847 p = skipwhite(s);
1848 if (*p == ']')
1849 break;
1850 else if (*p == ';')
1851 {
1852 if (*semicolon == 1)
1853 {
1854 EMSG(_("Double ; in list of variables"));
1855 return NULL;
1856 }
1857 *semicolon = 1;
1858 }
1859 else if (*p != ',')
1860 {
1861 EMSG2(_(e_invarg2), p);
1862 return NULL;
1863 }
1864 }
1865 return p + 1;
1866 }
1867 else
1868 return skip_var_one(arg);
1869}
1870
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001871/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001872 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1873 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001874 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 static char_u *
1876skip_var_one(arg)
1877 char_u *arg;
1878{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001879 if (*arg == '@' && arg[1] != NUL)
1880 return arg + 2;
1881 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1882 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883}
1884
Bram Moolenaara7043832005-01-21 11:56:39 +00001885/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 * List variables for hashtab "ht" with prefix "prefix".
1887 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001890list_hashtable_vars(ht, prefix, empty)
1891 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001892 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001894{
Bram Moolenaar33570922005-01-25 22:26:29 +00001895 hashitem_T *hi;
1896 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001897 int todo;
1898
1899 todo = ht->ht_used;
1900 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1901 {
1902 if (!HASHITEM_EMPTY(hi))
1903 {
1904 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 di = HI2DI(hi);
1906 if (empty || di->di_tv.v_type != VAR_STRING
1907 || di->di_tv.vval.v_string != NULL)
1908 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 }
1910 }
1911}
1912
1913/*
1914 * List global variables.
1915 */
1916 static void
1917list_glob_vars()
1918{
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001920}
1921
1922/*
1923 * List buffer variables.
1924 */
1925 static void
1926list_buf_vars()
1927{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001928 char_u numbuf[NUMBUFLEN];
1929
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001931
1932 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1933 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934}
1935
1936/*
1937 * List window variables.
1938 */
1939 static void
1940list_win_vars()
1941{
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001943}
1944
1945/*
1946 * List Vim variables.
1947 */
1948 static void
1949list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950{
Bram Moolenaar33570922005-01-25 22:26:29 +00001951 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952}
1953
1954/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001955 * List script-local variables, if there is a script.
1956 */
1957 static void
1958list_script_vars()
1959{
1960 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1961 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1962}
1963
1964/*
1965 * List function variables, if there is a function.
1966 */
1967 static void
1968list_func_vars()
1969{
1970 if (current_funccal != NULL)
1971 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
1972 (char_u *)"l:", FALSE);
1973}
1974
1975/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 * List variables in "arg".
1977 */
1978 static char_u *
1979list_arg_vars(eap, arg)
1980 exarg_T *eap;
1981 char_u *arg;
1982{
1983 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001984 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001986 char_u *name_start;
1987 char_u *arg_subsc;
1988 char_u *tofree;
1989 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990
1991 while (!ends_excmd(*arg) && !got_int)
1992 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001995 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1997 {
1998 emsg_severe = TRUE;
1999 EMSG(_(e_trailing));
2000 break;
2001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002005 /* get_name_len() takes care of expanding curly braces */
2006 name_start = name = arg;
2007 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2008 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 /* This is mainly to keep test 49 working: when expanding
2011 * curly braces fails overrule the exception error message. */
2012 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002014 emsg_severe = TRUE;
2015 EMSG2(_(e_invarg2), arg);
2016 break;
2017 }
2018 error = TRUE;
2019 }
2020 else
2021 {
2022 if (tofree != NULL)
2023 name = tofree;
2024 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002025 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 else
2027 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002028 /* handle d.key, l[idx], f(expr) */
2029 arg_subsc = arg;
2030 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002031 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002032 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002033 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002035 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002036 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002037 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002038 case 'g': list_glob_vars(); break;
2039 case 'b': list_buf_vars(); break;
2040 case 'w': list_win_vars(); break;
2041 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002042 case 's': list_script_vars(); break;
2043 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002044 default:
2045 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002046 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002047 }
2048 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 {
2050 char_u numbuf[NUMBUFLEN];
2051 char_u *tf;
2052 int c;
2053 char_u *s;
2054
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002055 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 c = *arg;
2057 *arg = NUL;
2058 list_one_var_a((char_u *)"",
2059 arg == arg_subsc ? name : name_start,
2060 tv.v_type, s == NULL ? (char_u *)"" : s);
2061 *arg = c;
2062 vim_free(tf);
2063 }
2064 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002066 }
2067 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002068
2069 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002071
2072 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002073 }
2074
2075 return arg;
2076}
2077
2078/*
2079 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2080 * Returns a pointer to the char just after the var name.
2081 * Returns NULL if there is an error.
2082 */
2083 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002087 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002088 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090{
2091 int c1;
2092 char_u *name;
2093 char_u *p;
2094 char_u *arg_end = NULL;
2095 int len;
2096 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098
2099 /*
2100 * ":let $VAR = expr": Set environment variable.
2101 */
2102 if (*arg == '$')
2103 {
2104 /* Find the end of the name. */
2105 ++arg;
2106 name = arg;
2107 len = get_env_len(&arg);
2108 if (len == 0)
2109 EMSG2(_(e_invarg2), name - 1);
2110 else
2111 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002112 if (op != NULL && (*op == '+' || *op == '-'))
2113 EMSG2(_(e_letwrong), op);
2114 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 EMSG(_(e_letunexp));
2117 else
2118 {
2119 c1 = name[len];
2120 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002121 p = get_tv_string_chk(tv);
2122 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002123 {
2124 int mustfree = FALSE;
2125 char_u *s = vim_getenv(name, &mustfree);
2126
2127 if (s != NULL)
2128 {
2129 p = tofree = concat_str(s, p);
2130 if (mustfree)
2131 vim_free(s);
2132 }
2133 }
2134 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002135 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002137 if (STRICMP(name, "HOME") == 0)
2138 init_homedir();
2139 else if (didset_vim && STRICMP(name, "VIM") == 0)
2140 didset_vim = FALSE;
2141 else if (didset_vimruntime
2142 && STRICMP(name, "VIMRUNTIME") == 0)
2143 didset_vimruntime = FALSE;
2144 arg_end = arg;
2145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002146 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002147 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 }
2149 }
2150 }
2151
2152 /*
2153 * ":let &option = expr": Set option value.
2154 * ":let &l:option = expr": Set local option value.
2155 * ":let &g:option = expr": Set global option value.
2156 */
2157 else if (*arg == '&')
2158 {
2159 /* Find the end of the name. */
2160 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161 if (p == NULL || (endchars != NULL
2162 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 EMSG(_(e_letunexp));
2164 else
2165 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002166 long n;
2167 int opt_type;
2168 long numval;
2169 char_u *stringval = NULL;
2170 char_u *s;
2171
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 c1 = *p;
2173 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002174
2175 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 s = get_tv_string_chk(tv); /* != NULL if number or string */
2177 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002178 {
2179 opt_type = get_option_value(arg, &numval,
2180 &stringval, opt_flags);
2181 if ((opt_type == 1 && *op == '.')
2182 || (opt_type == 0 && *op != '.'))
2183 EMSG2(_(e_letwrong), op);
2184 else
2185 {
2186 if (opt_type == 1) /* number */
2187 {
2188 if (*op == '+')
2189 n = numval + n;
2190 else
2191 n = numval - n;
2192 }
2193 else if (opt_type == 0 && stringval != NULL) /* string */
2194 {
2195 s = concat_str(stringval, s);
2196 vim_free(stringval);
2197 stringval = s;
2198 }
2199 }
2200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002201 if (s != NULL)
2202 {
2203 set_option_value(arg, n, s, opt_flags);
2204 arg_end = p;
2205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002207 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
2209 }
2210
2211 /*
2212 * ":let @r = expr": Set register contents.
2213 */
2214 else if (*arg == '@')
2215 {
2216 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002217 if (op != NULL && (*op == '+' || *op == '-'))
2218 EMSG2(_(e_letwrong), op);
2219 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002220 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 EMSG(_(e_letunexp));
2222 else
2223 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002224 char_u *tofree = NULL;
2225 char_u *s;
2226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002227 p = get_tv_string_chk(tv);
2228 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002230 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002231 if (s != NULL)
2232 {
2233 p = tofree = concat_str(s, p);
2234 vim_free(s);
2235 }
2236 }
2237 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002238 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002239 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 arg_end = arg + 1;
2241 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002242 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 }
2244 }
2245
2246 /*
2247 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002250 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002254 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2258 EMSG(_(e_letunexp));
2259 else
2260 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002261 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 arg_end = p;
2263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 }
2267
2268 else
2269 EMSG2(_(e_invarg2), arg);
2270
2271 return arg_end;
2272}
2273
2274/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002275 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2276 */
2277 static int
2278check_changedtick(arg)
2279 char_u *arg;
2280{
2281 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2282 {
2283 EMSG2(_(e_readonlyvar), arg);
2284 return TRUE;
2285 }
2286 return FALSE;
2287}
2288
2289/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002290 * Get an lval: variable, Dict item or List item that can be assigned a value
2291 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2292 * "name.key", "name.key[expr]" etc.
2293 * Indexing only works if "name" is an existing List or Dictionary.
2294 * "name" points to the start of the name.
2295 * If "rettv" is not NULL it points to the value to be assigned.
2296 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2297 * wrong; must end in space or cmd separator.
2298 *
2299 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 */
2303 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002304get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002306 typval_T *rettv;
2307 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002308 int unlet;
2309 int skip;
2310 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002311 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002314 char_u *expr_start, *expr_end;
2315 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 dictitem_T *v;
2317 typval_T var1;
2318 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002321 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002322 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002323 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002325 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002327
2328 if (skip)
2329 {
2330 /* When skipping just find the end of the name. */
2331 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002332 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 }
2334
2335 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002336 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (expr_start != NULL)
2338 {
2339 /* Don't expand the name when we already know there is an error. */
2340 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2341 && *p != '[' && *p != '.')
2342 {
2343 EMSG(_(e_trailing));
2344 return NULL;
2345 }
2346
2347 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2348 if (lp->ll_exp_name == NULL)
2349 {
2350 /* Report an invalid expression in braces, unless the
2351 * expression evaluation has been cancelled due to an
2352 * aborting error, an interrupt, or an exception. */
2353 if (!aborting() && !quiet)
2354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002355 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 EMSG2(_(e_invarg2), name);
2357 return NULL;
2358 }
2359 }
2360 lp->ll_name = lp->ll_exp_name;
2361 }
2362 else
2363 lp->ll_name = name;
2364
2365 /* Without [idx] or .key we are done. */
2366 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2367 return p;
2368
2369 cc = *p;
2370 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002371 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 if (v == NULL && !quiet)
2373 EMSG2(_(e_undefvar), lp->ll_name);
2374 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 if (v == NULL)
2376 return NULL;
2377
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 /*
2379 * Loop until no more [idx] or .key is following.
2380 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002381 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002384 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2385 && !(lp->ll_tv->v_type == VAR_DICT
2386 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002388 if (!quiet)
2389 EMSG(_("E689: Can only index a List or Dictionary"));
2390 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002392 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 if (!quiet)
2395 EMSG(_("E708: [:] must come last"));
2396 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002398
Bram Moolenaar8c711452005-01-14 21:53:12 +00002399 len = -1;
2400 if (*p == '.')
2401 {
2402 key = p + 1;
2403 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2404 ;
2405 if (len == 0)
2406 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 if (!quiet)
2408 EMSG(_(e_emptykey));
2409 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 }
2411 p = key + len;
2412 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002413 else
2414 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002416 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 if (*p == ':')
2418 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002419 else
2420 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 empty1 = FALSE;
2422 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002423 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 if (get_tv_string_chk(&var1) == NULL)
2425 {
2426 /* not a number or string */
2427 clear_tv(&var1);
2428 return NULL;
2429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 }
2431
2432 /* Optionally get the second index [ :expr]. */
2433 if (*p == ':')
2434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002435 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002439 if (!empty1)
2440 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002442 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (rettv != NULL && (rettv->v_type != VAR_LIST
2444 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
2447 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 if (!empty1)
2449 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452 p = skipwhite(p + 1);
2453 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 else
2456 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002458 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 if (!empty1)
2461 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (get_tv_string_chk(&var2) == NULL)
2465 {
2466 /* not a number or string */
2467 if (!empty1)
2468 clear_tv(&var1);
2469 clear_tv(&var2);
2470 return NULL;
2471 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002477
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (!quiet)
2481 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002482 if (!empty1)
2483 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 }
2488
2489 /* Skip to past ']'. */
2490 ++p;
2491 }
2492
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 {
2495 if (len == -1)
2496 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 if (*key == NUL)
2500 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 if (!quiet)
2502 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002503 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
2506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 lp->ll_list = NULL;
2508 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002509 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002512 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002516 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002517 if (len == -1)
2518 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 }
2521 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 if (len == -1)
2526 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 p = NULL;
2529 break;
2530 }
2531 if (len == -1)
2532 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 }
2535 else
2536 {
2537 /*
2538 * Get the number and item for the only or first index of the List.
2539 */
2540 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 else
2543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 clear_tv(&var1);
2546 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 lp->ll_list = lp->ll_tv->vval.v_list;
2549 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2550 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 if (!quiet)
2553 EMSGN(_(e_listidx), lp->ll_n1);
2554 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 }
2558
2559 /*
2560 * May need to find the item or absolute index for the second
2561 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 * When no index given: "lp->ll_empty2" is TRUE.
2563 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002567 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002568 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002572 if (ni == NULL)
2573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (!quiet)
2575 EMSGN(_(e_listidx), lp->ll_n2);
2576 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 }
2580
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2582 if (lp->ll_n1 < 0)
2583 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2584 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002585 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (!quiet)
2587 EMSGN(_(e_listidx), lp->ll_n2);
2588 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002589 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002590 }
2591
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 }
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 return p;
2597}
2598
2599/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 */
2602 static void
2603clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605{
2606 vim_free(lp->ll_exp_name);
2607 vim_free(lp->ll_newkey);
2608}
2609
2610/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002611 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002613 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 */
2615 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002619 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622{
2623 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 listitem_T *ri;
2625 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626
2627 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 cc = *endp;
2632 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 if (op != NULL && *op != '=')
2634 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002636
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002637 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002638 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2639 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 {
2641 if (tv_op(&tv, rettv, op) == OK)
2642 set_var(lp->ll_name, &tv, FALSE);
2643 clear_tv(&tv);
2644 }
2645 }
2646 else
2647 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002651 else if (tv_check_lock(lp->ll_newkey == NULL
2652 ? lp->ll_tv->v_lock
2653 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2654 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 else if (lp->ll_range)
2656 {
2657 /*
2658 * Assign the List values to the list items.
2659 */
2660 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002662 if (op != NULL && *op != '=')
2663 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2664 else
2665 {
2666 clear_tv(&lp->ll_li->li_tv);
2667 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2668 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 ri = ri->li_next;
2670 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2671 break;
2672 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002675 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 ri = NULL;
2678 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 lp->ll_li = lp->ll_li->li_next;
2682 ++lp->ll_n1;
2683 }
2684 if (ri != NULL)
2685 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 else if (lp->ll_empty2
2687 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 : lp->ll_n1 != lp->ll_n2)
2689 EMSG(_("E711: List value has not enough items"));
2690 }
2691 else
2692 {
2693 /*
2694 * Assign to a List or Dictionary item.
2695 */
2696 if (lp->ll_newkey != NULL)
2697 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002698 if (op != NULL && *op != '=')
2699 {
2700 EMSG2(_(e_letwrong), op);
2701 return;
2702 }
2703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002705 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (di == NULL)
2707 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002708 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2709 {
2710 vim_free(di);
2711 return;
2712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 else if (op != NULL && *op != '=')
2716 {
2717 tv_op(lp->ll_tv, rettv, op);
2718 return;
2719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002720 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 /*
2724 * Assign the value to the variable or list item.
2725 */
2726 if (copy)
2727 copy_tv(rettv, lp->ll_tv);
2728 else
2729 {
2730 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002731 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002733 }
2734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002735}
2736
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2739 * Returns OK or FAIL.
2740 */
2741 static int
2742tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002743 typval_T *tv1;
2744 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 char_u *op;
2746{
2747 long n;
2748 char_u numbuf[NUMBUFLEN];
2749 char_u *s;
2750
2751 /* Can't do anything with a Funcref or a Dict on the right. */
2752 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2753 {
2754 switch (tv1->v_type)
2755 {
2756 case VAR_DICT:
2757 case VAR_FUNC:
2758 break;
2759
2760 case VAR_LIST:
2761 if (*op != '+' || tv2->v_type != VAR_LIST)
2762 break;
2763 /* List += List */
2764 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2765 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2766 return OK;
2767
2768 case VAR_NUMBER:
2769 case VAR_STRING:
2770 if (tv2->v_type == VAR_LIST)
2771 break;
2772 if (*op == '+' || *op == '-')
2773 {
2774 /* nr += nr or nr -= nr*/
2775 n = get_tv_number(tv1);
2776 if (*op == '+')
2777 n += get_tv_number(tv2);
2778 else
2779 n -= get_tv_number(tv2);
2780 clear_tv(tv1);
2781 tv1->v_type = VAR_NUMBER;
2782 tv1->vval.v_number = n;
2783 }
2784 else
2785 {
2786 /* str .= str */
2787 s = get_tv_string(tv1);
2788 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2789 clear_tv(tv1);
2790 tv1->v_type = VAR_STRING;
2791 tv1->vval.v_string = s;
2792 }
2793 return OK;
2794 }
2795 }
2796
2797 EMSG2(_(e_letwrong), op);
2798 return FAIL;
2799}
2800
2801/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002802 * Add a watcher to a list.
2803 */
2804 static void
2805list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002806 list_T *l;
2807 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002808{
2809 lw->lw_next = l->lv_watch;
2810 l->lv_watch = lw;
2811}
2812
2813/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002814 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002815 * No warning when it isn't found...
2816 */
2817 static void
2818list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 list_T *l;
2820 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002821{
Bram Moolenaar33570922005-01-25 22:26:29 +00002822 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002823
2824 lwp = &l->lv_watch;
2825 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2826 {
2827 if (lw == lwrem)
2828 {
2829 *lwp = lw->lw_next;
2830 break;
2831 }
2832 lwp = &lw->lw_next;
2833 }
2834}
2835
2836/*
2837 * Just before removing an item from a list: advance watchers to the next
2838 * item.
2839 */
2840 static void
2841list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002842 list_T *l;
2843 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002844{
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846
2847 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2848 if (lw->lw_item == item)
2849 lw->lw_item = item->li_next;
2850}
2851
2852/*
2853 * Evaluate the expression used in a ":for var in expr" command.
2854 * "arg" points to "var".
2855 * Set "*errp" to TRUE for an error, FALSE otherwise;
2856 * Return a pointer that holds the info. Null when there is an error.
2857 */
2858 void *
2859eval_for_line(arg, errp, nextcmdp, skip)
2860 char_u *arg;
2861 int *errp;
2862 char_u **nextcmdp;
2863 int skip;
2864{
Bram Moolenaar33570922005-01-25 22:26:29 +00002865 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002866 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 typval_T tv;
2868 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002869
2870 *errp = TRUE; /* default: there is an error */
2871
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002873 if (fi == NULL)
2874 return NULL;
2875
2876 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2877 if (expr == NULL)
2878 return fi;
2879
2880 expr = skipwhite(expr);
2881 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2882 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002883 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002884 return fi;
2885 }
2886
2887 if (skip)
2888 ++emsg_skip;
2889 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2890 {
2891 *errp = FALSE;
2892 if (!skip)
2893 {
2894 l = tv.vval.v_list;
2895 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002896 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002897 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002898 clear_tv(&tv);
2899 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900 else
2901 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002902 /* No need to increment the refcount, it's already set for the
2903 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 fi->fi_list = l;
2905 list_add_watch(l, &fi->fi_lw);
2906 fi->fi_lw.lw_item = l->lv_first;
2907 }
2908 }
2909 }
2910 if (skip)
2911 --emsg_skip;
2912
2913 return fi;
2914}
2915
2916/*
2917 * Use the first item in a ":for" list. Advance to the next.
2918 * Assign the values to the variable (list). "arg" points to the first one.
2919 * Return TRUE when a valid item was found, FALSE when at end of list or
2920 * something wrong.
2921 */
2922 int
2923next_for_item(fi_void, arg)
2924 void *fi_void;
2925 char_u *arg;
2926{
Bram Moolenaar33570922005-01-25 22:26:29 +00002927 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002928 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002929 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002930
2931 item = fi->fi_lw.lw_item;
2932 if (item == NULL)
2933 result = FALSE;
2934 else
2935 {
2936 fi->fi_lw.lw_item = item->li_next;
2937 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2938 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2939 }
2940 return result;
2941}
2942
2943/*
2944 * Free the structure used to store info used by ":for".
2945 */
2946 void
2947free_for_info(fi_void)
2948 void *fi_void;
2949{
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002951
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002952 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002953 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002954 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002955 list_unref(fi->fi_list);
2956 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957 vim_free(fi);
2958}
2959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2961
2962 void
2963set_context_for_expression(xp, arg, cmdidx)
2964 expand_T *xp;
2965 char_u *arg;
2966 cmdidx_T cmdidx;
2967{
2968 int got_eq = FALSE;
2969 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002970 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002972 if (cmdidx == CMD_let)
2973 {
2974 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002975 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002976 {
2977 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002978 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002979 {
2980 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002982 if (vim_iswhite(*p))
2983 break;
2984 }
2985 return;
2986 }
2987 }
2988 else
2989 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2990 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 while ((xp->xp_pattern = vim_strpbrk(arg,
2992 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2993 {
2994 c = *xp->xp_pattern;
2995 if (c == '&')
2996 {
2997 c = xp->xp_pattern[1];
2998 if (c == '&')
2999 {
3000 ++xp->xp_pattern;
3001 xp->xp_context = cmdidx != CMD_let || got_eq
3002 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3003 }
3004 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003005 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003007 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3008 xp->xp_pattern += 2;
3009
3010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012 else if (c == '$')
3013 {
3014 /* environment variable */
3015 xp->xp_context = EXPAND_ENV_VARS;
3016 }
3017 else if (c == '=')
3018 {
3019 got_eq = TRUE;
3020 xp->xp_context = EXPAND_EXPRESSION;
3021 }
3022 else if (c == '<'
3023 && xp->xp_context == EXPAND_FUNCTIONS
3024 && vim_strchr(xp->xp_pattern, '(') == NULL)
3025 {
3026 /* Function name can start with "<SNR>" */
3027 break;
3028 }
3029 else if (cmdidx != CMD_let || got_eq)
3030 {
3031 if (c == '"') /* string */
3032 {
3033 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3034 if (c == '\\' && xp->xp_pattern[1] != NUL)
3035 ++xp->xp_pattern;
3036 xp->xp_context = EXPAND_NOTHING;
3037 }
3038 else if (c == '\'') /* literal string */
3039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003040 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3042 /* skip */ ;
3043 xp->xp_context = EXPAND_NOTHING;
3044 }
3045 else if (c == '|')
3046 {
3047 if (xp->xp_pattern[1] == '|')
3048 {
3049 ++xp->xp_pattern;
3050 xp->xp_context = EXPAND_EXPRESSION;
3051 }
3052 else
3053 xp->xp_context = EXPAND_COMMANDS;
3054 }
3055 else
3056 xp->xp_context = EXPAND_EXPRESSION;
3057 }
3058 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059 /* Doesn't look like something valid, expand as an expression
3060 * anyway. */
3061 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 arg = xp->xp_pattern;
3063 if (*arg != NUL)
3064 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3065 /* skip */ ;
3066 }
3067 xp->xp_pattern = arg;
3068}
3069
3070#endif /* FEAT_CMDL_COMPL */
3071
3072/*
3073 * ":1,25call func(arg1, arg2)" function call.
3074 */
3075 void
3076ex_call(eap)
3077 exarg_T *eap;
3078{
3079 char_u *arg = eap->arg;
3080 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003084 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 linenr_T lnum;
3086 int doesrange;
3087 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003088 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003090 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3091 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 if (tofree == NULL)
3093 return;
3094
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 /* Increase refcount on dictionary, it could get deleted when evaluating
3096 * the arguments. */
3097 if (fudi.fd_dict != NULL)
3098 ++fudi.fd_dict->dv_refcount;
3099
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3101 len = STRLEN(tofree);
3102 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar532c7802005-01-27 14:44:31 +00003104 /* Skip white space to allow ":call func ()". Not good, but required for
3105 * backward compatibility. */
3106 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 if (*startarg != '(')
3110 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003111 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 goto end;
3113 }
3114
3115 /*
3116 * When skipping, evaluate the function once, to find the end of the
3117 * arguments.
3118 * When the function takes a range, this is discovered after the first
3119 * call, and the loop is broken.
3120 */
3121 if (eap->skip)
3122 {
3123 ++emsg_skip;
3124 lnum = eap->line2; /* do it once, also with an invalid range */
3125 }
3126 else
3127 lnum = eap->line1;
3128 for ( ; lnum <= eap->line2; ++lnum)
3129 {
3130 if (!eap->skip && eap->addr_count > 0)
3131 {
3132 curwin->w_cursor.lnum = lnum;
3133 curwin->w_cursor.col = 0;
3134 }
3135 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003137 eap->line1, eap->line2, &doesrange,
3138 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 failed = TRUE;
3141 break;
3142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003143 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (doesrange || eap->skip)
3145 break;
3146 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003147 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003148 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003149 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (aborting())
3151 break;
3152 }
3153 if (eap->skip)
3154 --emsg_skip;
3155
3156 if (!failed)
3157 {
3158 /* Check for trailing illegal characters and a following command. */
3159 if (!ends_excmd(*arg))
3160 {
3161 emsg_severe = TRUE;
3162 EMSG(_(e_trailing));
3163 }
3164 else
3165 eap->nextcmd = check_nextcmd(arg);
3166 }
3167
3168end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003169 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171}
3172
3173/*
3174 * ":unlet[!] var1 ... " command.
3175 */
3176 void
3177ex_unlet(eap)
3178 exarg_T *eap;
3179{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003180 ex_unletlock(eap, eap->arg, 0);
3181}
3182
3183/*
3184 * ":lockvar" and ":unlockvar" commands
3185 */
3186 void
3187ex_lockvar(eap)
3188 exarg_T *eap;
3189{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003191 int deep = 2;
3192
3193 if (eap->forceit)
3194 deep = -1;
3195 else if (vim_isdigit(*arg))
3196 {
3197 deep = getdigits(&arg);
3198 arg = skipwhite(arg);
3199 }
3200
3201 ex_unletlock(eap, arg, deep);
3202}
3203
3204/*
3205 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3206 */
3207 static void
3208ex_unletlock(eap, argstart, deep)
3209 exarg_T *eap;
3210 char_u *argstart;
3211 int deep;
3212{
3213 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003216 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 do
3219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003221 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3222 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223 if (lv.ll_name == NULL)
3224 error = TRUE; /* error but continue parsing */
3225 if (name_end == NULL || (!vim_iswhite(*name_end)
3226 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 if (name_end != NULL)
3229 {
3230 emsg_severe = TRUE;
3231 EMSG(_(e_trailing));
3232 }
3233 if (!(eap->skip || error))
3234 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 break;
3236 }
3237
3238 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003239 {
3240 if (eap->cmdidx == CMD_unlet)
3241 {
3242 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3243 error = TRUE;
3244 }
3245 else
3246 {
3247 if (do_lock_var(&lv, name_end, deep,
3248 eap->cmdidx == CMD_lockvar) == FAIL)
3249 error = TRUE;
3250 }
3251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003253 if (!eap->skip)
3254 clear_lval(&lv);
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 arg = skipwhite(name_end);
3257 } while (!ends_excmd(*arg));
3258
3259 eap->nextcmd = check_nextcmd(arg);
3260}
3261
Bram Moolenaar8c711452005-01-14 21:53:12 +00003262 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003263do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003264 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003265 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003266 int forceit;
3267{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003268 int ret = OK;
3269 int cc;
3270
3271 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003273 cc = *name_end;
3274 *name_end = NUL;
3275
3276 /* Normal name or expanded name. */
3277 if (check_changedtick(lp->ll_name))
3278 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003279 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003280 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003281 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003282 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003283 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3284 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003285 else if (lp->ll_range)
3286 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003287 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003288
3289 /* Delete a range of List items. */
3290 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3291 {
3292 li = lp->ll_li->li_next;
3293 listitem_remove(lp->ll_list, lp->ll_li);
3294 lp->ll_li = li;
3295 ++lp->ll_n1;
3296 }
3297 }
3298 else
3299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003300 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003301 /* unlet a List item. */
3302 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003303 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003304 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003305 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 }
3307
3308 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003309}
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311/*
3312 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003313 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
3315 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003316do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003318 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 hashtab_T *ht;
3321 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003322 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar33570922005-01-25 22:26:29 +00003324 ht = find_var_ht(name, &varname);
3325 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 hi = hash_find(ht, varname);
3328 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003329 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003330 if (var_check_ro(HI2DI(hi)->di_flags, name))
3331 return FAIL;
3332 delete_var(ht, hi);
3333 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003336 if (forceit)
3337 return OK;
3338 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 return FAIL;
3340}
3341
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003342/*
3343 * Lock or unlock variable indicated by "lp".
3344 * "deep" is the levels to go (-1 for unlimited);
3345 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3346 */
3347 static int
3348do_lock_var(lp, name_end, deep, lock)
3349 lval_T *lp;
3350 char_u *name_end;
3351 int deep;
3352 int lock;
3353{
3354 int ret = OK;
3355 int cc;
3356 dictitem_T *di;
3357
3358 if (deep == 0) /* nothing to do */
3359 return OK;
3360
3361 if (lp->ll_tv == NULL)
3362 {
3363 cc = *name_end;
3364 *name_end = NUL;
3365
3366 /* Normal name or expanded name. */
3367 if (check_changedtick(lp->ll_name))
3368 ret = FAIL;
3369 else
3370 {
3371 di = find_var(lp->ll_name, NULL);
3372 if (di == NULL)
3373 ret = FAIL;
3374 else
3375 {
3376 if (lock)
3377 di->di_flags |= DI_FLAGS_LOCK;
3378 else
3379 di->di_flags &= ~DI_FLAGS_LOCK;
3380 item_lock(&di->di_tv, deep, lock);
3381 }
3382 }
3383 *name_end = cc;
3384 }
3385 else if (lp->ll_range)
3386 {
3387 listitem_T *li = lp->ll_li;
3388
3389 /* (un)lock a range of List items. */
3390 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3391 {
3392 item_lock(&li->li_tv, deep, lock);
3393 li = li->li_next;
3394 ++lp->ll_n1;
3395 }
3396 }
3397 else if (lp->ll_list != NULL)
3398 /* (un)lock a List item. */
3399 item_lock(&lp->ll_li->li_tv, deep, lock);
3400 else
3401 /* un(lock) a Dictionary item. */
3402 item_lock(&lp->ll_di->di_tv, deep, lock);
3403
3404 return ret;
3405}
3406
3407/*
3408 * Lock or unlock an item. "deep" is nr of levels to go.
3409 */
3410 static void
3411item_lock(tv, deep, lock)
3412 typval_T *tv;
3413 int deep;
3414 int lock;
3415{
3416 static int recurse = 0;
3417 list_T *l;
3418 listitem_T *li;
3419 dict_T *d;
3420 hashitem_T *hi;
3421 int todo;
3422
3423 if (recurse >= DICT_MAXNEST)
3424 {
3425 EMSG(_("E743: variable nested too deep for (un)lock"));
3426 return;
3427 }
3428 if (deep == 0)
3429 return;
3430 ++recurse;
3431
3432 /* lock/unlock the item itself */
3433 if (lock)
3434 tv->v_lock |= VAR_LOCKED;
3435 else
3436 tv->v_lock &= ~VAR_LOCKED;
3437
3438 switch (tv->v_type)
3439 {
3440 case VAR_LIST:
3441 if ((l = tv->vval.v_list) != NULL)
3442 {
3443 if (lock)
3444 l->lv_lock |= VAR_LOCKED;
3445 else
3446 l->lv_lock &= ~VAR_LOCKED;
3447 if (deep < 0 || deep > 1)
3448 /* recursive: lock/unlock the items the List contains */
3449 for (li = l->lv_first; li != NULL; li = li->li_next)
3450 item_lock(&li->li_tv, deep - 1, lock);
3451 }
3452 break;
3453 case VAR_DICT:
3454 if ((d = tv->vval.v_dict) != NULL)
3455 {
3456 if (lock)
3457 d->dv_lock |= VAR_LOCKED;
3458 else
3459 d->dv_lock &= ~VAR_LOCKED;
3460 if (deep < 0 || deep > 1)
3461 {
3462 /* recursive: lock/unlock the items the List contains */
3463 todo = d->dv_hashtab.ht_used;
3464 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3465 {
3466 if (!HASHITEM_EMPTY(hi))
3467 {
3468 --todo;
3469 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3470 }
3471 }
3472 }
3473 }
3474 }
3475 --recurse;
3476}
3477
Bram Moolenaara40058a2005-07-11 22:42:07 +00003478/*
3479 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3480 * it refers to a List or Dictionary that is locked.
3481 */
3482 static int
3483tv_islocked(tv)
3484 typval_T *tv;
3485{
3486 return (tv->v_lock & VAR_LOCKED)
3487 || (tv->v_type == VAR_LIST
3488 && tv->vval.v_list != NULL
3489 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3490 || (tv->v_type == VAR_DICT
3491 && tv->vval.v_dict != NULL
3492 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3493}
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3496/*
3497 * Delete all "menutrans_" variables.
3498 */
3499 void
3500del_menutrans_vars()
3501{
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003503 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 hash_lock(&globvarht);
3506 todo = globvarht.ht_used;
3507 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003508 {
3509 if (!HASHITEM_EMPTY(hi))
3510 {
3511 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003512 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3513 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003514 }
3515 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003516 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517}
3518#endif
3519
3520#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3521
3522/*
3523 * Local string buffer for the next two functions to store a variable name
3524 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3525 * get_user_var_name().
3526 */
3527
3528static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3529
3530static char_u *varnamebuf = NULL;
3531static int varnamebuflen = 0;
3532
3533/*
3534 * Function to concatenate a prefix and a variable name.
3535 */
3536 static char_u *
3537cat_prefix_varname(prefix, name)
3538 int prefix;
3539 char_u *name;
3540{
3541 int len;
3542
3543 len = (int)STRLEN(name) + 3;
3544 if (len > varnamebuflen)
3545 {
3546 vim_free(varnamebuf);
3547 len += 10; /* some additional space */
3548 varnamebuf = alloc(len);
3549 if (varnamebuf == NULL)
3550 {
3551 varnamebuflen = 0;
3552 return NULL;
3553 }
3554 varnamebuflen = len;
3555 }
3556 *varnamebuf = prefix;
3557 varnamebuf[1] = ':';
3558 STRCPY(varnamebuf + 2, name);
3559 return varnamebuf;
3560}
3561
3562/*
3563 * Function given to ExpandGeneric() to obtain the list of user defined
3564 * (global/buffer/window/built-in) variable names.
3565 */
3566/*ARGSUSED*/
3567 char_u *
3568get_user_var_name(xp, idx)
3569 expand_T *xp;
3570 int idx;
3571{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003572 static long_u gdone;
3573 static long_u bdone;
3574 static long_u wdone;
3575 static int vidx;
3576 static hashitem_T *hi;
3577 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003580 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003581
3582 /* Global variables */
3583 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003585 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003587 else
3588 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003589 while (HASHITEM_EMPTY(hi))
3590 ++hi;
3591 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3592 return cat_prefix_varname('g', hi->hi_key);
3593 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003595
3596 /* b: variables */
3597 ht = &curbuf->b_vars.dv_hashtab;
3598 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003600 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003601 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003602 else
3603 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003604 while (HASHITEM_EMPTY(hi))
3605 ++hi;
3606 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003608 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003610 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return (char_u *)"b:changedtick";
3612 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003613
3614 /* w: variables */
3615 ht = &curwin->w_vars.dv_hashtab;
3616 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003618 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003619 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003620 else
3621 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003622 while (HASHITEM_EMPTY(hi))
3623 ++hi;
3624 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003626
3627 /* v: variables */
3628 if (vidx < VV_LEN)
3629 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 vim_free(varnamebuf);
3632 varnamebuf = NULL;
3633 varnamebuflen = 0;
3634 return NULL;
3635}
3636
3637#endif /* FEAT_CMDL_COMPL */
3638
3639/*
3640 * types for expressions.
3641 */
3642typedef enum
3643{
3644 TYPE_UNKNOWN = 0
3645 , TYPE_EQUAL /* == */
3646 , TYPE_NEQUAL /* != */
3647 , TYPE_GREATER /* > */
3648 , TYPE_GEQUAL /* >= */
3649 , TYPE_SMALLER /* < */
3650 , TYPE_SEQUAL /* <= */
3651 , TYPE_MATCH /* =~ */
3652 , TYPE_NOMATCH /* !~ */
3653} exptype_T;
3654
3655/*
3656 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3659 */
3660
3661/*
3662 * Handle zero level expression.
3663 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003664 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003665 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 * Return OK or FAIL.
3667 */
3668 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 char_u **nextcmd;
3673 int evaluate;
3674{
3675 int ret;
3676 char_u *p;
3677
3678 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003679 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (ret == FAIL || !ends_excmd(*p))
3681 {
3682 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003683 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /*
3685 * Report the invalid expression unless the expression evaluation has
3686 * been cancelled due to an aborting error, an interrupt, or an
3687 * exception.
3688 */
3689 if (!aborting())
3690 EMSG2(_(e_invexpr2), arg);
3691 ret = FAIL;
3692 }
3693 if (nextcmd != NULL)
3694 *nextcmd = check_nextcmd(p);
3695
3696 return ret;
3697}
3698
3699/*
3700 * Handle top level expression:
3701 * expr1 ? expr0 : expr0
3702 *
3703 * "arg" must point to the first non-white of the expression.
3704 * "arg" is advanced to the next non-white after the recognized expression.
3705 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003706 * Note: "rettv.v_lock" is not set.
3707 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 * Return OK or FAIL.
3709 */
3710 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 int evaluate;
3715{
3716 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
3719 /*
3720 * Get the first variable.
3721 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003722 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 return FAIL;
3724
3725 if ((*arg)[0] == '?')
3726 {
3727 result = FALSE;
3728 if (evaluate)
3729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003730 int error = FALSE;
3731
3732 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 if (error)
3736 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 /*
3740 * Get the second variable.
3741 */
3742 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 return FAIL;
3745
3746 /*
3747 * Check for the ":".
3748 */
3749 if ((*arg)[0] != ':')
3750 {
3751 EMSG(_("E109: Missing ':' after '?'"));
3752 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 return FAIL;
3755 }
3756
3757 /*
3758 * Get the third variable.
3759 */
3760 *arg = skipwhite(*arg + 1);
3761 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3762 {
3763 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003764 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return FAIL;
3766 }
3767 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770
3771 return OK;
3772}
3773
3774/*
3775 * Handle first level expression:
3776 * expr2 || expr2 || expr2 logical OR
3777 *
3778 * "arg" must point to the first non-white of the expression.
3779 * "arg" is advanced to the next non-white after the recognized expression.
3780 *
3781 * Return OK or FAIL.
3782 */
3783 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003784eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 int evaluate;
3788{
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 long result;
3791 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793
3794 /*
3795 * Get the first variable.
3796 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return FAIL;
3799
3800 /*
3801 * Repeat until there is no following "||".
3802 */
3803 first = TRUE;
3804 result = FALSE;
3805 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3806 {
3807 if (evaluate && first)
3808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003809 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003811 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 if (error)
3813 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 first = FALSE;
3815 }
3816
3817 /*
3818 * Get the second variable.
3819 */
3820 *arg = skipwhite(*arg + 2);
3821 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3822 return FAIL;
3823
3824 /*
3825 * Compute the result.
3826 */
3827 if (evaluate && !result)
3828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003829 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003831 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (error)
3833 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 if (evaluate)
3836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003837 rettv->v_type = VAR_NUMBER;
3838 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
3841
3842 return OK;
3843}
3844
3845/*
3846 * Handle second level expression:
3847 * expr3 && expr3 && expr3 logical AND
3848 *
3849 * "arg" must point to the first non-white of the expression.
3850 * "arg" is advanced to the next non-white after the recognized expression.
3851 *
3852 * Return OK or FAIL.
3853 */
3854 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 int evaluate;
3859{
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 long result;
3862 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003863 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864
3865 /*
3866 * Get the first variable.
3867 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return FAIL;
3870
3871 /*
3872 * Repeat until there is no following "&&".
3873 */
3874 first = TRUE;
3875 result = TRUE;
3876 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3877 {
3878 if (evaluate && first)
3879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003882 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 if (error)
3884 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 first = FALSE;
3886 }
3887
3888 /*
3889 * Get the second variable.
3890 */
3891 *arg = skipwhite(*arg + 2);
3892 if (eval4(arg, &var2, evaluate && result) == FAIL)
3893 return FAIL;
3894
3895 /*
3896 * Compute the result.
3897 */
3898 if (evaluate && result)
3899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003902 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 if (error)
3904 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906 if (evaluate)
3907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003908 rettv->v_type = VAR_NUMBER;
3909 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 }
3912
3913 return OK;
3914}
3915
3916/*
3917 * Handle third level expression:
3918 * var1 == var2
3919 * var1 =~ var2
3920 * var1 != var2
3921 * var1 !~ var2
3922 * var1 > var2
3923 * var1 >= var2
3924 * var1 < var2
3925 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003926 * var1 is var2
3927 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 *
3929 * "arg" must point to the first non-white of the expression.
3930 * "arg" is advanced to the next non-white after the recognized expression.
3931 *
3932 * Return OK or FAIL.
3933 */
3934 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003935eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 int evaluate;
3939{
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 char_u *p;
3942 int i;
3943 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003944 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 int len = 2;
3946 long n1, n2;
3947 char_u *s1, *s2;
3948 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3949 regmatch_T regmatch;
3950 int ic;
3951 char_u *save_cpo;
3952
3953 /*
3954 * Get the first variable.
3955 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return FAIL;
3958
3959 p = *arg;
3960 switch (p[0])
3961 {
3962 case '=': if (p[1] == '=')
3963 type = TYPE_EQUAL;
3964 else if (p[1] == '~')
3965 type = TYPE_MATCH;
3966 break;
3967 case '!': if (p[1] == '=')
3968 type = TYPE_NEQUAL;
3969 else if (p[1] == '~')
3970 type = TYPE_NOMATCH;
3971 break;
3972 case '>': if (p[1] != '=')
3973 {
3974 type = TYPE_GREATER;
3975 len = 1;
3976 }
3977 else
3978 type = TYPE_GEQUAL;
3979 break;
3980 case '<': if (p[1] != '=')
3981 {
3982 type = TYPE_SMALLER;
3983 len = 1;
3984 }
3985 else
3986 type = TYPE_SEQUAL;
3987 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 case 'i': if (p[1] == 's')
3989 {
3990 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3991 len = 5;
3992 if (!vim_isIDc(p[len]))
3993 {
3994 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3995 type_is = TRUE;
3996 }
3997 }
3998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000
4001 /*
4002 * If there is a comparitive operator, use it.
4003 */
4004 if (type != TYPE_UNKNOWN)
4005 {
4006 /* extra question mark appended: ignore case */
4007 if (p[len] == '?')
4008 {
4009 ic = TRUE;
4010 ++len;
4011 }
4012 /* extra '#' appended: match case */
4013 else if (p[len] == '#')
4014 {
4015 ic = FALSE;
4016 ++len;
4017 }
4018 /* nothing appened: use 'ignorecase' */
4019 else
4020 ic = p_ic;
4021
4022 /*
4023 * Get the second variable.
4024 */
4025 *arg = skipwhite(p + len);
4026 if (eval5(arg, &var2, evaluate) == FAIL)
4027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return FAIL;
4030 }
4031
4032 if (evaluate)
4033 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004034 if (type_is && rettv->v_type != var2.v_type)
4035 {
4036 /* For "is" a different type always means FALSE, for "notis"
4037 * it means TRUE. */
4038 n1 = (type == TYPE_NEQUAL);
4039 }
4040 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4041 {
4042 if (type_is)
4043 {
4044 n1 = (rettv->v_type == var2.v_type
4045 && rettv->vval.v_list == var2.vval.v_list);
4046 if (type == TYPE_NEQUAL)
4047 n1 = !n1;
4048 }
4049 else if (rettv->v_type != var2.v_type
4050 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4051 {
4052 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004053 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004054 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004055 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004056 clear_tv(rettv);
4057 clear_tv(&var2);
4058 return FAIL;
4059 }
4060 else
4061 {
4062 /* Compare two Lists for being equal or unequal. */
4063 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4064 if (type == TYPE_NEQUAL)
4065 n1 = !n1;
4066 }
4067 }
4068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004069 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4070 {
4071 if (type_is)
4072 {
4073 n1 = (rettv->v_type == var2.v_type
4074 && rettv->vval.v_dict == var2.vval.v_dict);
4075 if (type == TYPE_NEQUAL)
4076 n1 = !n1;
4077 }
4078 else if (rettv->v_type != var2.v_type
4079 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4080 {
4081 if (rettv->v_type != var2.v_type)
4082 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4083 else
4084 EMSG(_("E736: Invalid operation for Dictionary"));
4085 clear_tv(rettv);
4086 clear_tv(&var2);
4087 return FAIL;
4088 }
4089 else
4090 {
4091 /* Compare two Dictionaries for being equal or unequal. */
4092 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4093 if (type == TYPE_NEQUAL)
4094 n1 = !n1;
4095 }
4096 }
4097
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004098 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4099 {
4100 if (rettv->v_type != var2.v_type
4101 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4102 {
4103 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004104 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004105 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004106 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004107 clear_tv(rettv);
4108 clear_tv(&var2);
4109 return FAIL;
4110 }
4111 else
4112 {
4113 /* Compare two Funcrefs for being equal or unequal. */
4114 if (rettv->vval.v_string == NULL
4115 || var2.vval.v_string == NULL)
4116 n1 = FALSE;
4117 else
4118 n1 = STRCMP(rettv->vval.v_string,
4119 var2.vval.v_string) == 0;
4120 if (type == TYPE_NEQUAL)
4121 n1 = !n1;
4122 }
4123 }
4124
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 /*
4126 * If one of the two variables is a number, compare as a number.
4127 * When using "=~" or "!~", always compare as string.
4128 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004129 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 n1 = get_tv_number(rettv);
4133 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 switch (type)
4135 {
4136 case TYPE_EQUAL: n1 = (n1 == n2); break;
4137 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4138 case TYPE_GREATER: n1 = (n1 > n2); break;
4139 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4140 case TYPE_SMALLER: n1 = (n1 < n2); break;
4141 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4142 case TYPE_UNKNOWN:
4143 case TYPE_MATCH:
4144 case TYPE_NOMATCH: break; /* avoid gcc warning */
4145 }
4146 }
4147 else
4148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149 s1 = get_tv_string_buf(rettv, buf1);
4150 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4152 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4153 else
4154 i = 0;
4155 n1 = FALSE;
4156 switch (type)
4157 {
4158 case TYPE_EQUAL: n1 = (i == 0); break;
4159 case TYPE_NEQUAL: n1 = (i != 0); break;
4160 case TYPE_GREATER: n1 = (i > 0); break;
4161 case TYPE_GEQUAL: n1 = (i >= 0); break;
4162 case TYPE_SMALLER: n1 = (i < 0); break;
4163 case TYPE_SEQUAL: n1 = (i <= 0); break;
4164
4165 case TYPE_MATCH:
4166 case TYPE_NOMATCH:
4167 /* avoid 'l' flag in 'cpoptions' */
4168 save_cpo = p_cpo;
4169 p_cpo = (char_u *)"";
4170 regmatch.regprog = vim_regcomp(s2,
4171 RE_MAGIC + RE_STRING);
4172 regmatch.rm_ic = ic;
4173 if (regmatch.regprog != NULL)
4174 {
4175 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4176 vim_free(regmatch.regprog);
4177 if (type == TYPE_NOMATCH)
4178 n1 = !n1;
4179 }
4180 p_cpo = save_cpo;
4181 break;
4182
4183 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4184 }
4185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
4187 clear_tv(&var2);
4188 rettv->v_type = VAR_NUMBER;
4189 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 }
4192
4193 return OK;
4194}
4195
4196/*
4197 * Handle fourth level expression:
4198 * + number addition
4199 * - number subtraction
4200 * . string concatenation
4201 *
4202 * "arg" must point to the first non-white of the expression.
4203 * "arg" is advanced to the next non-white after the recognized expression.
4204 *
4205 * Return OK or FAIL.
4206 */
4207 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 int evaluate;
4212{
Bram Moolenaar33570922005-01-25 22:26:29 +00004213 typval_T var2;
4214 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 int op;
4216 long n1, n2;
4217 char_u *s1, *s2;
4218 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4219 char_u *p;
4220
4221 /*
4222 * Get the first variable.
4223 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return FAIL;
4226
4227 /*
4228 * Repeat computing, until no '+', '-' or '.' is following.
4229 */
4230 for (;;)
4231 {
4232 op = **arg;
4233 if (op != '+' && op != '-' && op != '.')
4234 break;
4235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 if (op != '+' || rettv->v_type != VAR_LIST)
4237 {
4238 /* For "list + ...", an illegal use of the first operand as
4239 * a number cannot be determined before evaluating the 2nd
4240 * operand: if this is also a list, all is ok.
4241 * For "something . ...", "something - ..." or "non-list + ...",
4242 * we know that the first operand needs to be a string or number
4243 * without evaluating the 2nd operand. So check before to avoid
4244 * side effects after an error. */
4245 if (evaluate && get_tv_string_chk(rettv) == NULL)
4246 {
4247 clear_tv(rettv);
4248 return FAIL;
4249 }
4250 }
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 /*
4253 * Get the second variable.
4254 */
4255 *arg = skipwhite(*arg + 1);
4256 if (eval6(arg, &var2, evaluate) == FAIL)
4257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260 }
4261
4262 if (evaluate)
4263 {
4264 /*
4265 * Compute the result.
4266 */
4267 if (op == '.')
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4270 s2 = get_tv_string_buf_chk(&var2, buf2);
4271 if (s2 == NULL) /* type error ? */
4272 {
4273 clear_tv(rettv);
4274 clear_tv(&var2);
4275 return FAIL;
4276 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004277 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(rettv);
4279 rettv->v_type = VAR_STRING;
4280 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004282 else if (op == '+' && rettv->v_type == VAR_LIST
4283 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 {
4285 /* concatenate Lists */
4286 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4287 &var3) == FAIL)
4288 {
4289 clear_tv(rettv);
4290 clear_tv(&var2);
4291 return FAIL;
4292 }
4293 clear_tv(rettv);
4294 *rettv = var3;
4295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 else
4297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 int error = FALSE;
4299
4300 n1 = get_tv_number_chk(rettv, &error);
4301 if (error)
4302 {
4303 /* This can only happen for "list + non-list".
4304 * For "non-list + ..." or "something - ...", we returned
4305 * before evaluating the 2nd operand. */
4306 clear_tv(rettv);
4307 return FAIL;
4308 }
4309 n2 = get_tv_number_chk(&var2, &error);
4310 if (error)
4311 {
4312 clear_tv(rettv);
4313 clear_tv(&var2);
4314 return FAIL;
4315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (op == '+')
4318 n1 = n1 + n2;
4319 else
4320 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 rettv->v_type = VAR_NUMBER;
4322 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 }
4327 return OK;
4328}
4329
4330/*
4331 * Handle fifth level expression:
4332 * * number multiplication
4333 * / number division
4334 * % number modulo
4335 *
4336 * "arg" must point to the first non-white of the expression.
4337 * "arg" is advanced to the next non-white after the recognized expression.
4338 *
4339 * Return OK or FAIL.
4340 */
4341 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int evaluate;
4346{
Bram Moolenaar33570922005-01-25 22:26:29 +00004347 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 int op;
4349 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351
4352 /*
4353 * Get the first variable.
4354 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 return FAIL;
4357
4358 /*
4359 * Repeat computing, until no '*', '/' or '%' is following.
4360 */
4361 for (;;)
4362 {
4363 op = **arg;
4364 if (op != '*' && op != '/' && op != '%')
4365 break;
4366
4367 if (evaluate)
4368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004369 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004371 if (error)
4372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 else
4375 n1 = 0;
4376
4377 /*
4378 * Get the second variable.
4379 */
4380 *arg = skipwhite(*arg + 1);
4381 if (eval7(arg, &var2, evaluate) == FAIL)
4382 return FAIL;
4383
4384 if (evaluate)
4385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 if (error)
4389 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /*
4392 * Compute the result.
4393 */
4394 if (op == '*')
4395 n1 = n1 * n2;
4396 else if (op == '/')
4397 {
4398 if (n2 == 0) /* give an error message? */
4399 n1 = 0x7fffffffL;
4400 else
4401 n1 = n1 / n2;
4402 }
4403 else
4404 {
4405 if (n2 == 0) /* give an error message? */
4406 n1 = 0;
4407 else
4408 n1 = n1 % n2;
4409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 rettv->v_type = VAR_NUMBER;
4411 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 }
4414
4415 return OK;
4416}
4417
4418/*
4419 * Handle sixth level expression:
4420 * number number constant
4421 * "string" string contstant
4422 * 'string' literal string contstant
4423 * &option-name option value
4424 * @r register contents
4425 * identifier variable value
4426 * function() function call
4427 * $VAR environment variable
4428 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004429 * [expr, expr] List
4430 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 *
4432 * Also handle:
4433 * ! in front logical NOT
4434 * - in front unary minus
4435 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436 * trailing [] subscript in String or List
4437 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 *
4439 * "arg" must point to the first non-white of the expression.
4440 * "arg" is advanced to the next non-white after the recognized expression.
4441 *
4442 * Return OK or FAIL.
4443 */
4444 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004445eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 int evaluate;
4449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 long n;
4451 int len;
4452 char_u *s;
4453 int val;
4454 char_u *start_leader, *end_leader;
4455 int ret = OK;
4456 char_u *alias;
4457
4458 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004459 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004460 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004462 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 /*
4465 * Skip '!' and '-' characters. They are handled later.
4466 */
4467 start_leader = *arg;
4468 while (**arg == '!' || **arg == '-' || **arg == '+')
4469 *arg = skipwhite(*arg + 1);
4470 end_leader = *arg;
4471
4472 switch (**arg)
4473 {
4474 /*
4475 * Number constant.
4476 */
4477 case '0':
4478 case '1':
4479 case '2':
4480 case '3':
4481 case '4':
4482 case '5':
4483 case '6':
4484 case '7':
4485 case '8':
4486 case '9':
4487 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4488 *arg += len;
4489 if (evaluate)
4490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004491 rettv->v_type = VAR_NUMBER;
4492 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 break;
4495
4496 /*
4497 * String constant: "string".
4498 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 break;
4501
4502 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004505 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 break;
4507
4508 /*
4509 * List: [expr, expr]
4510 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004511 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 break;
4513
4514 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515 * Dictionary: {key: val, key: val}
4516 */
4517 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4518 break;
4519
4520 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004521 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004523 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 break;
4525
4526 /*
4527 * Environment variable: $VAR.
4528 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 break;
4531
4532 /*
4533 * Register contents: @r.
4534 */
4535 case '@': ++*arg;
4536 if (evaluate)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004539 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541 if (**arg != NUL)
4542 ++*arg;
4543 break;
4544
4545 /*
4546 * nested expression: (expression).
4547 */
4548 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (**arg == ')')
4551 ++*arg;
4552 else if (ret == OK)
4553 {
4554 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 ret = FAIL;
4557 }
4558 break;
4559
Bram Moolenaar8c711452005-01-14 21:53:12 +00004560 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 break;
4562 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563
4564 if (ret == NOTDONE)
4565 {
4566 /*
4567 * Must be a variable or function name.
4568 * Can also be a curly-braces kind of name: {expr}.
4569 */
4570 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004571 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 if (alias != NULL)
4573 s = alias;
4574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004575 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004576 ret = FAIL;
4577 else
4578 {
4579 if (**arg == '(') /* recursive! */
4580 {
4581 /* If "s" is the name of a variable of type VAR_FUNC
4582 * use its contents. */
4583 s = deref_func_name(s, &len);
4584
4585 /* Invoke the function. */
4586 ret = get_func_tv(s, len, rettv, arg,
4587 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004588 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004589 /* Stop the expression evaluation when immediately
4590 * aborting on error, or when an interrupt occurred or
4591 * an exception was thrown but not caught. */
4592 if (aborting())
4593 {
4594 if (ret == OK)
4595 clear_tv(rettv);
4596 ret = FAIL;
4597 }
4598 }
4599 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004600 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004601 else
4602 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004603 }
4604
4605 if (alias != NULL)
4606 vim_free(alias);
4607 }
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 *arg = skipwhite(*arg);
4610
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004611 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4612 * expr(expr). */
4613 if (ret == OK)
4614 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 /*
4617 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4618 */
4619 if (ret == OK && evaluate && end_leader > start_leader)
4620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004621 int error = FALSE;
4622
4623 val = get_tv_number_chk(rettv, &error);
4624 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 clear_tv(rettv);
4627 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 else
4630 {
4631 while (end_leader > start_leader)
4632 {
4633 --end_leader;
4634 if (*end_leader == '!')
4635 val = !val;
4636 else if (*end_leader == '-')
4637 val = -val;
4638 }
4639 clear_tv(rettv);
4640 rettv->v_type = VAR_NUMBER;
4641 rettv->vval.v_number = val;
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644
4645 return ret;
4646}
4647
4648/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649 * Evaluate an "[expr]" or "[expr:expr]" index.
4650 * "*arg" points to the '['.
4651 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4652 */
4653 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004654eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004655 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004656 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004658 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004659{
4660 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004663 long len = -1;
4664 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004665 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004666 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004670 if (verbose)
4671 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672 return FAIL;
4673 }
4674
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 /*
4678 * dict.name
4679 */
4680 key = *arg + 1;
4681 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4682 ;
4683 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 }
4687 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 /*
4690 * something[idx]
4691 *
4692 * Get the (first) variable from inside the [].
4693 */
4694 *arg = skipwhite(*arg + 1);
4695 if (**arg == ':')
4696 empty1 = TRUE;
4697 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4698 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4700 {
4701 /* not a number or string */
4702 clear_tv(&var1);
4703 return FAIL;
4704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705
4706 /*
4707 * Get the second variable from inside the [:].
4708 */
4709 if (**arg == ':')
4710 {
4711 range = TRUE;
4712 *arg = skipwhite(*arg + 1);
4713 if (**arg == ']')
4714 empty2 = TRUE;
4715 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 if (!empty1)
4718 clear_tv(&var1);
4719 return FAIL;
4720 }
4721 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4722 {
4723 /* not a number or string */
4724 if (!empty1)
4725 clear_tv(&var1);
4726 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004727 return FAIL;
4728 }
4729 }
4730
4731 /* Check for the ']'. */
4732 if (**arg != ']')
4733 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004734 if (verbose)
4735 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004736 clear_tv(&var1);
4737 if (range)
4738 clear_tv(&var2);
4739 return FAIL;
4740 }
4741 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 }
4743
4744 if (evaluate)
4745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004746 n1 = 0;
4747 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 n1 = get_tv_number(&var1);
4750 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004751 }
4752 if (range)
4753 {
4754 if (empty2)
4755 n2 = -1;
4756 else
4757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 n2 = get_tv_number(&var2);
4759 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 }
4761 }
4762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 {
4765 case VAR_NUMBER:
4766 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 len = (long)STRLEN(s);
4769 if (range)
4770 {
4771 /* The resulting variable is a substring. If the indexes
4772 * are out of range the result is empty. */
4773 if (n1 < 0)
4774 {
4775 n1 = len + n1;
4776 if (n1 < 0)
4777 n1 = 0;
4778 }
4779 if (n2 < 0)
4780 n2 = len + n2;
4781 else if (n2 >= len)
4782 n2 = len;
4783 if (n1 >= len || n2 < 0 || n1 > n2)
4784 s = NULL;
4785 else
4786 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4787 }
4788 else
4789 {
4790 /* The resulting variable is a string of a single
4791 * character. If the index is too big or negative the
4792 * result is empty. */
4793 if (n1 >= len || n1 < 0)
4794 s = NULL;
4795 else
4796 s = vim_strnsave(s + n1, 1);
4797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 clear_tv(rettv);
4799 rettv->v_type = VAR_STRING;
4800 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 break;
4802
4803 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004804 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 if (n1 < 0)
4806 n1 = len + n1;
4807 if (!empty1 && (n1 < 0 || n1 >= len))
4808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004809 if (verbose)
4810 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 return FAIL;
4812 }
4813 if (range)
4814 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004815 list_T *l;
4816 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817
4818 if (n2 < 0)
4819 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004822 if (verbose)
4823 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 return FAIL;
4825 }
4826 l = list_alloc();
4827 if (l == NULL)
4828 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004830 n1 <= n2; ++n1)
4831 {
4832 if (list_append_tv(l, &item->li_tv) == FAIL)
4833 {
4834 list_free(l);
4835 return FAIL;
4836 }
4837 item = item->li_next;
4838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 clear_tv(rettv);
4840 rettv->v_type = VAR_LIST;
4841 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004842 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843 }
4844 else
4845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004847 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(rettv);
4849 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 }
4851 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004852
4853 case VAR_DICT:
4854 if (range)
4855 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004856 if (verbose)
4857 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 if (len == -1)
4859 clear_tv(&var1);
4860 return FAIL;
4861 }
4862 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004863 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004864
4865 if (len == -1)
4866 {
4867 key = get_tv_string(&var1);
4868 if (*key == NUL)
4869 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004870 if (verbose)
4871 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004872 clear_tv(&var1);
4873 return FAIL;
4874 }
4875 }
4876
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004877 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004878
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004879 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004880 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881 if (len == -1)
4882 clear_tv(&var1);
4883 if (item == NULL)
4884 return FAIL;
4885
4886 copy_tv(&item->di_tv, &var1);
4887 clear_tv(rettv);
4888 *rettv = var1;
4889 }
4890 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004891 }
4892 }
4893
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004894 return OK;
4895}
4896
4897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 * Get an option value.
4899 * "arg" points to the '&' or '+' before the option name.
4900 * "arg" is advanced to character after the option name.
4901 * Return OK or FAIL.
4902 */
4903 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004906 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 int evaluate;
4908{
4909 char_u *option_end;
4910 long numval;
4911 char_u *stringval;
4912 int opt_type;
4913 int c;
4914 int working = (**arg == '+'); /* has("+option") */
4915 int ret = OK;
4916 int opt_flags;
4917
4918 /*
4919 * Isolate the option name and find its value.
4920 */
4921 option_end = find_option_end(arg, &opt_flags);
4922 if (option_end == NULL)
4923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 EMSG2(_("E112: Option name missing: %s"), *arg);
4926 return FAIL;
4927 }
4928
4929 if (!evaluate)
4930 {
4931 *arg = option_end;
4932 return OK;
4933 }
4934
4935 c = *option_end;
4936 *option_end = NUL;
4937 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 if (opt_type == -3) /* invalid name */
4941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 EMSG2(_("E113: Unknown option: %s"), *arg);
4944 ret = FAIL;
4945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 if (opt_type == -2) /* hidden string option */
4949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 rettv->v_type = VAR_STRING;
4951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 else if (opt_type == -1) /* hidden number option */
4954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 rettv->v_type = VAR_NUMBER;
4956 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 else if (opt_type == 1) /* number option */
4959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960 rettv->v_type = VAR_NUMBER;
4961 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else /* string option */
4964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 rettv->v_type = VAR_STRING;
4966 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 }
4969 else if (working && (opt_type == -2 || opt_type == -1))
4970 ret = FAIL;
4971
4972 *option_end = c; /* put back for error messages */
4973 *arg = option_end;
4974
4975 return ret;
4976}
4977
4978/*
4979 * Allocate a variable for a string constant.
4980 * Return OK or FAIL.
4981 */
4982 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 int evaluate;
4987{
4988 char_u *p;
4989 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 int extra = 0;
4991
4992 /*
4993 * Find the end of the string, skipping backslashed characters.
4994 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004995 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 if (*p == '\\' && p[1] != NUL)
4998 {
4999 ++p;
5000 /* A "\<x>" form occupies at least 4 characters, and produces up
5001 * to 6 characters: reserve space for 2 extra */
5002 if (*p == '<')
5003 extra += 2;
5004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006
5007 if (*p != '"')
5008 {
5009 EMSG2(_("E114: Missing quote: %s"), *arg);
5010 return FAIL;
5011 }
5012
5013 /* If only parsing, set *arg and return here */
5014 if (!evaluate)
5015 {
5016 *arg = p + 1;
5017 return OK;
5018 }
5019
5020 /*
5021 * Copy the string into allocated memory, handling backslashed
5022 * characters.
5023 */
5024 name = alloc((unsigned)(p - *arg + extra));
5025 if (name == NULL)
5026 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 rettv->v_type = VAR_STRING;
5028 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 if (*p == '\\')
5033 {
5034 switch (*++p)
5035 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 case 'b': *name++ = BS; ++p; break;
5037 case 'e': *name++ = ESC; ++p; break;
5038 case 'f': *name++ = FF; ++p; break;
5039 case 'n': *name++ = NL; ++p; break;
5040 case 'r': *name++ = CAR; ++p; break;
5041 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 case 'X': /* hex: "\x1", "\x12" */
5044 case 'x':
5045 case 'u': /* Unicode: "\u0023" */
5046 case 'U':
5047 if (vim_isxdigit(p[1]))
5048 {
5049 int n, nr;
5050 int c = toupper(*p);
5051
5052 if (c == 'X')
5053 n = 2;
5054 else
5055 n = 4;
5056 nr = 0;
5057 while (--n >= 0 && vim_isxdigit(p[1]))
5058 {
5059 ++p;
5060 nr = (nr << 4) + hex2nr(*p);
5061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063#ifdef FEAT_MBYTE
5064 /* For "\u" store the number according to
5065 * 'encoding'. */
5066 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else
5069#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073
5074 /* octal: "\1", "\12", "\123" */
5075 case '0':
5076 case '1':
5077 case '2':
5078 case '3':
5079 case '4':
5080 case '5':
5081 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 case '7': *name = *p++ - '0';
5083 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 *name = (*name << 3) + *p++ - '0';
5086 if (*p >= '0' && *p <= '7')
5087 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (extra != 0)
5095 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098 }
5099 /* FALLTHROUGH */
5100
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 }
5104 }
5105 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 *arg = p + 1;
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 return OK;
5113}
5114
5115/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 * Return OK or FAIL.
5118 */
5119 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 int evaluate;
5124{
5125 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005126 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005127 int reduce = 0;
5128
5129 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131 */
5132 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5133 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005134 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005135 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005137 break;
5138 ++reduce;
5139 ++p;
5140 }
5141 }
5142
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005146 return FAIL;
5147 }
5148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005150 if (!evaluate)
5151 {
5152 *arg = p + 1;
5153 return OK;
5154 }
5155
5156 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005158 */
5159 str = alloc((unsigned)((p - *arg) - reduce));
5160 if (str == NULL)
5161 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 rettv->v_type = VAR_STRING;
5163 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005164
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005166 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005168 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005170 break;
5171 ++p;
5172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005176 *arg = p + 1;
5177
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005178 return OK;
5179}
5180
5181/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182 * Allocate a variable for a List and fill it from "*arg".
5183 * Return OK or FAIL.
5184 */
5185 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 int evaluate;
5190{
Bram Moolenaar33570922005-01-25 22:26:29 +00005191 list_T *l = NULL;
5192 typval_T tv;
5193 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194
5195 if (evaluate)
5196 {
5197 l = list_alloc();
5198 if (l == NULL)
5199 return FAIL;
5200 }
5201
5202 *arg = skipwhite(*arg + 1);
5203 while (**arg != ']' && **arg != NUL)
5204 {
5205 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5206 goto failret;
5207 if (evaluate)
5208 {
5209 item = listitem_alloc();
5210 if (item != NULL)
5211 {
5212 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005213 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214 list_append(l, item);
5215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005216 else
5217 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218 }
5219
5220 if (**arg == ']')
5221 break;
5222 if (**arg != ',')
5223 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 goto failret;
5226 }
5227 *arg = skipwhite(*arg + 1);
5228 }
5229
5230 if (**arg != ']')
5231 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005233failret:
5234 if (evaluate)
5235 list_free(l);
5236 return FAIL;
5237 }
5238
5239 *arg = skipwhite(*arg + 1);
5240 if (evaluate)
5241 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005242 rettv->v_type = VAR_LIST;
5243 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005244 ++l->lv_refcount;
5245 }
5246
5247 return OK;
5248}
5249
5250/*
5251 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005252 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005254 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255list_alloc()
5256{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005257 list_T *l;
5258
5259 l = (list_T *)alloc_clear(sizeof(list_T));
5260 if (l != NULL)
5261 {
5262 /* Prepend the list to the list of lists for garbage collection. */
5263 if (first_list != NULL)
5264 first_list->lv_used_prev = l;
5265 l->lv_used_prev = NULL;
5266 l->lv_used_next = first_list;
5267 first_list = l;
5268 }
5269 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270}
5271
5272/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005273 * Allocate an empty list for a return value.
5274 * Returns OK or FAIL.
5275 */
5276 static int
5277rettv_list_alloc(rettv)
5278 typval_T *rettv;
5279{
5280 list_T *l = list_alloc();
5281
5282 if (l == NULL)
5283 return FAIL;
5284
5285 rettv->vval.v_list = l;
5286 rettv->v_type = VAR_LIST;
5287 ++l->lv_refcount;
5288 return OK;
5289}
5290
5291/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 * Unreference a list: decrement the reference count and free it when it
5293 * becomes zero.
5294 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005295 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005299 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5300 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301}
5302
5303/*
5304 * Free a list, including all items it points to.
5305 * Ignores the reference count.
5306 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005307 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310{
Bram Moolenaar33570922005-01-25 22:26:29 +00005311 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312
Bram Moolenaard9fba312005-06-26 22:34:35 +00005313 /* Avoid that recursive reference to the list frees us again. */
5314 l->lv_refcount = DEL_REFCOUNT;
5315
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005316 /* Remove the list from the list of lists for garbage collection. */
5317 if (l->lv_used_prev == NULL)
5318 first_list = l->lv_used_next;
5319 else
5320 l->lv_used_prev->lv_used_next = l->lv_used_next;
5321 if (l->lv_used_next != NULL)
5322 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5323
Bram Moolenaard9fba312005-06-26 22:34:35 +00005324 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005326 /* Remove the item before deleting it. */
5327 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 listitem_free(item);
5329 }
5330 vim_free(l);
5331}
5332
5333/*
5334 * Allocate a list item.
5335 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337listitem_alloc()
5338{
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340}
5341
5342/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005343 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 */
5345 static void
5346listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 vim_free(item);
5351}
5352
5353/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005354 * Remove a list item from a List and free it. Also clears the value.
5355 */
5356 static void
5357listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 list_T *l;
5359 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005360{
5361 list_remove(l, item, item);
5362 listitem_free(item);
5363}
5364
5365/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 * Get the number of items in a list.
5367 */
5368 static long
5369list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005370 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 if (l == NULL)
5373 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005374 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375}
5376
5377/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005378 * Return TRUE when two lists have exactly the same values.
5379 */
5380 static int
5381list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005382 list_T *l1;
5383 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005384 int ic; /* ignore case for strings */
5385{
Bram Moolenaar33570922005-01-25 22:26:29 +00005386 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005387
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005388 if (list_len(l1) != list_len(l2))
5389 return FALSE;
5390
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005391 for (item1 = l1->lv_first, item2 = l2->lv_first;
5392 item1 != NULL && item2 != NULL;
5393 item1 = item1->li_next, item2 = item2->li_next)
5394 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5395 return FALSE;
5396 return item1 == NULL && item2 == NULL;
5397}
5398
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005399#if defined(FEAT_PYTHON) || defined(PROTO)
5400/*
5401 * Return the dictitem that an entry in a hashtable points to.
5402 */
5403 dictitem_T *
5404dict_lookup(hi)
5405 hashitem_T *hi;
5406{
5407 return HI2DI(hi);
5408}
5409#endif
5410
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005411/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005412 * Return TRUE when two dictionaries have exactly the same key/values.
5413 */
5414 static int
5415dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 dict_T *d1;
5417 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005418 int ic; /* ignore case for strings */
5419{
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 hashitem_T *hi;
5421 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005422 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005423
5424 if (dict_len(d1) != dict_len(d2))
5425 return FALSE;
5426
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 todo = d1->dv_hashtab.ht_used;
5428 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005430 if (!HASHITEM_EMPTY(hi))
5431 {
5432 item2 = dict_find(d2, hi->hi_key, -1);
5433 if (item2 == NULL)
5434 return FALSE;
5435 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5436 return FALSE;
5437 --todo;
5438 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005439 }
5440 return TRUE;
5441}
5442
5443/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005444 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005445 * Compares the items just like "==" would compare them, but strings and
5446 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005447 */
5448 static int
5449tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005450 typval_T *tv1;
5451 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005452 int ic; /* ignore case */
5453{
5454 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005455 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005456
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005457 if (tv1->v_type != tv2->v_type)
5458 return FALSE;
5459
5460 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005461 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005462 case VAR_LIST:
5463 /* recursive! */
5464 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5465
5466 case VAR_DICT:
5467 /* recursive! */
5468 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5469
5470 case VAR_FUNC:
5471 return (tv1->vval.v_string != NULL
5472 && tv2->vval.v_string != NULL
5473 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5474
5475 case VAR_NUMBER:
5476 return tv1->vval.v_number == tv2->vval.v_number;
5477
5478 case VAR_STRING:
5479 s1 = get_tv_string_buf(tv1, buf1);
5480 s2 = get_tv_string_buf(tv2, buf2);
5481 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005482 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005483
5484 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005485 return TRUE;
5486}
5487
5488/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 * Locate item with index "n" in list "l" and return it.
5490 * A negative index is counted from the end; -1 is the last item.
5491 * Returns NULL when "n" is out of range.
5492 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005493 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 long n;
5497{
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 long idx;
5500
5501 if (l == NULL)
5502 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005503
5504 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005506 n = l->lv_len + n;
5507
5508 /* Check for index out of range. */
5509 if (n < 0 || n >= l->lv_len)
5510 return NULL;
5511
5512 /* When there is a cached index may start search from there. */
5513 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005515 if (n < l->lv_idx / 2)
5516 {
5517 /* closest to the start of the list */
5518 item = l->lv_first;
5519 idx = 0;
5520 }
5521 else if (n > (l->lv_idx + l->lv_len) / 2)
5522 {
5523 /* closest to the end of the list */
5524 item = l->lv_last;
5525 idx = l->lv_len - 1;
5526 }
5527 else
5528 {
5529 /* closest to the cached index */
5530 item = l->lv_idx_item;
5531 idx = l->lv_idx;
5532 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 }
5534 else
5535 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005536 if (n < l->lv_len / 2)
5537 {
5538 /* closest to the start of the list */
5539 item = l->lv_first;
5540 idx = 0;
5541 }
5542 else
5543 {
5544 /* closest to the end of the list */
5545 item = l->lv_last;
5546 idx = l->lv_len - 1;
5547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005549
5550 while (n > idx)
5551 {
5552 /* search forward */
5553 item = item->li_next;
5554 ++idx;
5555 }
5556 while (n < idx)
5557 {
5558 /* search backward */
5559 item = item->li_prev;
5560 --idx;
5561 }
5562
5563 /* cache the used index */
5564 l->lv_idx = idx;
5565 l->lv_idx_item = item;
5566
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 return item;
5568}
5569
5570/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005571 * Get list item "l[idx]" as a number.
5572 */
5573 static long
5574list_find_nr(l, idx, errorp)
5575 list_T *l;
5576 long idx;
5577 int *errorp; /* set to TRUE when something wrong */
5578{
5579 listitem_T *li;
5580
5581 li = list_find(l, idx);
5582 if (li == NULL)
5583 {
5584 if (errorp != NULL)
5585 *errorp = TRUE;
5586 return -1L;
5587 }
5588 return get_tv_number_chk(&li->li_tv, errorp);
5589}
5590
5591/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005592 * Locate "item" list "l" and return its index.
5593 * Returns -1 when "item" is not in the list.
5594 */
5595 static long
5596list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 list_T *l;
5598 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005599{
5600 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005602
5603 if (l == NULL)
5604 return -1;
5605 idx = 0;
5606 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5607 ++idx;
5608 if (li == NULL)
5609 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005610 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005611}
5612
5613/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 * Append item "item" to the end of list "l".
5615 */
5616 static void
5617list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005618 list_T *l;
5619 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620{
5621 if (l->lv_last == NULL)
5622 {
5623 /* empty list */
5624 l->lv_first = item;
5625 l->lv_last = item;
5626 item->li_prev = NULL;
5627 }
5628 else
5629 {
5630 l->lv_last->li_next = item;
5631 item->li_prev = l->lv_last;
5632 l->lv_last = item;
5633 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 item->li_next = NULL;
5636}
5637
5638/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005639 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005640 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641 */
5642 static int
5643list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 list_T *l;
5645 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005647 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648
Bram Moolenaar05159a02005-02-26 23:04:13 +00005649 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005651 copy_tv(tv, &li->li_tv);
5652 list_append(l, li);
5653 return OK;
5654}
5655
5656/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005657 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005658 * Return FAIL when out of memory.
5659 */
5660 int
5661list_append_dict(list, dict)
5662 list_T *list;
5663 dict_T *dict;
5664{
5665 listitem_T *li = listitem_alloc();
5666
5667 if (li == NULL)
5668 return FAIL;
5669 li->li_tv.v_type = VAR_DICT;
5670 li->li_tv.v_lock = 0;
5671 li->li_tv.vval.v_dict = dict;
5672 list_append(list, li);
5673 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 return OK;
5675}
5676
5677/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005678 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005679 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005680 * Returns FAIL when out of memory.
5681 */
5682 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005683list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005684 list_T *l;
5685 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005686 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005687{
5688 listitem_T *li = listitem_alloc();
5689
5690 if (li == NULL)
5691 return FAIL;
5692 list_append(l, li);
5693 li->li_tv.v_type = VAR_STRING;
5694 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005695 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5696 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005697 return FAIL;
5698 return OK;
5699}
5700
5701/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005702 * Append "n" to list "l".
5703 * Returns FAIL when out of memory.
5704 */
5705 static int
5706list_append_number(l, n)
5707 list_T *l;
5708 varnumber_T n;
5709{
5710 listitem_T *li;
5711
5712 li = listitem_alloc();
5713 if (li == NULL)
5714 return FAIL;
5715 li->li_tv.v_type = VAR_NUMBER;
5716 li->li_tv.v_lock = 0;
5717 li->li_tv.vval.v_number = n;
5718 list_append(l, li);
5719 return OK;
5720}
5721
5722/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005724 * If "item" is NULL append at the end.
5725 * Return FAIL when out of memory.
5726 */
5727 static int
5728list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 list_T *l;
5730 typval_T *tv;
5731 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005732{
Bram Moolenaar33570922005-01-25 22:26:29 +00005733 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005734
5735 if (ni == NULL)
5736 return FAIL;
5737 copy_tv(tv, &ni->li_tv);
5738 if (item == NULL)
5739 /* Append new item at end of list. */
5740 list_append(l, ni);
5741 else
5742 {
5743 /* Insert new item before existing item. */
5744 ni->li_prev = item->li_prev;
5745 ni->li_next = item;
5746 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005747 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005748 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005749 ++l->lv_idx;
5750 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005751 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005752 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005753 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005754 l->lv_idx_item = NULL;
5755 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005756 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005757 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005758 }
5759 return OK;
5760}
5761
5762/*
5763 * Extend "l1" with "l2".
5764 * If "bef" is NULL append at the end, otherwise insert before this item.
5765 * Returns FAIL when out of memory.
5766 */
5767 static int
5768list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005769 list_T *l1;
5770 list_T *l2;
5771 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005772{
Bram Moolenaar33570922005-01-25 22:26:29 +00005773 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005774
5775 for (item = l2->lv_first; item != NULL; item = item->li_next)
5776 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5777 return FAIL;
5778 return OK;
5779}
5780
5781/*
5782 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5783 * Return FAIL when out of memory.
5784 */
5785 static int
5786list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005787 list_T *l1;
5788 list_T *l2;
5789 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005790{
Bram Moolenaar33570922005-01-25 22:26:29 +00005791 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005792
5793 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005794 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005795 if (l == NULL)
5796 return FAIL;
5797 tv->v_type = VAR_LIST;
5798 tv->vval.v_list = l;
5799
5800 /* append all items from the second list */
5801 return list_extend(l, l2, NULL);
5802}
5803
5804/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005805 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005807 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 * Returns NULL when out of memory.
5809 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005811list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005812 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005814 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815{
Bram Moolenaar33570922005-01-25 22:26:29 +00005816 list_T *copy;
5817 listitem_T *item;
5818 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819
5820 if (orig == NULL)
5821 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822
5823 copy = list_alloc();
5824 if (copy != NULL)
5825 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005826 if (copyID != 0)
5827 {
5828 /* Do this before adding the items, because one of the items may
5829 * refer back to this list. */
5830 orig->lv_copyID = copyID;
5831 orig->lv_copylist = copy;
5832 }
5833 for (item = orig->lv_first; item != NULL && !got_int;
5834 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 {
5836 ni = listitem_alloc();
5837 if (ni == NULL)
5838 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005839 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005840 {
5841 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5842 {
5843 vim_free(ni);
5844 break;
5845 }
5846 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005848 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 list_append(copy, ni);
5850 }
5851 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005852 if (item != NULL)
5853 {
5854 list_unref(copy);
5855 copy = NULL;
5856 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 }
5858
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 return copy;
5860}
5861
5862/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005863 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005864 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005866 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005867list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 list_T *l;
5869 listitem_T *item;
5870 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005871{
Bram Moolenaar33570922005-01-25 22:26:29 +00005872 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005874 /* notify watchers */
5875 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005877 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005878 list_fix_watch(l, ip);
5879 if (ip == item2)
5880 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005882
5883 if (item2->li_next == NULL)
5884 l->lv_last = item->li_prev;
5885 else
5886 item2->li_next->li_prev = item->li_prev;
5887 if (item->li_prev == NULL)
5888 l->lv_first = item2->li_next;
5889 else
5890 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005891 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892}
5893
5894/*
5895 * Return an allocated string with the string representation of a list.
5896 * May return NULL.
5897 */
5898 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005899list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005901 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902{
5903 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904
5905 if (tv->vval.v_list == NULL)
5906 return NULL;
5907 ga_init2(&ga, (int)sizeof(char), 80);
5908 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005909 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005910 {
5911 vim_free(ga.ga_data);
5912 return NULL;
5913 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 ga_append(&ga, ']');
5915 ga_append(&ga, NUL);
5916 return (char_u *)ga.ga_data;
5917}
5918
5919/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 * Join list "l" into a string in "*gap", using separator "sep".
5921 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005924 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005925list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 char_u *sep;
5929 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931{
5932 int first = TRUE;
5933 char_u *tofree;
5934 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005935 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 char_u *s;
5937
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005938 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005939 {
5940 if (first)
5941 first = FALSE;
5942 else
5943 ga_concat(gap, sep);
5944
5945 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005946 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005947 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005948 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 if (s != NULL)
5950 ga_concat(gap, s);
5951 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005952 if (s == NULL)
5953 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005955 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956}
5957
5958/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 * Garbage collection for lists and dictionaries.
5960 *
5961 * We use reference counts to be able to free most items right away when they
5962 * are no longer used. But for composite items it's possible that it becomes
5963 * unused while the reference count is > 0: When there is a recursive
5964 * reference. Example:
5965 * :let l = [1, 2, 3]
5966 * :let d = {9: l}
5967 * :let l[1] = d
5968 *
5969 * Since this is quite unusual we handle this with garbage collection: every
5970 * once in a while find out which lists and dicts are not referenced from any
5971 * variable.
5972 *
5973 * Here is a good reference text about garbage collection (refers to Python
5974 * but it applies to all reference-counting mechanisms):
5975 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005976 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005977
5978/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005979 * Do garbage collection for lists and dicts.
5980 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005981 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 int
5983garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005984{
5985 dict_T *dd;
5986 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 int copyID = ++current_copyID;
5988 buf_T *buf;
5989 win_T *wp;
5990 int i;
5991 funccall_T *fc;
5992 int did_free = FALSE;
5993
5994 /*
5995 * 1. Go through all accessible variables and mark all lists and dicts
5996 * with copyID.
5997 */
5998 /* script-local variables */
5999 for (i = 1; i <= ga_scripts.ga_len; ++i)
6000 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6001
6002 /* buffer-local variables */
6003 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6004 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6005
6006 /* window-local variables */
6007 FOR_ALL_WINDOWS(wp)
6008 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6009
6010 /* global variables */
6011 set_ref_in_ht(&globvarht, copyID);
6012
6013 /* function-local variables */
6014 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6015 {
6016 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6017 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6018 }
6019
6020 /*
6021 * 2. Go through the list of dicts and free items without the copyID.
6022 */
6023 for (dd = first_dict; dd != NULL; )
6024 if (dd->dv_copyID != copyID)
6025 {
6026 dict_free(dd);
6027 did_free = TRUE;
6028
6029 /* restart, next dict may also have been freed */
6030 dd = first_dict;
6031 }
6032 else
6033 dd = dd->dv_used_next;
6034
6035 /*
6036 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006037 * But don't free a list that has a watcher (used in a for loop), these
6038 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 */
6040 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006041 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006042 {
6043 list_free(ll);
6044 did_free = TRUE;
6045
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006046 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 ll = first_list;
6048 }
6049 else
6050 ll = ll->lv_used_next;
6051
6052 return did_free;
6053}
6054
6055/*
6056 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6057 */
6058 static void
6059set_ref_in_ht(ht, copyID)
6060 hashtab_T *ht;
6061 int copyID;
6062{
6063 int todo;
6064 hashitem_T *hi;
6065
6066 todo = ht->ht_used;
6067 for (hi = ht->ht_array; todo > 0; ++hi)
6068 if (!HASHITEM_EMPTY(hi))
6069 {
6070 --todo;
6071 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6072 }
6073}
6074
6075/*
6076 * Mark all lists and dicts referenced through list "l" with "copyID".
6077 */
6078 static void
6079set_ref_in_list(l, copyID)
6080 list_T *l;
6081 int copyID;
6082{
6083 listitem_T *li;
6084
6085 for (li = l->lv_first; li != NULL; li = li->li_next)
6086 set_ref_in_item(&li->li_tv, copyID);
6087}
6088
6089/*
6090 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6091 */
6092 static void
6093set_ref_in_item(tv, copyID)
6094 typval_T *tv;
6095 int copyID;
6096{
6097 dict_T *dd;
6098 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006099
6100 switch (tv->v_type)
6101 {
6102 case VAR_DICT:
6103 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006104 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006105 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006106 /* Didn't see this dict yet. */
6107 dd->dv_copyID = copyID;
6108 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006109 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006110 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006111
6112 case VAR_LIST:
6113 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006114 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006115 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006116 /* Didn't see this list yet. */
6117 ll->lv_copyID = copyID;
6118 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006119 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006120 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006121 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006122 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006123}
6124
6125/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006126 * Allocate an empty header for a dictionary.
6127 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006128 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006129dict_alloc()
6130{
Bram Moolenaar33570922005-01-25 22:26:29 +00006131 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006132
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006134 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006135 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006136 /* Add the list to the hashtable for garbage collection. */
6137 if (first_dict != NULL)
6138 first_dict->dv_used_prev = d;
6139 d->dv_used_next = first_dict;
6140 d->dv_used_prev = NULL;
6141
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006143 d->dv_lock = 0;
6144 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006145 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006146 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006147 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006148}
6149
6150/*
6151 * Unreference a Dictionary: decrement the reference count and free it when it
6152 * becomes zero.
6153 */
6154 static void
6155dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006157{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006158 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6159 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006160}
6161
6162/*
6163 * Free a Dictionary, including all items it contains.
6164 * Ignores the reference count.
6165 */
6166 static void
6167dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006168 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006169{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006170 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006172 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173
Bram Moolenaard9fba312005-06-26 22:34:35 +00006174 /* Avoid that recursive reference to the dict frees us again. */
6175 d->dv_refcount = DEL_REFCOUNT;
6176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006177 /* Remove the dict from the list of dicts for garbage collection. */
6178 if (d->dv_used_prev == NULL)
6179 first_dict = d->dv_used_next;
6180 else
6181 d->dv_used_prev->dv_used_next = d->dv_used_next;
6182 if (d->dv_used_next != NULL)
6183 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6184
6185 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006186 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 todo = d->dv_hashtab.ht_used;
6188 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006190 if (!HASHITEM_EMPTY(hi))
6191 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006192 /* Remove the item before deleting it, just in case there is
6193 * something recursive causing trouble. */
6194 di = HI2DI(hi);
6195 hash_remove(&d->dv_hashtab, hi);
6196 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 --todo;
6198 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006199 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006201 vim_free(d);
6202}
6203
6204/*
6205 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006206 * The "key" is copied to the new item.
6207 * Note that the value of the item "di_tv" still needs to be initialized!
6208 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006209 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211dictitem_alloc(key)
6212 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006213{
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215
Bram Moolenaar33570922005-01-25 22:26:29 +00006216 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006219 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006220 di->di_flags = 0;
6221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006223}
6224
6225/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006226 * Make a copy of a Dictionary item.
6227 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006229dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231{
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006233
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006235 if (di != NULL)
6236 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006237 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006238 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006239 copy_tv(&org->di_tv, &di->di_tv);
6240 }
6241 return di;
6242}
6243
6244/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006245 * Remove item "item" from Dictionary "dict" and free it.
6246 */
6247 static void
6248dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 dict_T *dict;
6250 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251{
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006255 if (HASHITEM_EMPTY(hi))
6256 EMSG2(_(e_intern2), "dictitem_remove()");
6257 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006259 dictitem_free(item);
6260}
6261
6262/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 * Free a dict item. Also clears the value.
6264 */
6265 static void
6266dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006267 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006269 clear_tv(&item->di_tv);
6270 vim_free(item);
6271}
6272
6273/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006274 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6275 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006276 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006277 * Returns NULL when out of memory.
6278 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006280dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006282 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006283 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006284{
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 dict_T *copy;
6286 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006288 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289
6290 if (orig == NULL)
6291 return NULL;
6292
6293 copy = dict_alloc();
6294 if (copy != NULL)
6295 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006296 if (copyID != 0)
6297 {
6298 orig->dv_copyID = copyID;
6299 orig->dv_copydict = copy;
6300 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006301 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006302 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006304 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006306 --todo;
6307
6308 di = dictitem_alloc(hi->hi_key);
6309 if (di == NULL)
6310 break;
6311 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006312 {
6313 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6314 copyID) == FAIL)
6315 {
6316 vim_free(di);
6317 break;
6318 }
6319 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006320 else
6321 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6322 if (dict_add(copy, di) == FAIL)
6323 {
6324 dictitem_free(di);
6325 break;
6326 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006327 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006328 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006329
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006331 if (todo > 0)
6332 {
6333 dict_unref(copy);
6334 copy = NULL;
6335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006336 }
6337
6338 return copy;
6339}
6340
6341/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006342 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 dict_T *d;
6348 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006349{
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351}
6352
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 * Add a number or string entry to dictionary "d".
6355 * When "str" is NULL use number "nr", otherwise use "str".
6356 * Returns FAIL when out of memory and when key already exists.
6357 */
6358 int
6359dict_add_nr_str(d, key, nr, str)
6360 dict_T *d;
6361 char *key;
6362 long nr;
6363 char_u *str;
6364{
6365 dictitem_T *item;
6366
6367 item = dictitem_alloc((char_u *)key);
6368 if (item == NULL)
6369 return FAIL;
6370 item->di_tv.v_lock = 0;
6371 if (str == NULL)
6372 {
6373 item->di_tv.v_type = VAR_NUMBER;
6374 item->di_tv.vval.v_number = nr;
6375 }
6376 else
6377 {
6378 item->di_tv.v_type = VAR_STRING;
6379 item->di_tv.vval.v_string = vim_strsave(str);
6380 }
6381 if (dict_add(d, item) == FAIL)
6382 {
6383 dictitem_free(item);
6384 return FAIL;
6385 }
6386 return OK;
6387}
6388
6389/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006390 * Get the number of items in a Dictionary.
6391 */
6392 static long
6393dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006396 if (d == NULL)
6397 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006399}
6400
6401/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 * Find item "key[len]" in Dictionary "d".
6403 * If "len" is negative use strlen(key).
6404 * Returns NULL when not found.
6405 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006409 char_u *key;
6410 int len;
6411{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412#define AKEYLEN 200
6413 char_u buf[AKEYLEN];
6414 char_u *akey;
6415 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006418 if (len < 0)
6419 akey = key;
6420 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006421 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006422 tofree = akey = vim_strnsave(key, len);
6423 if (akey == NULL)
6424 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006426 else
6427 {
6428 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006429 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006430 akey = buf;
6431 }
6432
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006434 vim_free(tofree);
6435 if (HASHITEM_EMPTY(hi))
6436 return NULL;
6437 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438}
6439
6440/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006441 * Get a string item from a dictionary.
6442 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006443 * Returns NULL if the entry doesn't exist or out of memory.
6444 */
6445 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006446get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006447 dict_T *d;
6448 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006449 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006450{
6451 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006452 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006453
6454 di = dict_find(d, key, -1);
6455 if (di == NULL)
6456 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006457 s = get_tv_string(&di->di_tv);
6458 if (save && s != NULL)
6459 s = vim_strsave(s);
6460 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006461}
6462
6463/*
6464 * Get a number item from a dictionary.
6465 * Returns 0 if the entry doesn't exist or out of memory.
6466 */
6467 long
6468get_dict_number(d, key)
6469 dict_T *d;
6470 char_u *key;
6471{
6472 dictitem_T *di;
6473
6474 di = dict_find(d, key, -1);
6475 if (di == NULL)
6476 return 0;
6477 return get_tv_number(&di->di_tv);
6478}
6479
6480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006481 * Return an allocated string with the string representation of a Dictionary.
6482 * May return NULL.
6483 */
6484 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006485dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006488{
6489 garray_T ga;
6490 int first = TRUE;
6491 char_u *tofree;
6492 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006494 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006496 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006498 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 return NULL;
6500 ga_init2(&ga, (int)sizeof(char), 80);
6501 ga_append(&ga, '{');
6502
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006506 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006508 --todo;
6509
6510 if (first)
6511 first = FALSE;
6512 else
6513 ga_concat(&ga, (char_u *)", ");
6514
6515 tofree = string_quote(hi->hi_key, FALSE);
6516 if (tofree != NULL)
6517 {
6518 ga_concat(&ga, tofree);
6519 vim_free(tofree);
6520 }
6521 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006522 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006523 if (s != NULL)
6524 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006525 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 if (s == NULL)
6527 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006529 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006530 if (todo > 0)
6531 {
6532 vim_free(ga.ga_data);
6533 return NULL;
6534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535
6536 ga_append(&ga, '}');
6537 ga_append(&ga, NUL);
6538 return (char_u *)ga.ga_data;
6539}
6540
6541/*
6542 * Allocate a variable for a Dictionary and fill it from "*arg".
6543 * Return OK or FAIL. Returns NOTDONE for {expr}.
6544 */
6545 static int
6546get_dict_tv(arg, rettv, evaluate)
6547 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006548 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006549 int evaluate;
6550{
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 dict_T *d = NULL;
6552 typval_T tvkey;
6553 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006554 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006556 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006557 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558
6559 /*
6560 * First check if it's not a curly-braces thing: {expr}.
6561 * Must do this without evaluating, otherwise a function may be called
6562 * twice. Unfortunately this means we need to call eval1() twice for the
6563 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006564 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006565 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006566 if (*start != '}')
6567 {
6568 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6569 return FAIL;
6570 if (*start == '}')
6571 return NOTDONE;
6572 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573
6574 if (evaluate)
6575 {
6576 d = dict_alloc();
6577 if (d == NULL)
6578 return FAIL;
6579 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006580 tvkey.v_type = VAR_UNKNOWN;
6581 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582
6583 *arg = skipwhite(*arg + 1);
6584 while (**arg != '}' && **arg != NUL)
6585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006586 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587 goto failret;
6588 if (**arg != ':')
6589 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006590 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 goto failret;
6593 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006594 key = get_tv_string_buf_chk(&tvkey, buf);
6595 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006597 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6598 if (key != NULL)
6599 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006600 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601 goto failret;
6602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006603
6604 *arg = skipwhite(*arg + 1);
6605 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6606 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006607 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608 goto failret;
6609 }
6610 if (evaluate)
6611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006612 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613 if (item != NULL)
6614 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006615 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006616 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 clear_tv(&tv);
6618 goto failret;
6619 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006620 item = dictitem_alloc(key);
6621 clear_tv(&tvkey);
6622 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006623 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006624 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006625 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006626 if (dict_add(d, item) == FAIL)
6627 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006628 }
6629 }
6630
6631 if (**arg == '}')
6632 break;
6633 if (**arg != ',')
6634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006635 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006636 goto failret;
6637 }
6638 *arg = skipwhite(*arg + 1);
6639 }
6640
6641 if (**arg != '}')
6642 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006643 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644failret:
6645 if (evaluate)
6646 dict_free(d);
6647 return FAIL;
6648 }
6649
6650 *arg = skipwhite(*arg + 1);
6651 if (evaluate)
6652 {
6653 rettv->v_type = VAR_DICT;
6654 rettv->vval.v_dict = d;
6655 ++d->dv_refcount;
6656 }
6657
6658 return OK;
6659}
6660
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 * Return a string with the string representation of a variable.
6663 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006665 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006666 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 * May return NULL;
6668 */
6669 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006670echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006676 static int recurse = 0;
6677 char_u *r = NULL;
6678
Bram Moolenaar33570922005-01-25 22:26:29 +00006679 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006680 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006681 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006682 *tofree = NULL;
6683 return NULL;
6684 }
6685 ++recurse;
6686
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 switch (tv->v_type)
6688 {
6689 case VAR_FUNC:
6690 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006691 r = tv->vval.v_string;
6692 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006695 if (tv->vval.v_list == NULL)
6696 {
6697 *tofree = NULL;
6698 r = NULL;
6699 }
6700 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6701 {
6702 *tofree = NULL;
6703 r = (char_u *)"[...]";
6704 }
6705 else
6706 {
6707 tv->vval.v_list->lv_copyID = copyID;
6708 *tofree = list2string(tv, copyID);
6709 r = *tofree;
6710 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006712
Bram Moolenaar8c711452005-01-14 21:53:12 +00006713 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006714 if (tv->vval.v_dict == NULL)
6715 {
6716 *tofree = NULL;
6717 r = NULL;
6718 }
6719 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6720 {
6721 *tofree = NULL;
6722 r = (char_u *)"{...}";
6723 }
6724 else
6725 {
6726 tv->vval.v_dict->dv_copyID = copyID;
6727 *tofree = dict2string(tv, copyID);
6728 r = *tofree;
6729 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006730 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006732 case VAR_STRING:
6733 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006734 *tofree = NULL;
6735 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006736 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006740 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006742
6743 --recurse;
6744 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006745}
6746
6747/*
6748 * Return a string with the string representation of a variable.
6749 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6750 * "numbuf" is used for a number.
6751 * Puts quotes around strings, so that they can be parsed back by eval().
6752 * May return NULL;
6753 */
6754 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006755tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006757 char_u **tofree;
6758 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006759 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760{
6761 switch (tv->v_type)
6762 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763 case VAR_FUNC:
6764 *tofree = string_quote(tv->vval.v_string, TRUE);
6765 return *tofree;
6766 case VAR_STRING:
6767 *tofree = string_quote(tv->vval.v_string, FALSE);
6768 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006769 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006770 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006772 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006774 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006776 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777}
6778
6779/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 * Return string "str" in ' quotes, doubling ' characters.
6781 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006782 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006783 */
6784 static char_u *
6785string_quote(str, function)
6786 char_u *str;
6787 int function;
6788{
Bram Moolenaar33570922005-01-25 22:26:29 +00006789 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 char_u *p, *r, *s;
6791
Bram Moolenaar33570922005-01-25 22:26:29 +00006792 len = (function ? 13 : 3);
6793 if (str != NULL)
6794 {
6795 len += STRLEN(str);
6796 for (p = str; *p != NUL; mb_ptr_adv(p))
6797 if (*p == '\'')
6798 ++len;
6799 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 s = r = alloc(len);
6801 if (r != NULL)
6802 {
6803 if (function)
6804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006805 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 r += 10;
6807 }
6808 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006809 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006810 if (str != NULL)
6811 for (p = str; *p != NUL; )
6812 {
6813 if (*p == '\'')
6814 *r++ = '\'';
6815 MB_COPY_CHAR(p, r);
6816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006818 if (function)
6819 *r++ = ')';
6820 *r++ = NUL;
6821 }
6822 return s;
6823}
6824
6825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 * Get the value of an environment variable.
6827 * "arg" is pointing to the '$'. It is advanced to after the name.
6828 * If the environment variable was not set, silently assume it is empty.
6829 * Always return OK.
6830 */
6831 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006832get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 int evaluate;
6836{
6837 char_u *string = NULL;
6838 int len;
6839 int cc;
6840 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006841 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842
6843 ++*arg;
6844 name = *arg;
6845 len = get_env_len(arg);
6846 if (evaluate)
6847 {
6848 if (len != 0)
6849 {
6850 cc = name[len];
6851 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006852 /* first try vim_getenv(), fast for normal environment vars */
6853 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006855 {
6856 if (!mustfree)
6857 string = vim_strsave(string);
6858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 else
6860 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006861 if (mustfree)
6862 vim_free(string);
6863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 /* next try expanding things like $VIM and ${HOME} */
6865 string = expand_env_save(name - 1);
6866 if (string != NULL && *string == '$')
6867 {
6868 vim_free(string);
6869 string = NULL;
6870 }
6871 }
6872 name[len] = cc;
6873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006874 rettv->v_type = VAR_STRING;
6875 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
6877
6878 return OK;
6879}
6880
6881/*
6882 * Array with names and number of arguments of all internal functions
6883 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6884 */
6885static struct fst
6886{
6887 char *f_name; /* function name */
6888 char f_min_argc; /* minimal number of arguments */
6889 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006890 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006891 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892} functions[] =
6893{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006894 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {"append", 2, 2, f_append},
6896 {"argc", 0, 0, f_argc},
6897 {"argidx", 0, 0, f_argidx},
6898 {"argv", 1, 1, f_argv},
6899 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006900 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"bufexists", 1, 1, f_bufexists},
6902 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6903 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6904 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6905 {"buflisted", 1, 1, f_buflisted},
6906 {"bufloaded", 1, 1, f_bufloaded},
6907 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006908 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {"bufwinnr", 1, 1, f_bufwinnr},
6910 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006911 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006912 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006913 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"char2nr", 1, 1, f_char2nr},
6915 {"cindent", 1, 1, f_cindent},
6916 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006917#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006918 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006919 {"complete_add", 1, 1, f_complete_add},
6920 {"complete_check", 0, 0, f_complete_check},
6921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006923 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006924 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006926 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006927 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {"delete", 1, 1, f_delete},
6929 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006930 {"diff_filler", 1, 1, f_diff_filler},
6931 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006932 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006934 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 {"eventhandler", 0, 0, f_eventhandler},
6936 {"executable", 1, 1, f_executable},
6937 {"exists", 1, 1, f_exists},
6938 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006939 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6941 {"filereadable", 1, 1, f_filereadable},
6942 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006943 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006944 {"finddir", 1, 3, f_finddir},
6945 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {"fnamemodify", 2, 2, f_fnamemodify},
6947 {"foldclosed", 1, 1, f_foldclosed},
6948 {"foldclosedend", 1, 1, f_foldclosedend},
6949 {"foldlevel", 1, 1, f_foldlevel},
6950 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006951 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006953 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006955 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006956 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"getbufvar", 2, 2, f_getbufvar},
6958 {"getchar", 0, 1, f_getchar},
6959 {"getcharmod", 0, 0, f_getcharmod},
6960 {"getcmdline", 0, 0, f_getcmdline},
6961 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006962 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006964 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006965 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {"getfsize", 1, 1, f_getfsize},
6967 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006968 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006969 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006970 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006971 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006972 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006973 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {"getregtype", 0, 1, f_getregtype},
6975 {"getwinposx", 0, 0, f_getwinposx},
6976 {"getwinposy", 0, 0, f_getwinposy},
6977 {"getwinvar", 2, 2, f_getwinvar},
6978 {"glob", 1, 1, f_glob},
6979 {"globpath", 2, 2, f_globpath},
6980 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006981 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00006982 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6984 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6985 {"histadd", 2, 2, f_histadd},
6986 {"histdel", 1, 2, f_histdel},
6987 {"histget", 1, 2, f_histget},
6988 {"histnr", 1, 1, f_histnr},
6989 {"hlID", 1, 1, f_hlID},
6990 {"hlexists", 1, 1, f_hlexists},
6991 {"hostname", 0, 0, f_hostname},
6992 {"iconv", 3, 3, f_iconv},
6993 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006994 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006995 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006997 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {"inputrestore", 0, 0, f_inputrestore},
6999 {"inputsave", 0, 0, f_inputsave},
7000 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007001 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007003 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007005 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007008 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {"libcall", 3, 3, f_libcall},
7010 {"libcallnr", 3, 3, f_libcallnr},
7011 {"line", 1, 1, f_line},
7012 {"line2byte", 1, 1, f_line2byte},
7013 {"lispindent", 1, 1, f_lispindent},
7014 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007015 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007016 {"maparg", 1, 3, f_maparg},
7017 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007018 {"match", 2, 4, f_match},
7019 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007020 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007021 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007022 {"max", 1, 1, f_max},
7023 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007024#ifdef vim_mkdir
7025 {"mkdir", 1, 3, f_mkdir},
7026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {"mode", 0, 0, f_mode},
7028 {"nextnonblank", 1, 1, f_nextnonblank},
7029 {"nr2char", 1, 1, f_nr2char},
7030 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007031 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007032 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007034 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {"remote_expr", 2, 3, f_remote_expr},
7036 {"remote_foreground", 1, 1, f_remote_foreground},
7037 {"remote_peek", 1, 2, f_remote_peek},
7038 {"remote_read", 1, 1, f_remote_read},
7039 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007040 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007042 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007044 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007045 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007046 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007047 {"searchpair", 3, 6, f_searchpair},
7048 {"searchpairpos", 3, 6, f_searchpairpos},
7049 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"server2client", 2, 2, f_server2client},
7051 {"serverlist", 0, 0, f_serverlist},
7052 {"setbufvar", 3, 3, f_setbufvar},
7053 {"setcmdpos", 1, 1, f_setcmdpos},
7054 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007055 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007056 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007057 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 {"setreg", 2, 3, f_setreg},
7059 {"setwinvar", 3, 3, f_setwinvar},
7060 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007061 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007062 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007063 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007064 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007065 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007066 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#ifdef HAVE_STRFTIME
7068 {"strftime", 1, 2, f_strftime},
7069#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007070 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007071 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {"strlen", 1, 1, f_strlen},
7073 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007074 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {"strtrans", 1, 1, f_strtrans},
7076 {"submatch", 1, 1, f_submatch},
7077 {"substitute", 4, 4, f_substitute},
7078 {"synID", 3, 3, f_synID},
7079 {"synIDattr", 2, 3, f_synIDattr},
7080 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007081 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007082 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007083 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007084 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007085 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007086 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007088 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"tolower", 1, 1, f_tolower},
7090 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007091 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"virtcol", 1, 1, f_virtcol},
7095 {"visualmode", 0, 1, f_visualmode},
7096 {"winbufnr", 1, 1, f_winbufnr},
7097 {"wincol", 0, 0, f_wincol},
7098 {"winheight", 1, 1, f_winheight},
7099 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007100 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007102 {"winrestview", 1, 1, f_winrestview},
7103 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007105 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106};
7107
7108#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7109
7110/*
7111 * Function given to ExpandGeneric() to obtain the list of internal
7112 * or user defined function names.
7113 */
7114 char_u *
7115get_function_name(xp, idx)
7116 expand_T *xp;
7117 int idx;
7118{
7119 static int intidx = -1;
7120 char_u *name;
7121
7122 if (idx == 0)
7123 intidx = -1;
7124 if (intidx < 0)
7125 {
7126 name = get_user_func_name(xp, idx);
7127 if (name != NULL)
7128 return name;
7129 }
7130 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7131 {
7132 STRCPY(IObuff, functions[intidx].f_name);
7133 STRCAT(IObuff, "(");
7134 if (functions[intidx].f_max_argc == 0)
7135 STRCAT(IObuff, ")");
7136 return IObuff;
7137 }
7138
7139 return NULL;
7140}
7141
7142/*
7143 * Function given to ExpandGeneric() to obtain the list of internal or
7144 * user defined variable or function names.
7145 */
7146/*ARGSUSED*/
7147 char_u *
7148get_expr_name(xp, idx)
7149 expand_T *xp;
7150 int idx;
7151{
7152 static int intidx = -1;
7153 char_u *name;
7154
7155 if (idx == 0)
7156 intidx = -1;
7157 if (intidx < 0)
7158 {
7159 name = get_function_name(xp, idx);
7160 if (name != NULL)
7161 return name;
7162 }
7163 return get_user_var_name(xp, ++intidx);
7164}
7165
7166#endif /* FEAT_CMDL_COMPL */
7167
7168/*
7169 * Find internal function in table above.
7170 * Return index, or -1 if not found
7171 */
7172 static int
7173find_internal_func(name)
7174 char_u *name; /* name of the function */
7175{
7176 int first = 0;
7177 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7178 int cmp;
7179 int x;
7180
7181 /*
7182 * Find the function name in the table. Binary search.
7183 */
7184 while (first <= last)
7185 {
7186 x = first + ((unsigned)(last - first) >> 1);
7187 cmp = STRCMP(name, functions[x].f_name);
7188 if (cmp < 0)
7189 last = x - 1;
7190 else if (cmp > 0)
7191 first = x + 1;
7192 else
7193 return x;
7194 }
7195 return -1;
7196}
7197
7198/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007199 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7200 * name it contains, otherwise return "name".
7201 */
7202 static char_u *
7203deref_func_name(name, lenp)
7204 char_u *name;
7205 int *lenp;
7206{
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007208 int cc;
7209
7210 cc = name[*lenp];
7211 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007212 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007213 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007216 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007217 {
7218 *lenp = 0;
7219 return (char_u *)""; /* just in case */
7220 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 *lenp = STRLEN(v->di_tv.vval.v_string);
7222 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007223 }
7224
7225 return name;
7226}
7227
7228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 * Allocate a variable for the result of a function.
7230 * Return OK or FAIL.
7231 */
7232 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7234 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 char_u *name; /* name of the function */
7236 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 char_u **arg; /* argument, pointing to the '(' */
7239 linenr_T firstline; /* first line of range */
7240 linenr_T lastline; /* last line of range */
7241 int *doesrange; /* return: function handled range */
7242 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 char_u *argp;
7246 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 int argcount = 0; /* number of arguments found */
7249
7250 /*
7251 * Get the arguments.
7252 */
7253 argp = *arg;
7254 while (argcount < MAX_FUNC_ARGS)
7255 {
7256 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7257 if (*argp == ')' || *argp == ',' || *argp == NUL)
7258 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7260 {
7261 ret = FAIL;
7262 break;
7263 }
7264 ++argcount;
7265 if (*argp != ',')
7266 break;
7267 }
7268 if (*argp == ')')
7269 ++argp;
7270 else
7271 ret = FAIL;
7272
7273 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007274 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 {
7278 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007279 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007281 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007285 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
7287 *arg = skipwhite(argp);
7288 return ret;
7289}
7290
7291
7292/*
7293 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007294 * Return OK when the function can't be called, FAIL otherwise.
7295 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 */
7297 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007298call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 char_u *name; /* name of the function */
7301 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 linenr_T firstline; /* first line of range */
7306 linenr_T lastline; /* last line of range */
7307 int *doesrange; /* return: function handled range */
7308 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#define ERROR_UNKNOWN 0
7313#define ERROR_TOOMANY 1
7314#define ERROR_TOOFEW 2
7315#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007316#define ERROR_DICT 4
7317#define ERROR_NONE 5
7318#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 int error = ERROR_NONE;
7320 int i;
7321 int llen;
7322 ufunc_T *fp;
7323 int cc;
7324#define FLEN_FIXED 40
7325 char_u fname_buf[FLEN_FIXED + 1];
7326 char_u *fname;
7327
7328 /*
7329 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7330 * Change <SNR>123_name() to K_SNR 123_name().
7331 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7332 */
7333 cc = name[len];
7334 name[len] = NUL;
7335 llen = eval_fname_script(name);
7336 if (llen > 0)
7337 {
7338 fname_buf[0] = K_SPECIAL;
7339 fname_buf[1] = KS_EXTRA;
7340 fname_buf[2] = (int)KE_SNR;
7341 i = 3;
7342 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7343 {
7344 if (current_SID <= 0)
7345 error = ERROR_SCRIPT;
7346 else
7347 {
7348 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7349 i = (int)STRLEN(fname_buf);
7350 }
7351 }
7352 if (i + STRLEN(name + llen) < FLEN_FIXED)
7353 {
7354 STRCPY(fname_buf + i, name + llen);
7355 fname = fname_buf;
7356 }
7357 else
7358 {
7359 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7360 if (fname == NULL)
7361 error = ERROR_OTHER;
7362 else
7363 {
7364 mch_memmove(fname, fname_buf, (size_t)i);
7365 STRCPY(fname + i, name + llen);
7366 }
7367 }
7368 }
7369 else
7370 fname = name;
7371
7372 *doesrange = FALSE;
7373
7374
7375 /* execute the function if no errors detected and executing */
7376 if (evaluate && error == ERROR_NONE)
7377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 error = ERROR_UNKNOWN;
7380
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007381 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
7383 /*
7384 * User defined function.
7385 */
7386 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007387
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007389 /* Trigger FuncUndefined event, may load the function. */
7390 if (fp == NULL
7391 && apply_autocmds(EVENT_FUNCUNDEFINED,
7392 fname, fname, TRUE, NULL)
7393 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007395 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 fp = find_func(fname);
7397 }
7398#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007399 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007400 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007401 {
7402 /* loaded a package, search for the function again */
7403 fp = find_func(fname);
7404 }
7405
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (fp != NULL)
7407 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007408 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007410 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007412 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007414 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 else
7417 {
7418 /*
7419 * Call the user function.
7420 * Save and restore search patterns, script variables and
7421 * redo buffer.
7422 */
7423 save_search_patterns();
7424 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007425 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007428 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7429 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7430 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007431 /* Function was unreferenced while being used, free it
7432 * now. */
7433 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 restoreRedobuff();
7435 restore_search_patterns();
7436 error = ERROR_NONE;
7437 }
7438 }
7439 }
7440 else
7441 {
7442 /*
7443 * Find the function name in the table, call its implementation.
7444 */
7445 i = find_internal_func(fname);
7446 if (i >= 0)
7447 {
7448 if (argcount < functions[i].f_min_argc)
7449 error = ERROR_TOOFEW;
7450 else if (argcount > functions[i].f_max_argc)
7451 error = ERROR_TOOMANY;
7452 else
7453 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 error = ERROR_NONE;
7457 }
7458 }
7459 }
7460 /*
7461 * The function call (or "FuncUndefined" autocommand sequence) might
7462 * have been aborted by an error, an interrupt, or an explicitly thrown
7463 * exception that has not been caught so far. This situation can be
7464 * tested for by calling aborting(). For an error in an internal
7465 * function or for the "E132" error in call_user_func(), however, the
7466 * throw point at which the "force_abort" flag (temporarily reset by
7467 * emsg()) is normally updated has not been reached yet. We need to
7468 * update that flag first to make aborting() reliable.
7469 */
7470 update_force_abort();
7471 }
7472 if (error == ERROR_NONE)
7473 ret = OK;
7474
7475 /*
7476 * Report an error unless the argument evaluation or function call has been
7477 * cancelled due to an aborting error, an interrupt, or an exception.
7478 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 if (!aborting())
7480 {
7481 switch (error)
7482 {
7483 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007484 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 break;
7486 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007487 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 break;
7489 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007490 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 name);
7492 break;
7493 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007494 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 name);
7496 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007498 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 name);
7500 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 }
7502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
7504 name[len] = cc;
7505 if (fname != name && fname != fname_buf)
7506 vim_free(fname);
7507
7508 return ret;
7509}
7510
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007511/*
7512 * Give an error message with a function name. Handle <SNR> things.
7513 */
7514 static void
7515emsg_funcname(msg, name)
7516 char *msg;
7517 char_u *name;
7518{
7519 char_u *p;
7520
7521 if (*name == K_SPECIAL)
7522 p = concat_str((char_u *)"<SNR>", name + 3);
7523 else
7524 p = name;
7525 EMSG2(_(msg), p);
7526 if (p != name)
7527 vim_free(p);
7528}
7529
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530/*********************************************
7531 * Implementation of the built-in functions
7532 */
7533
7534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007535 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 */
7537 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007538f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007539 typval_T *argvars;
7540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541{
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007544 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007545 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007547 if ((l = argvars[0].vval.v_list) != NULL
7548 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7549 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007551 }
7552 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007553 EMSG(_(e_listreq));
7554}
7555
7556/*
7557 * "append(lnum, string/list)" function
7558 */
7559 static void
7560f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 typval_T *argvars;
7562 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007563{
7564 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007565 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 list_T *l = NULL;
7567 listitem_T *li = NULL;
7568 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007569 long added = 0;
7570
Bram Moolenaar0d660222005-01-07 21:51:51 +00007571 lnum = get_tv_lnum(argvars);
7572 if (lnum >= 0
7573 && lnum <= curbuf->b_ml.ml_line_count
7574 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007578 l = argvars[1].vval.v_list;
7579 if (l == NULL)
7580 return;
7581 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007583 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007584 for (;;)
7585 {
7586 if (l == NULL)
7587 tv = &argvars[1]; /* append a string */
7588 else if (li == NULL)
7589 break; /* end of list */
7590 else
7591 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592 line = get_tv_string_chk(tv);
7593 if (line == NULL) /* type error */
7594 {
7595 rettv->vval.v_number = 1; /* Failed */
7596 break;
7597 }
7598 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007599 ++added;
7600 if (l == NULL)
7601 break;
7602 li = li->li_next;
7603 }
7604
7605 appended_lines_mark(lnum, added);
7606 if (curwin->w_cursor.lnum > lnum)
7607 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007609 else
7610 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611}
7612
7613/*
7614 * "argc()" function
7615 */
7616/* ARGSUSED */
7617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 typval_T *argvars;
7620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007622 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
7624
7625/*
7626 * "argidx()" function
7627 */
7628/* ARGSUSED */
7629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *argvars;
7632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007634 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635}
7636
7637/*
7638 * "argv(nr)" function
7639 */
7640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 typval_T *argvars;
7643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
7645 int idx;
7646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007647 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651 rettv->vval.v_string = NULL;
7652 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
7655/*
7656 * "browse(save, title, initdir, default)" function
7657 */
7658/* ARGSUSED */
7659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007660f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007661 typval_T *argvars;
7662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
7664#ifdef FEAT_BROWSE
7665 int save;
7666 char_u *title;
7667 char_u *initdir;
7668 char_u *defname;
7669 char_u buf[NUMBUFLEN];
7670 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007673 save = get_tv_number_chk(&argvars[0], &error);
7674 title = get_tv_string_chk(&argvars[1]);
7675 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7676 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007678 if (error || title == NULL || initdir == NULL || defname == NULL)
7679 rettv->vval.v_string = NULL;
7680 else
7681 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007682 do_browse(save ? BROWSE_SAVE : 0,
7683 title, defname, NULL, initdir, NULL, curbuf);
7684#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007686#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007688}
7689
7690/*
7691 * "browsedir(title, initdir)" function
7692 */
7693/* ARGSUSED */
7694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 typval_T *argvars;
7697 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007698{
7699#ifdef FEAT_BROWSE
7700 char_u *title;
7701 char_u *initdir;
7702 char_u buf[NUMBUFLEN];
7703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007704 title = get_tv_string_chk(&argvars[0]);
7705 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007707 if (title == NULL || initdir == NULL)
7708 rettv->vval.v_string = NULL;
7709 else
7710 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007711 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007713 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717
Bram Moolenaar33570922005-01-25 22:26:29 +00007718static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720/*
7721 * Find a buffer by number or exact name.
7722 */
7723 static buf_T *
7724find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007729 if (avar->v_type == VAR_NUMBER)
7730 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007731 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007733 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007734 if (buf == NULL)
7735 {
7736 /* No full path name match, try a match with a URL or a "nofile"
7737 * buffer, these don't use the full path. */
7738 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7739 if (buf->b_fname != NULL
7740 && (path_with_url(buf->b_fname)
7741#ifdef FEAT_QUICKFIX
7742 || bt_nofile(buf)
7743#endif
7744 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007745 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007746 break;
7747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
7749 return buf;
7750}
7751
7752/*
7753 * "bufexists(expr)" function
7754 */
7755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007756f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 typval_T *argvars;
7758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761}
7762
7763/*
7764 * "buflisted(expr)" function
7765 */
7766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 typval_T *argvars;
7769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 buf_T *buf;
7772
7773 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775}
7776
7777/*
7778 * "bufloaded(expr)" function
7779 */
7780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007782 typval_T *argvars;
7783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784{
7785 buf_T *buf;
7786
7787 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790
Bram Moolenaar33570922005-01-25 22:26:29 +00007791static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007792
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793/*
7794 * Get buffer by number or pattern.
7795 */
7796 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 int save_magic;
7802 char_u *save_cpo;
7803 buf_T *buf;
7804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 if (tv->v_type == VAR_NUMBER)
7806 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007807 if (tv->v_type != VAR_STRING)
7808 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 if (name == NULL || *name == NUL)
7810 return curbuf;
7811 if (name[0] == '$' && name[1] == NUL)
7812 return lastbuf;
7813
7814 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7815 save_magic = p_magic;
7816 p_magic = TRUE;
7817 save_cpo = p_cpo;
7818 p_cpo = (char_u *)"";
7819
7820 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7821 TRUE, FALSE));
7822
7823 p_magic = save_magic;
7824 p_cpo = save_cpo;
7825
7826 /* If not found, try expanding the name, like done for bufexists(). */
7827 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 return buf;
7831}
7832
7833/*
7834 * "bufname(expr)" function
7835 */
7836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007838 typval_T *argvars;
7839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841 buf_T *buf;
7842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007843 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 buf = get_buf_tv(&argvars[0]);
7846 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007848 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 --emsg_off;
7852}
7853
7854/*
7855 * "bufnr(expr)" function
7856 */
7857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 typval_T *argvars;
7860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007863 int error = FALSE;
7864 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007869 --emsg_off;
7870
7871 /* If the buffer isn't found and the second argument is not zero create a
7872 * new buffer. */
7873 if (buf == NULL
7874 && argvars[1].v_type != VAR_UNKNOWN
7875 && get_tv_number_chk(&argvars[1], &error) != 0
7876 && !error
7877 && (name = get_tv_string_chk(&argvars[0])) != NULL
7878 && !error)
7879 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7880
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007884 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885}
7886
7887/*
7888 * "bufwinnr(nr)" function
7889 */
7890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 typval_T *argvars;
7893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895#ifdef FEAT_WINDOWS
7896 win_T *wp;
7897 int winnr = 0;
7898#endif
7899 buf_T *buf;
7900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007901 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#ifdef FEAT_WINDOWS
7905 for (wp = firstwin; wp; wp = wp->w_next)
7906 {
7907 ++winnr;
7908 if (wp->w_buffer == buf)
7909 break;
7910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007911 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914#endif
7915 --emsg_off;
7916}
7917
7918/*
7919 * "byte2line(byte)" function
7920 */
7921/*ARGSUSED*/
7922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007924 typval_T *argvars;
7925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007928 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929#else
7930 long boff = 0;
7931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007932 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007934 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007936 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 (linenr_T)0, &boff);
7938#endif
7939}
7940
7941/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007942 * "byteidx()" function
7943 */
7944/*ARGSUSED*/
7945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007946f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007947 typval_T *argvars;
7948 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007949{
7950#ifdef FEAT_MBYTE
7951 char_u *t;
7952#endif
7953 char_u *str;
7954 long idx;
7955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956 str = get_tv_string_chk(&argvars[0]);
7957 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007959 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007960 return;
7961
7962#ifdef FEAT_MBYTE
7963 t = str;
7964 for ( ; idx > 0; idx--)
7965 {
7966 if (*t == NUL) /* EOL reached */
7967 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007968 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007971#else
7972 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007973 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007974#endif
7975}
7976
7977/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007978 * "call(func, arglist)" function
7979 */
7980 static void
7981f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 typval_T *argvars;
7983 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007984{
7985 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007986 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007987 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007988 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007989 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007990 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991
7992 rettv->vval.v_number = 0;
7993 if (argvars[1].v_type != VAR_LIST)
7994 {
7995 EMSG(_(e_listreq));
7996 return;
7997 }
7998 if (argvars[1].vval.v_list == NULL)
7999 return;
8000
8001 if (argvars[0].v_type == VAR_FUNC)
8002 func = argvars[0].vval.v_string;
8003 else
8004 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008005 if (*func == NUL)
8006 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008007
Bram Moolenaare9a41262005-01-15 22:18:47 +00008008 if (argvars[2].v_type != VAR_UNKNOWN)
8009 {
8010 if (argvars[2].v_type != VAR_DICT)
8011 {
8012 EMSG(_(e_dictreq));
8013 return;
8014 }
8015 selfdict = argvars[2].vval.v_dict;
8016 }
8017
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008018 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8019 item = item->li_next)
8020 {
8021 if (argc == MAX_FUNC_ARGS)
8022 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008023 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008024 break;
8025 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008026 /* Make a copy of each argument. This is needed to be able to set
8027 * v_lock to VAR_FIXED in the copy without changing the original list.
8028 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008029 copy_tv(&item->li_tv, &argv[argc++]);
8030 }
8031
8032 if (item == NULL)
8033 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008034 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8035 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036
8037 /* Free the arguments. */
8038 while (argc > 0)
8039 clear_tv(&argv[--argc]);
8040}
8041
8042/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008043 * "changenr()" function
8044 */
8045/*ARGSUSED*/
8046 static void
8047f_changenr(argvars, rettv)
8048 typval_T *argvars;
8049 typval_T *rettv;
8050{
8051 rettv->vval.v_number = curbuf->b_u_seq_cur;
8052}
8053
8054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 * "char2nr(string)" function
8056 */
8057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008058f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 typval_T *argvars;
8060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062#ifdef FEAT_MBYTE
8063 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008064 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 else
8066#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008067 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068}
8069
8070/*
8071 * "cindent(lnum)" function
8072 */
8073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 typval_T *argvars;
8076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078#ifdef FEAT_CINDENT
8079 pos_T pos;
8080 linenr_T lnum;
8081
8082 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008083 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8085 {
8086 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008087 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 curwin->w_cursor = pos;
8089 }
8090 else
8091#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093}
8094
8095/*
8096 * "col(string)" function
8097 */
8098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 typval_T *argvars;
8101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 colnr_T col = 0;
8104 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008105 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008107 fp = var2fpos(&argvars[0], FALSE, &fnum);
8108 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {
8110 if (fp->col == MAXCOL)
8111 {
8112 /* '> can be MAXCOL, get the length of the line then */
8113 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8114 col = STRLEN(ml_get(fp->lnum)) + 1;
8115 else
8116 col = MAXCOL;
8117 }
8118 else
8119 {
8120 col = fp->col + 1;
8121#ifdef FEAT_VIRTUALEDIT
8122 /* col(".") when the cursor is on the NUL at the end of the line
8123 * because of "coladd" can be seen as an extra column. */
8124 if (virtual_active() && fp == &curwin->w_cursor)
8125 {
8126 char_u *p = ml_get_cursor();
8127
8128 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8129 curwin->w_virtcol - curwin->w_cursor.coladd))
8130 {
8131# ifdef FEAT_MBYTE
8132 int l;
8133
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008134 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 col += l;
8136# else
8137 if (*p != NUL && p[1] == NUL)
8138 ++col;
8139# endif
8140 }
8141 }
8142#endif
8143 }
8144 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146}
8147
Bram Moolenaar572cb562005-08-05 21:35:02 +00008148#if defined(FEAT_INS_EXPAND)
8149/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008150 * "complete()" function
8151 */
8152/*ARGSUSED*/
8153 static void
8154f_complete(argvars, rettv)
8155 typval_T *argvars;
8156 typval_T *rettv;
8157{
8158 int startcol;
8159
8160 if ((State & INSERT) == 0)
8161 {
8162 EMSG(_("E785: complete() can only be used in Insert mode"));
8163 return;
8164 }
8165 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8166 {
8167 EMSG(_(e_invarg));
8168 return;
8169 }
8170
8171 startcol = get_tv_number_chk(&argvars[0], NULL);
8172 if (startcol <= 0)
8173 return;
8174
8175 set_completion(startcol - 1, argvars[1].vval.v_list);
8176}
8177
8178/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008179 * "complete_add()" function
8180 */
8181/*ARGSUSED*/
8182 static void
8183f_complete_add(argvars, rettv)
8184 typval_T *argvars;
8185 typval_T *rettv;
8186{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008187 char_u *word;
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008188 char_u *kind = NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008189 char_u *extra = NULL;
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008190 char_u *info = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008191 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008192
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008193 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8194 {
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008195 word = get_dict_string(argvars[0].vval.v_dict, (char_u *)"word", FALSE);
8196 kind = get_dict_string(argvars[0].vval.v_dict, (char_u *)"kind", FALSE);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008197 extra = get_dict_string(argvars[0].vval.v_dict,
8198 (char_u *)"menu", FALSE);
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008199 info = get_dict_string(argvars[0].vval.v_dict,
8200 (char_u *)"info", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008201 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008202 }
8203 else
8204 word = get_tv_string_chk(&argvars[0]);
8205 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008206 rettv->vval.v_number = ins_compl_add(word, -1, icase,
Bram Moolenaare48ec1f2006-03-11 21:38:31 +00008207 NULL, kind, extra, info, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008208}
8209
8210/*
8211 * "complete_check()" function
8212 */
8213/*ARGSUSED*/
8214 static void
8215f_complete_check(argvars, rettv)
8216 typval_T *argvars;
8217 typval_T *rettv;
8218{
8219 int saved = RedrawingDisabled;
8220
8221 RedrawingDisabled = 0;
8222 ins_compl_check_keys(0);
8223 rettv->vval.v_number = compl_interrupted;
8224 RedrawingDisabled = saved;
8225}
8226#endif
8227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228/*
8229 * "confirm(message, buttons[, default [, type]])" function
8230 */
8231/*ARGSUSED*/
8232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008234 typval_T *argvars;
8235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8238 char_u *message;
8239 char_u *buttons = NULL;
8240 char_u buf[NUMBUFLEN];
8241 char_u buf2[NUMBUFLEN];
8242 int def = 1;
8243 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008244 char_u *typestr;
8245 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008247 message = get_tv_string_chk(&argvars[0]);
8248 if (message == NULL)
8249 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008250 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8253 if (buttons == NULL)
8254 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008255 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008257 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008258 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008260 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8261 if (typestr == NULL)
8262 error = TRUE;
8263 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008265 switch (TOUPPER_ASC(*typestr))
8266 {
8267 case 'E': type = VIM_ERROR; break;
8268 case 'Q': type = VIM_QUESTION; break;
8269 case 'I': type = VIM_INFO; break;
8270 case 'W': type = VIM_WARNING; break;
8271 case 'G': type = VIM_GENERIC; break;
8272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 }
8274 }
8275 }
8276 }
8277
8278 if (buttons == NULL || *buttons == NUL)
8279 buttons = (char_u *)_("&Ok");
8280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008281 if (error)
8282 rettv->vval.v_number = 0;
8283 else
8284 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 def, NULL);
8286#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288#endif
8289}
8290
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291/*
8292 * "copy()" function
8293 */
8294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008299 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008300}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301
8302/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008303 * "count()" function
8304 */
8305 static void
8306f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *argvars;
8308 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008309{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008310 long n = 0;
8311 int ic = FALSE;
8312
Bram Moolenaare9a41262005-01-15 22:18:47 +00008313 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008314 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008315 listitem_T *li;
8316 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008317 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008318
Bram Moolenaare9a41262005-01-15 22:18:47 +00008319 if ((l = argvars[0].vval.v_list) != NULL)
8320 {
8321 li = l->lv_first;
8322 if (argvars[2].v_type != VAR_UNKNOWN)
8323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008324 int error = FALSE;
8325
8326 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008327 if (argvars[3].v_type != VAR_UNKNOWN)
8328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008329 idx = get_tv_number_chk(&argvars[3], &error);
8330 if (!error)
8331 {
8332 li = list_find(l, idx);
8333 if (li == NULL)
8334 EMSGN(_(e_listidx), idx);
8335 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008337 if (error)
8338 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008339 }
8340
8341 for ( ; li != NULL; li = li->li_next)
8342 if (tv_equal(&li->li_tv, &argvars[1], ic))
8343 ++n;
8344 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008345 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008346 else if (argvars[0].v_type == VAR_DICT)
8347 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008348 int todo;
8349 dict_T *d;
8350 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008351
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008352 if ((d = argvars[0].vval.v_dict) != NULL)
8353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008354 int error = FALSE;
8355
Bram Moolenaare9a41262005-01-15 22:18:47 +00008356 if (argvars[2].v_type != VAR_UNKNOWN)
8357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008358 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008359 if (argvars[3].v_type != VAR_UNKNOWN)
8360 EMSG(_(e_invarg));
8361 }
8362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008363 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008365 {
8366 if (!HASHITEM_EMPTY(hi))
8367 {
8368 --todo;
8369 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8370 ++n;
8371 }
8372 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008373 }
8374 }
8375 else
8376 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008377 rettv->vval.v_number = n;
8378}
8379
8380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8382 *
8383 * Checks the existence of a cscope connection.
8384 */
8385/*ARGSUSED*/
8386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008388 typval_T *argvars;
8389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391#ifdef FEAT_CSCOPE
8392 int num = 0;
8393 char_u *dbpath = NULL;
8394 char_u *prepend = NULL;
8395 char_u buf[NUMBUFLEN];
8396
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008397 if (argvars[0].v_type != VAR_UNKNOWN
8398 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400 num = (int)get_tv_number(&argvars[0]);
8401 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008402 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008403 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 }
8405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008408 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409#endif
8410}
8411
8412/*
8413 * "cursor(lnum, col)" function
8414 *
8415 * Moves the cursor to the specified line and column
8416 */
8417/*ARGSUSED*/
8418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008419f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 typval_T *argvars;
8421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008424#ifdef FEAT_VIRTUALEDIT
8425 long coladd = 0;
8426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427
Bram Moolenaara5525202006-03-02 22:52:09 +00008428 if (argvars[1].v_type == VAR_UNKNOWN)
8429 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008430 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008431
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008432 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008433 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008434 line = pos.lnum;
8435 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008436#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008437 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008438#endif
8439 }
8440 else
8441 {
8442 line = get_tv_lnum(argvars);
8443 col = get_tv_number_chk(&argvars[1], NULL);
8444#ifdef FEAT_VIRTUALEDIT
8445 if (argvars[2].v_type != VAR_UNKNOWN)
8446 coladd = get_tv_number_chk(&argvars[2], NULL);
8447#endif
8448 }
8449 if (line < 0 || col < 0
8450#ifdef FEAT_VIRTUALEDIT
8451 || coladd < 0
8452#endif
8453 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008454 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 if (line > 0)
8456 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (col > 0)
8458 curwin->w_cursor.col = col - 1;
8459#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008460 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461#endif
8462
8463 /* Make sure the cursor is in a valid position. */
8464 check_cursor();
8465#ifdef FEAT_MBYTE
8466 /* Correct cursor for multi-byte character. */
8467 if (has_mbyte)
8468 mb_adjust_cursor();
8469#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008470
8471 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472}
8473
8474/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 */
8477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008479 typval_T *argvars;
8480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008482 int noref = 0;
8483
8484 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008485 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008486 if (noref < 0 || noref > 1)
8487 EMSG(_(e_invarg));
8488 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008489 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490}
8491
8492/*
8493 * "delete()" function
8494 */
8495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 typval_T *argvars;
8498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504}
8505
8506/*
8507 * "did_filetype()" function
8508 */
8509/*ARGSUSED*/
8510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008511f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008512 typval_T *argvars;
8513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008518 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519#endif
8520}
8521
8522/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008523 * "diff_filler()" function
8524 */
8525/*ARGSUSED*/
8526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008527f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008528 typval_T *argvars;
8529 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008530{
8531#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008533#endif
8534}
8535
8536/*
8537 * "diff_hlID()" function
8538 */
8539/*ARGSUSED*/
8540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *argvars;
8543 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008544{
8545#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008547 static linenr_T prev_lnum = 0;
8548 static int changedtick = 0;
8549 static int fnum = 0;
8550 static int change_start = 0;
8551 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008552 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008553 int filler_lines;
8554 int col;
8555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008556 if (lnum < 0) /* ignore type error in {lnum} arg */
8557 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008558 if (lnum != prev_lnum
8559 || changedtick != curbuf->b_changedtick
8560 || fnum != curbuf->b_fnum)
8561 {
8562 /* New line, buffer, change: need to get the values. */
8563 filler_lines = diff_check(curwin, lnum);
8564 if (filler_lines < 0)
8565 {
8566 if (filler_lines == -1)
8567 {
8568 change_start = MAXCOL;
8569 change_end = -1;
8570 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8571 hlID = HLF_ADD; /* added line */
8572 else
8573 hlID = HLF_CHD; /* changed line */
8574 }
8575 else
8576 hlID = HLF_ADD; /* added line */
8577 }
8578 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008579 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008580 prev_lnum = lnum;
8581 changedtick = curbuf->b_changedtick;
8582 fnum = curbuf->b_fnum;
8583 }
8584
8585 if (hlID == HLF_CHD || hlID == HLF_TXD)
8586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008587 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008588 if (col >= change_start && col <= change_end)
8589 hlID = HLF_TXD; /* changed text */
8590 else
8591 hlID = HLF_CHD; /* changed line */
8592 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008593 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008594#endif
8595}
8596
8597/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008598 * "empty({expr})" function
8599 */
8600 static void
8601f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008602 typval_T *argvars;
8603 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008604{
8605 int n;
8606
8607 switch (argvars[0].v_type)
8608 {
8609 case VAR_STRING:
8610 case VAR_FUNC:
8611 n = argvars[0].vval.v_string == NULL
8612 || *argvars[0].vval.v_string == NUL;
8613 break;
8614 case VAR_NUMBER:
8615 n = argvars[0].vval.v_number == 0;
8616 break;
8617 case VAR_LIST:
8618 n = argvars[0].vval.v_list == NULL
8619 || argvars[0].vval.v_list->lv_first == NULL;
8620 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008621 case VAR_DICT:
8622 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008623 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008624 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008625 default:
8626 EMSG2(_(e_intern2), "f_empty()");
8627 n = 0;
8628 }
8629
8630 rettv->vval.v_number = n;
8631}
8632
8633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 * "escape({string}, {chars})" function
8635 */
8636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 typval_T *argvars;
8639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 char_u buf[NUMBUFLEN];
8642
Bram Moolenaar758711c2005-02-02 23:11:38 +00008643 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8644 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646}
8647
8648/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008649 * "eval()" function
8650 */
8651/*ARGSUSED*/
8652 static void
8653f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008654 typval_T *argvars;
8655 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008656{
8657 char_u *s;
8658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008659 s = get_tv_string_chk(&argvars[0]);
8660 if (s != NULL)
8661 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8664 {
8665 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008666 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008668 else if (*s != NUL)
8669 EMSG(_(e_trailing));
8670}
8671
8672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 * "eventhandler()" function
8674 */
8675/*ARGSUSED*/
8676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008677f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008678 typval_T *argvars;
8679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682}
8683
8684/*
8685 * "executable()" function
8686 */
8687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008688f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008689 typval_T *argvars;
8690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693}
8694
8695/*
8696 * "exists()" function
8697 */
8698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008700 typval_T *argvars;
8701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 char_u *p;
8704 char_u *name;
8705 int n = FALSE;
8706 int len = 0;
8707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 if (*p == '$') /* environment variable */
8710 {
8711 /* first try "normal" environment variables (fast) */
8712 if (mch_getenv(p + 1) != NULL)
8713 n = TRUE;
8714 else
8715 {
8716 /* try expanding things like $VIM and ${HOME} */
8717 p = expand_env_save(p);
8718 if (p != NULL && *p != '$')
8719 n = TRUE;
8720 vim_free(p);
8721 }
8722 }
8723 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 else if (*p == '*') /* internal or user defined function */
8726 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008727 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 }
8729 else if (*p == ':')
8730 {
8731 n = cmd_exists(p + 1);
8732 }
8733 else if (*p == '#')
8734 {
8735#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008736 if (p[1] == '#')
8737 n = autocmd_supported(p + 2);
8738 else
8739 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740#endif
8741 }
8742 else /* internal variable */
8743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008744 char_u *tofree;
8745 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008747 /* get_name_len() takes care of expanding curly braces */
8748 name = p;
8749 len = get_name_len(&p, &tofree, TRUE, FALSE);
8750 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008752 if (tofree != NULL)
8753 name = tofree;
8754 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8755 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008757 /* handle d.key, l[idx], f(expr) */
8758 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8759 if (n)
8760 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
8762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008764 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 }
8766
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768}
8769
8770/*
8771 * "expand()" function
8772 */
8773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *argvars;
8776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
8778 char_u *s;
8779 int len;
8780 char_u *errormsg;
8781 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8782 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008783 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 rettv->v_type = VAR_STRING;
8786 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 if (*s == '%' || *s == '#' || *s == '<')
8788 {
8789 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 --emsg_off;
8792 }
8793 else
8794 {
8795 /* When the optional second argument is non-zero, don't remove matches
8796 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008797 if (argvars[1].v_type != VAR_UNKNOWN
8798 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 if (!error)
8801 {
8802 ExpandInit(&xpc);
8803 xpc.xp_context = EXPAND_FILES;
8804 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8805 ExpandCleanup(&xpc);
8806 }
8807 else
8808 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 }
8810}
8811
8812/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008813 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008814 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008815 */
8816 static void
8817f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 typval_T *argvars;
8819 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008820{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008821 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008822 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008823 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 list_T *l1, *l2;
8825 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008826 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008827 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008828
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 l1 = argvars[0].vval.v_list;
8830 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008831 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8832 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008833 {
8834 if (argvars[2].v_type != VAR_UNKNOWN)
8835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008836 before = get_tv_number_chk(&argvars[2], &error);
8837 if (error)
8838 return; /* type error; errmsg already given */
8839
Bram Moolenaar758711c2005-02-02 23:11:38 +00008840 if (before == l1->lv_len)
8841 item = NULL;
8842 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008844 item = list_find(l1, before);
8845 if (item == NULL)
8846 {
8847 EMSGN(_(e_listidx), before);
8848 return;
8849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 }
8851 }
8852 else
8853 item = NULL;
8854 list_extend(l1, l2, item);
8855
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 copy_tv(&argvars[0], rettv);
8857 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008859 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8860 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008861 dict_T *d1, *d2;
8862 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008863 char_u *action;
8864 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008866 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008867
8868 d1 = argvars[0].vval.v_dict;
8869 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008870 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8871 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008872 {
8873 /* Check the third argument. */
8874 if (argvars[2].v_type != VAR_UNKNOWN)
8875 {
8876 static char *(av[]) = {"keep", "force", "error"};
8877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008878 action = get_tv_string_chk(&argvars[2]);
8879 if (action == NULL)
8880 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008881 for (i = 0; i < 3; ++i)
8882 if (STRCMP(action, av[i]) == 0)
8883 break;
8884 if (i == 3)
8885 {
8886 EMSGN(_(e_invarg2), action);
8887 return;
8888 }
8889 }
8890 else
8891 action = (char_u *)"force";
8892
8893 /* Go over all entries in the second dict and add them to the
8894 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008895 todo = d2->dv_hashtab.ht_used;
8896 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008898 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008899 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008900 --todo;
8901 di1 = dict_find(d1, hi2->hi_key, -1);
8902 if (di1 == NULL)
8903 {
8904 di1 = dictitem_copy(HI2DI(hi2));
8905 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8906 dictitem_free(di1);
8907 }
8908 else if (*action == 'e')
8909 {
8910 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8911 break;
8912 }
8913 else if (*action == 'f')
8914 {
8915 clear_tv(&di1->di_tv);
8916 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8917 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008918 }
8919 }
8920
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 copy_tv(&argvars[0], rettv);
8922 }
8923 }
8924 else
8925 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008926}
8927
8928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 * "filereadable()" function
8930 */
8931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008933 typval_T *argvars;
8934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
8936 FILE *fd;
8937 char_u *p;
8938 int n;
8939
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8942 {
8943 n = TRUE;
8944 fclose(fd);
8945 }
8946 else
8947 n = FALSE;
8948
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950}
8951
8952/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008953 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 * rights to write into.
8955 */
8956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *argvars;
8959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008961 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
8963
Bram Moolenaar33570922005-01-25 22:26:29 +00008964static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008965
8966 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008967findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *argvars;
8969 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008970 int dir;
8971{
8972#ifdef FEAT_SEARCHPATH
8973 char_u *fname;
8974 char_u *fresult = NULL;
8975 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8976 char_u *p;
8977 char_u pathbuf[NUMBUFLEN];
8978 int count = 1;
8979 int first = TRUE;
8980
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008982
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8986 if (p == NULL)
8987 count = -1; /* error */
8988 else
8989 {
8990 if (*p != NUL)
8991 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008993 if (argvars[2].v_type != VAR_UNKNOWN)
8994 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8995 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008996 }
8997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000 do
9001 {
9002 vim_free(fresult);
9003 fresult = find_file_in_path_option(first ? fname : NULL,
9004 first ? (int)STRLEN(fname) : 0,
9005 0, first, path, dir, NULL);
9006 first = FALSE;
9007 } while (--count > 0 && fresult != NULL);
9008 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009009
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009010 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009011#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009013#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009015}
9016
Bram Moolenaar33570922005-01-25 22:26:29 +00009017static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9018static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009019
9020/*
9021 * Implementation of map() and filter().
9022 */
9023 static void
9024filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009025 typval_T *argvars;
9026 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009027 int map;
9028{
9029 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009030 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009031 listitem_T *li, *nli;
9032 list_T *l = NULL;
9033 dictitem_T *di;
9034 hashtab_T *ht;
9035 hashitem_T *hi;
9036 dict_T *d = NULL;
9037 typval_T save_val;
9038 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009040 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009041 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009042 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009043
9044 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009046 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009047 if ((l = argvars[0].vval.v_list) == NULL
9048 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 return;
9050 }
9051 else if (argvars[0].v_type == VAR_DICT)
9052 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009053 if ((d = argvars[0].vval.v_dict) == NULL
9054 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009055 return;
9056 }
9057 else
9058 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009059 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009060 return;
9061 }
9062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009063 expr = get_tv_string_buf_chk(&argvars[1], buf);
9064 /* On type errors, the preceding call has already displayed an error
9065 * message. Avoid a misleading error message for an empty string that
9066 * was not passed as argument. */
9067 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 prepare_vimvar(VV_VAL, &save_val);
9070 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009071
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009072 /* We reset "did_emsg" to be able to detect whether an error
9073 * occurred during evaluation of the expression. */
9074 save_did_emsg = did_emsg;
9075 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009079 prepare_vimvar(VV_KEY, &save_key);
9080 vimvars[VV_KEY].vv_type = VAR_STRING;
9081
9082 ht = &d->dv_hashtab;
9083 hash_lock(ht);
9084 todo = ht->ht_used;
9085 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087 if (!HASHITEM_EMPTY(hi))
9088 {
9089 --todo;
9090 di = HI2DI(hi);
9091 if (tv_check_lock(di->di_tv.v_lock, msg))
9092 break;
9093 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009094 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009095 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009096 break;
9097 if (!map && rem)
9098 dictitem_remove(d, di);
9099 clear_tv(&vimvars[VV_KEY].vv_tv);
9100 }
9101 }
9102 hash_unlock(ht);
9103
9104 restore_vimvar(VV_KEY, &save_key);
9105 }
9106 else
9107 {
9108 for (li = l->lv_first; li != NULL; li = nli)
9109 {
9110 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009111 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009112 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009113 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009114 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009115 break;
9116 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009118 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009119 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009121 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009122
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009123 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009125
9126 copy_tv(&argvars[0], rettv);
9127}
9128
9129 static int
9130filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009132 char_u *expr;
9133 int map;
9134 int *remp;
9135{
Bram Moolenaar33570922005-01-25 22:26:29 +00009136 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009137 char_u *s;
9138
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009140 s = expr;
9141 if (eval1(&s, &rettv, TRUE) == FAIL)
9142 return FAIL;
9143 if (*s != NUL) /* check for trailing chars after expr */
9144 {
9145 EMSG2(_(e_invexpr2), s);
9146 return FAIL;
9147 }
9148 if (map)
9149 {
9150 /* map(): replace the list item value */
9151 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009152 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009153 *tv = rettv;
9154 }
9155 else
9156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 int error = FALSE;
9158
Bram Moolenaare9a41262005-01-15 22:18:47 +00009159 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009161 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 /* On type error, nothing has been removed; return FAIL to stop the
9163 * loop. The error message was given by get_tv_number_chk(). */
9164 if (error)
9165 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009166 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009168 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009169}
9170
9171/*
9172 * "filter()" function
9173 */
9174 static void
9175f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009178{
9179 filter_map(argvars, rettv, FALSE);
9180}
9181
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009182/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009183 * "finddir({fname}[, {path}[, {count}]])" function
9184 */
9185 static void
9186f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 typval_T *argvars;
9188 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009189{
9190 findfilendir(argvars, rettv, TRUE);
9191}
9192
9193/*
9194 * "findfile({fname}[, {path}[, {count}]])" function
9195 */
9196 static void
9197f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009198 typval_T *argvars;
9199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009200{
9201 findfilendir(argvars, rettv, FALSE);
9202}
9203
9204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 * "fnamemodify({fname}, {mods})" function
9206 */
9207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009209 typval_T *argvars;
9210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 char_u *fname;
9213 char_u *mods;
9214 int usedlen = 0;
9215 int len;
9216 char_u *fbuf = NULL;
9217 char_u buf[NUMBUFLEN];
9218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 fname = get_tv_string_chk(&argvars[0]);
9220 mods = get_tv_string_buf_chk(&argvars[1], buf);
9221 if (fname == NULL || mods == NULL)
9222 fname = NULL;
9223 else
9224 {
9225 len = (int)STRLEN(fname);
9226 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 vim_free(fbuf);
9235}
9236
Bram Moolenaar33570922005-01-25 22:26:29 +00009237static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
9239/*
9240 * "foldclosed()" function
9241 */
9242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009244 typval_T *argvars;
9245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 int end;
9247{
9248#ifdef FEAT_FOLDING
9249 linenr_T lnum;
9250 linenr_T first, last;
9251
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9254 {
9255 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9256 {
9257 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009258 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 return;
9262 }
9263 }
9264#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266}
9267
9268/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009269 * "foldclosed()" function
9270 */
9271 static void
9272f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009273 typval_T *argvars;
9274 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009275{
9276 foldclosed_both(argvars, rettv, FALSE);
9277}
9278
9279/*
9280 * "foldclosedend()" function
9281 */
9282 static void
9283f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 typval_T *argvars;
9285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009286{
9287 foldclosed_both(argvars, rettv, TRUE);
9288}
9289
9290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 * "foldlevel()" function
9292 */
9293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *argvars;
9296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297{
9298#ifdef FEAT_FOLDING
9299 linenr_T lnum;
9300
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 else
9305#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307}
9308
9309/*
9310 * "foldtext()" function
9311 */
9312/*ARGSUSED*/
9313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009315 typval_T *argvars;
9316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318#ifdef FEAT_FOLDING
9319 linenr_T lnum;
9320 char_u *s;
9321 char_u *r;
9322 int len;
9323 char *txt;
9324#endif
9325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326 rettv->v_type = VAR_STRING;
9327 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009329 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9330 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9331 <= curbuf->b_ml.ml_line_count
9332 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
9334 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009335 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9336 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 {
9338 if (!linewhite(lnum))
9339 break;
9340 ++lnum;
9341 }
9342
9343 /* Find interesting text in this line. */
9344 s = skipwhite(ml_get(lnum));
9345 /* skip C comment-start */
9346 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009349 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009350 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009351 {
9352 s = skipwhite(ml_get(lnum + 1));
9353 if (*s == '*')
9354 s = skipwhite(s + 1);
9355 }
9356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 txt = _("+-%s%3ld lines: ");
9358 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009359 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 + 20 /* for %3ld */
9361 + STRLEN(s))); /* concatenated */
9362 if (r != NULL)
9363 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9365 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9366 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 len = (int)STRLEN(r);
9368 STRCAT(r, s);
9369 /* remove 'foldmarker' and 'commentstring' */
9370 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 }
9373 }
9374#endif
9375}
9376
9377/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009378 * "foldtextresult(lnum)" function
9379 */
9380/*ARGSUSED*/
9381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009385{
9386#ifdef FEAT_FOLDING
9387 linenr_T lnum;
9388 char_u *text;
9389 char_u buf[51];
9390 foldinfo_T foldinfo;
9391 int fold_count;
9392#endif
9393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->v_type = VAR_STRING;
9395 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009396#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 /* treat illegal types and illegal string values for {lnum} the same */
9399 if (lnum < 0)
9400 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009401 fold_count = foldedCount(curwin, lnum, &foldinfo);
9402 if (fold_count > 0)
9403 {
9404 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9405 &foldinfo, buf);
9406 if (text == buf)
9407 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009409 }
9410#endif
9411}
9412
9413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 * "foreground()" function
9415 */
9416/*ARGSUSED*/
9417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423#ifdef FEAT_GUI
9424 if (gui.in_use)
9425 gui_mch_set_foreground();
9426#else
9427# ifdef WIN32
9428 win32_set_foreground();
9429# endif
9430#endif
9431}
9432
9433/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009434 * "function()" function
9435 */
9436/*ARGSUSED*/
9437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009441{
9442 char_u *s;
9443
Bram Moolenaara7043832005-01-21 11:56:39 +00009444 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009446 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009447 EMSG2(_(e_invarg2), s);
9448 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009449 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009450 else
9451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 rettv->vval.v_string = vim_strsave(s);
9453 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009454 }
9455}
9456
9457/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009458 * "garbagecollect()" function
9459 */
9460/*ARGSUSED*/
9461 static void
9462f_garbagecollect(argvars, rettv)
9463 typval_T *argvars;
9464 typval_T *rettv;
9465{
9466 garbage_collect();
9467}
9468
9469/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009470 * "get()" function
9471 */
9472 static void
9473f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009474 typval_T *argvars;
9475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009476{
Bram Moolenaar33570922005-01-25 22:26:29 +00009477 listitem_T *li;
9478 list_T *l;
9479 dictitem_T *di;
9480 dict_T *d;
9481 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009482
Bram Moolenaare9a41262005-01-15 22:18:47 +00009483 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009484 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009485 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 int error = FALSE;
9488
9489 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9490 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009491 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009492 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009493 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009494 else if (argvars[0].v_type == VAR_DICT)
9495 {
9496 if ((d = argvars[0].vval.v_dict) != NULL)
9497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009498 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009499 if (di != NULL)
9500 tv = &di->di_tv;
9501 }
9502 }
9503 else
9504 EMSG2(_(e_listdictarg), "get()");
9505
9506 if (tv == NULL)
9507 {
9508 if (argvars[2].v_type == VAR_UNKNOWN)
9509 rettv->vval.v_number = 0;
9510 else
9511 copy_tv(&argvars[2], rettv);
9512 }
9513 else
9514 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009515}
9516
Bram Moolenaar342337a2005-07-21 21:11:17 +00009517static 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 +00009518
9519/*
9520 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009521 * Return a range (from start to end) of lines in rettv from the specified
9522 * buffer.
9523 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009524 */
9525 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009526get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009527 buf_T *buf;
9528 linenr_T start;
9529 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009530 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009531 typval_T *rettv;
9532{
9533 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009534
Bram Moolenaar342337a2005-07-21 21:11:17 +00009535 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009536 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009537 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009538 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009539 }
9540 else
9541 rettv->vval.v_number = 0;
9542
9543 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9544 return;
9545
9546 if (!retlist)
9547 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009548 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9549 p = ml_get_buf(buf, start, FALSE);
9550 else
9551 p = (char_u *)"";
9552
9553 rettv->v_type = VAR_STRING;
9554 rettv->vval.v_string = vim_strsave(p);
9555 }
9556 else
9557 {
9558 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009559 return;
9560
9561 if (start < 1)
9562 start = 1;
9563 if (end > buf->b_ml.ml_line_count)
9564 end = buf->b_ml.ml_line_count;
9565 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009566 if (list_append_string(rettv->vval.v_list,
9567 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009568 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009569 }
9570}
9571
9572/*
9573 * "getbufline()" function
9574 */
9575 static void
9576f_getbufline(argvars, rettv)
9577 typval_T *argvars;
9578 typval_T *rettv;
9579{
9580 linenr_T lnum;
9581 linenr_T end;
9582 buf_T *buf;
9583
9584 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9585 ++emsg_off;
9586 buf = get_buf_tv(&argvars[0]);
9587 --emsg_off;
9588
Bram Moolenaar661b1822005-07-28 22:36:45 +00009589 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009590 if (argvars[2].v_type == VAR_UNKNOWN)
9591 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009592 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009593 end = get_tv_lnum_buf(&argvars[2], buf);
9594
Bram Moolenaar342337a2005-07-21 21:11:17 +00009595 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009596}
9597
Bram Moolenaar0d660222005-01-07 21:51:51 +00009598/*
9599 * "getbufvar()" function
9600 */
9601 static void
9602f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009603 typval_T *argvars;
9604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009605{
9606 buf_T *buf;
9607 buf_T *save_curbuf;
9608 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009609 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9612 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009613 ++emsg_off;
9614 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009615
9616 rettv->v_type = VAR_STRING;
9617 rettv->vval.v_string = NULL;
9618
9619 if (buf != NULL && varname != NULL)
9620 {
9621 if (*varname == '&') /* buffer-local-option */
9622 {
9623 /* set curbuf to be our buf, temporarily */
9624 save_curbuf = curbuf;
9625 curbuf = buf;
9626
9627 get_option_tv(&varname, rettv, TRUE);
9628
9629 /* restore previous notion of curbuf */
9630 curbuf = save_curbuf;
9631 }
9632 else
9633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 if (*varname == NUL)
9635 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9636 * scope prefix before the NUL byte is required by
9637 * find_var_in_ht(). */
9638 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009639 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009640 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009641 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009643 }
9644 }
9645
9646 --emsg_off;
9647}
9648
9649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 * "getchar()" function
9651 */
9652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 typval_T *argvars;
9655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659
9660 ++no_mapping;
9661 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009662 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 /* getchar(): blocking wait. */
9664 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009665 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 /* getchar(1): only check if char avail */
9667 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009668 else if (error || vpeekc() == NUL)
9669 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 n = 0;
9671 else
9672 /* getchar(0) and char avail: return char */
9673 n = safe_vgetc();
9674 --no_mapping;
9675 --allow_keys;
9676
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 if (IS_SPECIAL(n) || mod_mask != 0)
9679 {
9680 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9681 int i = 0;
9682
9683 /* Turn a special key into three bytes, plus modifier. */
9684 if (mod_mask != 0)
9685 {
9686 temp[i++] = K_SPECIAL;
9687 temp[i++] = KS_MODIFIER;
9688 temp[i++] = mod_mask;
9689 }
9690 if (IS_SPECIAL(n))
9691 {
9692 temp[i++] = K_SPECIAL;
9693 temp[i++] = K_SECOND(n);
9694 temp[i++] = K_THIRD(n);
9695 }
9696#ifdef FEAT_MBYTE
9697 else if (has_mbyte)
9698 i += (*mb_char2bytes)(n, temp + i);
9699#endif
9700 else
9701 temp[i++] = n;
9702 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->v_type = VAR_STRING;
9704 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 }
9706}
9707
9708/*
9709 * "getcharmod()" function
9710 */
9711/*ARGSUSED*/
9712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 typval_T *argvars;
9715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718}
9719
9720/*
9721 * "getcmdline()" function
9722 */
9723/*ARGSUSED*/
9724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009726 typval_T *argvars;
9727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->v_type = VAR_STRING;
9730 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
9734 * "getcmdpos()" function
9735 */
9736/*ARGSUSED*/
9737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
9745/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009746 * "getcmdtype()" function
9747 */
9748/*ARGSUSED*/
9749 static void
9750f_getcmdtype(argvars, rettv)
9751 typval_T *argvars;
9752 typval_T *rettv;
9753{
9754 rettv->v_type = VAR_STRING;
9755 rettv->vval.v_string = alloc(2);
9756 if (rettv->vval.v_string != NULL)
9757 {
9758 rettv->vval.v_string[0] = get_cmdline_type();
9759 rettv->vval.v_string[1] = NUL;
9760 }
9761}
9762
9763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 * "getcwd()" function
9765 */
9766/*ARGSUSED*/
9767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 typval_T *argvars;
9770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772 char_u cwd[MAXPATHL];
9773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 else
9778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009781 if (rettv->vval.v_string != NULL)
9782 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783#endif
9784 }
9785}
9786
9787/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009788 * "getfontname()" function
9789 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009790/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009793 typval_T *argvars;
9794 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->v_type = VAR_STRING;
9797 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009798#ifdef FEAT_GUI
9799 if (gui.in_use)
9800 {
9801 GuiFont font;
9802 char_u *name = NULL;
9803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009804 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009805 {
9806 /* Get the "Normal" font. Either the name saved by
9807 * hl_set_font_name() or from the font ID. */
9808 font = gui.norm_font;
9809 name = hl_get_font_name();
9810 }
9811 else
9812 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009814 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9815 return;
9816 font = gui_mch_get_font(name, FALSE);
9817 if (font == NOFONT)
9818 return; /* Invalid font name, return empty string. */
9819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009821 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009822 gui_mch_free_font(font);
9823 }
9824#endif
9825}
9826
9827/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009828 * "getfperm({fname})" function
9829 */
9830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009832 typval_T *argvars;
9833 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009834{
9835 char_u *fname;
9836 struct stat st;
9837 char_u *perm = NULL;
9838 char_u flags[] = "rwx";
9839 int i;
9840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009842
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009844 if (mch_stat((char *)fname, &st) >= 0)
9845 {
9846 perm = vim_strsave((char_u *)"---------");
9847 if (perm != NULL)
9848 {
9849 for (i = 0; i < 9; i++)
9850 {
9851 if (st.st_mode & (1 << (8 - i)))
9852 perm[i] = flags[i % 3];
9853 }
9854 }
9855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009857}
9858
9859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 * "getfsize({fname})" function
9861 */
9862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 char_u *fname;
9868 struct stat st;
9869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873
9874 if (mch_stat((char *)fname, &st) >= 0)
9875 {
9876 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
9881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883}
9884
9885/*
9886 * "getftime({fname})" function
9887 */
9888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009890 typval_T *argvars;
9891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892{
9893 char_u *fname;
9894 struct stat st;
9895
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
9898 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902}
9903
9904/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009905 * "getftype({fname})" function
9906 */
9907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 typval_T *argvars;
9910 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009911{
9912 char_u *fname;
9913 struct stat st;
9914 char_u *type = NULL;
9915 char *t;
9916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009920 if (mch_lstat((char *)fname, &st) >= 0)
9921 {
9922#ifdef S_ISREG
9923 if (S_ISREG(st.st_mode))
9924 t = "file";
9925 else if (S_ISDIR(st.st_mode))
9926 t = "dir";
9927# ifdef S_ISLNK
9928 else if (S_ISLNK(st.st_mode))
9929 t = "link";
9930# endif
9931# ifdef S_ISBLK
9932 else if (S_ISBLK(st.st_mode))
9933 t = "bdev";
9934# endif
9935# ifdef S_ISCHR
9936 else if (S_ISCHR(st.st_mode))
9937 t = "cdev";
9938# endif
9939# ifdef S_ISFIFO
9940 else if (S_ISFIFO(st.st_mode))
9941 t = "fifo";
9942# endif
9943# ifdef S_ISSOCK
9944 else if (S_ISSOCK(st.st_mode))
9945 t = "fifo";
9946# endif
9947 else
9948 t = "other";
9949#else
9950# ifdef S_IFMT
9951 switch (st.st_mode & S_IFMT)
9952 {
9953 case S_IFREG: t = "file"; break;
9954 case S_IFDIR: t = "dir"; break;
9955# ifdef S_IFLNK
9956 case S_IFLNK: t = "link"; break;
9957# endif
9958# ifdef S_IFBLK
9959 case S_IFBLK: t = "bdev"; break;
9960# endif
9961# ifdef S_IFCHR
9962 case S_IFCHR: t = "cdev"; break;
9963# endif
9964# ifdef S_IFIFO
9965 case S_IFIFO: t = "fifo"; break;
9966# endif
9967# ifdef S_IFSOCK
9968 case S_IFSOCK: t = "socket"; break;
9969# endif
9970 default: t = "other";
9971 }
9972# else
9973 if (mch_isdir(fname))
9974 t = "dir";
9975 else
9976 t = "file";
9977# endif
9978#endif
9979 type = vim_strsave((char_u *)t);
9980 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009982}
9983
9984/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009985 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009986 */
9987 static void
9988f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 typval_T *argvars;
9990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009991{
9992 linenr_T lnum;
9993 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009994 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009995
9996 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009997 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009998 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009999 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010000 retlist = FALSE;
10001 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010002 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010003 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010004 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010005 retlist = TRUE;
10006 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010007
Bram Moolenaar342337a2005-07-21 21:11:17 +000010008 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010009}
10010
10011/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010012 * "getpos(string)" function
10013 */
10014 static void
10015f_getpos(argvars, rettv)
10016 typval_T *argvars;
10017 typval_T *rettv;
10018{
10019 pos_T *fp;
10020 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010021 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010022
10023 if (rettv_list_alloc(rettv) == OK)
10024 {
10025 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010026 fp = var2fpos(&argvars[0], TRUE, &fnum);
10027 if (fnum != -1)
10028 list_append_number(l, (varnumber_T)fnum);
10029 else
10030 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010031 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10032 : (varnumber_T)0);
10033 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10034 : (varnumber_T)0);
10035 list_append_number(l,
10036#ifdef FEAT_VIRTUALEDIT
10037 (fp != NULL) ? (varnumber_T)fp->coladd :
10038#endif
10039 (varnumber_T)0);
10040 }
10041 else
10042 rettv->vval.v_number = FALSE;
10043}
10044
10045/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010046 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010047 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010048/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010049 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010050f_getqflist(argvars, rettv)
10051 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010052 typval_T *rettv;
10053{
10054#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010055 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010056#endif
10057
10058 rettv->vval.v_number = FALSE;
10059#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010060 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010061 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010062 wp = NULL;
10063 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10064 {
10065 wp = find_win_by_nr(&argvars[0]);
10066 if (wp == NULL)
10067 return;
10068 }
10069
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010070 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010071 }
10072#endif
10073}
10074
10075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 * "getreg()" function
10077 */
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
10083 char_u *strregname;
10084 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010085 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010088 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 strregname = get_tv_string_chk(&argvars[0]);
10091 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010092 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010096 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 regname = (strregname == NULL ? '"' : *strregname);
10098 if (regname == 0)
10099 regname = '"';
10100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 rettv->vval.v_string = error ? NULL :
10103 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104}
10105
10106/*
10107 * "getregtype()" function
10108 */
10109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010111 typval_T *argvars;
10112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113{
10114 char_u *strregname;
10115 int regname;
10116 char_u buf[NUMBUFLEN + 2];
10117 long reglen = 0;
10118
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010119 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 {
10121 strregname = get_tv_string_chk(&argvars[0]);
10122 if (strregname == NULL) /* type error; errmsg already given */
10123 {
10124 rettv->v_type = VAR_STRING;
10125 rettv->vval.v_string = NULL;
10126 return;
10127 }
10128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 else
10130 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010131 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
10133 regname = (strregname == NULL ? '"' : *strregname);
10134 if (regname == 0)
10135 regname = '"';
10136
10137 buf[0] = NUL;
10138 buf[1] = NUL;
10139 switch (get_reg_type(regname, &reglen))
10140 {
10141 case MLINE: buf[0] = 'V'; break;
10142 case MCHAR: buf[0] = 'v'; break;
10143#ifdef FEAT_VISUAL
10144 case MBLOCK:
10145 buf[0] = Ctrl_V;
10146 sprintf((char *)buf + 1, "%ld", reglen + 1);
10147 break;
10148#endif
10149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 rettv->v_type = VAR_STRING;
10151 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
10153
10154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 * "getwinposx()" function
10156 */
10157/*ARGSUSED*/
10158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010160 typval_T *argvars;
10161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164#ifdef FEAT_GUI
10165 if (gui.in_use)
10166 {
10167 int x, y;
10168
10169 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 }
10172#endif
10173}
10174
10175/*
10176 * "getwinposy()" function
10177 */
10178/*ARGSUSED*/
10179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010181 typval_T *argvars;
10182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185#ifdef FEAT_GUI
10186 if (gui.in_use)
10187 {
10188 int x, y;
10189
10190 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 }
10193#endif
10194}
10195
Bram Moolenaara40058a2005-07-11 22:42:07 +000010196 static win_T *
10197find_win_by_nr(vp)
10198 typval_T *vp;
10199{
10200#ifdef FEAT_WINDOWS
10201 win_T *wp;
10202#endif
10203 int nr;
10204
10205 nr = get_tv_number_chk(vp, NULL);
10206
10207#ifdef FEAT_WINDOWS
10208 if (nr < 0)
10209 return NULL;
10210 if (nr == 0)
10211 return curwin;
10212
10213 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10214 if (--nr <= 0)
10215 break;
10216 return wp;
10217#else
10218 if (nr == 0 || nr == 1)
10219 return curwin;
10220 return NULL;
10221#endif
10222}
10223
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224/*
10225 * "getwinvar()" function
10226 */
10227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010229 typval_T *argvars;
10230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231{
10232 win_T *win, *oldcurwin;
10233 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010234 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 varname = get_tv_string_chk(&argvars[1]);
10238 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240 rettv->v_type = VAR_STRING;
10241 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242
10243 if (win != NULL && varname != NULL)
10244 {
10245 if (*varname == '&') /* window-local-option */
10246 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010247 /* Set curwin to be our win, temporarily. Also set curbuf, so
10248 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 oldcurwin = curwin;
10250 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010251 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010253 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254
10255 /* restore previous notion of curwin */
10256 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010257 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 }
10259 else
10260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 if (*varname == NUL)
10262 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10263 * scope prefix before the NUL byte is required by
10264 * find_var_in_ht(). */
10265 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010267 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 }
10271 }
10272
10273 --emsg_off;
10274}
10275
10276/*
10277 * "glob()" function
10278 */
10279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010281 typval_T *argvars;
10282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283{
10284 expand_T xpc;
10285
10286 ExpandInit(&xpc);
10287 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010288 rettv->v_type = VAR_STRING;
10289 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10291 ExpandCleanup(&xpc);
10292}
10293
10294/*
10295 * "globpath()" function
10296 */
10297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010299 typval_T *argvars;
10300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
10302 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010303 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 if (file == NULL)
10307 rettv->vval.v_string = NULL;
10308 else
10309 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310}
10311
10312/*
10313 * "has()" function
10314 */
10315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010317 typval_T *argvars;
10318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319{
10320 int i;
10321 char_u *name;
10322 int n = FALSE;
10323 static char *(has_list[]) =
10324 {
10325#ifdef AMIGA
10326 "amiga",
10327# ifdef FEAT_ARP
10328 "arp",
10329# endif
10330#endif
10331#ifdef __BEOS__
10332 "beos",
10333#endif
10334#ifdef MSDOS
10335# ifdef DJGPP
10336 "dos32",
10337# else
10338 "dos16",
10339# endif
10340#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010341#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 "mac",
10343#endif
10344#if defined(MACOS_X_UNIX)
10345 "macunix",
10346#endif
10347#ifdef OS2
10348 "os2",
10349#endif
10350#ifdef __QNX__
10351 "qnx",
10352#endif
10353#ifdef RISCOS
10354 "riscos",
10355#endif
10356#ifdef UNIX
10357 "unix",
10358#endif
10359#ifdef VMS
10360 "vms",
10361#endif
10362#ifdef WIN16
10363 "win16",
10364#endif
10365#ifdef WIN32
10366 "win32",
10367#endif
10368#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10369 "win32unix",
10370#endif
10371#ifdef WIN64
10372 "win64",
10373#endif
10374#ifdef EBCDIC
10375 "ebcdic",
10376#endif
10377#ifndef CASE_INSENSITIVE_FILENAME
10378 "fname_case",
10379#endif
10380#ifdef FEAT_ARABIC
10381 "arabic",
10382#endif
10383#ifdef FEAT_AUTOCMD
10384 "autocmd",
10385#endif
10386#ifdef FEAT_BEVAL
10387 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010388# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10389 "balloon_multiline",
10390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391#endif
10392#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10393 "builtin_terms",
10394# ifdef ALL_BUILTIN_TCAPS
10395 "all_builtin_terms",
10396# endif
10397#endif
10398#ifdef FEAT_BYTEOFF
10399 "byte_offset",
10400#endif
10401#ifdef FEAT_CINDENT
10402 "cindent",
10403#endif
10404#ifdef FEAT_CLIENTSERVER
10405 "clientserver",
10406#endif
10407#ifdef FEAT_CLIPBOARD
10408 "clipboard",
10409#endif
10410#ifdef FEAT_CMDL_COMPL
10411 "cmdline_compl",
10412#endif
10413#ifdef FEAT_CMDHIST
10414 "cmdline_hist",
10415#endif
10416#ifdef FEAT_COMMENTS
10417 "comments",
10418#endif
10419#ifdef FEAT_CRYPT
10420 "cryptv",
10421#endif
10422#ifdef FEAT_CSCOPE
10423 "cscope",
10424#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010425#ifdef CURSOR_SHAPE
10426 "cursorshape",
10427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428#ifdef DEBUG
10429 "debug",
10430#endif
10431#ifdef FEAT_CON_DIALOG
10432 "dialog_con",
10433#endif
10434#ifdef FEAT_GUI_DIALOG
10435 "dialog_gui",
10436#endif
10437#ifdef FEAT_DIFF
10438 "diff",
10439#endif
10440#ifdef FEAT_DIGRAPHS
10441 "digraphs",
10442#endif
10443#ifdef FEAT_DND
10444 "dnd",
10445#endif
10446#ifdef FEAT_EMACS_TAGS
10447 "emacs_tags",
10448#endif
10449 "eval", /* always present, of course! */
10450#ifdef FEAT_EX_EXTRA
10451 "ex_extra",
10452#endif
10453#ifdef FEAT_SEARCH_EXTRA
10454 "extra_search",
10455#endif
10456#ifdef FEAT_FKMAP
10457 "farsi",
10458#endif
10459#ifdef FEAT_SEARCHPATH
10460 "file_in_path",
10461#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010462#if defined(UNIX) && !defined(USE_SYSTEM)
10463 "filterpipe",
10464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465#ifdef FEAT_FIND_ID
10466 "find_in_path",
10467#endif
10468#ifdef FEAT_FOLDING
10469 "folding",
10470#endif
10471#ifdef FEAT_FOOTER
10472 "footer",
10473#endif
10474#if !defined(USE_SYSTEM) && defined(UNIX)
10475 "fork",
10476#endif
10477#ifdef FEAT_GETTEXT
10478 "gettext",
10479#endif
10480#ifdef FEAT_GUI
10481 "gui",
10482#endif
10483#ifdef FEAT_GUI_ATHENA
10484# ifdef FEAT_GUI_NEXTAW
10485 "gui_neXtaw",
10486# else
10487 "gui_athena",
10488# endif
10489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#ifdef FEAT_GUI_GTK
10491 "gui_gtk",
10492# ifdef HAVE_GTK2
10493 "gui_gtk2",
10494# endif
10495#endif
10496#ifdef FEAT_GUI_MAC
10497 "gui_mac",
10498#endif
10499#ifdef FEAT_GUI_MOTIF
10500 "gui_motif",
10501#endif
10502#ifdef FEAT_GUI_PHOTON
10503 "gui_photon",
10504#endif
10505#ifdef FEAT_GUI_W16
10506 "gui_win16",
10507#endif
10508#ifdef FEAT_GUI_W32
10509 "gui_win32",
10510#endif
10511#ifdef FEAT_HANGULIN
10512 "hangul_input",
10513#endif
10514#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10515 "iconv",
10516#endif
10517#ifdef FEAT_INS_EXPAND
10518 "insert_expand",
10519#endif
10520#ifdef FEAT_JUMPLIST
10521 "jumplist",
10522#endif
10523#ifdef FEAT_KEYMAP
10524 "keymap",
10525#endif
10526#ifdef FEAT_LANGMAP
10527 "langmap",
10528#endif
10529#ifdef FEAT_LIBCALL
10530 "libcall",
10531#endif
10532#ifdef FEAT_LINEBREAK
10533 "linebreak",
10534#endif
10535#ifdef FEAT_LISP
10536 "lispindent",
10537#endif
10538#ifdef FEAT_LISTCMDS
10539 "listcmds",
10540#endif
10541#ifdef FEAT_LOCALMAP
10542 "localmap",
10543#endif
10544#ifdef FEAT_MENU
10545 "menu",
10546#endif
10547#ifdef FEAT_SESSION
10548 "mksession",
10549#endif
10550#ifdef FEAT_MODIFY_FNAME
10551 "modify_fname",
10552#endif
10553#ifdef FEAT_MOUSE
10554 "mouse",
10555#endif
10556#ifdef FEAT_MOUSESHAPE
10557 "mouseshape",
10558#endif
10559#if defined(UNIX) || defined(VMS)
10560# ifdef FEAT_MOUSE_DEC
10561 "mouse_dec",
10562# endif
10563# ifdef FEAT_MOUSE_GPM
10564 "mouse_gpm",
10565# endif
10566# ifdef FEAT_MOUSE_JSB
10567 "mouse_jsbterm",
10568# endif
10569# ifdef FEAT_MOUSE_NET
10570 "mouse_netterm",
10571# endif
10572# ifdef FEAT_MOUSE_PTERM
10573 "mouse_pterm",
10574# endif
10575# ifdef FEAT_MOUSE_XTERM
10576 "mouse_xterm",
10577# endif
10578#endif
10579#ifdef FEAT_MBYTE
10580 "multi_byte",
10581#endif
10582#ifdef FEAT_MBYTE_IME
10583 "multi_byte_ime",
10584#endif
10585#ifdef FEAT_MULTI_LANG
10586 "multi_lang",
10587#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010588#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010589#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010590 "mzscheme",
10591#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593#ifdef FEAT_OLE
10594 "ole",
10595#endif
10596#ifdef FEAT_OSFILETYPE
10597 "osfiletype",
10598#endif
10599#ifdef FEAT_PATH_EXTRA
10600 "path_extra",
10601#endif
10602#ifdef FEAT_PERL
10603#ifndef DYNAMIC_PERL
10604 "perl",
10605#endif
10606#endif
10607#ifdef FEAT_PYTHON
10608#ifndef DYNAMIC_PYTHON
10609 "python",
10610#endif
10611#endif
10612#ifdef FEAT_POSTSCRIPT
10613 "postscript",
10614#endif
10615#ifdef FEAT_PRINTER
10616 "printer",
10617#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010618#ifdef FEAT_PROFILE
10619 "profile",
10620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621#ifdef FEAT_QUICKFIX
10622 "quickfix",
10623#endif
10624#ifdef FEAT_RIGHTLEFT
10625 "rightleft",
10626#endif
10627#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10628 "ruby",
10629#endif
10630#ifdef FEAT_SCROLLBIND
10631 "scrollbind",
10632#endif
10633#ifdef FEAT_CMDL_INFO
10634 "showcmd",
10635 "cmdline_info",
10636#endif
10637#ifdef FEAT_SIGNS
10638 "signs",
10639#endif
10640#ifdef FEAT_SMARTINDENT
10641 "smartindent",
10642#endif
10643#ifdef FEAT_SNIFF
10644 "sniff",
10645#endif
10646#ifdef FEAT_STL_OPT
10647 "statusline",
10648#endif
10649#ifdef FEAT_SUN_WORKSHOP
10650 "sun_workshop",
10651#endif
10652#ifdef FEAT_NETBEANS_INTG
10653 "netbeans_intg",
10654#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010655#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010656 "spell",
10657#endif
10658#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 "syntax",
10660#endif
10661#if defined(USE_SYSTEM) || !defined(UNIX)
10662 "system",
10663#endif
10664#ifdef FEAT_TAG_BINS
10665 "tag_binary",
10666#endif
10667#ifdef FEAT_TAG_OLDSTATIC
10668 "tag_old_static",
10669#endif
10670#ifdef FEAT_TAG_ANYWHITE
10671 "tag_any_white",
10672#endif
10673#ifdef FEAT_TCL
10674# ifndef DYNAMIC_TCL
10675 "tcl",
10676# endif
10677#endif
10678#ifdef TERMINFO
10679 "terminfo",
10680#endif
10681#ifdef FEAT_TERMRESPONSE
10682 "termresponse",
10683#endif
10684#ifdef FEAT_TEXTOBJ
10685 "textobjects",
10686#endif
10687#ifdef HAVE_TGETENT
10688 "tgetent",
10689#endif
10690#ifdef FEAT_TITLE
10691 "title",
10692#endif
10693#ifdef FEAT_TOOLBAR
10694 "toolbar",
10695#endif
10696#ifdef FEAT_USR_CMDS
10697 "user-commands", /* was accidentally included in 5.4 */
10698 "user_commands",
10699#endif
10700#ifdef FEAT_VIMINFO
10701 "viminfo",
10702#endif
10703#ifdef FEAT_VERTSPLIT
10704 "vertsplit",
10705#endif
10706#ifdef FEAT_VIRTUALEDIT
10707 "virtualedit",
10708#endif
10709#ifdef FEAT_VISUAL
10710 "visual",
10711#endif
10712#ifdef FEAT_VISUALEXTRA
10713 "visualextra",
10714#endif
10715#ifdef FEAT_VREPLACE
10716 "vreplace",
10717#endif
10718#ifdef FEAT_WILDIGN
10719 "wildignore",
10720#endif
10721#ifdef FEAT_WILDMENU
10722 "wildmenu",
10723#endif
10724#ifdef FEAT_WINDOWS
10725 "windows",
10726#endif
10727#ifdef FEAT_WAK
10728 "winaltkeys",
10729#endif
10730#ifdef FEAT_WRITEBACKUP
10731 "writebackup",
10732#endif
10733#ifdef FEAT_XIM
10734 "xim",
10735#endif
10736#ifdef FEAT_XFONTSET
10737 "xfontset",
10738#endif
10739#ifdef USE_XSMP
10740 "xsmp",
10741#endif
10742#ifdef USE_XSMP_INTERACT
10743 "xsmp_interact",
10744#endif
10745#ifdef FEAT_XCLIPBOARD
10746 "xterm_clipboard",
10747#endif
10748#ifdef FEAT_XTERM_SAVE
10749 "xterm_save",
10750#endif
10751#if defined(UNIX) && defined(FEAT_X11)
10752 "X11",
10753#endif
10754 NULL
10755 };
10756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 for (i = 0; has_list[i] != NULL; ++i)
10759 if (STRICMP(name, has_list[i]) == 0)
10760 {
10761 n = TRUE;
10762 break;
10763 }
10764
10765 if (n == FALSE)
10766 {
10767 if (STRNICMP(name, "patch", 5) == 0)
10768 n = has_patch(atoi((char *)name + 5));
10769 else if (STRICMP(name, "vim_starting") == 0)
10770 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010771#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10772 else if (STRICMP(name, "balloon_multiline") == 0)
10773 n = multiline_balloon_available();
10774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775#ifdef DYNAMIC_TCL
10776 else if (STRICMP(name, "tcl") == 0)
10777 n = tcl_enabled(FALSE);
10778#endif
10779#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10780 else if (STRICMP(name, "iconv") == 0)
10781 n = iconv_enabled(FALSE);
10782#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010783#ifdef DYNAMIC_MZSCHEME
10784 else if (STRICMP(name, "mzscheme") == 0)
10785 n = mzscheme_enabled(FALSE);
10786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787#ifdef DYNAMIC_RUBY
10788 else if (STRICMP(name, "ruby") == 0)
10789 n = ruby_enabled(FALSE);
10790#endif
10791#ifdef DYNAMIC_PYTHON
10792 else if (STRICMP(name, "python") == 0)
10793 n = python_enabled(FALSE);
10794#endif
10795#ifdef DYNAMIC_PERL
10796 else if (STRICMP(name, "perl") == 0)
10797 n = perl_enabled(FALSE);
10798#endif
10799#ifdef FEAT_GUI
10800 else if (STRICMP(name, "gui_running") == 0)
10801 n = (gui.in_use || gui.starting);
10802# ifdef FEAT_GUI_W32
10803 else if (STRICMP(name, "gui_win32s") == 0)
10804 n = gui_is_win32s();
10805# endif
10806# ifdef FEAT_BROWSE
10807 else if (STRICMP(name, "browse") == 0)
10808 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10809# endif
10810#endif
10811#ifdef FEAT_SYN_HL
10812 else if (STRICMP(name, "syntax_items") == 0)
10813 n = syntax_present(curbuf);
10814#endif
10815#if defined(WIN3264)
10816 else if (STRICMP(name, "win95") == 0)
10817 n = mch_windows95();
10818#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010819#ifdef FEAT_NETBEANS_INTG
10820 else if (STRICMP(name, "netbeans_enabled") == 0)
10821 n = usingNetbeans;
10822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 }
10824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826}
10827
10828/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010829 * "has_key()" function
10830 */
10831 static void
10832f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 typval_T *argvars;
10834 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835{
10836 rettv->vval.v_number = 0;
10837 if (argvars[0].v_type != VAR_DICT)
10838 {
10839 EMSG(_(e_dictreq));
10840 return;
10841 }
10842 if (argvars[0].vval.v_dict == NULL)
10843 return;
10844
10845 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010846 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010847}
10848
10849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 * "hasmapto()" function
10851 */
10852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *argvars;
10855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856{
10857 char_u *name;
10858 char_u *mode;
10859 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010860 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010863 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 mode = (char_u *)"nvo";
10865 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010868 if (argvars[2].v_type != VAR_UNKNOWN)
10869 abbr = get_tv_number(&argvars[2]);
10870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871
Bram Moolenaar2c932302006-03-18 21:42:09 +000010872 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876}
10877
10878/*
10879 * "histadd()" function
10880 */
10881/*ARGSUSED*/
10882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *argvars;
10885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
10887#ifdef FEAT_CMDHIST
10888 int histype;
10889 char_u *str;
10890 char_u buf[NUMBUFLEN];
10891#endif
10892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 if (check_restricted() || check_secure())
10895 return;
10896#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10898 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 if (histype >= 0)
10900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010901 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 if (*str != NUL)
10903 {
10904 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 return;
10907 }
10908 }
10909#endif
10910}
10911
10912/*
10913 * "histdel()" function
10914 */
10915/*ARGSUSED*/
10916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010918 typval_T *argvars;
10919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921#ifdef FEAT_CMDHIST
10922 int n;
10923 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10927 if (str == NULL)
10928 n = 0;
10929 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010931 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010932 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 else
10937 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 get_tv_string_buf(&argvars[1], buf));
10940 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943#endif
10944}
10945
10946/*
10947 * "histget()" function
10948 */
10949/*ARGSUSED*/
10950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 typval_T *argvars;
10953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954{
10955#ifdef FEAT_CMDHIST
10956 int type;
10957 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10961 if (str == NULL)
10962 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964 {
10965 type = get_histtype(str);
10966 if (argvars[1].v_type == VAR_UNKNOWN)
10967 idx = get_history_idx(type);
10968 else
10969 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10970 /* -1 on type error */
10971 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977}
10978
10979/*
10980 * "histnr()" function
10981 */
10982/*ARGSUSED*/
10983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 typval_T *argvars;
10986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987{
10988 int i;
10989
10990#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 char_u *history = get_tv_string_chk(&argvars[0]);
10992
10993 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 if (i >= HIST_CMD && i < HIST_COUNT)
10995 i = get_history_idx(i);
10996 else
10997#endif
10998 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000}
11001
11002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 * "highlightID(name)" function
11004 */
11005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011007 typval_T *argvars;
11008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011}
11012
11013/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011014 * "highlight_exists()" function
11015 */
11016 static void
11017f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *argvars;
11019 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011020{
11021 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11022}
11023
11024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 * "hostname()" function
11026 */
11027/*ARGSUSED*/
11028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 typval_T *argvars;
11031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
11033 char_u hostname[256];
11034
11035 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->v_type = VAR_STRING;
11037 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038}
11039
11040/*
11041 * iconv() function
11042 */
11043/*ARGSUSED*/
11044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011046 typval_T *argvars;
11047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048{
11049#ifdef FEAT_MBYTE
11050 char_u buf1[NUMBUFLEN];
11051 char_u buf2[NUMBUFLEN];
11052 char_u *from, *to, *str;
11053 vimconv_T vimconv;
11054#endif
11055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 rettv->v_type = VAR_STRING;
11057 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058
11059#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 str = get_tv_string(&argvars[0]);
11061 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11062 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 vimconv.vc_type = CONV_NONE;
11064 convert_setup(&vimconv, from, to);
11065
11066 /* If the encodings are equal, no conversion needed. */
11067 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071
11072 convert_setup(&vimconv, NULL, NULL);
11073 vim_free(from);
11074 vim_free(to);
11075#endif
11076}
11077
11078/*
11079 * "indent()" function
11080 */
11081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011083 typval_T *argvars;
11084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085{
11086 linenr_T lnum;
11087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093}
11094
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011095/*
11096 * "index()" function
11097 */
11098 static void
11099f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011100 typval_T *argvars;
11101 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011102{
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 list_T *l;
11104 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011105 long idx = 0;
11106 int ic = FALSE;
11107
11108 rettv->vval.v_number = -1;
11109 if (argvars[0].v_type != VAR_LIST)
11110 {
11111 EMSG(_(e_listreq));
11112 return;
11113 }
11114 l = argvars[0].vval.v_list;
11115 if (l != NULL)
11116 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011117 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011118 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 int error = FALSE;
11121
Bram Moolenaar758711c2005-02-02 23:11:38 +000011122 /* Start at specified item. Use the cached index that list_find()
11123 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011125 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011126 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 ic = get_tv_number_chk(&argvars[3], &error);
11128 if (error)
11129 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011130 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011131
Bram Moolenaar758711c2005-02-02 23:11:38 +000011132 for ( ; item != NULL; item = item->li_next, ++idx)
11133 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011134 {
11135 rettv->vval.v_number = idx;
11136 break;
11137 }
11138 }
11139}
11140
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141static int inputsecret_flag = 0;
11142
11143/*
11144 * "input()" function
11145 * Also handles inputsecret() when inputsecret is set.
11146 */
11147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011149 typval_T *argvars;
11150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 char_u *p = NULL;
11154 int c;
11155 char_u buf[NUMBUFLEN];
11156 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011158 int xp_type = EXPAND_NOTHING;
11159 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
11163#ifdef NO_CONSOLE_INPUT
11164 /* While starting up, there is no place to enter text. */
11165 if (no_console_input())
11166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 return;
11169 }
11170#endif
11171
11172 cmd_silent = FALSE; /* Want to see the prompt. */
11173 if (prompt != NULL)
11174 {
11175 /* Only the part of the message after the last NL is considered as
11176 * prompt for the command line */
11177 p = vim_strrchr(prompt, '\n');
11178 if (p == NULL)
11179 p = prompt;
11180 else
11181 {
11182 ++p;
11183 c = *p;
11184 *p = NUL;
11185 msg_start();
11186 msg_clr_eos();
11187 msg_puts_attr(prompt, echo_attr);
11188 msg_didout = FALSE;
11189 msg_starthere();
11190 *p = c;
11191 }
11192 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 if (argvars[1].v_type != VAR_UNKNOWN)
11195 {
11196 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11197 if (defstr != NULL)
11198 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199
Bram Moolenaar4463f292005-09-25 22:20:24 +000011200 if (argvars[2].v_type != VAR_UNKNOWN)
11201 {
11202 char_u *xp_name;
11203 int xp_namelen;
11204 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011205
Bram Moolenaar4463f292005-09-25 22:20:24 +000011206 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011207
Bram Moolenaar4463f292005-09-25 22:20:24 +000011208 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11209 if (xp_name == NULL)
11210 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011211
Bram Moolenaar4463f292005-09-25 22:20:24 +000011212 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011213
Bram Moolenaar4463f292005-09-25 22:20:24 +000011214 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11215 &xp_arg) == FAIL)
11216 return;
11217 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011218 }
11219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 if (defstr != NULL)
11221 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011222 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11223 xp_type, xp_arg);
11224
11225 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 /* since the user typed this, no need to wait for return */
11228 need_wait_return = FALSE;
11229 msg_didout = FALSE;
11230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 cmd_silent = cmd_silent_save;
11232}
11233
11234/*
11235 * "inputdialog()" function
11236 */
11237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011238f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011239 typval_T *argvars;
11240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241{
11242#if defined(FEAT_GUI_TEXTDIALOG)
11243 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11244 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11245 {
11246 char_u *message;
11247 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011248 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 message = get_tv_string_chk(&argvars[0]);
11251 if (argvars[1].v_type != VAR_UNKNOWN
11252 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011253 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 else
11255 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256 if (message != NULL && defstr != NULL
11257 && do_dialog(VIM_QUESTION, NULL, message,
11258 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 else
11261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011262 if (message != NULL && defstr != NULL
11263 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011264 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 rettv->vval.v_string = vim_strsave(
11266 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 }
11272 else
11273#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275}
11276
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011277/*
11278 * "inputlist()" function
11279 */
11280 static void
11281f_inputlist(argvars, rettv)
11282 typval_T *argvars;
11283 typval_T *rettv;
11284{
11285 listitem_T *li;
11286 int selected;
11287 int mouse_used;
11288
11289 rettv->vval.v_number = 0;
11290#ifdef NO_CONSOLE_INPUT
11291 /* While starting up, there is no place to enter text. */
11292 if (no_console_input())
11293 return;
11294#endif
11295 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11296 {
11297 EMSG2(_(e_listarg), "inputlist()");
11298 return;
11299 }
11300
11301 msg_start();
11302 lines_left = Rows; /* avoid more prompt */
11303 msg_scroll = TRUE;
11304 msg_clr_eos();
11305
11306 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11307 {
11308 msg_puts(get_tv_string(&li->li_tv));
11309 msg_putchar('\n');
11310 }
11311
11312 /* Ask for choice. */
11313 selected = prompt_for_number(&mouse_used);
11314 if (mouse_used)
11315 selected -= lines_left;
11316
11317 rettv->vval.v_number = selected;
11318}
11319
11320
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11322
11323/*
11324 * "inputrestore()" function
11325 */
11326/*ARGSUSED*/
11327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 typval_T *argvars;
11330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
11332 if (ga_userinput.ga_len > 0)
11333 {
11334 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11336 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 }
11339 else if (p_verbose > 1)
11340 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011341 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 }
11344}
11345
11346/*
11347 * "inputsave()" function
11348 */
11349/*ARGSUSED*/
11350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011352 typval_T *argvars;
11353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
11355 /* Add an entry to the stack of typehead storage. */
11356 if (ga_grow(&ga_userinput, 1) == OK)
11357 {
11358 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11359 + ga_userinput.ga_len);
11360 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 }
11363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365}
11366
11367/*
11368 * "inputsecret()" function
11369 */
11370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011372 typval_T *argvars;
11373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
11375 ++cmdline_star;
11376 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 --cmdline_star;
11379 --inputsecret_flag;
11380}
11381
11382/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011383 * "insert()" function
11384 */
11385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *argvars;
11388 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011389{
11390 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 listitem_T *item;
11392 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011394
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011395 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011396 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011397 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011398 else if ((l = argvars[0].vval.v_list) != NULL
11399 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011400 {
11401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 before = get_tv_number_chk(&argvars[2], &error);
11403 if (error)
11404 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011405
Bram Moolenaar758711c2005-02-02 23:11:38 +000011406 if (before == l->lv_len)
11407 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011408 else
11409 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011410 item = list_find(l, before);
11411 if (item == NULL)
11412 {
11413 EMSGN(_(e_listidx), before);
11414 l = NULL;
11415 }
11416 }
11417 if (l != NULL)
11418 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011419 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011420 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011421 }
11422 }
11423}
11424
11425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 * "isdirectory()" function
11427 */
11428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011430 typval_T *argvars;
11431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434}
11435
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011436/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011437 * "islocked()" function
11438 */
11439 static void
11440f_islocked(argvars, rettv)
11441 typval_T *argvars;
11442 typval_T *rettv;
11443{
11444 lval_T lv;
11445 char_u *end;
11446 dictitem_T *di;
11447
11448 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011449 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11450 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011451 if (end != NULL && lv.ll_name != NULL)
11452 {
11453 if (*end != NUL)
11454 EMSG(_(e_trailing));
11455 else
11456 {
11457 if (lv.ll_tv == NULL)
11458 {
11459 if (check_changedtick(lv.ll_name))
11460 rettv->vval.v_number = 1; /* always locked */
11461 else
11462 {
11463 di = find_var(lv.ll_name, NULL);
11464 if (di != NULL)
11465 {
11466 /* Consider a variable locked when:
11467 * 1. the variable itself is locked
11468 * 2. the value of the variable is locked.
11469 * 3. the List or Dict value is locked.
11470 */
11471 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11472 || tv_islocked(&di->di_tv));
11473 }
11474 }
11475 }
11476 else if (lv.ll_range)
11477 EMSG(_("E745: Range not allowed"));
11478 else if (lv.ll_newkey != NULL)
11479 EMSG2(_(e_dictkey), lv.ll_newkey);
11480 else if (lv.ll_list != NULL)
11481 /* List item. */
11482 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11483 else
11484 /* Dictionary item. */
11485 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11486 }
11487 }
11488
11489 clear_lval(&lv);
11490}
11491
Bram Moolenaar33570922005-01-25 22:26:29 +000011492static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011493
11494/*
11495 * Turn a dict into a list:
11496 * "what" == 0: list of keys
11497 * "what" == 1: list of values
11498 * "what" == 2: list of items
11499 */
11500 static void
11501dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011504 int what;
11505{
Bram Moolenaar33570922005-01-25 22:26:29 +000011506 list_T *l2;
11507 dictitem_T *di;
11508 hashitem_T *hi;
11509 listitem_T *li;
11510 listitem_T *li2;
11511 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011512 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011513
11514 rettv->vval.v_number = 0;
11515 if (argvars[0].v_type != VAR_DICT)
11516 {
11517 EMSG(_(e_dictreq));
11518 return;
11519 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011520 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011521 return;
11522
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011523 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011524 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011525
Bram Moolenaar33570922005-01-25 22:26:29 +000011526 todo = d->dv_hashtab.ht_used;
11527 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011528 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011529 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011530 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011531 --todo;
11532 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011533
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011534 li = listitem_alloc();
11535 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011536 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011537 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011538
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011539 if (what == 0)
11540 {
11541 /* keys() */
11542 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011543 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011544 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11545 }
11546 else if (what == 1)
11547 {
11548 /* values() */
11549 copy_tv(&di->di_tv, &li->li_tv);
11550 }
11551 else
11552 {
11553 /* items() */
11554 l2 = list_alloc();
11555 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011556 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011557 li->li_tv.vval.v_list = l2;
11558 if (l2 == NULL)
11559 break;
11560 ++l2->lv_refcount;
11561
11562 li2 = listitem_alloc();
11563 if (li2 == NULL)
11564 break;
11565 list_append(l2, li2);
11566 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011567 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011568 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11569
11570 li2 = listitem_alloc();
11571 if (li2 == NULL)
11572 break;
11573 list_append(l2, li2);
11574 copy_tv(&di->di_tv, &li2->li_tv);
11575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011576 }
11577 }
11578}
11579
11580/*
11581 * "items(dict)" function
11582 */
11583 static void
11584f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011587{
11588 dict_list(argvars, rettv, 2);
11589}
11590
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011592 * "join()" function
11593 */
11594 static void
11595f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 typval_T *argvars;
11597 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011598{
11599 garray_T ga;
11600 char_u *sep;
11601
11602 rettv->vval.v_number = 0;
11603 if (argvars[0].v_type != VAR_LIST)
11604 {
11605 EMSG(_(e_listreq));
11606 return;
11607 }
11608 if (argvars[0].vval.v_list == NULL)
11609 return;
11610 if (argvars[1].v_type == VAR_UNKNOWN)
11611 sep = (char_u *)" ";
11612 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011613 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011614
11615 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011616
11617 if (sep != NULL)
11618 {
11619 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011620 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621 ga_append(&ga, NUL);
11622 rettv->vval.v_string = (char_u *)ga.ga_data;
11623 }
11624 else
11625 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011626}
11627
11628/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011629 * "keys()" function
11630 */
11631 static void
11632f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 typval_T *argvars;
11634 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011635{
11636 dict_list(argvars, rettv, 0);
11637}
11638
11639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 * "last_buffer_nr()" function.
11641 */
11642/*ARGSUSED*/
11643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011645 typval_T *argvars;
11646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647{
11648 int n = 0;
11649 buf_T *buf;
11650
11651 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11652 if (n < buf->b_fnum)
11653 n = buf->b_fnum;
11654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011656}
11657
11658/*
11659 * "len()" function
11660 */
11661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011662f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011663 typval_T *argvars;
11664 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011665{
11666 switch (argvars[0].v_type)
11667 {
11668 case VAR_STRING:
11669 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 rettv->vval.v_number = (varnumber_T)STRLEN(
11671 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011672 break;
11673 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011675 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011676 case VAR_DICT:
11677 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11678 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011679 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011680 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011681 break;
11682 }
11683}
11684
Bram Moolenaar33570922005-01-25 22:26:29 +000011685static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011686
11687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011689 typval_T *argvars;
11690 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011691 int type;
11692{
11693#ifdef FEAT_LIBCALL
11694 char_u *string_in;
11695 char_u **string_result;
11696 int nr_result;
11697#endif
11698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011700 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011704
11705 if (check_restricted() || check_secure())
11706 return;
11707
11708#ifdef FEAT_LIBCALL
11709 /* The first two args must be strings, otherwise its meaningless */
11710 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11711 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011712 string_in = NULL;
11713 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011714 string_in = argvars[2].vval.v_string;
11715 if (type == VAR_NUMBER)
11716 string_result = NULL;
11717 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011718 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011719 if (mch_libcall(argvars[0].vval.v_string,
11720 argvars[1].vval.v_string,
11721 string_in,
11722 argvars[2].vval.v_number,
11723 string_result,
11724 &nr_result) == OK
11725 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011727 }
11728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729}
11730
11731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011732 * "libcall()" function
11733 */
11734 static void
11735f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011736 typval_T *argvars;
11737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011738{
11739 libcall_common(argvars, rettv, VAR_STRING);
11740}
11741
11742/*
11743 * "libcallnr()" function
11744 */
11745 static void
11746f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011747 typval_T *argvars;
11748 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011749{
11750 libcall_common(argvars, rettv, VAR_NUMBER);
11751}
11752
11753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 * "line(string)" function
11755 */
11756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011758 typval_T *argvars;
11759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760{
11761 linenr_T lnum = 0;
11762 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011763 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011765 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 if (fp != NULL)
11767 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011768 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769}
11770
11771/*
11772 * "line2byte(lnum)" function
11773 */
11774/*ARGSUSED*/
11775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011777 typval_T *argvars;
11778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779{
11780#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782#else
11783 linenr_T lnum;
11784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011785 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011789 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11790 if (rettv->vval.v_number >= 0)
11791 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792#endif
11793}
11794
11795/*
11796 * "lispindent(lnum)" function
11797 */
11798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011800 typval_T *argvars;
11801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802{
11803#ifdef FEAT_LISP
11804 pos_T pos;
11805 linenr_T lnum;
11806
11807 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11810 {
11811 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 curwin->w_cursor = pos;
11814 }
11815 else
11816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818}
11819
11820/*
11821 * "localtime()" function
11822 */
11823/*ARGSUSED*/
11824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011826 typval_T *argvars;
11827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830}
11831
Bram Moolenaar33570922005-01-25 22:26:29 +000011832static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833
11834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011836 typval_T *argvars;
11837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 int exact;
11839{
11840 char_u *keys;
11841 char_u *which;
11842 char_u buf[NUMBUFLEN];
11843 char_u *keys_buf = NULL;
11844 char_u *rhs;
11845 int mode;
11846 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011847 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848
11849 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850 rettv->v_type = VAR_STRING;
11851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 if (*keys == NUL)
11855 return;
11856
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011857 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011860 if (argvars[2].v_type != VAR_UNKNOWN)
11861 abbr = get_tv_number(&argvars[2]);
11862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 else
11864 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 if (which == NULL)
11866 return;
11867
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 mode = get_map_mode(&which, 0);
11869
11870 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011871 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 vim_free(keys_buf);
11873 if (rhs != NULL)
11874 {
11875 ga_init(&ga);
11876 ga.ga_itemsize = 1;
11877 ga.ga_growsize = 40;
11878
11879 while (*rhs != NUL)
11880 ga_concat(&ga, str2special(&rhs, FALSE));
11881
11882 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011883 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 }
11885}
11886
11887/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011888 * "map()" function
11889 */
11890 static void
11891f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011892 typval_T *argvars;
11893 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011894{
11895 filter_map(argvars, rettv, TRUE);
11896}
11897
11898/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011899 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900 */
11901 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011903 typval_T *argvars;
11904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011906 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907}
11908
11909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 */
11912 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011913f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011914 typval_T *argvars;
11915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011917 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918}
11919
Bram Moolenaar33570922005-01-25 22:26:29 +000011920static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921
11922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011923find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011924 typval_T *argvars;
11925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 int type;
11927{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011928 char_u *str = NULL;
11929 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 char_u *pat;
11931 regmatch_T regmatch;
11932 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011933 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 char_u *save_cpo;
11935 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011936 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011937 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011938 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011939 list_T *l = NULL;
11940 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011941 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011942 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943
11944 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11945 save_cpo = p_cpo;
11946 p_cpo = (char_u *)"";
11947
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011948 rettv->vval.v_number = -1;
11949 if (type == 3)
11950 {
11951 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011952 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011953 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011954 }
11955 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011957 rettv->v_type = VAR_STRING;
11958 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011961 if (argvars[0].v_type == VAR_LIST)
11962 {
11963 if ((l = argvars[0].vval.v_list) == NULL)
11964 goto theend;
11965 li = l->lv_first;
11966 }
11967 else
11968 expr = str = get_tv_string(&argvars[0]);
11969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11971 if (pat == NULL)
11972 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011973
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 int error = FALSE;
11977
11978 start = get_tv_number_chk(&argvars[2], &error);
11979 if (error)
11980 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011981 if (l != NULL)
11982 {
11983 li = list_find(l, start);
11984 if (li == NULL)
11985 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011986 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011987 }
11988 else
11989 {
11990 if (start < 0)
11991 start = 0;
11992 if (start > (long)STRLEN(str))
11993 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011994 /* When "count" argument is there ignore matches before "start",
11995 * otherwise skip part of the string. Differs when pattern is "^"
11996 * or "\<". */
11997 if (argvars[3].v_type != VAR_UNKNOWN)
11998 startcol = start;
11999 else
12000 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012001 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012002
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012003 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012004 nth = get_tv_number_chk(&argvars[3], &error);
12005 if (error)
12006 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 }
12008
12009 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12010 if (regmatch.regprog != NULL)
12011 {
12012 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012013
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012014 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012015 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012016 if (l != NULL)
12017 {
12018 if (li == NULL)
12019 {
12020 match = FALSE;
12021 break;
12022 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012023 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012024 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012025 if (str == NULL)
12026 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012027 }
12028
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012029 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012030
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012031 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012032 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012033 if (l == NULL && !match)
12034 break;
12035
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012036 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012037 if (l != NULL)
12038 {
12039 li = li->li_next;
12040 ++idx;
12041 }
12042 else
12043 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012044#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012045 startcol = regmatch.startp[0]
12046 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012047#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012048 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012049#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012050 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012051 }
12052
12053 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012055 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012056 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012057 int i;
12058
12059 /* return list with matched string and submatches */
12060 for (i = 0; i < NSUBEXP; ++i)
12061 {
12062 if (regmatch.endp[i] == NULL)
12063 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012064 if (list_append_string(rettv->vval.v_list,
12065 regmatch.startp[i],
12066 (int)(regmatch.endp[i] - regmatch.startp[i]))
12067 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012068 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012069 }
12070 }
12071 else if (type == 2)
12072 {
12073 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012074 if (l != NULL)
12075 copy_tv(&li->li_tv, rettv);
12076 else
12077 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012079 }
12080 else if (l != NULL)
12081 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 else
12083 {
12084 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 (varnumber_T)(regmatch.startp[0] - str);
12087 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012088 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012090 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 }
12092 }
12093 vim_free(regmatch.regprog);
12094 }
12095
12096theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012097 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 p_cpo = save_cpo;
12099}
12100
12101/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012102 * "match()" function
12103 */
12104 static void
12105f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012108{
12109 find_some_match(argvars, rettv, 1);
12110}
12111
12112/*
12113 * "matchend()" function
12114 */
12115 static void
12116f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012117 typval_T *argvars;
12118 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012119{
12120 find_some_match(argvars, rettv, 0);
12121}
12122
12123/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012124 * "matchlist()" function
12125 */
12126 static void
12127f_matchlist(argvars, rettv)
12128 typval_T *argvars;
12129 typval_T *rettv;
12130{
12131 find_some_match(argvars, rettv, 3);
12132}
12133
12134/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135 * "matchstr()" function
12136 */
12137 static void
12138f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012139 typval_T *argvars;
12140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141{
12142 find_some_match(argvars, rettv, 2);
12143}
12144
Bram Moolenaar33570922005-01-25 22:26:29 +000012145static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012146
12147 static void
12148max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012149 typval_T *argvars;
12150 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012151 int domax;
12152{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012153 long n = 0;
12154 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012155 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012156
12157 if (argvars[0].v_type == VAR_LIST)
12158 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012159 list_T *l;
12160 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012161
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012162 l = argvars[0].vval.v_list;
12163 if (l != NULL)
12164 {
12165 li = l->lv_first;
12166 if (li != NULL)
12167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012168 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012169 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012170 {
12171 li = li->li_next;
12172 if (li == NULL)
12173 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012175 if (domax ? i > n : i < n)
12176 n = i;
12177 }
12178 }
12179 }
12180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012181 else if (argvars[0].v_type == VAR_DICT)
12182 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012183 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012184 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012185 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012186 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012187
12188 d = argvars[0].vval.v_dict;
12189 if (d != NULL)
12190 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 todo = d->dv_hashtab.ht_used;
12192 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012193 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012194 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012195 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012196 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012197 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012198 if (first)
12199 {
12200 n = i;
12201 first = FALSE;
12202 }
12203 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012204 n = i;
12205 }
12206 }
12207 }
12208 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012209 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012210 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012211 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012212}
12213
12214/*
12215 * "max()" function
12216 */
12217 static void
12218f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012219 typval_T *argvars;
12220 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012221{
12222 max_min(argvars, rettv, TRUE);
12223}
12224
12225/*
12226 * "min()" function
12227 */
12228 static void
12229f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012230 typval_T *argvars;
12231 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012232{
12233 max_min(argvars, rettv, FALSE);
12234}
12235
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012236static int mkdir_recurse __ARGS((char_u *dir, int prot));
12237
12238/*
12239 * Create the directory in which "dir" is located, and higher levels when
12240 * needed.
12241 */
12242 static int
12243mkdir_recurse(dir, prot)
12244 char_u *dir;
12245 int prot;
12246{
12247 char_u *p;
12248 char_u *updir;
12249 int r = FAIL;
12250
12251 /* Get end of directory name in "dir".
12252 * We're done when it's "/" or "c:/". */
12253 p = gettail_sep(dir);
12254 if (p <= get_past_head(dir))
12255 return OK;
12256
12257 /* If the directory exists we're done. Otherwise: create it.*/
12258 updir = vim_strnsave(dir, (int)(p - dir));
12259 if (updir == NULL)
12260 return FAIL;
12261 if (mch_isdir(updir))
12262 r = OK;
12263 else if (mkdir_recurse(updir, prot) == OK)
12264 r = vim_mkdir_emsg(updir, prot);
12265 vim_free(updir);
12266 return r;
12267}
12268
12269#ifdef vim_mkdir
12270/*
12271 * "mkdir()" function
12272 */
12273 static void
12274f_mkdir(argvars, rettv)
12275 typval_T *argvars;
12276 typval_T *rettv;
12277{
12278 char_u *dir;
12279 char_u buf[NUMBUFLEN];
12280 int prot = 0755;
12281
12282 rettv->vval.v_number = FAIL;
12283 if (check_restricted() || check_secure())
12284 return;
12285
12286 dir = get_tv_string_buf(&argvars[0], buf);
12287 if (argvars[1].v_type != VAR_UNKNOWN)
12288 {
12289 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 prot = get_tv_number_chk(&argvars[2], NULL);
12291 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012292 mkdir_recurse(dir, prot);
12293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012294 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012295}
12296#endif
12297
Bram Moolenaar0d660222005-01-07 21:51:51 +000012298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 * "mode()" function
12300 */
12301/*ARGSUSED*/
12302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012304 typval_T *argvars;
12305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306{
12307 char_u buf[2];
12308
12309#ifdef FEAT_VISUAL
12310 if (VIsual_active)
12311 {
12312 if (VIsual_select)
12313 buf[0] = VIsual_mode + 's' - 'v';
12314 else
12315 buf[0] = VIsual_mode;
12316 }
12317 else
12318#endif
12319 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12320 buf[0] = 'r';
12321 else if (State & INSERT)
12322 {
12323 if (State & REPLACE_FLAG)
12324 buf[0] = 'R';
12325 else
12326 buf[0] = 'i';
12327 }
12328 else if (State & CMDLINE)
12329 buf[0] = 'c';
12330 else
12331 buf[0] = 'n';
12332
12333 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334 rettv->vval.v_string = vim_strsave(buf);
12335 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336}
12337
12338/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012339 * "nextnonblank()" function
12340 */
12341 static void
12342f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012343 typval_T *argvars;
12344 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012345{
12346 linenr_T lnum;
12347
12348 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012350 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012351 {
12352 lnum = 0;
12353 break;
12354 }
12355 if (*skipwhite(ml_get(lnum)) != NUL)
12356 break;
12357 }
12358 rettv->vval.v_number = lnum;
12359}
12360
12361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 * "nr2char()" function
12363 */
12364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 typval_T *argvars;
12367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368{
12369 char_u buf[NUMBUFLEN];
12370
12371#ifdef FEAT_MBYTE
12372 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 else
12375#endif
12376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 buf[1] = NUL;
12379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 rettv->v_type = VAR_STRING;
12381 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012382}
12383
12384/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385 * "prevnonblank()" function
12386 */
12387 static void
12388f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012389 typval_T *argvars;
12390 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012391{
12392 linenr_T lnum;
12393
12394 lnum = get_tv_lnum(argvars);
12395 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12396 lnum = 0;
12397 else
12398 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12399 --lnum;
12400 rettv->vval.v_number = lnum;
12401}
12402
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012403#ifdef HAVE_STDARG_H
12404/* This dummy va_list is here because:
12405 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12406 * - locally in the function results in a "used before set" warning
12407 * - using va_start() to initialize it gives "function with fixed args" error */
12408static va_list ap;
12409#endif
12410
Bram Moolenaar8c711452005-01-14 21:53:12 +000012411/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012412 * "printf()" function
12413 */
12414 static void
12415f_printf(argvars, rettv)
12416 typval_T *argvars;
12417 typval_T *rettv;
12418{
12419 rettv->v_type = VAR_STRING;
12420 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012421#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012422 {
12423 char_u buf[NUMBUFLEN];
12424 int len;
12425 char_u *s;
12426 int saved_did_emsg = did_emsg;
12427 char *fmt;
12428
12429 /* Get the required length, allocate the buffer and do it for real. */
12430 did_emsg = FALSE;
12431 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012432 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012433 if (!did_emsg)
12434 {
12435 s = alloc(len + 1);
12436 if (s != NULL)
12437 {
12438 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012439 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012440 }
12441 }
12442 did_emsg |= saved_did_emsg;
12443 }
12444#endif
12445}
12446
12447/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012448 * "pumvisible()" function
12449 */
12450/*ARGSUSED*/
12451 static void
12452f_pumvisible(argvars, rettv)
12453 typval_T *argvars;
12454 typval_T *rettv;
12455{
12456 rettv->vval.v_number = 0;
12457#ifdef FEAT_INS_EXPAND
12458 if (pum_visible())
12459 rettv->vval.v_number = 1;
12460#endif
12461}
12462
12463/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012464 * "range()" function
12465 */
12466 static void
12467f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012468 typval_T *argvars;
12469 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012470{
12471 long start;
12472 long end;
12473 long stride = 1;
12474 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012475 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012477 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012478 if (argvars[1].v_type == VAR_UNKNOWN)
12479 {
12480 end = start - 1;
12481 start = 0;
12482 }
12483 else
12484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012485 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012486 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012487 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012488 }
12489
12490 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 if (error)
12492 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012493 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012494 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012495 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012496 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012497 else
12498 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012499 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012500 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012501 if (list_append_number(rettv->vval.v_list,
12502 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012503 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012504 }
12505}
12506
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012507/*
12508 * "readfile()" function
12509 */
12510 static void
12511f_readfile(argvars, rettv)
12512 typval_T *argvars;
12513 typval_T *rettv;
12514{
12515 int binary = FALSE;
12516 char_u *fname;
12517 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012518 listitem_T *li;
12519#define FREAD_SIZE 200 /* optimized for text lines */
12520 char_u buf[FREAD_SIZE];
12521 int readlen; /* size of last fread() */
12522 int buflen; /* nr of valid chars in buf[] */
12523 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12524 int tolist; /* first byte in buf[] still to be put in list */
12525 int chop; /* how many CR to chop off */
12526 char_u *prev = NULL; /* previously read bytes, if any */
12527 int prevlen = 0; /* length of "prev" if not NULL */
12528 char_u *s;
12529 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012530 long maxline = MAXLNUM;
12531 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012532
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012533 if (argvars[1].v_type != VAR_UNKNOWN)
12534 {
12535 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12536 binary = TRUE;
12537 if (argvars[2].v_type != VAR_UNKNOWN)
12538 maxline = get_tv_number(&argvars[2]);
12539 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012540
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012541 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012542 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012543
12544 /* Always open the file in binary mode, library functions have a mind of
12545 * their own about CR-LF conversion. */
12546 fname = get_tv_string(&argvars[0]);
12547 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12548 {
12549 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12550 return;
12551 }
12552
12553 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012554 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012555 {
12556 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12557 buflen = filtd + readlen;
12558 tolist = 0;
12559 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12560 {
12561 if (buf[filtd] == '\n' || readlen <= 0)
12562 {
12563 /* Only when in binary mode add an empty list item when the
12564 * last line ends in a '\n'. */
12565 if (!binary && readlen == 0 && filtd == 0)
12566 break;
12567
12568 /* Found end-of-line or end-of-file: add a text line to the
12569 * list. */
12570 chop = 0;
12571 if (!binary)
12572 while (filtd - chop - 1 >= tolist
12573 && buf[filtd - chop - 1] == '\r')
12574 ++chop;
12575 len = filtd - tolist - chop;
12576 if (prev == NULL)
12577 s = vim_strnsave(buf + tolist, len);
12578 else
12579 {
12580 s = alloc((unsigned)(prevlen + len + 1));
12581 if (s != NULL)
12582 {
12583 mch_memmove(s, prev, prevlen);
12584 vim_free(prev);
12585 prev = NULL;
12586 mch_memmove(s + prevlen, buf + tolist, len);
12587 s[prevlen + len] = NUL;
12588 }
12589 }
12590 tolist = filtd + 1;
12591
12592 li = listitem_alloc();
12593 if (li == NULL)
12594 {
12595 vim_free(s);
12596 break;
12597 }
12598 li->li_tv.v_type = VAR_STRING;
12599 li->li_tv.v_lock = 0;
12600 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012601 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012602
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012603 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012604 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012605 if (readlen <= 0)
12606 break;
12607 }
12608 else if (buf[filtd] == NUL)
12609 buf[filtd] = '\n';
12610 }
12611 if (readlen <= 0)
12612 break;
12613
12614 if (tolist == 0)
12615 {
12616 /* "buf" is full, need to move text to an allocated buffer */
12617 if (prev == NULL)
12618 {
12619 prev = vim_strnsave(buf, buflen);
12620 prevlen = buflen;
12621 }
12622 else
12623 {
12624 s = alloc((unsigned)(prevlen + buflen));
12625 if (s != NULL)
12626 {
12627 mch_memmove(s, prev, prevlen);
12628 mch_memmove(s + prevlen, buf, buflen);
12629 vim_free(prev);
12630 prev = s;
12631 prevlen += buflen;
12632 }
12633 }
12634 filtd = 0;
12635 }
12636 else
12637 {
12638 mch_memmove(buf, buf + tolist, buflen - tolist);
12639 filtd -= tolist;
12640 }
12641 }
12642
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012643 /*
12644 * For a negative line count use only the lines at the end of the file,
12645 * free the rest.
12646 */
12647 if (maxline < 0)
12648 while (cnt > -maxline)
12649 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012650 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012651 --cnt;
12652 }
12653
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012654 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012655 fclose(fd);
12656}
12657
12658
Bram Moolenaar0d660222005-01-07 21:51:51 +000012659#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12660static void make_connection __ARGS((void));
12661static int check_connection __ARGS((void));
12662
12663 static void
12664make_connection()
12665{
12666 if (X_DISPLAY == NULL
12667# ifdef FEAT_GUI
12668 && !gui.in_use
12669# endif
12670 )
12671 {
12672 x_force_connect = TRUE;
12673 setup_term_clip();
12674 x_force_connect = FALSE;
12675 }
12676}
12677
12678 static int
12679check_connection()
12680{
12681 make_connection();
12682 if (X_DISPLAY == NULL)
12683 {
12684 EMSG(_("E240: No connection to Vim server"));
12685 return FAIL;
12686 }
12687 return OK;
12688}
12689#endif
12690
12691#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012692static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012693
12694 static void
12695remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012696 typval_T *argvars;
12697 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698 int expr;
12699{
12700 char_u *server_name;
12701 char_u *keys;
12702 char_u *r = NULL;
12703 char_u buf[NUMBUFLEN];
12704# ifdef WIN32
12705 HWND w;
12706# else
12707 Window w;
12708# endif
12709
12710 if (check_restricted() || check_secure())
12711 return;
12712
12713# ifdef FEAT_X11
12714 if (check_connection() == FAIL)
12715 return;
12716# endif
12717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 server_name = get_tv_string_chk(&argvars[0]);
12719 if (server_name == NULL)
12720 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 keys = get_tv_string_buf(&argvars[1], buf);
12722# ifdef WIN32
12723 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12724# else
12725 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12726 < 0)
12727# endif
12728 {
12729 if (r != NULL)
12730 EMSG(r); /* sending worked but evaluation failed */
12731 else
12732 EMSG2(_("E241: Unable to send to %s"), server_name);
12733 return;
12734 }
12735
12736 rettv->vval.v_string = r;
12737
12738 if (argvars[2].v_type != VAR_UNKNOWN)
12739 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012740 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012741 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012743
12744 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012745 v.di_tv.v_type = VAR_STRING;
12746 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 idvar = get_tv_string_chk(&argvars[2]);
12748 if (idvar != NULL)
12749 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012750 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012751 }
12752}
12753#endif
12754
12755/*
12756 * "remote_expr()" function
12757 */
12758/*ARGSUSED*/
12759 static void
12760f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012761 typval_T *argvars;
12762 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012763{
12764 rettv->v_type = VAR_STRING;
12765 rettv->vval.v_string = NULL;
12766#ifdef FEAT_CLIENTSERVER
12767 remote_common(argvars, rettv, TRUE);
12768#endif
12769}
12770
12771/*
12772 * "remote_foreground()" function
12773 */
12774/*ARGSUSED*/
12775 static void
12776f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012777 typval_T *argvars;
12778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012779{
12780 rettv->vval.v_number = 0;
12781#ifdef FEAT_CLIENTSERVER
12782# ifdef WIN32
12783 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012784 {
12785 char_u *server_name = get_tv_string_chk(&argvars[0]);
12786
12787 if (server_name != NULL)
12788 serverForeground(server_name);
12789 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012790# else
12791 /* Send a foreground() expression to the server. */
12792 argvars[1].v_type = VAR_STRING;
12793 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12794 argvars[2].v_type = VAR_UNKNOWN;
12795 remote_common(argvars, rettv, TRUE);
12796 vim_free(argvars[1].vval.v_string);
12797# endif
12798#endif
12799}
12800
12801/*ARGSUSED*/
12802 static void
12803f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012804 typval_T *argvars;
12805 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012806{
12807#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012808 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012809 char_u *s = NULL;
12810# ifdef WIN32
12811 int n = 0;
12812# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012813 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012814
12815 if (check_restricted() || check_secure())
12816 {
12817 rettv->vval.v_number = -1;
12818 return;
12819 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012820 serverid = get_tv_string_chk(&argvars[0]);
12821 if (serverid == NULL)
12822 {
12823 rettv->vval.v_number = -1;
12824 return; /* type error; errmsg already given */
12825 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012826# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012827 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012828 if (n == 0)
12829 rettv->vval.v_number = -1;
12830 else
12831 {
12832 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12833 rettv->vval.v_number = (s != NULL);
12834 }
12835# else
12836 rettv->vval.v_number = 0;
12837 if (check_connection() == FAIL)
12838 return;
12839
12840 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012841 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012842# endif
12843
12844 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012846 char_u *retvar;
12847
Bram Moolenaar33570922005-01-25 22:26:29 +000012848 v.di_tv.v_type = VAR_STRING;
12849 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012850 retvar = get_tv_string_chk(&argvars[1]);
12851 if (retvar != NULL)
12852 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012853 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012854 }
12855#else
12856 rettv->vval.v_number = -1;
12857#endif
12858}
12859
12860/*ARGSUSED*/
12861 static void
12862f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *argvars;
12864 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865{
12866 char_u *r = NULL;
12867
12868#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012869 char_u *serverid = get_tv_string_chk(&argvars[0]);
12870
12871 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012872 {
12873# ifdef WIN32
12874 /* The server's HWND is encoded in the 'id' parameter */
12875 int n = 0;
12876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012877 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012878 if (n != 0)
12879 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12880 if (r == NULL)
12881# else
12882 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012883 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012884# endif
12885 EMSG(_("E277: Unable to read a server reply"));
12886 }
12887#endif
12888 rettv->v_type = VAR_STRING;
12889 rettv->vval.v_string = r;
12890}
12891
12892/*
12893 * "remote_send()" function
12894 */
12895/*ARGSUSED*/
12896 static void
12897f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012898 typval_T *argvars;
12899 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012900{
12901 rettv->v_type = VAR_STRING;
12902 rettv->vval.v_string = NULL;
12903#ifdef FEAT_CLIENTSERVER
12904 remote_common(argvars, rettv, FALSE);
12905#endif
12906}
12907
12908/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012909 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012910 */
12911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012913 typval_T *argvars;
12914 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012915{
Bram Moolenaar33570922005-01-25 22:26:29 +000012916 list_T *l;
12917 listitem_T *item, *item2;
12918 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012919 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012920 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012921 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012922 dict_T *d;
12923 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012924
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012925 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012926 if (argvars[0].v_type == VAR_DICT)
12927 {
12928 if (argvars[2].v_type != VAR_UNKNOWN)
12929 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012930 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012931 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012933 key = get_tv_string_chk(&argvars[1]);
12934 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012936 di = dict_find(d, key, -1);
12937 if (di == NULL)
12938 EMSG2(_(e_dictkey), key);
12939 else
12940 {
12941 *rettv = di->di_tv;
12942 init_tv(&di->di_tv);
12943 dictitem_remove(d, di);
12944 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012945 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012946 }
12947 }
12948 else if (argvars[0].v_type != VAR_LIST)
12949 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012950 else if ((l = argvars[0].vval.v_list) != NULL
12951 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012953 int error = FALSE;
12954
12955 idx = get_tv_number_chk(&argvars[1], &error);
12956 if (error)
12957 ; /* type error: do nothing, errmsg already given */
12958 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012959 EMSGN(_(e_listidx), idx);
12960 else
12961 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012962 if (argvars[2].v_type == VAR_UNKNOWN)
12963 {
12964 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012965 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012966 *rettv = item->li_tv;
12967 vim_free(item);
12968 }
12969 else
12970 {
12971 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012972 end = get_tv_number_chk(&argvars[2], &error);
12973 if (error)
12974 ; /* type error: do nothing */
12975 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012976 EMSGN(_(e_listidx), end);
12977 else
12978 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012979 int cnt = 0;
12980
12981 for (li = item; li != NULL; li = li->li_next)
12982 {
12983 ++cnt;
12984 if (li == item2)
12985 break;
12986 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012987 if (li == NULL) /* didn't find "item2" after "item" */
12988 EMSG(_(e_invrange));
12989 else
12990 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012991 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012992 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012993 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012994 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012995 l->lv_first = item;
12996 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012997 item->li_prev = NULL;
12998 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012999 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013000 }
13001 }
13002 }
13003 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013004 }
13005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006}
13007
13008/*
13009 * "rename({from}, {to})" function
13010 */
13011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013013 typval_T *argvars;
13014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015{
13016 char_u buf[NUMBUFLEN];
13017
13018 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013021 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13022 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023}
13024
13025/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013026 * "repeat()" function
13027 */
13028/*ARGSUSED*/
13029 static void
13030f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013031 typval_T *argvars;
13032 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013033{
13034 char_u *p;
13035 int n;
13036 int slen;
13037 int len;
13038 char_u *r;
13039 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013040
13041 n = get_tv_number(&argvars[1]);
13042 if (argvars[0].v_type == VAR_LIST)
13043 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013044 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013045 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013046 if (list_extend(rettv->vval.v_list,
13047 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013048 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013049 }
13050 else
13051 {
13052 p = get_tv_string(&argvars[0]);
13053 rettv->v_type = VAR_STRING;
13054 rettv->vval.v_string = NULL;
13055
13056 slen = (int)STRLEN(p);
13057 len = slen * n;
13058 if (len <= 0)
13059 return;
13060
13061 r = alloc(len + 1);
13062 if (r != NULL)
13063 {
13064 for (i = 0; i < n; i++)
13065 mch_memmove(r + i * slen, p, (size_t)slen);
13066 r[len] = NUL;
13067 }
13068
13069 rettv->vval.v_string = r;
13070 }
13071}
13072
13073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 * "resolve()" function
13075 */
13076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013078 typval_T *argvars;
13079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080{
13081 char_u *p;
13082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013083 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#ifdef FEAT_SHORTCUT
13085 {
13086 char_u *v = NULL;
13087
13088 v = mch_resolve_shortcut(p);
13089 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013092 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 }
13094#else
13095# ifdef HAVE_READLINK
13096 {
13097 char_u buf[MAXPATHL + 1];
13098 char_u *cpy;
13099 int len;
13100 char_u *remain = NULL;
13101 char_u *q;
13102 int is_relative_to_current = FALSE;
13103 int has_trailing_pathsep = FALSE;
13104 int limit = 100;
13105
13106 p = vim_strsave(p);
13107
13108 if (p[0] == '.' && (vim_ispathsep(p[1])
13109 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13110 is_relative_to_current = TRUE;
13111
13112 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013113 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114 has_trailing_pathsep = TRUE;
13115
13116 q = getnextcomp(p);
13117 if (*q != NUL)
13118 {
13119 /* Separate the first path component in "p", and keep the
13120 * remainder (beginning with the path separator). */
13121 remain = vim_strsave(q - 1);
13122 q[-1] = NUL;
13123 }
13124
13125 for (;;)
13126 {
13127 for (;;)
13128 {
13129 len = readlink((char *)p, (char *)buf, MAXPATHL);
13130 if (len <= 0)
13131 break;
13132 buf[len] = NUL;
13133
13134 if (limit-- == 0)
13135 {
13136 vim_free(p);
13137 vim_free(remain);
13138 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 goto fail;
13141 }
13142
13143 /* Ensure that the result will have a trailing path separator
13144 * if the argument has one. */
13145 if (remain == NULL && has_trailing_pathsep)
13146 add_pathsep(buf);
13147
13148 /* Separate the first path component in the link value and
13149 * concatenate the remainders. */
13150 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13151 if (*q != NUL)
13152 {
13153 if (remain == NULL)
13154 remain = vim_strsave(q - 1);
13155 else
13156 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013157 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 if (cpy != NULL)
13159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 vim_free(remain);
13161 remain = cpy;
13162 }
13163 }
13164 q[-1] = NUL;
13165 }
13166
13167 q = gettail(p);
13168 if (q > p && *q == NUL)
13169 {
13170 /* Ignore trailing path separator. */
13171 q[-1] = NUL;
13172 q = gettail(p);
13173 }
13174 if (q > p && !mch_isFullName(buf))
13175 {
13176 /* symlink is relative to directory of argument */
13177 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13178 if (cpy != NULL)
13179 {
13180 STRCPY(cpy, p);
13181 STRCPY(gettail(cpy), buf);
13182 vim_free(p);
13183 p = cpy;
13184 }
13185 }
13186 else
13187 {
13188 vim_free(p);
13189 p = vim_strsave(buf);
13190 }
13191 }
13192
13193 if (remain == NULL)
13194 break;
13195
13196 /* Append the first path component of "remain" to "p". */
13197 q = getnextcomp(remain + 1);
13198 len = q - remain - (*q != NUL);
13199 cpy = vim_strnsave(p, STRLEN(p) + len);
13200 if (cpy != NULL)
13201 {
13202 STRNCAT(cpy, remain, len);
13203 vim_free(p);
13204 p = cpy;
13205 }
13206 /* Shorten "remain". */
13207 if (*q != NUL)
13208 STRCPY(remain, q - 1);
13209 else
13210 {
13211 vim_free(remain);
13212 remain = NULL;
13213 }
13214 }
13215
13216 /* If the result is a relative path name, make it explicitly relative to
13217 * the current directory if and only if the argument had this form. */
13218 if (!vim_ispathsep(*p))
13219 {
13220 if (is_relative_to_current
13221 && *p != NUL
13222 && !(p[0] == '.'
13223 && (p[1] == NUL
13224 || vim_ispathsep(p[1])
13225 || (p[1] == '.'
13226 && (p[2] == NUL
13227 || vim_ispathsep(p[2]))))))
13228 {
13229 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013230 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 if (cpy != NULL)
13232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 vim_free(p);
13234 p = cpy;
13235 }
13236 }
13237 else if (!is_relative_to_current)
13238 {
13239 /* Strip leading "./". */
13240 q = p;
13241 while (q[0] == '.' && vim_ispathsep(q[1]))
13242 q += 2;
13243 if (q > p)
13244 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13245 }
13246 }
13247
13248 /* Ensure that the result will have no trailing path separator
13249 * if the argument had none. But keep "/" or "//". */
13250 if (!has_trailing_pathsep)
13251 {
13252 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013253 if (after_pathsep(p, q))
13254 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 }
13256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 }
13259# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013260 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261# endif
13262#endif
13263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
13266#ifdef HAVE_READLINK
13267fail:
13268#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013269 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270}
13271
13272/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013273 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 */
13275 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013276f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 typval_T *argvars;
13278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279{
Bram Moolenaar33570922005-01-25 22:26:29 +000013280 list_T *l;
13281 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282
Bram Moolenaar0d660222005-01-07 21:51:51 +000013283 rettv->vval.v_number = 0;
13284 if (argvars[0].v_type != VAR_LIST)
13285 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013286 else if ((l = argvars[0].vval.v_list) != NULL
13287 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013288 {
13289 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013290 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013291 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013292 while (li != NULL)
13293 {
13294 ni = li->li_prev;
13295 list_append(l, li);
13296 li = ni;
13297 }
13298 rettv->vval.v_list = l;
13299 rettv->v_type = VAR_LIST;
13300 ++l->lv_refcount;
13301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302}
13303
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013304#define SP_NOMOVE 0x01 /* don't move cursor */
13305#define SP_REPEAT 0x02 /* repeat to find outer pair */
13306#define SP_RETCOUNT 0x04 /* return matchcount */
13307#define SP_SETPCMARK 0x08 /* set previous context mark */
13308#define SP_START 0x10 /* accept match at start position */
13309#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13310#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013311
Bram Moolenaar33570922005-01-25 22:26:29 +000013312static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013313
13314/*
13315 * Get flags for a search function.
13316 * Possibly sets "p_ws".
13317 * Returns BACKWARD, FORWARD or zero (for an error).
13318 */
13319 static int
13320get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322 int *flagsp;
13323{
13324 int dir = FORWARD;
13325 char_u *flags;
13326 char_u nbuf[NUMBUFLEN];
13327 int mask;
13328
13329 if (varp->v_type != VAR_UNKNOWN)
13330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013331 flags = get_tv_string_buf_chk(varp, nbuf);
13332 if (flags == NULL)
13333 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013334 while (*flags != NUL)
13335 {
13336 switch (*flags)
13337 {
13338 case 'b': dir = BACKWARD; break;
13339 case 'w': p_ws = TRUE; break;
13340 case 'W': p_ws = FALSE; break;
13341 default: mask = 0;
13342 if (flagsp != NULL)
13343 switch (*flags)
13344 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013345 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013346 case 'e': mask = SP_END; break;
13347 case 'm': mask = SP_RETCOUNT; break;
13348 case 'n': mask = SP_NOMOVE; break;
13349 case 'p': mask = SP_SUBPAT; break;
13350 case 'r': mask = SP_REPEAT; break;
13351 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013352 }
13353 if (mask == 0)
13354 {
13355 EMSG2(_(e_invarg2), flags);
13356 dir = 0;
13357 }
13358 else
13359 *flagsp |= mask;
13360 }
13361 if (dir == 0)
13362 break;
13363 ++flags;
13364 }
13365 }
13366 return dir;
13367}
13368
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013370 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013372 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013373search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013374 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013375 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013376 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013378 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 char_u *pat;
13380 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013381 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 int save_p_ws = p_ws;
13383 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013384 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013385 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013386 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013387 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013390 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013391 if (dir == 0)
13392 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013393 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013394 if (flags & SP_START)
13395 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013396 if (flags & SP_END)
13397 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013398
13399 /* Optional extra argument: line number to stop searching. */
13400 if (argvars[1].v_type != VAR_UNKNOWN
13401 && argvars[2].v_type != VAR_UNKNOWN)
13402 {
13403 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13404 if (lnum_stop < 0)
13405 goto theend;
13406 }
13407
Bram Moolenaar231334e2005-07-25 20:46:57 +000013408 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013409 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013410 * Check to make sure only those flags are set.
13411 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13412 * flags cannot be set. Check for that condition also.
13413 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013414 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013415 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013418 goto theend;
13419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013421 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013422 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13423 options, RE_SEARCH, (linenr_T)lnum_stop);
13424 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013426 if (flags & SP_SUBPAT)
13427 retval = subpatnum;
13428 else
13429 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013430 if (flags & SP_SETPCMARK)
13431 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013433 if (match_pos != NULL)
13434 {
13435 /* Store the match cursor position */
13436 match_pos->lnum = pos.lnum;
13437 match_pos->col = pos.col + 1;
13438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 /* "/$" will put the cursor after the end of the line, may need to
13440 * correct that here */
13441 check_cursor();
13442 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013443
13444 /* If 'n' flag is used: restore cursor position. */
13445 if (flags & SP_NOMOVE)
13446 curwin->w_cursor = save_cursor;
13447theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013449
13450 return retval;
13451}
13452
13453/*
13454 * "search()" function
13455 */
13456 static void
13457f_search(argvars, rettv)
13458 typval_T *argvars;
13459 typval_T *rettv;
13460{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013461 int flags = 0;
13462
13463 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464}
13465
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013467 * "searchdecl()" function
13468 */
13469 static void
13470f_searchdecl(argvars, rettv)
13471 typval_T *argvars;
13472 typval_T *rettv;
13473{
13474 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013475 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013476 int error = FALSE;
13477 char_u *name;
13478
13479 rettv->vval.v_number = 1; /* default: FAIL */
13480
13481 name = get_tv_string_chk(&argvars[0]);
13482 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013483 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013484 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013485 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13486 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13487 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013488 if (!error && name != NULL)
13489 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013490 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013491}
13492
13493/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013494 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013496 static int
13497searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013498 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013499 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500{
13501 char_u *spat, *mpat, *epat;
13502 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 int dir;
13505 int flags = 0;
13506 char_u nbuf1[NUMBUFLEN];
13507 char_u nbuf2[NUMBUFLEN];
13508 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013509 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013510 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 spat = get_tv_string_chk(&argvars[0]);
13514 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13515 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13516 if (spat == NULL || mpat == NULL || epat == NULL)
13517 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 /* Handle the optional fourth argument: flags */
13520 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013521 if (dir == 0)
13522 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013523
13524 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013525 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13526 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013527 if ((flags & (SP_END | SP_SUBPAT)) != 0
13528 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013529 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013530 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013531 goto theend;
13532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013534 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013535 if (argvars[3].v_type == VAR_UNKNOWN
13536 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 skip = (char_u *)"";
13538 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013541 if (argvars[5].v_type != VAR_UNKNOWN)
13542 {
13543 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13544 if (lnum_stop < 0)
13545 goto theend;
13546 }
13547 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013548 if (skip == NULL)
13549 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013551 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13552 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013553
13554theend:
13555 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013556
13557 return retval;
13558}
13559
13560/*
13561 * "searchpair()" function
13562 */
13563 static void
13564f_searchpair(argvars, rettv)
13565 typval_T *argvars;
13566 typval_T *rettv;
13567{
13568 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13569}
13570
13571/*
13572 * "searchpairpos()" function
13573 */
13574 static void
13575f_searchpairpos(argvars, rettv)
13576 typval_T *argvars;
13577 typval_T *rettv;
13578{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013579 pos_T match_pos;
13580 int lnum = 0;
13581 int col = 0;
13582
13583 rettv->vval.v_number = 0;
13584
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013585 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013586 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013587
13588 if (searchpair_cmn(argvars, &match_pos) > 0)
13589 {
13590 lnum = match_pos.lnum;
13591 col = match_pos.col;
13592 }
13593
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013594 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13595 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013596}
13597
13598/*
13599 * Search for a start/middle/end thing.
13600 * Used by searchpair(), see its documentation for the details.
13601 * Returns 0 or -1 for no match,
13602 */
13603 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013604do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013605 char_u *spat; /* start pattern */
13606 char_u *mpat; /* middle pattern */
13607 char_u *epat; /* end pattern */
13608 int dir; /* BACKWARD or FORWARD */
13609 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013610 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013611 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013612 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013613{
13614 char_u *save_cpo;
13615 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13616 long retval = 0;
13617 pos_T pos;
13618 pos_T firstpos;
13619 pos_T foundpos;
13620 pos_T save_cursor;
13621 pos_T save_pos;
13622 int n;
13623 int r;
13624 int nest = 1;
13625 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013626 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013627
13628 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13629 save_cpo = p_cpo;
13630 p_cpo = (char_u *)"";
13631
13632 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13633 * start/middle/end (pat3, for the top pair). */
13634 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13635 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13636 if (pat2 == NULL || pat3 == NULL)
13637 goto theend;
13638 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13639 if (*mpat == NUL)
13640 STRCPY(pat3, pat2);
13641 else
13642 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13643 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013644 if (flags & SP_START)
13645 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013646
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 save_cursor = curwin->w_cursor;
13648 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013649 clearpos(&firstpos);
13650 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 pat = pat3;
13652 for (;;)
13653 {
13654 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013655 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13657 /* didn't find it or found the first match again: FAIL */
13658 break;
13659
13660 if (firstpos.lnum == 0)
13661 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013662 if (equalpos(pos, foundpos))
13663 {
13664 /* Found the same position again. Can happen with a pattern that
13665 * has "\zs" at the end and searching backwards. Advance one
13666 * character and try again. */
13667 if (dir == BACKWARD)
13668 decl(&pos);
13669 else
13670 incl(&pos);
13671 }
13672 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673
13674 /* If the skip pattern matches, ignore this match. */
13675 if (*skip != NUL)
13676 {
13677 save_pos = curwin->w_cursor;
13678 curwin->w_cursor = pos;
13679 r = eval_to_bool(skip, &err, NULL, FALSE);
13680 curwin->w_cursor = save_pos;
13681 if (err)
13682 {
13683 /* Evaluating {skip} caused an error, break here. */
13684 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013685 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 break;
13687 }
13688 if (r)
13689 continue;
13690 }
13691
13692 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13693 {
13694 /* Found end when searching backwards or start when searching
13695 * forward: nested pair. */
13696 ++nest;
13697 pat = pat2; /* nested, don't search for middle */
13698 }
13699 else
13700 {
13701 /* Found end when searching forward or start when searching
13702 * backward: end of (nested) pair; or found middle in outer pair. */
13703 if (--nest == 1)
13704 pat = pat3; /* outer level, search for middle */
13705 }
13706
13707 if (nest == 0)
13708 {
13709 /* Found the match: return matchcount or line number. */
13710 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013711 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013713 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013714 if (flags & SP_SETPCMARK)
13715 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 curwin->w_cursor = pos;
13717 if (!(flags & SP_REPEAT))
13718 break;
13719 nest = 1; /* search for next unmatched */
13720 }
13721 }
13722
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013723 if (match_pos != NULL)
13724 {
13725 /* Store the match cursor position */
13726 match_pos->lnum = curwin->w_cursor.lnum;
13727 match_pos->col = curwin->w_cursor.col + 1;
13728 }
13729
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013731 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 curwin->w_cursor = save_cursor;
13733
13734theend:
13735 vim_free(pat2);
13736 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013738
13739 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740}
13741
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013742/*
13743 * "searchpos()" function
13744 */
13745 static void
13746f_searchpos(argvars, rettv)
13747 typval_T *argvars;
13748 typval_T *rettv;
13749{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013750 pos_T match_pos;
13751 int lnum = 0;
13752 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013753 int n;
13754 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013755
13756 rettv->vval.v_number = 0;
13757
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013758 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013759 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013760
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013761 n = search_cmn(argvars, &match_pos, &flags);
13762 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013763 {
13764 lnum = match_pos.lnum;
13765 col = match_pos.col;
13766 }
13767
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013768 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13769 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013770 if (flags & SP_SUBPAT)
13771 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013772}
13773
13774
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775/*ARGSUSED*/
13776 static void
13777f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013778 typval_T *argvars;
13779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013781#ifdef FEAT_CLIENTSERVER
13782 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013783 char_u *server = get_tv_string_chk(&argvars[0]);
13784 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013787 if (server == NULL || reply == NULL)
13788 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789 if (check_restricted() || check_secure())
13790 return;
13791# ifdef FEAT_X11
13792 if (check_connection() == FAIL)
13793 return;
13794# endif
13795
13796 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013798 EMSG(_("E258: Unable to send to client"));
13799 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013801 rettv->vval.v_number = 0;
13802#else
13803 rettv->vval.v_number = -1;
13804#endif
13805}
13806
13807/*ARGSUSED*/
13808 static void
13809f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 typval_T *argvars;
13811 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013812{
13813 char_u *r = NULL;
13814
13815#ifdef FEAT_CLIENTSERVER
13816# ifdef WIN32
13817 r = serverGetVimNames();
13818# else
13819 make_connection();
13820 if (X_DISPLAY != NULL)
13821 r = serverGetVimNames(X_DISPLAY);
13822# endif
13823#endif
13824 rettv->v_type = VAR_STRING;
13825 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826}
13827
13828/*
13829 * "setbufvar()" function
13830 */
13831/*ARGSUSED*/
13832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013834 typval_T *argvars;
13835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836{
13837 buf_T *buf;
13838#ifdef FEAT_AUTOCMD
13839 aco_save_T aco;
13840#else
13841 buf_T *save_curbuf;
13842#endif
13843 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013844 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 char_u nbuf[NUMBUFLEN];
13846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013847 rettv->vval.v_number = 0;
13848
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849 if (check_restricted() || check_secure())
13850 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13852 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013853 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 varp = &argvars[2];
13855
13856 if (buf != NULL && varname != NULL && varp != NULL)
13857 {
13858 /* set curbuf to be our buf, temporarily */
13859#ifdef FEAT_AUTOCMD
13860 aucmd_prepbuf(&aco, buf);
13861#else
13862 save_curbuf = curbuf;
13863 curbuf = buf;
13864#endif
13865
13866 if (*varname == '&')
13867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013868 long numval;
13869 char_u *strval;
13870 int error = FALSE;
13871
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 numval = get_tv_number_chk(varp, &error);
13874 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013875 if (!error && strval != NULL)
13876 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 }
13878 else
13879 {
13880 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13881 if (bufvarname != NULL)
13882 {
13883 STRCPY(bufvarname, "b:");
13884 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013885 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 vim_free(bufvarname);
13887 }
13888 }
13889
13890 /* reset notion of buffer */
13891#ifdef FEAT_AUTOCMD
13892 aucmd_restbuf(&aco);
13893#else
13894 curbuf = save_curbuf;
13895#endif
13896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897}
13898
13899/*
13900 * "setcmdpos()" function
13901 */
13902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013903f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013904 typval_T *argvars;
13905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013907 int pos = (int)get_tv_number(&argvars[0]) - 1;
13908
13909 if (pos >= 0)
13910 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911}
13912
13913/*
13914 * "setline()" function
13915 */
13916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013918 typval_T *argvars;
13919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920{
13921 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013922 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013923 list_T *l = NULL;
13924 listitem_T *li = NULL;
13925 long added = 0;
13926 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013928 lnum = get_tv_lnum(&argvars[0]);
13929 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013931 l = argvars[1].vval.v_list;
13932 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013934 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013935 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013936
13937 rettv->vval.v_number = 0; /* OK */
13938 for (;;)
13939 {
13940 if (l != NULL)
13941 {
13942 /* list argument, get next string */
13943 if (li == NULL)
13944 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013945 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013946 li = li->li_next;
13947 }
13948
13949 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013951 break;
13952 if (lnum <= curbuf->b_ml.ml_line_count)
13953 {
13954 /* existing line, replace it */
13955 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13956 {
13957 changed_bytes(lnum, 0);
13958 check_cursor_col();
13959 rettv->vval.v_number = 0; /* OK */
13960 }
13961 }
13962 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13963 {
13964 /* lnum is one past the last line, append the line */
13965 ++added;
13966 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13967 rettv->vval.v_number = 0; /* OK */
13968 }
13969
13970 if (l == NULL) /* only one string argument */
13971 break;
13972 ++lnum;
13973 }
13974
13975 if (added > 0)
13976 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977}
13978
13979/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013980 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013981 */
13982/*ARGSUSED*/
13983 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013984set_qf_ll_list(wp, list_arg, action_arg, rettv)
13985 win_T *wp;
13986 typval_T *list_arg;
13987 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013988 typval_T *rettv;
13989{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013990#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013991 char_u *act;
13992 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013993#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013994
Bram Moolenaar2641f772005-03-25 21:58:17 +000013995 rettv->vval.v_number = -1;
13996
13997#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013998 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013999 EMSG(_(e_listreq));
14000 else
14001 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014002 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014003
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014004 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014005 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014006 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007 if (act == NULL)
14008 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014009 if (*act == 'a' || *act == 'r')
14010 action = *act;
14011 }
14012
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014013 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014014 rettv->vval.v_number = 0;
14015 }
14016#endif
14017}
14018
14019/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014020 * "setloclist()" function
14021 */
14022/*ARGSUSED*/
14023 static void
14024f_setloclist(argvars, rettv)
14025 typval_T *argvars;
14026 typval_T *rettv;
14027{
14028 win_T *win;
14029
14030 rettv->vval.v_number = -1;
14031
14032 win = find_win_by_nr(&argvars[0]);
14033 if (win != NULL)
14034 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14035}
14036
14037/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014038 * "setpos()" function
14039 */
14040/*ARGSUSED*/
14041 static void
14042f_setpos(argvars, rettv)
14043 typval_T *argvars;
14044 typval_T *rettv;
14045{
14046 pos_T pos;
14047 int fnum;
14048 char_u *name;
14049
14050 name = get_tv_string_chk(argvars);
14051 if (name != NULL)
14052 {
14053 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14054 {
14055 --pos.col;
14056 if (name[0] == '.') /* cursor */
14057 {
14058 if (fnum == curbuf->b_fnum)
14059 {
14060 curwin->w_cursor = pos;
14061 check_cursor();
14062 }
14063 else
14064 EMSG(_(e_invarg));
14065 }
14066 else if (name[0] == '\'') /* mark */
14067 (void)setmark_pos(name[1], &pos, fnum);
14068 else
14069 EMSG(_(e_invarg));
14070 }
14071 }
14072}
14073
14074/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014075 * "setqflist()" function
14076 */
14077/*ARGSUSED*/
14078 static void
14079f_setqflist(argvars, rettv)
14080 typval_T *argvars;
14081 typval_T *rettv;
14082{
14083 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14084}
14085
14086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 * "setreg()" function
14088 */
14089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 typval_T *argvars;
14092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093{
14094 int regname;
14095 char_u *strregname;
14096 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 int append;
14099 char_u yank_type;
14100 long block_len;
14101
14102 block_len = -1;
14103 yank_type = MAUTO;
14104 append = FALSE;
14105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014107 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 if (strregname == NULL)
14110 return; /* type error; errmsg already given */
14111 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 if (regname == 0 || regname == '@')
14113 regname = '"';
14114 else if (regname == '=')
14115 return;
14116
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014117 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014119 stropt = get_tv_string_chk(&argvars[2]);
14120 if (stropt == NULL)
14121 return; /* type error */
14122 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 switch (*stropt)
14124 {
14125 case 'a': case 'A': /* append */
14126 append = TRUE;
14127 break;
14128 case 'v': case 'c': /* character-wise selection */
14129 yank_type = MCHAR;
14130 break;
14131 case 'V': case 'l': /* line-wise selection */
14132 yank_type = MLINE;
14133 break;
14134#ifdef FEAT_VISUAL
14135 case 'b': case Ctrl_V: /* block-wise selection */
14136 yank_type = MBLOCK;
14137 if (VIM_ISDIGIT(stropt[1]))
14138 {
14139 ++stropt;
14140 block_len = getdigits(&stropt) - 1;
14141 --stropt;
14142 }
14143 break;
14144#endif
14145 }
14146 }
14147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 strval = get_tv_string_chk(&argvars[1]);
14149 if (strval != NULL)
14150 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153}
14154
14155
14156/*
14157 * "setwinvar(expr)" function
14158 */
14159/*ARGSUSED*/
14160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014162 typval_T *argvars;
14163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164{
14165 win_T *win;
14166#ifdef FEAT_WINDOWS
14167 win_T *save_curwin;
14168#endif
14169 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014170 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 char_u nbuf[NUMBUFLEN];
14172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 rettv->vval.v_number = 0;
14174
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 if (check_restricted() || check_secure())
14176 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 varp = &argvars[2];
14180
14181 if (win != NULL && varname != NULL && varp != NULL)
14182 {
14183#ifdef FEAT_WINDOWS
14184 /* set curwin to be our win, temporarily */
14185 save_curwin = curwin;
14186 curwin = win;
14187 curbuf = curwin->w_buffer;
14188#endif
14189
14190 if (*varname == '&')
14191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 long numval;
14193 char_u *strval;
14194 int error = FALSE;
14195
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 numval = get_tv_number_chk(varp, &error);
14198 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 if (!error && strval != NULL)
14200 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 }
14202 else
14203 {
14204 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14205 if (winvarname != NULL)
14206 {
14207 STRCPY(winvarname, "w:");
14208 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014209 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 vim_free(winvarname);
14211 }
14212 }
14213
14214#ifdef FEAT_WINDOWS
14215 /* Restore current window, if it's still valid (autocomands can make
14216 * it invalid). */
14217 if (win_valid(save_curwin))
14218 {
14219 curwin = save_curwin;
14220 curbuf = curwin->w_buffer;
14221 }
14222#endif
14223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224}
14225
14226/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 */
14229 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 typval_T *argvars;
14232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014234 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235
Bram Moolenaar0d660222005-01-07 21:51:51 +000014236 p = get_tv_string(&argvars[0]);
14237 rettv->vval.v_string = vim_strsave(p);
14238 simplify_filename(rettv->vval.v_string); /* simplify in place */
14239 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240}
14241
Bram Moolenaar0d660222005-01-07 21:51:51 +000014242static int
14243#ifdef __BORLANDC__
14244 _RTLENTRYF
14245#endif
14246 item_compare __ARGS((const void *s1, const void *s2));
14247static int
14248#ifdef __BORLANDC__
14249 _RTLENTRYF
14250#endif
14251 item_compare2 __ARGS((const void *s1, const void *s2));
14252
14253static int item_compare_ic;
14254static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256#define ITEM_COMPARE_FAIL 999
14257
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014259 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014261 static int
14262#ifdef __BORLANDC__
14263_RTLENTRYF
14264#endif
14265item_compare(s1, s2)
14266 const void *s1;
14267 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014269 char_u *p1, *p2;
14270 char_u *tofree1, *tofree2;
14271 int res;
14272 char_u numbuf1[NUMBUFLEN];
14273 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014275 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14276 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014277 if (item_compare_ic)
14278 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014280 res = STRCMP(p1, p2);
14281 vim_free(tofree1);
14282 vim_free(tofree2);
14283 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284}
14285
14286 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014287#ifdef __BORLANDC__
14288_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014290item_compare2(s1, s2)
14291 const void *s1;
14292 const void *s2;
14293{
14294 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014295 typval_T rettv;
14296 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014297 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 /* shortcut after failure in previous call; compare all items equal */
14300 if (item_compare_func_err)
14301 return 0;
14302
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014303 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14304 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014305 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14306 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014307
14308 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14309 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014310 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311 clear_tv(&argv[0]);
14312 clear_tv(&argv[1]);
14313
14314 if (res == FAIL)
14315 res = ITEM_COMPARE_FAIL;
14316 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317 /* return value has wrong type */
14318 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14319 if (item_compare_func_err)
14320 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014321 clear_tv(&rettv);
14322 return res;
14323}
14324
14325/*
14326 * "sort({list})" function
14327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014329f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014330 typval_T *argvars;
14331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332{
Bram Moolenaar33570922005-01-25 22:26:29 +000014333 list_T *l;
14334 listitem_T *li;
14335 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014336 long len;
14337 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338
Bram Moolenaar0d660222005-01-07 21:51:51 +000014339 rettv->vval.v_number = 0;
14340 if (argvars[0].v_type != VAR_LIST)
14341 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 else
14343 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014345 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346 return;
14347 rettv->vval.v_list = l;
14348 rettv->v_type = VAR_LIST;
14349 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350
Bram Moolenaar0d660222005-01-07 21:51:51 +000014351 len = list_len(l);
14352 if (len <= 1)
14353 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354
Bram Moolenaar0d660222005-01-07 21:51:51 +000014355 item_compare_ic = FALSE;
14356 item_compare_func = NULL;
14357 if (argvars[1].v_type != VAR_UNKNOWN)
14358 {
14359 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361 else
14362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 int error = FALSE;
14364
14365 i = get_tv_number_chk(&argvars[1], &error);
14366 if (error)
14367 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014368 if (i == 1)
14369 item_compare_ic = TRUE;
14370 else
14371 item_compare_func = get_tv_string(&argvars[1]);
14372 }
14373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374
Bram Moolenaar0d660222005-01-07 21:51:51 +000014375 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014376 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377 if (ptrs == NULL)
14378 return;
14379 i = 0;
14380 for (li = l->lv_first; li != NULL; li = li->li_next)
14381 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014384 /* test the compare function */
14385 if (item_compare_func != NULL
14386 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14387 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014388 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014390 {
14391 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014392 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014393 item_compare_func == NULL ? item_compare : item_compare2);
14394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 if (!item_compare_func_err)
14396 {
14397 /* Clear the List and append the items in the sorted order. */
14398 l->lv_first = l->lv_last = NULL;
14399 l->lv_len = 0;
14400 for (i = 0; i < len; ++i)
14401 list_append(l, ptrs[i]);
14402 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014403 }
14404
14405 vim_free(ptrs);
14406 }
14407}
14408
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014409/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014410 * "soundfold({word})" function
14411 */
14412 static void
14413f_soundfold(argvars, rettv)
14414 typval_T *argvars;
14415 typval_T *rettv;
14416{
14417 char_u *s;
14418
14419 rettv->v_type = VAR_STRING;
14420 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014421#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014422 rettv->vval.v_string = eval_soundfold(s);
14423#else
14424 rettv->vval.v_string = vim_strsave(s);
14425#endif
14426}
14427
14428/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014429 * "spellbadword()" function
14430 */
14431/* ARGSUSED */
14432 static void
14433f_spellbadword(argvars, rettv)
14434 typval_T *argvars;
14435 typval_T *rettv;
14436{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014437 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014438 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014439 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014440
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014441 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014442 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014443
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014444#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014445 if (argvars[0].v_type == VAR_UNKNOWN)
14446 {
14447 /* Find the start and length of the badly spelled word. */
14448 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14449 if (len != 0)
14450 word = ml_get_cursor();
14451 }
14452 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14453 {
14454 char_u *str = get_tv_string_chk(&argvars[0]);
14455 int capcol = -1;
14456
14457 if (str != NULL)
14458 {
14459 /* Check the argument for spelling. */
14460 while (*str != NUL)
14461 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014462 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014463 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014464 {
14465 word = str;
14466 break;
14467 }
14468 str += len;
14469 }
14470 }
14471 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014472#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014473
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014474 list_append_string(rettv->vval.v_list, word, len);
14475 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014476 attr == HLF_SPB ? "bad" :
14477 attr == HLF_SPR ? "rare" :
14478 attr == HLF_SPL ? "local" :
14479 attr == HLF_SPC ? "caps" :
14480 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014481}
14482
14483/*
14484 * "spellsuggest()" function
14485 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014486/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014487 static void
14488f_spellsuggest(argvars, rettv)
14489 typval_T *argvars;
14490 typval_T *rettv;
14491{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014492#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014493 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014494 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014495 int maxcount;
14496 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014497 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014498 listitem_T *li;
14499 int need_capital = FALSE;
14500#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014501
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014502 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014503 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014504
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014505#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014506 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14507 {
14508 str = get_tv_string(&argvars[0]);
14509 if (argvars[1].v_type != VAR_UNKNOWN)
14510 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014511 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014512 if (maxcount <= 0)
14513 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014514 if (argvars[2].v_type != VAR_UNKNOWN)
14515 {
14516 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14517 if (typeerr)
14518 return;
14519 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014520 }
14521 else
14522 maxcount = 25;
14523
Bram Moolenaar4770d092006-01-12 23:22:24 +000014524 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014525
14526 for (i = 0; i < ga.ga_len; ++i)
14527 {
14528 str = ((char_u **)ga.ga_data)[i];
14529
14530 li = listitem_alloc();
14531 if (li == NULL)
14532 vim_free(str);
14533 else
14534 {
14535 li->li_tv.v_type = VAR_STRING;
14536 li->li_tv.v_lock = 0;
14537 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014538 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014539 }
14540 }
14541 ga_clear(&ga);
14542 }
14543#endif
14544}
14545
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014547f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 typval_T *argvars;
14549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014550{
14551 char_u *str;
14552 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014553 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554 regmatch_T regmatch;
14555 char_u patbuf[NUMBUFLEN];
14556 char_u *save_cpo;
14557 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014558 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014559 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014561
14562 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14563 save_cpo = p_cpo;
14564 p_cpo = (char_u *)"";
14565
14566 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014567 if (argvars[1].v_type != VAR_UNKNOWN)
14568 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014569 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14570 if (pat == NULL)
14571 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014572 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014573 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014574 }
14575 if (pat == NULL || *pat == NUL)
14576 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014577
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014578 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 if (typeerr)
14581 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14584 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014587 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014589 if (*str == NUL)
14590 match = FALSE; /* empty item at the end */
14591 else
14592 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 if (match)
14594 end = regmatch.startp[0];
14595 else
14596 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014597 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14598 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014600 if (list_append_string(rettv->vval.v_list, str,
14601 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603 }
14604 if (!match)
14605 break;
14606 /* Advance to just after the match. */
14607 if (regmatch.endp[0] > str)
14608 col = 0;
14609 else
14610 {
14611 /* Don't get stuck at the same match. */
14612#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014613 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614#else
14615 col = 1;
14616#endif
14617 }
14618 str = regmatch.endp[0];
14619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623
Bram Moolenaar0d660222005-01-07 21:51:51 +000014624 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625}
14626
Bram Moolenaar2c932302006-03-18 21:42:09 +000014627/*
14628 * "str2nr()" function
14629 */
14630 static void
14631f_str2nr(argvars, rettv)
14632 typval_T *argvars;
14633 typval_T *rettv;
14634{
14635 int base = 10;
14636 char_u *p;
14637 long n;
14638
14639 if (argvars[1].v_type != VAR_UNKNOWN)
14640 {
14641 base = get_tv_number(&argvars[1]);
14642 if (base != 8 && base != 10 && base != 16)
14643 {
14644 EMSG(_(e_invarg));
14645 return;
14646 }
14647 }
14648
14649 p = skipwhite(get_tv_string(&argvars[0]));
14650 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14651 rettv->vval.v_number = n;
14652}
14653
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654#ifdef HAVE_STRFTIME
14655/*
14656 * "strftime({format}[, {time}])" function
14657 */
14658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014659f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014660 typval_T *argvars;
14661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
14663 char_u result_buf[256];
14664 struct tm *curtime;
14665 time_t seconds;
14666 char_u *p;
14667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014671 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 seconds = time(NULL);
14673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014674 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 curtime = localtime(&seconds);
14676 /* MSVC returns NULL for an invalid value of seconds. */
14677 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679 else
14680 {
14681# ifdef FEAT_MBYTE
14682 vimconv_T conv;
14683 char_u *enc;
14684
14685 conv.vc_type = CONV_NONE;
14686 enc = enc_locale();
14687 convert_setup(&conv, p_enc, enc);
14688 if (conv.vc_type != CONV_NONE)
14689 p = string_convert(&conv, p, NULL);
14690# endif
14691 if (p != NULL)
14692 (void)strftime((char *)result_buf, sizeof(result_buf),
14693 (char *)p, curtime);
14694 else
14695 result_buf[0] = NUL;
14696
14697# ifdef FEAT_MBYTE
14698 if (conv.vc_type != CONV_NONE)
14699 vim_free(p);
14700 convert_setup(&conv, enc, p_enc);
14701 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014702 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 else
14704# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014705 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706
14707# ifdef FEAT_MBYTE
14708 /* Release conversion descriptors */
14709 convert_setup(&conv, NULL, NULL);
14710 vim_free(enc);
14711# endif
14712 }
14713}
14714#endif
14715
14716/*
14717 * "stridx()" function
14718 */
14719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014720f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 typval_T *argvars;
14722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723{
14724 char_u buf[NUMBUFLEN];
14725 char_u *needle;
14726 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014729 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014731 needle = get_tv_string_chk(&argvars[1]);
14732 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014733 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014734 if (needle == NULL || haystack == NULL)
14735 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736
Bram Moolenaar33570922005-01-25 22:26:29 +000014737 if (argvars[2].v_type != VAR_UNKNOWN)
14738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014739 int error = FALSE;
14740
14741 start_idx = get_tv_number_chk(&argvars[2], &error);
14742 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014743 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014744 if (start_idx >= 0)
14745 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014746 }
14747
14748 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14749 if (pos != NULL)
14750 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751}
14752
14753/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014754 * "string()" function
14755 */
14756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014757f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014758 typval_T *argvars;
14759 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014760{
14761 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014762 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014765 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014766 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014767 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768}
14769
14770/*
14771 * "strlen()" function
14772 */
14773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014775 typval_T *argvars;
14776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 rettv->vval.v_number = (varnumber_T)(STRLEN(
14779 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780}
14781
14782/*
14783 * "strpart()" function
14784 */
14785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014787 typval_T *argvars;
14788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789{
14790 char_u *p;
14791 int n;
14792 int len;
14793 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014796 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797 slen = (int)STRLEN(p);
14798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 n = get_tv_number_chk(&argvars[1], &error);
14800 if (error)
14801 len = 0;
14802 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014803 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804 else
14805 len = slen - n; /* default len: all bytes that are available. */
14806
14807 /*
14808 * Only return the overlap between the specified part and the actual
14809 * string.
14810 */
14811 if (n < 0)
14812 {
14813 len += n;
14814 n = 0;
14815 }
14816 else if (n > slen)
14817 n = slen;
14818 if (len < 0)
14819 len = 0;
14820 else if (n + len > slen)
14821 len = slen - n;
14822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014823 rettv->v_type = VAR_STRING;
14824 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825}
14826
14827/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014828 * "strridx()" function
14829 */
14830 static void
14831f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014832 typval_T *argvars;
14833 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014834{
14835 char_u buf[NUMBUFLEN];
14836 char_u *needle;
14837 char_u *haystack;
14838 char_u *rest;
14839 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014840 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014842 needle = get_tv_string_chk(&argvars[1]);
14843 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014844 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845
14846 rettv->vval.v_number = -1;
14847 if (needle == NULL || haystack == NULL)
14848 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014849 if (argvars[2].v_type != VAR_UNKNOWN)
14850 {
14851 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014852 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014853 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014855 }
14856 else
14857 end_idx = haystack_len;
14858
Bram Moolenaar0d660222005-01-07 21:51:51 +000014859 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014860 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014861 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014862 lastmatch = haystack + end_idx;
14863 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014864 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014865 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014866 for (rest = haystack; *rest != '\0'; ++rest)
14867 {
14868 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014869 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014870 break;
14871 lastmatch = rest;
14872 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014873 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874
14875 if (lastmatch == NULL)
14876 rettv->vval.v_number = -1;
14877 else
14878 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14879}
14880
14881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 * "strtrans()" function
14883 */
14884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014886 typval_T *argvars;
14887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 rettv->v_type = VAR_STRING;
14890 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891}
14892
14893/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014894 * "submatch()" function
14895 */
14896 static void
14897f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014898 typval_T *argvars;
14899 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014900{
14901 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014902 rettv->vval.v_string =
14903 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014904}
14905
14906/*
14907 * "substitute()" function
14908 */
14909 static void
14910f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014911 typval_T *argvars;
14912 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014913{
14914 char_u patbuf[NUMBUFLEN];
14915 char_u subbuf[NUMBUFLEN];
14916 char_u flagsbuf[NUMBUFLEN];
14917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 char_u *str = get_tv_string_chk(&argvars[0]);
14919 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14920 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14921 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14922
Bram Moolenaar0d660222005-01-07 21:51:51 +000014923 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014924 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14925 rettv->vval.v_string = NULL;
14926 else
14927 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014928}
14929
14930/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014931 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 */
14933/*ARGSUSED*/
14934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014936 typval_T *argvars;
14937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938{
14939 int id = 0;
14940#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014941 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 long col;
14943 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014944 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014946 lnum = get_tv_lnum(argvars); /* -1 on type error */
14947 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14948 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014950 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014951 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014952 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953#endif
14954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956}
14957
14958/*
14959 * "synIDattr(id, what [, mode])" function
14960 */
14961/*ARGSUSED*/
14962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 typval_T *argvars;
14965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966{
14967 char_u *p = NULL;
14968#ifdef FEAT_SYN_HL
14969 int id;
14970 char_u *what;
14971 char_u *mode;
14972 char_u modebuf[NUMBUFLEN];
14973 int modec;
14974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975 id = get_tv_number(&argvars[0]);
14976 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014977 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 modec = TOLOWER_ASC(mode[0]);
14981 if (modec != 't' && modec != 'c'
14982#ifdef FEAT_GUI
14983 && modec != 'g'
14984#endif
14985 )
14986 modec = 0; /* replace invalid with current */
14987 }
14988 else
14989 {
14990#ifdef FEAT_GUI
14991 if (gui.in_use)
14992 modec = 'g';
14993 else
14994#endif
14995 if (t_colors > 1)
14996 modec = 'c';
14997 else
14998 modec = 't';
14999 }
15000
15001
15002 switch (TOLOWER_ASC(what[0]))
15003 {
15004 case 'b':
15005 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15006 p = highlight_color(id, what, modec);
15007 else /* bold */
15008 p = highlight_has_attr(id, HL_BOLD, modec);
15009 break;
15010
15011 case 'f': /* fg[#] */
15012 p = highlight_color(id, what, modec);
15013 break;
15014
15015 case 'i':
15016 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15017 p = highlight_has_attr(id, HL_INVERSE, modec);
15018 else /* italic */
15019 p = highlight_has_attr(id, HL_ITALIC, modec);
15020 break;
15021
15022 case 'n': /* name */
15023 p = get_highlight_name(NULL, id - 1);
15024 break;
15025
15026 case 'r': /* reverse */
15027 p = highlight_has_attr(id, HL_INVERSE, modec);
15028 break;
15029
15030 case 's': /* standout */
15031 p = highlight_has_attr(id, HL_STANDOUT, modec);
15032 break;
15033
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015034 case 'u':
15035 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15036 /* underline */
15037 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15038 else
15039 /* undercurl */
15040 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 break;
15042 }
15043
15044 if (p != NULL)
15045 p = vim_strsave(p);
15046#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015047 rettv->v_type = VAR_STRING;
15048 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049}
15050
15051/*
15052 * "synIDtrans(id)" function
15053 */
15054/*ARGSUSED*/
15055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015057 typval_T *argvars;
15058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059{
15060 int id;
15061
15062#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015063 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064
15065 if (id > 0)
15066 id = syn_get_final_id(id);
15067 else
15068#endif
15069 id = 0;
15070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015071 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072}
15073
15074/*
15075 * "system()" function
15076 */
15077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015078f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015079 typval_T *argvars;
15080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015082 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015084 char_u *infile = NULL;
15085 char_u buf[NUMBUFLEN];
15086 int err = FALSE;
15087 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015089 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015090 {
15091 /*
15092 * Write the string to a temp file, to be used for input of the shell
15093 * command.
15094 */
15095 if ((infile = vim_tempname('i')) == NULL)
15096 {
15097 EMSG(_(e_notmp));
15098 return;
15099 }
15100
15101 fd = mch_fopen((char *)infile, WRITEBIN);
15102 if (fd == NULL)
15103 {
15104 EMSG2(_(e_notopen), infile);
15105 goto done;
15106 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015107 p = get_tv_string_buf_chk(&argvars[1], buf);
15108 if (p == NULL)
15109 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015110 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15111 err = TRUE;
15112 if (fclose(fd) != 0)
15113 err = TRUE;
15114 if (err)
15115 {
15116 EMSG(_("E677: Error writing temp file"));
15117 goto done;
15118 }
15119 }
15120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015122
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123#ifdef USE_CR
15124 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015125 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 {
15127 char_u *s;
15128
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015129 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130 {
15131 if (*s == CAR)
15132 *s = NL;
15133 }
15134 }
15135#else
15136# ifdef USE_CRNL
15137 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015138 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139 {
15140 char_u *s, *d;
15141
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015142 d = res;
15143 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 {
15145 if (s[0] == CAR && s[1] == NL)
15146 ++s;
15147 *d++ = *s;
15148 }
15149 *d = NUL;
15150 }
15151# endif
15152#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015153
15154done:
15155 if (infile != NULL)
15156 {
15157 mch_remove(infile);
15158 vim_free(infile);
15159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015160 rettv->v_type = VAR_STRING;
15161 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162}
15163
15164/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015165 * "tabpagebuflist()" function
15166 */
15167/* ARGSUSED */
15168 static void
15169f_tabpagebuflist(argvars, rettv)
15170 typval_T *argvars;
15171 typval_T *rettv;
15172{
15173#ifndef FEAT_WINDOWS
15174 rettv->vval.v_number = 0;
15175#else
15176 tabpage_T *tp;
15177 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015178
15179 if (argvars[0].v_type == VAR_UNKNOWN)
15180 wp = firstwin;
15181 else
15182 {
15183 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15184 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015185 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015186 }
15187 if (wp == NULL)
15188 rettv->vval.v_number = 0;
15189 else
15190 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015191 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015192 rettv->vval.v_number = 0;
15193 else
15194 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015195 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015196 if (list_append_number(rettv->vval.v_list,
15197 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015198 break;
15199 }
15200 }
15201#endif
15202}
15203
15204
15205/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015206 * "tabpagenr()" function
15207 */
15208/* ARGSUSED */
15209 static void
15210f_tabpagenr(argvars, rettv)
15211 typval_T *argvars;
15212 typval_T *rettv;
15213{
15214 int nr = 1;
15215#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015216 char_u *arg;
15217
15218 if (argvars[0].v_type != VAR_UNKNOWN)
15219 {
15220 arg = get_tv_string_chk(&argvars[0]);
15221 nr = 0;
15222 if (arg != NULL)
15223 {
15224 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015225 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015226 else
15227 EMSG2(_(e_invexpr2), arg);
15228 }
15229 }
15230 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015231 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015232#endif
15233 rettv->vval.v_number = nr;
15234}
15235
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015236
15237#ifdef FEAT_WINDOWS
15238static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15239
15240/*
15241 * Common code for tabpagewinnr() and winnr().
15242 */
15243 static int
15244get_winnr(tp, argvar)
15245 tabpage_T *tp;
15246 typval_T *argvar;
15247{
15248 win_T *twin;
15249 int nr = 1;
15250 win_T *wp;
15251 char_u *arg;
15252
15253 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15254 if (argvar->v_type != VAR_UNKNOWN)
15255 {
15256 arg = get_tv_string_chk(argvar);
15257 if (arg == NULL)
15258 nr = 0; /* type error; errmsg already given */
15259 else if (STRCMP(arg, "$") == 0)
15260 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15261 else if (STRCMP(arg, "#") == 0)
15262 {
15263 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15264 if (twin == NULL)
15265 nr = 0;
15266 }
15267 else
15268 {
15269 EMSG2(_(e_invexpr2), arg);
15270 nr = 0;
15271 }
15272 }
15273
15274 if (nr > 0)
15275 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15276 wp != twin; wp = wp->w_next)
15277 ++nr;
15278 return nr;
15279}
15280#endif
15281
15282/*
15283 * "tabpagewinnr()" function
15284 */
15285/* ARGSUSED */
15286 static void
15287f_tabpagewinnr(argvars, rettv)
15288 typval_T *argvars;
15289 typval_T *rettv;
15290{
15291 int nr = 1;
15292#ifdef FEAT_WINDOWS
15293 tabpage_T *tp;
15294
15295 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15296 if (tp == NULL)
15297 nr = 0;
15298 else
15299 nr = get_winnr(tp, &argvars[1]);
15300#endif
15301 rettv->vval.v_number = nr;
15302}
15303
15304
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015305/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015306 * "tagfiles()" function
15307 */
15308/*ARGSUSED*/
15309 static void
15310f_tagfiles(argvars, rettv)
15311 typval_T *argvars;
15312 typval_T *rettv;
15313{
15314 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015315 tagname_T tn;
15316 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015317
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015318 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015319 {
15320 rettv->vval.v_number = 0;
15321 return;
15322 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015323
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015324 for (first = TRUE; ; first = FALSE)
15325 if (get_tagfname(&tn, first, fname) == FAIL
15326 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015327 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015328 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015329}
15330
15331/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015332 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015333 */
15334 static void
15335f_taglist(argvars, rettv)
15336 typval_T *argvars;
15337 typval_T *rettv;
15338{
15339 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015340
15341 tag_pattern = get_tv_string(&argvars[0]);
15342
15343 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015344 if (*tag_pattern == NUL)
15345 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015346
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015347 if (rettv_list_alloc(rettv) == OK)
15348 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015349}
15350
15351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 * "tempname()" function
15353 */
15354/*ARGSUSED*/
15355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015357 typval_T *argvars;
15358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359{
15360 static int x = 'A';
15361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 rettv->v_type = VAR_STRING;
15363 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364
15365 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15366 * names. Skip 'I' and 'O', they are used for shell redirection. */
15367 do
15368 {
15369 if (x == 'Z')
15370 x = '0';
15371 else if (x == '9')
15372 x = 'A';
15373 else
15374 {
15375#ifdef EBCDIC
15376 if (x == 'I')
15377 x = 'J';
15378 else if (x == 'R')
15379 x = 'S';
15380 else
15381#endif
15382 ++x;
15383 }
15384 } while (x == 'I' || x == 'O');
15385}
15386
15387/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015388 * "test(list)" function: Just checking the walls...
15389 */
15390/*ARGSUSED*/
15391 static void
15392f_test(argvars, rettv)
15393 typval_T *argvars;
15394 typval_T *rettv;
15395{
15396 /* Used for unit testing. Change the code below to your liking. */
15397#if 0
15398 listitem_T *li;
15399 list_T *l;
15400 char_u *bad, *good;
15401
15402 if (argvars[0].v_type != VAR_LIST)
15403 return;
15404 l = argvars[0].vval.v_list;
15405 if (l == NULL)
15406 return;
15407 li = l->lv_first;
15408 if (li == NULL)
15409 return;
15410 bad = get_tv_string(&li->li_tv);
15411 li = li->li_next;
15412 if (li == NULL)
15413 return;
15414 good = get_tv_string(&li->li_tv);
15415 rettv->vval.v_number = test_edit_score(bad, good);
15416#endif
15417}
15418
15419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 * "tolower(string)" function
15421 */
15422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015424 typval_T *argvars;
15425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426{
15427 char_u *p;
15428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 p = vim_strsave(get_tv_string(&argvars[0]));
15430 rettv->v_type = VAR_STRING;
15431 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432
15433 if (p != NULL)
15434 while (*p != NUL)
15435 {
15436#ifdef FEAT_MBYTE
15437 int l;
15438
15439 if (enc_utf8)
15440 {
15441 int c, lc;
15442
15443 c = utf_ptr2char(p);
15444 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015445 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 /* TODO: reallocate string when byte count changes. */
15447 if (utf_char2len(lc) == l)
15448 utf_char2bytes(lc, p);
15449 p += l;
15450 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015451 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 p += l; /* skip multi-byte character */
15453 else
15454#endif
15455 {
15456 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15457 ++p;
15458 }
15459 }
15460}
15461
15462/*
15463 * "toupper(string)" function
15464 */
15465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015467 typval_T *argvars;
15468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015471 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472}
15473
15474/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015475 * "tr(string, fromstr, tostr)" function
15476 */
15477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015479 typval_T *argvars;
15480 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015481{
15482 char_u *instr;
15483 char_u *fromstr;
15484 char_u *tostr;
15485 char_u *p;
15486#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015487 int inlen;
15488 int fromlen;
15489 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015490 int idx;
15491 char_u *cpstr;
15492 int cplen;
15493 int first = TRUE;
15494#endif
15495 char_u buf[NUMBUFLEN];
15496 char_u buf2[NUMBUFLEN];
15497 garray_T ga;
15498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015500 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15501 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015502
15503 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015504 rettv->v_type = VAR_STRING;
15505 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015506 if (fromstr == NULL || tostr == NULL)
15507 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015508 ga_init2(&ga, (int)sizeof(char), 80);
15509
15510#ifdef FEAT_MBYTE
15511 if (!has_mbyte)
15512#endif
15513 /* not multi-byte: fromstr and tostr must be the same length */
15514 if (STRLEN(fromstr) != STRLEN(tostr))
15515 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015516#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015517error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015518#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015519 EMSG2(_(e_invarg2), fromstr);
15520 ga_clear(&ga);
15521 return;
15522 }
15523
15524 /* fromstr and tostr have to contain the same number of chars */
15525 while (*instr != NUL)
15526 {
15527#ifdef FEAT_MBYTE
15528 if (has_mbyte)
15529 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015530 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015531 cpstr = instr;
15532 cplen = inlen;
15533 idx = 0;
15534 for (p = fromstr; *p != NUL; p += fromlen)
15535 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015536 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015537 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15538 {
15539 for (p = tostr; *p != NUL; p += tolen)
15540 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015541 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015542 if (idx-- == 0)
15543 {
15544 cplen = tolen;
15545 cpstr = p;
15546 break;
15547 }
15548 }
15549 if (*p == NUL) /* tostr is shorter than fromstr */
15550 goto error;
15551 break;
15552 }
15553 ++idx;
15554 }
15555
15556 if (first && cpstr == instr)
15557 {
15558 /* Check that fromstr and tostr have the same number of
15559 * (multi-byte) characters. Done only once when a character
15560 * of instr doesn't appear in fromstr. */
15561 first = FALSE;
15562 for (p = tostr; *p != NUL; p += tolen)
15563 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015564 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015565 --idx;
15566 }
15567 if (idx != 0)
15568 goto error;
15569 }
15570
15571 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015572 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015573 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015574
15575 instr += inlen;
15576 }
15577 else
15578#endif
15579 {
15580 /* When not using multi-byte chars we can do it faster. */
15581 p = vim_strchr(fromstr, *instr);
15582 if (p != NULL)
15583 ga_append(&ga, tostr[p - fromstr]);
15584 else
15585 ga_append(&ga, *instr);
15586 ++instr;
15587 }
15588 }
15589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015591}
15592
15593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 * "type(expr)" function
15595 */
15596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015597f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015598 typval_T *argvars;
15599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015601 int n;
15602
15603 switch (argvars[0].v_type)
15604 {
15605 case VAR_NUMBER: n = 0; break;
15606 case VAR_STRING: n = 1; break;
15607 case VAR_FUNC: n = 2; break;
15608 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015609 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015610 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15611 }
15612 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613}
15614
15615/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015616 * "values(dict)" function
15617 */
15618 static void
15619f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015620 typval_T *argvars;
15621 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015622{
15623 dict_list(argvars, rettv, 1);
15624}
15625
15626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 * "virtcol(string)" function
15628 */
15629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015630f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015631 typval_T *argvars;
15632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633{
15634 colnr_T vcol = 0;
15635 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015636 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015638 fp = var2fpos(&argvars[0], FALSE, &fnum);
15639 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15640 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 {
15642 getvvcol(curwin, fp, NULL, NULL, &vcol);
15643 ++vcol;
15644 }
15645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015646 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647}
15648
15649/*
15650 * "visualmode()" function
15651 */
15652/*ARGSUSED*/
15653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015655 typval_T *argvars;
15656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657{
15658#ifdef FEAT_VISUAL
15659 char_u str[2];
15660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 str[0] = curbuf->b_visual_mode_eval;
15663 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015664 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665
15666 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015667 if ((argvars[0].v_type == VAR_NUMBER
15668 && argvars[0].vval.v_number != 0)
15669 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015670 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 curbuf->b_visual_mode_eval = NUL;
15672#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674#endif
15675}
15676
15677/*
15678 * "winbufnr(nr)" function
15679 */
15680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015681f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015682 typval_T *argvars;
15683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684{
15685 win_T *wp;
15686
15687 wp = find_win_by_nr(&argvars[0]);
15688 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692}
15693
15694/*
15695 * "wincol()" function
15696 */
15697/*ARGSUSED*/
15698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015699f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015700 typval_T *argvars;
15701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702{
15703 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015704 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705}
15706
15707/*
15708 * "winheight(nr)" function
15709 */
15710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015711f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015712 typval_T *argvars;
15713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714{
15715 win_T *wp;
15716
15717 wp = find_win_by_nr(&argvars[0]);
15718 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722}
15723
15724/*
15725 * "winline()" function
15726 */
15727/*ARGSUSED*/
15728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015729f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015730 typval_T *argvars;
15731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732{
15733 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015734 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735}
15736
15737/*
15738 * "winnr()" function
15739 */
15740/* ARGSUSED */
15741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015742f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015743 typval_T *argvars;
15744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745{
15746 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015747
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015749 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752}
15753
15754/*
15755 * "winrestcmd()" function
15756 */
15757/* ARGSUSED */
15758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015760 typval_T *argvars;
15761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762{
15763#ifdef FEAT_WINDOWS
15764 win_T *wp;
15765 int winnr = 1;
15766 garray_T ga;
15767 char_u buf[50];
15768
15769 ga_init2(&ga, (int)sizeof(char), 70);
15770 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15771 {
15772 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15773 ga_concat(&ga, buf);
15774# ifdef FEAT_VERTSPLIT
15775 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15776 ga_concat(&ga, buf);
15777# endif
15778 ++winnr;
15779 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015780 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015782 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787}
15788
15789/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015790 * "winrestview()" function
15791 */
15792/* ARGSUSED */
15793 static void
15794f_winrestview(argvars, rettv)
15795 typval_T *argvars;
15796 typval_T *rettv;
15797{
15798 dict_T *dict;
15799
15800 if (argvars[0].v_type != VAR_DICT
15801 || (dict = argvars[0].vval.v_dict) == NULL)
15802 EMSG(_(e_invarg));
15803 else
15804 {
15805 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15806 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15807#ifdef FEAT_VIRTUALEDIT
15808 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15809#endif
15810 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015811 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015812
15813 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15814#ifdef FEAT_DIFF
15815 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15816#endif
15817 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15818 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15819
15820 check_cursor();
15821 changed_cline_bef_curs();
15822 invalidate_botline();
15823 redraw_later(VALID);
15824
15825 if (curwin->w_topline == 0)
15826 curwin->w_topline = 1;
15827 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15828 curwin->w_topline = curbuf->b_ml.ml_line_count;
15829#ifdef FEAT_DIFF
15830 check_topfill(curwin, TRUE);
15831#endif
15832 }
15833}
15834
15835/*
15836 * "winsaveview()" function
15837 */
15838/* ARGSUSED */
15839 static void
15840f_winsaveview(argvars, rettv)
15841 typval_T *argvars;
15842 typval_T *rettv;
15843{
15844 dict_T *dict;
15845
15846 dict = dict_alloc();
15847 if (dict == NULL)
15848 return;
15849 rettv->v_type = VAR_DICT;
15850 rettv->vval.v_dict = dict;
15851 ++dict->dv_refcount;
15852
15853 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15854 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15855#ifdef FEAT_VIRTUALEDIT
15856 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15857#endif
15858 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15859
15860 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15861#ifdef FEAT_DIFF
15862 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15863#endif
15864 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15865 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15866}
15867
15868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 * "winwidth(nr)" function
15870 */
15871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015872f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 typval_T *argvars;
15874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875{
15876 win_T *wp;
15877
15878 wp = find_win_by_nr(&argvars[0]);
15879 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015880 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 else
15882#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015883 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886#endif
15887}
15888
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015890 * "writefile()" function
15891 */
15892 static void
15893f_writefile(argvars, rettv)
15894 typval_T *argvars;
15895 typval_T *rettv;
15896{
15897 int binary = FALSE;
15898 char_u *fname;
15899 FILE *fd;
15900 listitem_T *li;
15901 char_u *s;
15902 int ret = 0;
15903 int c;
15904
15905 if (argvars[0].v_type != VAR_LIST)
15906 {
15907 EMSG2(_(e_listarg), "writefile()");
15908 return;
15909 }
15910 if (argvars[0].vval.v_list == NULL)
15911 return;
15912
15913 if (argvars[2].v_type != VAR_UNKNOWN
15914 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15915 binary = TRUE;
15916
15917 /* Always open the file in binary mode, library functions have a mind of
15918 * their own about CR-LF conversion. */
15919 fname = get_tv_string(&argvars[1]);
15920 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15921 {
15922 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15923 ret = -1;
15924 }
15925 else
15926 {
15927 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15928 li = li->li_next)
15929 {
15930 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15931 {
15932 if (*s == '\n')
15933 c = putc(NUL, fd);
15934 else
15935 c = putc(*s, fd);
15936 if (c == EOF)
15937 {
15938 ret = -1;
15939 break;
15940 }
15941 }
15942 if (!binary || li->li_next != NULL)
15943 if (putc('\n', fd) == EOF)
15944 {
15945 ret = -1;
15946 break;
15947 }
15948 if (ret < 0)
15949 {
15950 EMSG(_(e_write));
15951 break;
15952 }
15953 }
15954 fclose(fd);
15955 }
15956
15957 rettv->vval.v_number = ret;
15958}
15959
15960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015962 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963 */
15964 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015965var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015966 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015968 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015969{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015970 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015972 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973
Bram Moolenaara5525202006-03-02 22:52:09 +000015974 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015975 if (varp->v_type == VAR_LIST)
15976 {
15977 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015978 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015979 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015980
15981 l = varp->vval.v_list;
15982 if (l == NULL)
15983 return NULL;
15984
15985 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015986 pos.lnum = list_find_nr(l, 0L, &error);
15987 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015988 return NULL; /* invalid line number */
15989
15990 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015991 pos.col = list_find_nr(l, 1L, &error);
15992 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015993 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015994 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015995 /* Accept a position up to the NUL after the line. */
15996 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015997 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015998 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015999
Bram Moolenaara5525202006-03-02 22:52:09 +000016000#ifdef FEAT_VIRTUALEDIT
16001 /* Get the virtual offset. Defaults to zero. */
16002 pos.coladd = list_find_nr(l, 2L, &error);
16003 if (error)
16004 pos.coladd = 0;
16005#endif
16006
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016007 return &pos;
16008 }
16009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016010 name = get_tv_string_chk(varp);
16011 if (name == NULL)
16012 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 if (name[0] == '.') /* cursor */
16014 return &curwin->w_cursor;
16015 if (name[0] == '\'') /* mark */
16016 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016017 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16019 return NULL;
16020 return pp;
16021 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016022
16023#ifdef FEAT_VIRTUALEDIT
16024 pos.coladd = 0;
16025#endif
16026
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016027 if (name[0] == 'w' && lnum)
16028 {
16029 pos.col = 0;
16030 if (name[1] == '0') /* "w0": first visible line */
16031 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016032 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016033 pos.lnum = curwin->w_topline;
16034 return &pos;
16035 }
16036 else if (name[1] == '$') /* "w$": last visible line */
16037 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016038 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016039 pos.lnum = curwin->w_botline - 1;
16040 return &pos;
16041 }
16042 }
16043 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044 {
16045 if (lnum)
16046 {
16047 pos.lnum = curbuf->b_ml.ml_line_count;
16048 pos.col = 0;
16049 }
16050 else
16051 {
16052 pos.lnum = curwin->w_cursor.lnum;
16053 pos.col = (colnr_T)STRLEN(ml_get_curline());
16054 }
16055 return &pos;
16056 }
16057 return NULL;
16058}
16059
16060/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016061 * Convert list in "arg" into a position and optional file number.
16062 * When "fnump" is NULL there is no file number, only 3 items.
16063 * Note that the column is passed on as-is, the caller may want to decrement
16064 * it to use 1 for the first column.
16065 * Return FAIL when conversion is not possible, doesn't check the position for
16066 * validity.
16067 */
16068 static int
16069list2fpos(arg, posp, fnump)
16070 typval_T *arg;
16071 pos_T *posp;
16072 int *fnump;
16073{
16074 list_T *l = arg->vval.v_list;
16075 long i = 0;
16076 long n;
16077
16078 /* List must be: [fnum, lnum, col, coladd] */
16079 if (arg->v_type != VAR_LIST || l == NULL
16080 || l->lv_len != (fnump == NULL ? 3 : 4))
16081 return FAIL;
16082
16083 if (fnump != NULL)
16084 {
16085 n = list_find_nr(l, i++, NULL); /* fnum */
16086 if (n < 0)
16087 return FAIL;
16088 if (n == 0)
16089 n = curbuf->b_fnum; /* current buffer */
16090 *fnump = n;
16091 }
16092
16093 n = list_find_nr(l, i++, NULL); /* lnum */
16094 if (n < 0)
16095 return FAIL;
16096 posp->lnum = n;
16097
16098 n = list_find_nr(l, i++, NULL); /* col */
16099 if (n < 0)
16100 return FAIL;
16101 posp->col = n;
16102
16103#ifdef FEAT_VIRTUALEDIT
16104 n = list_find_nr(l, i, NULL);
16105 if (n < 0)
16106 return FAIL;
16107 posp->coladd = n;
16108#endif
16109
16110 return OK;
16111}
16112
16113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 * Get the length of an environment variable name.
16115 * Advance "arg" to the first character after the name.
16116 * Return 0 for error.
16117 */
16118 static int
16119get_env_len(arg)
16120 char_u **arg;
16121{
16122 char_u *p;
16123 int len;
16124
16125 for (p = *arg; vim_isIDc(*p); ++p)
16126 ;
16127 if (p == *arg) /* no name found */
16128 return 0;
16129
16130 len = (int)(p - *arg);
16131 *arg = p;
16132 return len;
16133}
16134
16135/*
16136 * Get the length of the name of a function or internal variable.
16137 * "arg" is advanced to the first non-white character after the name.
16138 * Return 0 if something is wrong.
16139 */
16140 static int
16141get_id_len(arg)
16142 char_u **arg;
16143{
16144 char_u *p;
16145 int len;
16146
16147 /* Find the end of the name. */
16148 for (p = *arg; eval_isnamec(*p); ++p)
16149 ;
16150 if (p == *arg) /* no name found */
16151 return 0;
16152
16153 len = (int)(p - *arg);
16154 *arg = skipwhite(p);
16155
16156 return len;
16157}
16158
16159/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016160 * Get the length of the name of a variable or function.
16161 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016163 * Return -1 if curly braces expansion failed.
16164 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 * If the name contains 'magic' {}'s, expand them and return the
16166 * expanded name in an allocated string via 'alias' - caller must free.
16167 */
16168 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016169get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 char_u **arg;
16171 char_u **alias;
16172 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016173 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174{
16175 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 char_u *p;
16177 char_u *expr_start;
16178 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179
16180 *alias = NULL; /* default to no alias */
16181
16182 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16183 && (*arg)[2] == (int)KE_SNR)
16184 {
16185 /* hard coded <SNR>, already translated */
16186 *arg += 3;
16187 return get_id_len(arg) + 3;
16188 }
16189 len = eval_fname_script(*arg);
16190 if (len > 0)
16191 {
16192 /* literal "<SID>", "s:" or "<SNR>" */
16193 *arg += len;
16194 }
16195
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016197 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016199 p = find_name_end(*arg, &expr_start, &expr_end,
16200 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 if (expr_start != NULL)
16202 {
16203 char_u *temp_string;
16204
16205 if (!evaluate)
16206 {
16207 len += (int)(p - *arg);
16208 *arg = skipwhite(p);
16209 return len;
16210 }
16211
16212 /*
16213 * Include any <SID> etc in the expanded string:
16214 * Thus the -len here.
16215 */
16216 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16217 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016218 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 *alias = temp_string;
16220 *arg = skipwhite(p);
16221 return (int)STRLEN(temp_string);
16222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223
16224 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016225 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 EMSG2(_(e_invexpr2), *arg);
16227
16228 return len;
16229}
16230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016231/*
16232 * Find the end of a variable or function name, taking care of magic braces.
16233 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16234 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016235 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016236 * Return a pointer to just after the name. Equal to "arg" if there is no
16237 * valid name.
16238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016240find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 char_u *arg;
16242 char_u **expr_start;
16243 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016244 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 int mb_nest = 0;
16247 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 char_u *p;
16249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252 *expr_start = NULL;
16253 *expr_end = NULL;
16254 }
16255
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016256 /* Quick check for valid starting character. */
16257 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16258 return arg;
16259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 for (p = arg; *p != NUL
16261 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016262 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016263 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016264 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016265 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016266 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016267 if (*p == '\'')
16268 {
16269 /* skip over 'string' to avoid counting [ and ] inside it. */
16270 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16271 ;
16272 if (*p == NUL)
16273 break;
16274 }
16275 else if (*p == '"')
16276 {
16277 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16278 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16279 if (*p == '\\' && p[1] != NUL)
16280 ++p;
16281 if (*p == NUL)
16282 break;
16283 }
16284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016285 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016287 if (*p == '[')
16288 ++br_nest;
16289 else if (*p == ']')
16290 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016293 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016295 if (*p == '{')
16296 {
16297 mb_nest++;
16298 if (expr_start != NULL && *expr_start == NULL)
16299 *expr_start = p;
16300 }
16301 else if (*p == '}')
16302 {
16303 mb_nest--;
16304 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16305 *expr_end = p;
16306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 }
16309
16310 return p;
16311}
16312
16313/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016314 * Expands out the 'magic' {}'s in a variable/function name.
16315 * Note that this can call itself recursively, to deal with
16316 * constructs like foo{bar}{baz}{bam}
16317 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16318 * "in_start" ^
16319 * "expr_start" ^
16320 * "expr_end" ^
16321 * "in_end" ^
16322 *
16323 * Returns a new allocated string, which the caller must free.
16324 * Returns NULL for failure.
16325 */
16326 static char_u *
16327make_expanded_name(in_start, expr_start, expr_end, in_end)
16328 char_u *in_start;
16329 char_u *expr_start;
16330 char_u *expr_end;
16331 char_u *in_end;
16332{
16333 char_u c1;
16334 char_u *retval = NULL;
16335 char_u *temp_result;
16336 char_u *nextcmd = NULL;
16337
16338 if (expr_end == NULL || in_end == NULL)
16339 return NULL;
16340 *expr_start = NUL;
16341 *expr_end = NUL;
16342 c1 = *in_end;
16343 *in_end = NUL;
16344
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016345 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016346 if (temp_result != NULL && nextcmd == NULL)
16347 {
16348 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16349 + (in_end - expr_end) + 1));
16350 if (retval != NULL)
16351 {
16352 STRCPY(retval, in_start);
16353 STRCAT(retval, temp_result);
16354 STRCAT(retval, expr_end + 1);
16355 }
16356 }
16357 vim_free(temp_result);
16358
16359 *in_end = c1; /* put char back for error messages */
16360 *expr_start = '{';
16361 *expr_end = '}';
16362
16363 if (retval != NULL)
16364 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016365 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016366 if (expr_start != NULL)
16367 {
16368 /* Further expansion! */
16369 temp_result = make_expanded_name(retval, expr_start,
16370 expr_end, temp_result);
16371 vim_free(retval);
16372 retval = temp_result;
16373 }
16374 }
16375
16376 return retval;
16377}
16378
16379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016381 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 */
16383 static int
16384eval_isnamec(c)
16385 int c;
16386{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016387 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16388}
16389
16390/*
16391 * Return TRUE if character "c" can be used as the first character in a
16392 * variable or function name (excluding '{' and '}').
16393 */
16394 static int
16395eval_isnamec1(c)
16396 int c;
16397{
16398 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399}
16400
16401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 * Set number v: variable to "val".
16403 */
16404 void
16405set_vim_var_nr(idx, val)
16406 int idx;
16407 long val;
16408{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016409 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410}
16411
16412/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016413 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 */
16415 long
16416get_vim_var_nr(idx)
16417 int idx;
16418{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016419 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420}
16421
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016422#if defined(FEAT_AUTOCMD) || defined(PROTO)
16423/*
16424 * Get string v: variable value. Uses a static buffer, can only be used once.
16425 */
16426 char_u *
16427get_vim_var_str(idx)
16428 int idx;
16429{
16430 return get_tv_string(&vimvars[idx].vv_tv);
16431}
16432#endif
16433
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434/*
16435 * Set v:count, v:count1 and v:prevcount.
16436 */
16437 void
16438set_vcount(count, count1)
16439 long count;
16440 long count1;
16441{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016442 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16443 vimvars[VV_COUNT].vv_nr = count;
16444 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445}
16446
16447/*
16448 * Set string v: variable to a copy of "val".
16449 */
16450 void
16451set_vim_var_string(idx, val, len)
16452 int idx;
16453 char_u *val;
16454 int len; /* length of "val" to use or -1 (whole string) */
16455{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016456 /* Need to do this (at least) once, since we can't initialize a union.
16457 * Will always be invoked when "v:progname" is set. */
16458 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16459
Bram Moolenaare9a41262005-01-15 22:18:47 +000016460 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016462 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016464 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016466 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467}
16468
16469/*
16470 * Set v:register if needed.
16471 */
16472 void
16473set_reg_var(c)
16474 int c;
16475{
16476 char_u regname;
16477
16478 if (c == 0 || c == ' ')
16479 regname = '"';
16480 else
16481 regname = c;
16482 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016483 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 set_vim_var_string(VV_REG, &regname, 1);
16485}
16486
16487/*
16488 * Get or set v:exception. If "oldval" == NULL, return the current value.
16489 * Otherwise, restore the value to "oldval" and return NULL.
16490 * Must always be called in pairs to save and restore v:exception! Does not
16491 * take care of memory allocations.
16492 */
16493 char_u *
16494v_exception(oldval)
16495 char_u *oldval;
16496{
16497 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016498 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499
Bram Moolenaare9a41262005-01-15 22:18:47 +000016500 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501 return NULL;
16502}
16503
16504/*
16505 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16506 * Otherwise, restore the value to "oldval" and return NULL.
16507 * Must always be called in pairs to save and restore v:throwpoint! Does not
16508 * take care of memory allocations.
16509 */
16510 char_u *
16511v_throwpoint(oldval)
16512 char_u *oldval;
16513{
16514 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016515 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516
Bram Moolenaare9a41262005-01-15 22:18:47 +000016517 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518 return NULL;
16519}
16520
16521#if defined(FEAT_AUTOCMD) || defined(PROTO)
16522/*
16523 * Set v:cmdarg.
16524 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16525 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16526 * Must always be called in pairs!
16527 */
16528 char_u *
16529set_cmdarg(eap, oldarg)
16530 exarg_T *eap;
16531 char_u *oldarg;
16532{
16533 char_u *oldval;
16534 char_u *newval;
16535 unsigned len;
16536
Bram Moolenaare9a41262005-01-15 22:18:47 +000016537 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016538 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016540 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016541 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016542 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 }
16544
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016545 if (eap->force_bin == FORCE_BIN)
16546 len = 6;
16547 else if (eap->force_bin == FORCE_NOBIN)
16548 len = 8;
16549 else
16550 len = 0;
16551 if (eap->force_ff != 0)
16552 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16553# ifdef FEAT_MBYTE
16554 if (eap->force_enc != 0)
16555 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016556 if (eap->bad_char != 0)
16557 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016558# endif
16559
16560 newval = alloc(len + 1);
16561 if (newval == NULL)
16562 return NULL;
16563
16564 if (eap->force_bin == FORCE_BIN)
16565 sprintf((char *)newval, " ++bin");
16566 else if (eap->force_bin == FORCE_NOBIN)
16567 sprintf((char *)newval, " ++nobin");
16568 else
16569 *newval = NUL;
16570 if (eap->force_ff != 0)
16571 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16572 eap->cmd + eap->force_ff);
16573# ifdef FEAT_MBYTE
16574 if (eap->force_enc != 0)
16575 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16576 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016577 if (eap->bad_char != 0)
16578 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16579 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016580# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016581 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016582 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583}
16584#endif
16585
16586/*
16587 * Get the value of internal variable "name".
16588 * Return OK or FAIL.
16589 */
16590 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016591get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 char_u *name;
16593 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016594 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016595 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016596{
16597 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016598 typval_T *tv = NULL;
16599 typval_T atv;
16600 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602
16603 /* truncate the name, so that we can use strcmp() */
16604 cc = name[len];
16605 name[len] = NUL;
16606
16607 /*
16608 * Check for "b:changedtick".
16609 */
16610 if (STRCMP(name, "b:changedtick") == 0)
16611 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016612 atv.v_type = VAR_NUMBER;
16613 atv.vval.v_number = curbuf->b_changedtick;
16614 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615 }
16616
16617 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618 * Check for user-defined variables.
16619 */
16620 else
16621 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016622 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 }
16626
Bram Moolenaare9a41262005-01-15 22:18:47 +000016627 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016630 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 ret = FAIL;
16632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016634 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635
16636 name[len] = cc;
16637
16638 return ret;
16639}
16640
16641/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016642 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16643 * Also handle function call with Funcref variable: func(expr)
16644 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16645 */
16646 static int
16647handle_subscript(arg, rettv, evaluate, verbose)
16648 char_u **arg;
16649 typval_T *rettv;
16650 int evaluate; /* do more than finding the end */
16651 int verbose; /* give error messages */
16652{
16653 int ret = OK;
16654 dict_T *selfdict = NULL;
16655 char_u *s;
16656 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016657 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016658
16659 while (ret == OK
16660 && (**arg == '['
16661 || (**arg == '.' && rettv->v_type == VAR_DICT)
16662 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16663 && !vim_iswhite(*(*arg - 1)))
16664 {
16665 if (**arg == '(')
16666 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016667 /* need to copy the funcref so that we can clear rettv */
16668 functv = *rettv;
16669 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016670
16671 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016672 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016673 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016674 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16675 &len, evaluate, selfdict);
16676
16677 /* Clear the funcref afterwards, so that deleting it while
16678 * evaluating the arguments is possible (see test55). */
16679 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016680
16681 /* Stop the expression evaluation when immediately aborting on
16682 * error, or when an interrupt occurred or an exception was thrown
16683 * but not caught. */
16684 if (aborting())
16685 {
16686 if (ret == OK)
16687 clear_tv(rettv);
16688 ret = FAIL;
16689 }
16690 dict_unref(selfdict);
16691 selfdict = NULL;
16692 }
16693 else /* **arg == '[' || **arg == '.' */
16694 {
16695 dict_unref(selfdict);
16696 if (rettv->v_type == VAR_DICT)
16697 {
16698 selfdict = rettv->vval.v_dict;
16699 if (selfdict != NULL)
16700 ++selfdict->dv_refcount;
16701 }
16702 else
16703 selfdict = NULL;
16704 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16705 {
16706 clear_tv(rettv);
16707 ret = FAIL;
16708 }
16709 }
16710 }
16711 dict_unref(selfdict);
16712 return ret;
16713}
16714
16715/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016716 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16717 * value).
16718 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016719 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016720alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016721{
Bram Moolenaar33570922005-01-25 22:26:29 +000016722 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016723}
16724
16725/*
16726 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 * The string "s" must have been allocated, it is consumed.
16728 * Return NULL for out of memory, the variable otherwise.
16729 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016731alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016732 char_u *s;
16733{
Bram Moolenaar33570922005-01-25 22:26:29 +000016734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016736 rettv = alloc_tv();
16737 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016739 rettv->v_type = VAR_STRING;
16740 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 }
16742 else
16743 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016744 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745}
16746
16747/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016748 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016750 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016752 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753{
16754 if (varp != NULL)
16755 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016756 switch (varp->v_type)
16757 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016758 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016759 func_unref(varp->vval.v_string);
16760 /*FALLTHROUGH*/
16761 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016762 vim_free(varp->vval.v_string);
16763 break;
16764 case VAR_LIST:
16765 list_unref(varp->vval.v_list);
16766 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016767 case VAR_DICT:
16768 dict_unref(varp->vval.v_dict);
16769 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016770 case VAR_NUMBER:
16771 case VAR_UNKNOWN:
16772 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016773 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016774 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016775 break;
16776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 vim_free(varp);
16778 }
16779}
16780
16781/*
16782 * Free the memory for a variable value and set the value to NULL or 0.
16783 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016784 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016785clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016786 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787{
16788 if (varp != NULL)
16789 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016790 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016792 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016793 func_unref(varp->vval.v_string);
16794 /*FALLTHROUGH*/
16795 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016796 vim_free(varp->vval.v_string);
16797 varp->vval.v_string = NULL;
16798 break;
16799 case VAR_LIST:
16800 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016801 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016802 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016803 case VAR_DICT:
16804 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016805 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016806 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016807 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016808 varp->vval.v_number = 0;
16809 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810 case VAR_UNKNOWN:
16811 break;
16812 default:
16813 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016815 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816 }
16817}
16818
16819/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016820 * Set the value of a variable to NULL without freeing items.
16821 */
16822 static void
16823init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016824 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016825{
16826 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016828}
16829
16830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831 * Get the number value of a variable.
16832 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016833 * For incompatible types, return 0.
16834 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16835 * caller of incompatible types: it sets *denote to TRUE if "denote"
16836 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837 */
16838 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016839get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016840 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016842 int error = FALSE;
16843
16844 return get_tv_number_chk(varp, &error); /* return 0L on error */
16845}
16846
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016847 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016848get_tv_number_chk(varp, denote)
16849 typval_T *varp;
16850 int *denote;
16851{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016852 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016854 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016856 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016857 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016858 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016859 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016860 break;
16861 case VAR_STRING:
16862 if (varp->vval.v_string != NULL)
16863 vim_str2nr(varp->vval.v_string, NULL, NULL,
16864 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016865 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016866 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016867 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016868 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016869 case VAR_DICT:
16870 EMSG(_("E728: Using a Dictionary as a number"));
16871 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016872 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016873 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016874 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016876 if (denote == NULL) /* useful for values that must be unsigned */
16877 n = -1;
16878 else
16879 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016880 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881}
16882
16883/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016884 * Get the lnum from the first argument.
16885 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016886 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 */
16888 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891{
Bram Moolenaar33570922005-01-25 22:26:29 +000016892 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 linenr_T lnum;
16894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 if (lnum == 0) /* no valid number, try using line() */
16897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016898 rettv.v_type = VAR_NUMBER;
16899 f_line(argvars, &rettv);
16900 lnum = rettv.vval.v_number;
16901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 }
16903 return lnum;
16904}
16905
16906/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016907 * Get the lnum from the first argument.
16908 * Also accepts "$", then "buf" is used.
16909 * Returns 0 on error.
16910 */
16911 static linenr_T
16912get_tv_lnum_buf(argvars, buf)
16913 typval_T *argvars;
16914 buf_T *buf;
16915{
16916 if (argvars[0].v_type == VAR_STRING
16917 && argvars[0].vval.v_string != NULL
16918 && argvars[0].vval.v_string[0] == '$'
16919 && buf != NULL)
16920 return buf->b_ml.ml_line_count;
16921 return get_tv_number_chk(&argvars[0], NULL);
16922}
16923
16924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 * Get the string value of a variable.
16926 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016927 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16928 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929 * If the String variable has never been set, return an empty string.
16930 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16932 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 */
16934 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016935get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016936 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937{
16938 static char_u mybuf[NUMBUFLEN];
16939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016940 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941}
16942
16943 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016944get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016945 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016946 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 char_u *res = get_tv_string_buf_chk(varp, buf);
16949
16950 return res != NULL ? res : (char_u *)"";
16951}
16952
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016953 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016954get_tv_string_chk(varp)
16955 typval_T *varp;
16956{
16957 static char_u mybuf[NUMBUFLEN];
16958
16959 return get_tv_string_buf_chk(varp, mybuf);
16960}
16961
16962 static char_u *
16963get_tv_string_buf_chk(varp, buf)
16964 typval_T *varp;
16965 char_u *buf;
16966{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016967 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016969 case VAR_NUMBER:
16970 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16971 return buf;
16972 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016973 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016974 break;
16975 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016976 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016977 break;
16978 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016979 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016980 break;
16981 case VAR_STRING:
16982 if (varp->vval.v_string != NULL)
16983 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016985 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016986 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016989 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990}
16991
16992/*
16993 * Find variable "name" in the list of variables.
16994 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016995 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016996 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016999 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017000find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017005 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006
Bram Moolenaara7043832005-01-21 11:56:39 +000017007 ht = find_var_ht(name, &varname);
17008 if (htp != NULL)
17009 *htp = ht;
17010 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017012 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013}
17014
17015/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017016 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017017 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017019 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017020find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017022 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017023 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017024{
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 hashitem_T *hi;
17026
17027 if (*varname == NUL)
17028 {
17029 /* Must be something like "s:", otherwise "ht" would be NULL. */
17030 switch (varname[-2])
17031 {
17032 case 's': return &SCRIPT_SV(current_SID).sv_var;
17033 case 'g': return &globvars_var;
17034 case 'v': return &vimvars_var;
17035 case 'b': return &curbuf->b_bufvar;
17036 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017037 case 'l': return current_funccal == NULL
17038 ? NULL : &current_funccal->l_vars_var;
17039 case 'a': return current_funccal == NULL
17040 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017041 }
17042 return NULL;
17043 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017044
17045 hi = hash_find(ht, varname);
17046 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017047 {
17048 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017049 * worked find the variable again. Don't auto-load a script if it was
17050 * loaded already, otherwise it would be loaded every time when
17051 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017052 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017053 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017054 hi = hash_find(ht, varname);
17055 if (HASHITEM_EMPTY(hi))
17056 return NULL;
17057 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017058 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017059}
17060
17061/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017062 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017063 * Set "varname" to the start of name without ':'.
17064 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017066find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 char_u *name;
17068 char_u **varname;
17069{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017070 hashitem_T *hi;
17071
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 if (name[1] != ':')
17073 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017074 /* The name must not start with a colon or #. */
17075 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 return NULL;
17077 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017078
17079 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017080 hi = hash_find(&compat_hashtab, name);
17081 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017082 return &compat_hashtab;
17083
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 return &globvarht; /* global variable */
17086 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 }
17088 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017089 if (*name == 'g') /* global variable */
17090 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017091 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17092 */
17093 if (vim_strchr(name + 2, ':') != NULL
17094 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017095 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017097 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017099 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017100 if (*name == 'v') /* v: variable */
17101 return &vimvarht;
17102 if (*name == 'a' && current_funccal != NULL) /* function argument */
17103 return &current_funccal->l_avars.dv_hashtab;
17104 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17105 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 if (*name == 's' /* script variable */
17107 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17108 return &SCRIPT_VARS(current_SID);
17109 return NULL;
17110}
17111
17112/*
17113 * Get the string value of a (global/local) variable.
17114 * Returns NULL when it doesn't exist.
17115 */
17116 char_u *
17117get_var_value(name)
17118 char_u *name;
17119{
Bram Moolenaar33570922005-01-25 22:26:29 +000017120 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121
Bram Moolenaara7043832005-01-21 11:56:39 +000017122 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 if (v == NULL)
17124 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017125 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126}
17127
17128/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130 * sourcing this script and when executing functions defined in the script.
17131 */
17132 void
17133new_script_vars(id)
17134 scid_T id;
17135{
Bram Moolenaara7043832005-01-21 11:56:39 +000017136 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017137 hashtab_T *ht;
17138 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017139
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17141 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017142 /* Re-allocating ga_data means that an ht_array pointing to
17143 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017145 for (i = 1; i <= ga_scripts.ga_len; ++i)
17146 {
17147 ht = &SCRIPT_VARS(i);
17148 if (ht->ht_mask == HT_INIT_SIZE - 1)
17149 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017150 sv = &SCRIPT_SV(i);
17151 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017152 }
17153
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 while (ga_scripts.ga_len < id)
17155 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17157 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 }
17160 }
17161}
17162
17163/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017164 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17165 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 */
17167 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017168init_var_dict(dict, dict_var)
17169 dict_T *dict;
17170 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171{
Bram Moolenaar33570922005-01-25 22:26:29 +000017172 hash_init(&dict->dv_hashtab);
17173 dict->dv_refcount = 99999;
17174 dict_var->di_tv.vval.v_dict = dict;
17175 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017176 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17178 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179}
17180
17181/*
17182 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017183 * Frees all allocated variables and the value they contain.
17184 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 */
17186 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017187vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 hashtab_T *ht;
17189{
17190 vars_clear_ext(ht, TRUE);
17191}
17192
17193/*
17194 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17195 */
17196 static void
17197vars_clear_ext(ht, free_val)
17198 hashtab_T *ht;
17199 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200{
Bram Moolenaara7043832005-01-21 11:56:39 +000017201 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017202 hashitem_T *hi;
17203 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204
Bram Moolenaar33570922005-01-25 22:26:29 +000017205 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017206 todo = ht->ht_used;
17207 for (hi = ht->ht_array; todo > 0; ++hi)
17208 {
17209 if (!HASHITEM_EMPTY(hi))
17210 {
17211 --todo;
17212
Bram Moolenaar33570922005-01-25 22:26:29 +000017213 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017214 * ht_array might change then. hash_clear() takes care of it
17215 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 v = HI2DI(hi);
17217 if (free_val)
17218 clear_tv(&v->di_tv);
17219 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17220 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017221 }
17222 }
17223 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017224 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225}
17226
Bram Moolenaara7043832005-01-21 11:56:39 +000017227/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 * Delete a variable from hashtab "ht" at item "hi".
17229 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017232delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017233 hashtab_T *ht;
17234 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235{
Bram Moolenaar33570922005-01-25 22:26:29 +000017236 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017237
17238 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 clear_tv(&di->di_tv);
17240 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241}
17242
17243/*
17244 * List the value of one internal variable.
17245 */
17246 static void
17247list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017248 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 char_u *prefix;
17250{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017251 char_u *tofree;
17252 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017253 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017254
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017255 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017256 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017257 s == NULL ? (char_u *)"" : s);
17258 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259}
17260
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 static void
17262list_one_var_a(prefix, name, type, string)
17263 char_u *prefix;
17264 char_u *name;
17265 int type;
17266 char_u *string;
17267{
17268 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17269 if (name != NULL) /* "a:" vars don't have a name stored */
17270 msg_puts(name);
17271 msg_putchar(' ');
17272 msg_advance(22);
17273 if (type == VAR_NUMBER)
17274 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017275 else if (type == VAR_FUNC)
17276 msg_putchar('*');
17277 else if (type == VAR_LIST)
17278 {
17279 msg_putchar('[');
17280 if (*string == '[')
17281 ++string;
17282 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017283 else if (type == VAR_DICT)
17284 {
17285 msg_putchar('{');
17286 if (*string == '{')
17287 ++string;
17288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 else
17290 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293
17294 if (type == VAR_FUNC)
17295 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296}
17297
17298/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 * If the variable already exists, the value is updated.
17301 * Otherwise the variable is created.
17302 */
17303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017304set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017306 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017307 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308{
Bram Moolenaar33570922005-01-25 22:26:29 +000017309 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017311 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017314 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017315 {
17316 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17317 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17318 ? name[2] : name[0]))
17319 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017320 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017321 return;
17322 }
17323 if (function_exists(name))
17324 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017325 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017326 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017327 return;
17328 }
17329 }
17330
Bram Moolenaara7043832005-01-21 11:56:39 +000017331 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017332 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017333 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017334 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017335 return;
17336 }
17337
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017338 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017342 if (var_check_ro(v->di_flags, name)
17343 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017344 return;
17345 if (v->di_tv.v_type != tv->v_type
17346 && !((v->di_tv.v_type == VAR_STRING
17347 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 && (tv->v_type == VAR_STRING
17349 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017350 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017351 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017352 return;
17353 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017354
17355 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017356 * Handle setting internal v: variables separately: we don't change
17357 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017358 */
17359 if (ht == &vimvarht)
17360 {
17361 if (v->di_tv.v_type == VAR_STRING)
17362 {
17363 vim_free(v->di_tv.vval.v_string);
17364 if (copy || tv->v_type != VAR_STRING)
17365 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17366 else
17367 {
17368 /* Take over the string to avoid an extra alloc/free. */
17369 v->di_tv.vval.v_string = tv->vval.v_string;
17370 tv->vval.v_string = NULL;
17371 }
17372 }
17373 else if (v->di_tv.v_type != VAR_NUMBER)
17374 EMSG2(_(e_intern2), "set_var()");
17375 else
17376 v->di_tv.vval.v_number = get_tv_number(tv);
17377 return;
17378 }
17379
17380 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 }
17382 else /* add a new variable */
17383 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017384 /* Make sure the variable name is valid. */
17385 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017386 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17387 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017388 {
17389 EMSG2(_(e_illvar), varname);
17390 return;
17391 }
17392
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017393 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17394 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017395 if (v == NULL)
17396 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017398 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017400 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 return;
17402 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017403 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017407 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017408 else
17409 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017411 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414}
17415
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017416/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017417 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17418 * Also give an error message.
17419 */
17420 static int
17421var_check_ro(flags, name)
17422 int flags;
17423 char_u *name;
17424{
17425 if (flags & DI_FLAGS_RO)
17426 {
17427 EMSG2(_(e_readonlyvar), name);
17428 return TRUE;
17429 }
17430 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17431 {
17432 EMSG2(_(e_readonlysbx), name);
17433 return TRUE;
17434 }
17435 return FALSE;
17436}
17437
17438/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017439 * Return TRUE if typeval "tv" is set to be locked (immutable).
17440 * Also give an error message, using "name".
17441 */
17442 static int
17443tv_check_lock(lock, name)
17444 int lock;
17445 char_u *name;
17446{
17447 if (lock & VAR_LOCKED)
17448 {
17449 EMSG2(_("E741: Value is locked: %s"),
17450 name == NULL ? (char_u *)_("Unknown") : name);
17451 return TRUE;
17452 }
17453 if (lock & VAR_FIXED)
17454 {
17455 EMSG2(_("E742: Cannot change value of %s"),
17456 name == NULL ? (char_u *)_("Unknown") : name);
17457 return TRUE;
17458 }
17459 return FALSE;
17460}
17461
17462/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017463 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017464 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017465 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017468copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017469 typval_T *from;
17470 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017472 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017473 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017474 switch (from->v_type)
17475 {
17476 case VAR_NUMBER:
17477 to->vval.v_number = from->vval.v_number;
17478 break;
17479 case VAR_STRING:
17480 case VAR_FUNC:
17481 if (from->vval.v_string == NULL)
17482 to->vval.v_string = NULL;
17483 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017484 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017485 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017486 if (from->v_type == VAR_FUNC)
17487 func_ref(to->vval.v_string);
17488 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017489 break;
17490 case VAR_LIST:
17491 if (from->vval.v_list == NULL)
17492 to->vval.v_list = NULL;
17493 else
17494 {
17495 to->vval.v_list = from->vval.v_list;
17496 ++to->vval.v_list->lv_refcount;
17497 }
17498 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017499 case VAR_DICT:
17500 if (from->vval.v_dict == NULL)
17501 to->vval.v_dict = NULL;
17502 else
17503 {
17504 to->vval.v_dict = from->vval.v_dict;
17505 ++to->vval.v_dict->dv_refcount;
17506 }
17507 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017508 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017510 break;
17511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512}
17513
17514/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017515 * Make a copy of an item.
17516 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017517 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17518 * reference to an already copied list/dict can be used.
17519 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017520 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017521 static int
17522item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017523 typval_T *from;
17524 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017525 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017526 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017527{
17528 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017529 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017530
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017532 {
17533 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017534 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017535 }
17536 ++recurse;
17537
17538 switch (from->v_type)
17539 {
17540 case VAR_NUMBER:
17541 case VAR_STRING:
17542 case VAR_FUNC:
17543 copy_tv(from, to);
17544 break;
17545 case VAR_LIST:
17546 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017547 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017548 if (from->vval.v_list == NULL)
17549 to->vval.v_list = NULL;
17550 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17551 {
17552 /* use the copy made earlier */
17553 to->vval.v_list = from->vval.v_list->lv_copylist;
17554 ++to->vval.v_list->lv_refcount;
17555 }
17556 else
17557 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17558 if (to->vval.v_list == NULL)
17559 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017560 break;
17561 case VAR_DICT:
17562 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017563 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017564 if (from->vval.v_dict == NULL)
17565 to->vval.v_dict = NULL;
17566 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17567 {
17568 /* use the copy made earlier */
17569 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17570 ++to->vval.v_dict->dv_refcount;
17571 }
17572 else
17573 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17574 if (to->vval.v_dict == NULL)
17575 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017576 break;
17577 default:
17578 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017579 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017580 }
17581 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017582 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017583}
17584
17585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 * ":echo expr1 ..." print each argument separated with a space, add a
17587 * newline at the end.
17588 * ":echon expr1 ..." print each argument plain.
17589 */
17590 void
17591ex_echo(eap)
17592 exarg_T *eap;
17593{
17594 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017595 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017596 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 char_u *p;
17598 int needclr = TRUE;
17599 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017600 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601
17602 if (eap->skip)
17603 ++emsg_skip;
17604 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17605 {
17606 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017607 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 {
17609 /*
17610 * Report the invalid expression unless the expression evaluation
17611 * has been cancelled due to an aborting error, an interrupt, or an
17612 * exception.
17613 */
17614 if (!aborting())
17615 EMSG2(_(e_invexpr2), p);
17616 break;
17617 }
17618 if (!eap->skip)
17619 {
17620 if (atstart)
17621 {
17622 atstart = FALSE;
17623 /* Call msg_start() after eval1(), evaluating the expression
17624 * may cause a message to appear. */
17625 if (eap->cmdidx == CMD_echo)
17626 msg_start();
17627 }
17628 else if (eap->cmdidx == CMD_echo)
17629 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017630 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017631 if (p != NULL)
17632 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017634 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017636 if (*p != TAB && needclr)
17637 {
17638 /* remove any text still there from the command */
17639 msg_clr_eos();
17640 needclr = FALSE;
17641 }
17642 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 }
17644 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017645 {
17646#ifdef FEAT_MBYTE
17647 if (has_mbyte)
17648 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017649 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017650
17651 (void)msg_outtrans_len_attr(p, i, echo_attr);
17652 p += i - 1;
17653 }
17654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017656 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017659 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 arg = skipwhite(arg);
17663 }
17664 eap->nextcmd = check_nextcmd(arg);
17665
17666 if (eap->skip)
17667 --emsg_skip;
17668 else
17669 {
17670 /* remove text that may still be there from the command */
17671 if (needclr)
17672 msg_clr_eos();
17673 if (eap->cmdidx == CMD_echo)
17674 msg_end();
17675 }
17676}
17677
17678/*
17679 * ":echohl {name}".
17680 */
17681 void
17682ex_echohl(eap)
17683 exarg_T *eap;
17684{
17685 int id;
17686
17687 id = syn_name2id(eap->arg);
17688 if (id == 0)
17689 echo_attr = 0;
17690 else
17691 echo_attr = syn_id2attr(id);
17692}
17693
17694/*
17695 * ":execute expr1 ..." execute the result of an expression.
17696 * ":echomsg expr1 ..." Print a message
17697 * ":echoerr expr1 ..." Print an error
17698 * Each gets spaces around each argument and a newline at the end for
17699 * echo commands
17700 */
17701 void
17702ex_execute(eap)
17703 exarg_T *eap;
17704{
17705 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017706 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 int ret = OK;
17708 char_u *p;
17709 garray_T ga;
17710 int len;
17711 int save_did_emsg;
17712
17713 ga_init2(&ga, 1, 80);
17714
17715 if (eap->skip)
17716 ++emsg_skip;
17717 while (*arg != NUL && *arg != '|' && *arg != '\n')
17718 {
17719 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017720 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 {
17722 /*
17723 * Report the invalid expression unless the expression evaluation
17724 * has been cancelled due to an aborting error, an interrupt, or an
17725 * exception.
17726 */
17727 if (!aborting())
17728 EMSG2(_(e_invexpr2), p);
17729 ret = FAIL;
17730 break;
17731 }
17732
17733 if (!eap->skip)
17734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017735 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 len = (int)STRLEN(p);
17737 if (ga_grow(&ga, len + 2) == FAIL)
17738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 ret = FAIL;
17741 break;
17742 }
17743 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 ga.ga_len += len;
17747 }
17748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 arg = skipwhite(arg);
17751 }
17752
17753 if (ret != FAIL && ga.ga_data != NULL)
17754 {
17755 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017758 out_flush();
17759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 else if (eap->cmdidx == CMD_echoerr)
17761 {
17762 /* We don't want to abort following commands, restore did_emsg. */
17763 save_did_emsg = did_emsg;
17764 EMSG((char_u *)ga.ga_data);
17765 if (!force_abort)
17766 did_emsg = save_did_emsg;
17767 }
17768 else if (eap->cmdidx == CMD_execute)
17769 do_cmdline((char_u *)ga.ga_data,
17770 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17771 }
17772
17773 ga_clear(&ga);
17774
17775 if (eap->skip)
17776 --emsg_skip;
17777
17778 eap->nextcmd = check_nextcmd(arg);
17779}
17780
17781/*
17782 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17783 * "arg" points to the "&" or '+' when called, to "option" when returning.
17784 * Returns NULL when no option name found. Otherwise pointer to the char
17785 * after the option name.
17786 */
17787 static char_u *
17788find_option_end(arg, opt_flags)
17789 char_u **arg;
17790 int *opt_flags;
17791{
17792 char_u *p = *arg;
17793
17794 ++p;
17795 if (*p == 'g' && p[1] == ':')
17796 {
17797 *opt_flags = OPT_GLOBAL;
17798 p += 2;
17799 }
17800 else if (*p == 'l' && p[1] == ':')
17801 {
17802 *opt_flags = OPT_LOCAL;
17803 p += 2;
17804 }
17805 else
17806 *opt_flags = 0;
17807
17808 if (!ASCII_ISALPHA(*p))
17809 return NULL;
17810 *arg = p;
17811
17812 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17813 p += 4; /* termcap option */
17814 else
17815 while (ASCII_ISALPHA(*p))
17816 ++p;
17817 return p;
17818}
17819
17820/*
17821 * ":function"
17822 */
17823 void
17824ex_function(eap)
17825 exarg_T *eap;
17826{
17827 char_u *theline;
17828 int j;
17829 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 char_u *name = NULL;
17832 char_u *p;
17833 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017834 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 garray_T newargs;
17836 garray_T newlines;
17837 int varargs = FALSE;
17838 int mustend = FALSE;
17839 int flags = 0;
17840 ufunc_T *fp;
17841 int indent;
17842 int nesting;
17843 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 dictitem_T *v;
17845 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017846 static int func_nr = 0; /* number for nameless function */
17847 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017848 hashtab_T *ht;
17849 int todo;
17850 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017851 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852
17853 /*
17854 * ":function" without argument: list functions.
17855 */
17856 if (ends_excmd(*eap->arg))
17857 {
17858 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017859 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017860 todo = func_hashtab.ht_used;
17861 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017862 {
17863 if (!HASHITEM_EMPTY(hi))
17864 {
17865 --todo;
17866 fp = HI2UF(hi);
17867 if (!isdigit(*fp->uf_name))
17868 list_func_head(fp, FALSE);
17869 }
17870 }
17871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 eap->nextcmd = check_nextcmd(eap->arg);
17873 return;
17874 }
17875
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017876 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017877 * ":function /pat": list functions matching pattern.
17878 */
17879 if (*eap->arg == '/')
17880 {
17881 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17882 if (!eap->skip)
17883 {
17884 regmatch_T regmatch;
17885
17886 c = *p;
17887 *p = NUL;
17888 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17889 *p = c;
17890 if (regmatch.regprog != NULL)
17891 {
17892 regmatch.rm_ic = p_ic;
17893
17894 todo = func_hashtab.ht_used;
17895 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17896 {
17897 if (!HASHITEM_EMPTY(hi))
17898 {
17899 --todo;
17900 fp = HI2UF(hi);
17901 if (!isdigit(*fp->uf_name)
17902 && vim_regexec(&regmatch, fp->uf_name, 0))
17903 list_func_head(fp, FALSE);
17904 }
17905 }
17906 }
17907 }
17908 if (*p == '/')
17909 ++p;
17910 eap->nextcmd = check_nextcmd(p);
17911 return;
17912 }
17913
17914 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017915 * Get the function name. There are these situations:
17916 * func normal function name
17917 * "name" == func, "fudi.fd_dict" == NULL
17918 * dict.func new dictionary entry
17919 * "name" == NULL, "fudi.fd_dict" set,
17920 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17921 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017922 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017923 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17924 * dict.func existing dict entry that's not a Funcref
17925 * "name" == NULL, "fudi.fd_dict" set,
17926 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017929 name = trans_function_name(&p, eap->skip, 0, &fudi);
17930 paren = (vim_strchr(p, '(') != NULL);
17931 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 {
17933 /*
17934 * Return on an invalid expression in braces, unless the expression
17935 * evaluation has been cancelled due to an aborting error, an
17936 * interrupt, or an exception.
17937 */
17938 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017939 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017940 if (!eap->skip && fudi.fd_newkey != NULL)
17941 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017942 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 else
17946 eap->skip = TRUE;
17947 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000017948
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 /* An error in a function call during evaluation of an expression in magic
17950 * braces should not cause the function not to be defined. */
17951 saved_did_emsg = did_emsg;
17952 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953
17954 /*
17955 * ":function func" with only function name: list function.
17956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017957 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 {
17959 if (!ends_excmd(*skipwhite(p)))
17960 {
17961 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017962 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 }
17964 eap->nextcmd = check_nextcmd(p);
17965 if (eap->nextcmd != NULL)
17966 *p = NUL;
17967 if (!eap->skip && !got_int)
17968 {
17969 fp = find_func(name);
17970 if (fp != NULL)
17971 {
17972 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017973 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017975 if (FUNCLINE(fp, j) == NULL)
17976 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 msg_putchar('\n');
17978 msg_outnum((long)(j + 1));
17979 if (j < 9)
17980 msg_putchar(' ');
17981 if (j < 99)
17982 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017983 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984 out_flush(); /* show a line at a time */
17985 ui_breakcheck();
17986 }
17987 if (!got_int)
17988 {
17989 msg_putchar('\n');
17990 msg_puts((char_u *)" endfunction");
17991 }
17992 }
17993 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017994 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017996 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 }
17998
17999 /*
18000 * ":function name(arg1, arg2)" Define function.
18001 */
18002 p = skipwhite(p);
18003 if (*p != '(')
18004 {
18005 if (!eap->skip)
18006 {
18007 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018008 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 }
18010 /* attempt to continue by skipping some text */
18011 if (vim_strchr(p, '(') != NULL)
18012 p = vim_strchr(p, '(');
18013 }
18014 p = skipwhite(p + 1);
18015
18016 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18017 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18018
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018019 if (!eap->skip)
18020 {
18021 /* Check the name of the function. */
18022 if (name != NULL)
18023 arg = name;
18024 else
18025 arg = fudi.fd_newkey;
18026 if (arg != NULL)
18027 {
18028 if (*arg == K_SPECIAL)
18029 j = 3;
18030 else
18031 j = 0;
18032 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18033 : eval_isnamec(arg[j])))
18034 ++j;
18035 if (arg[j] != NUL)
18036 emsg_funcname(_(e_invarg2), arg);
18037 }
18038 }
18039
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 /*
18041 * Isolate the arguments: "arg1, arg2, ...)"
18042 */
18043 while (*p != ')')
18044 {
18045 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18046 {
18047 varargs = TRUE;
18048 p += 3;
18049 mustend = TRUE;
18050 }
18051 else
18052 {
18053 arg = p;
18054 while (ASCII_ISALNUM(*p) || *p == '_')
18055 ++p;
18056 if (arg == p || isdigit(*arg)
18057 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18058 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18059 {
18060 if (!eap->skip)
18061 EMSG2(_("E125: Illegal argument: %s"), arg);
18062 break;
18063 }
18064 if (ga_grow(&newargs, 1) == FAIL)
18065 goto erret;
18066 c = *p;
18067 *p = NUL;
18068 arg = vim_strsave(arg);
18069 if (arg == NULL)
18070 goto erret;
18071 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18072 *p = c;
18073 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 if (*p == ',')
18075 ++p;
18076 else
18077 mustend = TRUE;
18078 }
18079 p = skipwhite(p);
18080 if (mustend && *p != ')')
18081 {
18082 if (!eap->skip)
18083 EMSG2(_(e_invarg2), eap->arg);
18084 break;
18085 }
18086 }
18087 ++p; /* skip the ')' */
18088
Bram Moolenaare9a41262005-01-15 22:18:47 +000018089 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 for (;;)
18091 {
18092 p = skipwhite(p);
18093 if (STRNCMP(p, "range", 5) == 0)
18094 {
18095 flags |= FC_RANGE;
18096 p += 5;
18097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018098 else if (STRNCMP(p, "dict", 4) == 0)
18099 {
18100 flags |= FC_DICT;
18101 p += 4;
18102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 else if (STRNCMP(p, "abort", 5) == 0)
18104 {
18105 flags |= FC_ABORT;
18106 p += 5;
18107 }
18108 else
18109 break;
18110 }
18111
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018112 /* When there is a line break use what follows for the function body.
18113 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18114 if (*p == '\n')
18115 line_arg = p + 1;
18116 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 EMSG(_(e_trailing));
18118
18119 /*
18120 * Read the body of the function, until ":endfunction" is found.
18121 */
18122 if (KeyTyped)
18123 {
18124 /* Check if the function already exists, don't let the user type the
18125 * whole function before telling him it doesn't work! For a script we
18126 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018127 if (!eap->skip && !eap->forceit)
18128 {
18129 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18130 EMSG(_(e_funcdict));
18131 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018132 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018135 if (!eap->skip && did_emsg)
18136 goto erret;
18137
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 msg_putchar('\n'); /* don't overwrite the function name */
18139 cmdline_row = msg_row;
18140 }
18141
18142 indent = 2;
18143 nesting = 0;
18144 for (;;)
18145 {
18146 msg_scroll = TRUE;
18147 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018148 sourcing_lnum_off = sourcing_lnum;
18149
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018150 if (line_arg != NULL)
18151 {
18152 /* Use eap->arg, split up in parts by line breaks. */
18153 theline = line_arg;
18154 p = vim_strchr(theline, '\n');
18155 if (p == NULL)
18156 line_arg += STRLEN(line_arg);
18157 else
18158 {
18159 *p = NUL;
18160 line_arg = p + 1;
18161 }
18162 }
18163 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164 theline = getcmdline(':', 0L, indent);
18165 else
18166 theline = eap->getline(':', eap->cookie, indent);
18167 if (KeyTyped)
18168 lines_left = Rows - 1;
18169 if (theline == NULL)
18170 {
18171 EMSG(_("E126: Missing :endfunction"));
18172 goto erret;
18173 }
18174
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018175 /* Detect line continuation: sourcing_lnum increased more than one. */
18176 if (sourcing_lnum > sourcing_lnum_off + 1)
18177 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18178 else
18179 sourcing_lnum_off = 0;
18180
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 if (skip_until != NULL)
18182 {
18183 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18184 * don't check for ":endfunc". */
18185 if (STRCMP(theline, skip_until) == 0)
18186 {
18187 vim_free(skip_until);
18188 skip_until = NULL;
18189 }
18190 }
18191 else
18192 {
18193 /* skip ':' and blanks*/
18194 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18195 ;
18196
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018197 /* Check for "endfunction". */
18198 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018200 if (line_arg == NULL)
18201 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 break;
18203 }
18204
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018205 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 * at "end". */
18207 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18208 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018209 else if (STRNCMP(p, "if", 2) == 0
18210 || STRNCMP(p, "wh", 2) == 0
18211 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 || STRNCMP(p, "try", 3) == 0)
18213 indent += 2;
18214
18215 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018216 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018218 if (*p == '!')
18219 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 p += eval_fname_script(p);
18221 if (ASCII_ISALPHA(*p))
18222 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018223 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 if (*skipwhite(p) == '(')
18225 {
18226 ++nesting;
18227 indent += 2;
18228 }
18229 }
18230 }
18231
18232 /* Check for ":append" or ":insert". */
18233 p = skip_range(p, NULL);
18234 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18235 || (p[0] == 'i'
18236 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18237 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18238 skip_until = vim_strsave((char_u *)".");
18239
18240 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18241 arg = skipwhite(skiptowhite(p));
18242 if (arg[0] == '<' && arg[1] =='<'
18243 && ((p[0] == 'p' && p[1] == 'y'
18244 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18245 || (p[0] == 'p' && p[1] == 'e'
18246 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18247 || (p[0] == 't' && p[1] == 'c'
18248 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18249 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18250 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018251 || (p[0] == 'm' && p[1] == 'z'
18252 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 ))
18254 {
18255 /* ":python <<" continues until a dot, like ":append" */
18256 p = skipwhite(arg + 2);
18257 if (*p == NUL)
18258 skip_until = vim_strsave((char_u *)".");
18259 else
18260 skip_until = vim_strsave(p);
18261 }
18262 }
18263
18264 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018265 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018266 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018267 if (line_arg == NULL)
18268 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018270 }
18271
18272 /* Copy the line to newly allocated memory. get_one_sourceline()
18273 * allocates 250 bytes per line, this saves 80% on average. The cost
18274 * is an extra alloc/free. */
18275 p = vim_strsave(theline);
18276 if (p != NULL)
18277 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018278 if (line_arg == NULL)
18279 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018280 theline = p;
18281 }
18282
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018283 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18284
18285 /* Add NULL lines for continuation lines, so that the line count is
18286 * equal to the index in the growarray. */
18287 while (sourcing_lnum_off-- > 0)
18288 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018289
18290 /* Check for end of eap->arg. */
18291 if (line_arg != NULL && *line_arg == NUL)
18292 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 }
18294
18295 /* Don't define the function when skipping commands or when an error was
18296 * detected. */
18297 if (eap->skip || did_emsg)
18298 goto erret;
18299
18300 /*
18301 * If there are no errors, add the function
18302 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018303 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018304 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018305 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018306 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018307 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018308 emsg_funcname("E707: Function name conflicts with variable: %s",
18309 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018310 goto erret;
18311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018312
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018313 fp = find_func(name);
18314 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018316 if (!eap->forceit)
18317 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018318 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018319 goto erret;
18320 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018321 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018322 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018323 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018324 name);
18325 goto erret;
18326 }
18327 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018328 ga_clear_strings(&(fp->uf_args));
18329 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018330 vim_free(name);
18331 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 }
18334 else
18335 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018336 char numbuf[20];
18337
18338 fp = NULL;
18339 if (fudi.fd_newkey == NULL && !eap->forceit)
18340 {
18341 EMSG(_(e_funcdict));
18342 goto erret;
18343 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018344 if (fudi.fd_di == NULL)
18345 {
18346 /* Can't add a function to a locked dictionary */
18347 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18348 goto erret;
18349 }
18350 /* Can't change an existing function if it is locked */
18351 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18352 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018353
18354 /* Give the function a sequential number. Can only be used with a
18355 * Funcref! */
18356 vim_free(name);
18357 sprintf(numbuf, "%d", ++func_nr);
18358 name = vim_strsave((char_u *)numbuf);
18359 if (name == NULL)
18360 goto erret;
18361 }
18362
18363 if (fp == NULL)
18364 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018365 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018366 {
18367 int slen, plen;
18368 char_u *scriptname;
18369
18370 /* Check that the autoload name matches the script name. */
18371 j = FAIL;
18372 if (sourcing_name != NULL)
18373 {
18374 scriptname = autoload_name(name);
18375 if (scriptname != NULL)
18376 {
18377 p = vim_strchr(scriptname, '/');
18378 plen = STRLEN(p);
18379 slen = STRLEN(sourcing_name);
18380 if (slen > plen && fnamecmp(p,
18381 sourcing_name + slen - plen) == 0)
18382 j = OK;
18383 vim_free(scriptname);
18384 }
18385 }
18386 if (j == FAIL)
18387 {
18388 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18389 goto erret;
18390 }
18391 }
18392
18393 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 if (fp == NULL)
18395 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018396
18397 if (fudi.fd_dict != NULL)
18398 {
18399 if (fudi.fd_di == NULL)
18400 {
18401 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018402 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018403 if (fudi.fd_di == NULL)
18404 {
18405 vim_free(fp);
18406 goto erret;
18407 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018408 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18409 {
18410 vim_free(fudi.fd_di);
18411 goto erret;
18412 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018413 }
18414 else
18415 /* overwrite existing dict entry */
18416 clear_tv(&fudi.fd_di->di_tv);
18417 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018418 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018419 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018420 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018421 }
18422
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018424 STRCPY(fp->uf_name, name);
18425 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018427 fp->uf_args = newargs;
18428 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018429#ifdef FEAT_PROFILE
18430 fp->uf_tml_count = NULL;
18431 fp->uf_tml_total = NULL;
18432 fp->uf_tml_self = NULL;
18433 fp->uf_profiling = FALSE;
18434 if (prof_def_func())
18435 func_do_profile(fp);
18436#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018437 fp->uf_varargs = varargs;
18438 fp->uf_flags = flags;
18439 fp->uf_calls = 0;
18440 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018441 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442
18443erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444 ga_clear_strings(&newargs);
18445 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018446ret_free:
18447 vim_free(skip_until);
18448 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451}
18452
18453/*
18454 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018455 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018457 * flags:
18458 * TFN_INT: internal function name OK
18459 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 * Advances "pp" to just after the function name (if no error).
18461 */
18462 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018463trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 char_u **pp;
18465 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018466 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018467 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018469 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 char_u *start;
18471 char_u *end;
18472 int lead;
18473 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018475 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018476
18477 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018478 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018479 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018480
18481 /* Check for hard coded <SNR>: already translated function ID (from a user
18482 * command). */
18483 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18484 && (*pp)[2] == (int)KE_SNR)
18485 {
18486 *pp += 3;
18487 len = get_id_len(pp) + 3;
18488 return vim_strnsave(start, len);
18489 }
18490
18491 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18492 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018494 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018496
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018497 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18498 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018499 if (end == start)
18500 {
18501 if (!skip)
18502 EMSG(_("E129: Function name required"));
18503 goto theend;
18504 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018505 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018506 {
18507 /*
18508 * Report an invalid expression in braces, unless the expression
18509 * evaluation has been cancelled due to an aborting error, an
18510 * interrupt, or an exception.
18511 */
18512 if (!aborting())
18513 {
18514 if (end != NULL)
18515 EMSG2(_(e_invarg2), start);
18516 }
18517 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018518 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018519 goto theend;
18520 }
18521
18522 if (lv.ll_tv != NULL)
18523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018524 if (fdp != NULL)
18525 {
18526 fdp->fd_dict = lv.ll_dict;
18527 fdp->fd_newkey = lv.ll_newkey;
18528 lv.ll_newkey = NULL;
18529 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018530 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018531 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18532 {
18533 name = vim_strsave(lv.ll_tv->vval.v_string);
18534 *pp = end;
18535 }
18536 else
18537 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018538 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18539 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018540 EMSG(_(e_funcref));
18541 else
18542 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018543 name = NULL;
18544 }
18545 goto theend;
18546 }
18547
18548 if (lv.ll_name == NULL)
18549 {
18550 /* Error found, but continue after the function name. */
18551 *pp = end;
18552 goto theend;
18553 }
18554
18555 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018557 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018558 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18559 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18560 {
18561 /* When there was "s:" already or the name expanded to get a
18562 * leading "s:" then remove it. */
18563 lv.ll_name += 2;
18564 len -= 2;
18565 lead = 2;
18566 }
18567 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018568 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018569 {
18570 if (lead == 2) /* skip over "s:" */
18571 lv.ll_name += 2;
18572 len = (int)(end - lv.ll_name);
18573 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018574
18575 /*
18576 * Copy the function name to allocated memory.
18577 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18578 * Accept <SNR>123_name() outside a script.
18579 */
18580 if (skip)
18581 lead = 0; /* do nothing */
18582 else if (lead > 0)
18583 {
18584 lead = 3;
18585 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18586 {
18587 if (current_SID <= 0)
18588 {
18589 EMSG(_(e_usingsid));
18590 goto theend;
18591 }
18592 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18593 lead += (int)STRLEN(sid_buf);
18594 }
18595 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018596 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018597 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018598 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018599 goto theend;
18600 }
18601 name = alloc((unsigned)(len + lead + 1));
18602 if (name != NULL)
18603 {
18604 if (lead > 0)
18605 {
18606 name[0] = K_SPECIAL;
18607 name[1] = KS_EXTRA;
18608 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018609 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018610 STRCPY(name + 3, sid_buf);
18611 }
18612 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18613 name[len + lead] = NUL;
18614 }
18615 *pp = end;
18616
18617theend:
18618 clear_lval(&lv);
18619 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620}
18621
18622/*
18623 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18624 * Return 2 if "p" starts with "s:".
18625 * Return 0 otherwise.
18626 */
18627 static int
18628eval_fname_script(p)
18629 char_u *p;
18630{
18631 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18632 || STRNICMP(p + 1, "SNR>", 4) == 0))
18633 return 5;
18634 if (p[0] == 's' && p[1] == ':')
18635 return 2;
18636 return 0;
18637}
18638
18639/*
18640 * Return TRUE if "p" starts with "<SID>" or "s:".
18641 * Only works if eval_fname_script() returned non-zero for "p"!
18642 */
18643 static int
18644eval_fname_sid(p)
18645 char_u *p;
18646{
18647 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18648}
18649
18650/*
18651 * List the head of the function: "name(arg1, arg2)".
18652 */
18653 static void
18654list_func_head(fp, indent)
18655 ufunc_T *fp;
18656 int indent;
18657{
18658 int j;
18659
18660 msg_start();
18661 if (indent)
18662 MSG_PUTS(" ");
18663 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018664 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 {
18666 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018667 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 }
18669 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018670 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018672 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673 {
18674 if (j)
18675 MSG_PUTS(", ");
18676 msg_puts(FUNCARG(fp, j));
18677 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018678 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 {
18680 if (j)
18681 MSG_PUTS(", ");
18682 MSG_PUTS("...");
18683 }
18684 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018685 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018686 if (p_verbose > 0)
18687 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688}
18689
18690/*
18691 * Find a function by name, return pointer to it in ufuncs.
18692 * Return NULL for unknown function.
18693 */
18694 static ufunc_T *
18695find_func(name)
18696 char_u *name;
18697{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018698 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018700 hi = hash_find(&func_hashtab, name);
18701 if (!HASHITEM_EMPTY(hi))
18702 return HI2UF(hi);
18703 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704}
18705
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018706#if defined(EXITFREE) || defined(PROTO)
18707 void
18708free_all_functions()
18709{
18710 hashitem_T *hi;
18711
18712 /* Need to start all over every time, because func_free() may change the
18713 * hash table. */
18714 while (func_hashtab.ht_used > 0)
18715 for (hi = func_hashtab.ht_array; ; ++hi)
18716 if (!HASHITEM_EMPTY(hi))
18717 {
18718 func_free(HI2UF(hi));
18719 break;
18720 }
18721}
18722#endif
18723
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018724/*
18725 * Return TRUE if a function "name" exists.
18726 */
18727 static int
18728function_exists(name)
18729 char_u *name;
18730{
18731 char_u *p = name;
18732 int n = FALSE;
18733
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018734 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018735 if (p != NULL)
18736 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018737 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018738 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018739 else
18740 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018741 vim_free(p);
18742 }
18743 return n;
18744}
18745
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018746/*
18747 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018748 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018749 */
18750 static int
18751builtin_function(name)
18752 char_u *name;
18753{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018754 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18755 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018756}
18757
Bram Moolenaar05159a02005-02-26 23:04:13 +000018758#if defined(FEAT_PROFILE) || defined(PROTO)
18759/*
18760 * Start profiling function "fp".
18761 */
18762 static void
18763func_do_profile(fp)
18764 ufunc_T *fp;
18765{
18766 fp->uf_tm_count = 0;
18767 profile_zero(&fp->uf_tm_self);
18768 profile_zero(&fp->uf_tm_total);
18769 if (fp->uf_tml_count == NULL)
18770 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18771 (sizeof(int) * fp->uf_lines.ga_len));
18772 if (fp->uf_tml_total == NULL)
18773 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18774 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18775 if (fp->uf_tml_self == NULL)
18776 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18777 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18778 fp->uf_tml_idx = -1;
18779 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18780 || fp->uf_tml_self == NULL)
18781 return; /* out of memory */
18782
18783 fp->uf_profiling = TRUE;
18784}
18785
18786/*
18787 * Dump the profiling results for all functions in file "fd".
18788 */
18789 void
18790func_dump_profile(fd)
18791 FILE *fd;
18792{
18793 hashitem_T *hi;
18794 int todo;
18795 ufunc_T *fp;
18796 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018797 ufunc_T **sorttab;
18798 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018799
18800 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018801 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18802
Bram Moolenaar05159a02005-02-26 23:04:13 +000018803 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18804 {
18805 if (!HASHITEM_EMPTY(hi))
18806 {
18807 --todo;
18808 fp = HI2UF(hi);
18809 if (fp->uf_profiling)
18810 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018811 if (sorttab != NULL)
18812 sorttab[st_len++] = fp;
18813
Bram Moolenaar05159a02005-02-26 23:04:13 +000018814 if (fp->uf_name[0] == K_SPECIAL)
18815 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18816 else
18817 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18818 if (fp->uf_tm_count == 1)
18819 fprintf(fd, "Called 1 time\n");
18820 else
18821 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18822 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18823 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18824 fprintf(fd, "\n");
18825 fprintf(fd, "count total (s) self (s)\n");
18826
18827 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18828 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018829 if (FUNCLINE(fp, i) == NULL)
18830 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018831 prof_func_line(fd, fp->uf_tml_count[i],
18832 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018833 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18834 }
18835 fprintf(fd, "\n");
18836 }
18837 }
18838 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018839
18840 if (sorttab != NULL && st_len > 0)
18841 {
18842 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18843 prof_total_cmp);
18844 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18845 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18846 prof_self_cmp);
18847 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18848 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018849}
Bram Moolenaar73830342005-02-28 22:48:19 +000018850
18851 static void
18852prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18853 FILE *fd;
18854 ufunc_T **sorttab;
18855 int st_len;
18856 char *title;
18857 int prefer_self; /* when equal print only self time */
18858{
18859 int i;
18860 ufunc_T *fp;
18861
18862 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18863 fprintf(fd, "count total (s) self (s) function\n");
18864 for (i = 0; i < 20 && i < st_len; ++i)
18865 {
18866 fp = sorttab[i];
18867 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18868 prefer_self);
18869 if (fp->uf_name[0] == K_SPECIAL)
18870 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18871 else
18872 fprintf(fd, " %s()\n", fp->uf_name);
18873 }
18874 fprintf(fd, "\n");
18875}
18876
18877/*
18878 * Print the count and times for one function or function line.
18879 */
18880 static void
18881prof_func_line(fd, count, total, self, prefer_self)
18882 FILE *fd;
18883 int count;
18884 proftime_T *total;
18885 proftime_T *self;
18886 int prefer_self; /* when equal print only self time */
18887{
18888 if (count > 0)
18889 {
18890 fprintf(fd, "%5d ", count);
18891 if (prefer_self && profile_equal(total, self))
18892 fprintf(fd, " ");
18893 else
18894 fprintf(fd, "%s ", profile_msg(total));
18895 if (!prefer_self && profile_equal(total, self))
18896 fprintf(fd, " ");
18897 else
18898 fprintf(fd, "%s ", profile_msg(self));
18899 }
18900 else
18901 fprintf(fd, " ");
18902}
18903
18904/*
18905 * Compare function for total time sorting.
18906 */
18907 static int
18908#ifdef __BORLANDC__
18909_RTLENTRYF
18910#endif
18911prof_total_cmp(s1, s2)
18912 const void *s1;
18913 const void *s2;
18914{
18915 ufunc_T *p1, *p2;
18916
18917 p1 = *(ufunc_T **)s1;
18918 p2 = *(ufunc_T **)s2;
18919 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18920}
18921
18922/*
18923 * Compare function for self time sorting.
18924 */
18925 static int
18926#ifdef __BORLANDC__
18927_RTLENTRYF
18928#endif
18929prof_self_cmp(s1, s2)
18930 const void *s1;
18931 const void *s2;
18932{
18933 ufunc_T *p1, *p2;
18934
18935 p1 = *(ufunc_T **)s1;
18936 p2 = *(ufunc_T **)s2;
18937 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18938}
18939
Bram Moolenaar05159a02005-02-26 23:04:13 +000018940#endif
18941
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018942/* The names of packages that once were loaded is remembered. */
18943static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18944
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018945/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018946 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018947 * Return TRUE if a package was loaded.
18948 */
18949 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018950script_autoload(name, reload)
18951 char_u *name;
18952 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018953{
18954 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018955 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018956 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018957 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018958
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018959 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018960 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018961 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018962 return FALSE;
18963
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018964 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018965
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018966 /* Find the name in the list of previously loaded package names. Skip
18967 * "autoload/", it's always the same. */
18968 for (i = 0; i < ga_loaded.ga_len; ++i)
18969 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18970 break;
18971 if (!reload && i < ga_loaded.ga_len)
18972 ret = FALSE; /* was loaded already */
18973 else
18974 {
18975 /* Remember the name if it wasn't loaded already. */
18976 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18977 {
18978 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18979 tofree = NULL;
18980 }
18981
18982 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018983 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018984 ret = TRUE;
18985 }
18986
18987 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018988 return ret;
18989}
18990
18991/*
18992 * Return the autoload script name for a function or variable name.
18993 * Returns NULL when out of memory.
18994 */
18995 static char_u *
18996autoload_name(name)
18997 char_u *name;
18998{
18999 char_u *p;
19000 char_u *scriptname;
19001
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019002 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019003 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19004 if (scriptname == NULL)
19005 return FALSE;
19006 STRCPY(scriptname, "autoload/");
19007 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019008 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019009 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019010 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019011 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019012 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019013}
19014
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19016
19017/*
19018 * Function given to ExpandGeneric() to obtain the list of user defined
19019 * function names.
19020 */
19021 char_u *
19022get_user_func_name(xp, idx)
19023 expand_T *xp;
19024 int idx;
19025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019026 static long_u done;
19027 static hashitem_T *hi;
19028 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029
19030 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019032 done = 0;
19033 hi = func_hashtab.ht_array;
19034 }
19035 if (done < func_hashtab.ht_used)
19036 {
19037 if (done++ > 0)
19038 ++hi;
19039 while (HASHITEM_EMPTY(hi))
19040 ++hi;
19041 fp = HI2UF(hi);
19042
19043 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19044 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045
19046 cat_func_name(IObuff, fp);
19047 if (xp->xp_context != EXPAND_USER_FUNC)
19048 {
19049 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019050 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 STRCAT(IObuff, ")");
19052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 return IObuff;
19054 }
19055 return NULL;
19056}
19057
19058#endif /* FEAT_CMDL_COMPL */
19059
19060/*
19061 * Copy the function name of "fp" to buffer "buf".
19062 * "buf" must be able to hold the function name plus three bytes.
19063 * Takes care of script-local function names.
19064 */
19065 static void
19066cat_func_name(buf, fp)
19067 char_u *buf;
19068 ufunc_T *fp;
19069{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019070 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 {
19072 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019073 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 }
19075 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019076 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077}
19078
19079/*
19080 * ":delfunction {name}"
19081 */
19082 void
19083ex_delfunction(eap)
19084 exarg_T *eap;
19085{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019086 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 char_u *p;
19088 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019089 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090
19091 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019092 name = trans_function_name(&p, eap->skip, 0, &fudi);
19093 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019095 {
19096 if (fudi.fd_dict != NULL && !eap->skip)
19097 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100 if (!ends_excmd(*skipwhite(p)))
19101 {
19102 vim_free(name);
19103 EMSG(_(e_trailing));
19104 return;
19105 }
19106 eap->nextcmd = check_nextcmd(p);
19107 if (eap->nextcmd != NULL)
19108 *p = NUL;
19109
19110 if (!eap->skip)
19111 fp = find_func(name);
19112 vim_free(name);
19113
19114 if (!eap->skip)
19115 {
19116 if (fp == NULL)
19117 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019118 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 return;
19120 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019121 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 {
19123 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19124 return;
19125 }
19126
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019127 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019129 /* Delete the dict item that refers to the function, it will
19130 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019131 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019133 else
19134 func_free(fp);
19135 }
19136}
19137
19138/*
19139 * Free a function and remove it from the list of functions.
19140 */
19141 static void
19142func_free(fp)
19143 ufunc_T *fp;
19144{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019145 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019146
19147 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019148 ga_clear_strings(&(fp->uf_args));
19149 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019150#ifdef FEAT_PROFILE
19151 vim_free(fp->uf_tml_count);
19152 vim_free(fp->uf_tml_total);
19153 vim_free(fp->uf_tml_self);
19154#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019156 /* remove the function from the function hashtable */
19157 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19158 if (HASHITEM_EMPTY(hi))
19159 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019160 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019161 hash_remove(&func_hashtab, hi);
19162
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019163 vim_free(fp);
19164}
19165
19166/*
19167 * Unreference a Function: decrement the reference count and free it when it
19168 * becomes zero. Only for numbered functions.
19169 */
19170 static void
19171func_unref(name)
19172 char_u *name;
19173{
19174 ufunc_T *fp;
19175
19176 if (name != NULL && isdigit(*name))
19177 {
19178 fp = find_func(name);
19179 if (fp == NULL)
19180 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019181 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019182 {
19183 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019184 * when "uf_calls" becomes zero. */
19185 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019186 func_free(fp);
19187 }
19188 }
19189}
19190
19191/*
19192 * Count a reference to a Function.
19193 */
19194 static void
19195func_ref(name)
19196 char_u *name;
19197{
19198 ufunc_T *fp;
19199
19200 if (name != NULL && isdigit(*name))
19201 {
19202 fp = find_func(name);
19203 if (fp == NULL)
19204 EMSG2(_(e_intern2), "func_ref()");
19205 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019206 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 }
19208}
19209
19210/*
19211 * Call a user function.
19212 */
19213 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019214call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 ufunc_T *fp; /* pointer to function */
19216 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019217 typval_T *argvars; /* arguments */
19218 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 linenr_T firstline; /* first line of range */
19220 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019221 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222{
Bram Moolenaar33570922005-01-25 22:26:29 +000019223 char_u *save_sourcing_name;
19224 linenr_T save_sourcing_lnum;
19225 scid_T save_current_SID;
19226 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 int save_did_emsg;
19228 static int depth = 0;
19229 dictitem_T *v;
19230 int fixvar_idx = 0; /* index in fixvar[] */
19231 int i;
19232 int ai;
19233 char_u numbuf[NUMBUFLEN];
19234 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019235#ifdef FEAT_PROFILE
19236 proftime_T wait_start;
19237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238
19239 /* If depth of calling is getting too high, don't execute the function */
19240 if (depth >= p_mfd)
19241 {
19242 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019243 rettv->v_type = VAR_NUMBER;
19244 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 return;
19246 }
19247 ++depth;
19248
19249 line_breakcheck(); /* check for CTRL-C hit */
19250
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019251 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 fc.rettv = rettv;
19255 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 fc.linenr = 0;
19257 fc.returned = FALSE;
19258 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019260 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 fc.dbg_tick = debug_tick;
19262
Bram Moolenaar33570922005-01-25 22:26:29 +000019263 /*
19264 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19265 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19266 * each argument variable and saves a lot of time.
19267 */
19268 /*
19269 * Init l: variables.
19270 */
19271 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019272 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019273 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019274 /* Set l:self to "selfdict". */
19275 v = &fc.fixvar[fixvar_idx++].var;
19276 STRCPY(v->di_key, "self");
19277 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19278 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19279 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019280 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019281 v->di_tv.vval.v_dict = selfdict;
19282 ++selfdict->dv_refcount;
19283 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019284
Bram Moolenaar33570922005-01-25 22:26:29 +000019285 /*
19286 * Init a: variables.
19287 * Set a:0 to "argcount".
19288 * Set a:000 to a list with room for the "..." arguments.
19289 */
19290 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19291 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019292 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019293 v = &fc.fixvar[fixvar_idx++].var;
19294 STRCPY(v->di_key, "000");
19295 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19296 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19297 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019298 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 v->di_tv.vval.v_list = &fc.l_varlist;
19300 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19301 fc.l_varlist.lv_refcount = 99999;
19302
19303 /*
19304 * Set a:firstline to "firstline" and a:lastline to "lastline".
19305 * Set a:name to named arguments.
19306 * Set a:N to the "..." arguments.
19307 */
19308 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19309 (varnumber_T)firstline);
19310 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19311 (varnumber_T)lastline);
19312 for (i = 0; i < argcount; ++i)
19313 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019314 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019315 if (ai < 0)
19316 /* named argument a:name */
19317 name = FUNCARG(fp, i);
19318 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019319 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019320 /* "..." argument a:1, a:2, etc. */
19321 sprintf((char *)numbuf, "%d", ai + 1);
19322 name = numbuf;
19323 }
19324 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19325 {
19326 v = &fc.fixvar[fixvar_idx++].var;
19327 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19328 }
19329 else
19330 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019331 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19332 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019333 if (v == NULL)
19334 break;
19335 v->di_flags = DI_FLAGS_RO;
19336 }
19337 STRCPY(v->di_key, name);
19338 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19339
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019340 /* Note: the values are copied directly to avoid alloc/free.
19341 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019342 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019343 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019344
19345 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19346 {
19347 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19348 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019349 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019350 }
19351 }
19352
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 /* Don't redraw while executing the function. */
19354 ++RedrawingDisabled;
19355 save_sourcing_name = sourcing_name;
19356 save_sourcing_lnum = sourcing_lnum;
19357 sourcing_lnum = 1;
19358 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019359 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 if (sourcing_name != NULL)
19361 {
19362 if (save_sourcing_name != NULL
19363 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19364 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19365 else
19366 STRCPY(sourcing_name, "function ");
19367 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19368
19369 if (p_verbose >= 12)
19370 {
19371 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019372 verbose_enter_scroll();
19373
Bram Moolenaar555b2802005-05-19 21:08:39 +000019374 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375 if (p_verbose >= 14)
19376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019378 char_u numbuf[NUMBUFLEN];
19379 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380
19381 msg_puts((char_u *)"(");
19382 for (i = 0; i < argcount; ++i)
19383 {
19384 if (i > 0)
19385 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019386 if (argvars[i].v_type == VAR_NUMBER)
19387 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 else
19389 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019390 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019391 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019393 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 }
19395 }
19396 msg_puts((char_u *)")");
19397 }
19398 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019399
19400 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 --no_wait_return;
19402 }
19403 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019404#ifdef FEAT_PROFILE
19405 if (do_profiling)
19406 {
19407 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19408 func_do_profile(fp);
19409 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019410 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019411 {
19412 ++fp->uf_tm_count;
19413 profile_start(&fp->uf_tm_start);
19414 profile_zero(&fp->uf_tm_children);
19415 }
19416 script_prof_save(&wait_start);
19417 }
19418#endif
19419
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019421 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 save_did_emsg = did_emsg;
19423 did_emsg = FALSE;
19424
19425 /* call do_cmdline() to execute the lines */
19426 do_cmdline(NULL, get_func_line, (void *)&fc,
19427 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19428
19429 --RedrawingDisabled;
19430
19431 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019432 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019434 clear_tv(rettv);
19435 rettv->v_type = VAR_NUMBER;
19436 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 }
19438
Bram Moolenaar05159a02005-02-26 23:04:13 +000019439#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019440 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019441 {
19442 profile_end(&fp->uf_tm_start);
19443 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19444 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019445 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019446 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019447 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019448 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19449 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019450 }
19451 }
19452#endif
19453
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 /* when being verbose, mention the return value */
19455 if (p_verbose >= 12)
19456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019458 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019461 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019463 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19464 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019467 char_u buf[MSG_BUF_LEN];
19468 char_u numbuf[NUMBUFLEN];
19469 char_u *tofree;
19470
Bram Moolenaar555b2802005-05-19 21:08:39 +000019471 /* The value may be very long. Skip the middle part, so that we
19472 * have some idea how it starts and ends. smsg() would always
19473 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019474 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019475 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019476 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019477 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 }
19479 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019480
19481 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 --no_wait_return;
19483 }
19484
19485 vim_free(sourcing_name);
19486 sourcing_name = save_sourcing_name;
19487 sourcing_lnum = save_sourcing_lnum;
19488 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019489#ifdef FEAT_PROFILE
19490 if (do_profiling)
19491 script_prof_restore(&wait_start);
19492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493
19494 if (p_verbose >= 12 && sourcing_name != NULL)
19495 {
19496 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019497 verbose_enter_scroll();
19498
Bram Moolenaar555b2802005-05-19 21:08:39 +000019499 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019501
19502 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 --no_wait_return;
19504 }
19505
19506 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019507 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508
Bram Moolenaar33570922005-01-25 22:26:29 +000019509 /* The a: variables typevals were not alloced, only free the allocated
19510 * variables. */
19511 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19512
19513 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 --depth;
19515}
19516
19517/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 * Add a number variable "name" to dict "dp" with value "nr".
19519 */
19520 static void
19521add_nr_var(dp, v, name, nr)
19522 dict_T *dp;
19523 dictitem_T *v;
19524 char *name;
19525 varnumber_T nr;
19526{
19527 STRCPY(v->di_key, name);
19528 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19529 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19530 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019531 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019532 v->di_tv.vval.v_number = nr;
19533}
19534
19535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 * ":return [expr]"
19537 */
19538 void
19539ex_return(eap)
19540 exarg_T *eap;
19541{
19542 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019543 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 int returning = FALSE;
19545
19546 if (current_funccal == NULL)
19547 {
19548 EMSG(_("E133: :return not inside a function"));
19549 return;
19550 }
19551
19552 if (eap->skip)
19553 ++emsg_skip;
19554
19555 eap->nextcmd = NULL;
19556 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019557 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 {
19559 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019562 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 }
19564 /* It's safer to return also on error. */
19565 else if (!eap->skip)
19566 {
19567 /*
19568 * Return unless the expression evaluation has been cancelled due to an
19569 * aborting error, an interrupt, or an exception.
19570 */
19571 if (!aborting())
19572 returning = do_return(eap, FALSE, TRUE, NULL);
19573 }
19574
19575 /* When skipping or the return gets pending, advance to the next command
19576 * in this line (!returning). Otherwise, ignore the rest of the line.
19577 * Following lines will be ignored by get_func_line(). */
19578 if (returning)
19579 eap->nextcmd = NULL;
19580 else if (eap->nextcmd == NULL) /* no argument */
19581 eap->nextcmd = check_nextcmd(arg);
19582
19583 if (eap->skip)
19584 --emsg_skip;
19585}
19586
19587/*
19588 * Return from a function. Possibly makes the return pending. Also called
19589 * for a pending return at the ":endtry" or after returning from an extra
19590 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019592 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 * FALSE when the return gets pending.
19594 */
19595 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019596do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 exarg_T *eap;
19598 int reanimate;
19599 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019600 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601{
19602 int idx;
19603 struct condstack *cstack = eap->cstack;
19604
19605 if (reanimate)
19606 /* Undo the return. */
19607 current_funccal->returned = FALSE;
19608
19609 /*
19610 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19611 * not in its finally clause (which then is to be executed next) is found.
19612 * In this case, make the ":return" pending for execution at the ":endtry".
19613 * Otherwise, return normally.
19614 */
19615 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19616 if (idx >= 0)
19617 {
19618 cstack->cs_pending[idx] = CSTP_RETURN;
19619
19620 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019621 /* A pending return again gets pending. "rettv" points to an
19622 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 else
19626 {
19627 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019632 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 {
19634 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 else
19638 EMSG(_(e_outofmem));
19639 }
19640 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019641 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642
19643 if (reanimate)
19644 {
19645 /* The pending return value could be overwritten by a ":return"
19646 * without argument in a finally clause; reset the default
19647 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 current_funccal->rettv->v_type = VAR_NUMBER;
19649 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650 }
19651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019652 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 }
19654 else
19655 {
19656 current_funccal->returned = TRUE;
19657
19658 /* If the return is carried out now, store the return value. For
19659 * a return immediately after reanimation, the value is already
19660 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019661 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019664 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019666 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667 }
19668 }
19669
19670 return idx < 0;
19671}
19672
19673/*
19674 * Free the variable with a pending return value.
19675 */
19676 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019677discard_pending_return(rettv)
19678 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679{
Bram Moolenaar33570922005-01-25 22:26:29 +000019680 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681}
19682
19683/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019684 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 * is an allocated string. Used by report_pending() for verbose messages.
19686 */
19687 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019688get_return_cmd(rettv)
19689 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019691 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019693 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019695 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019696 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019697 if (s == NULL)
19698 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019699
19700 STRCPY(IObuff, ":return ");
19701 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19702 if (STRLEN(s) + 8 >= IOSIZE)
19703 STRCPY(IObuff + IOSIZE - 4, "...");
19704 vim_free(tofree);
19705 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706}
19707
19708/*
19709 * Get next function line.
19710 * Called by do_cmdline() to get the next line.
19711 * Returns allocated string, or NULL for end of function.
19712 */
19713/* ARGSUSED */
19714 char_u *
19715get_func_line(c, cookie, indent)
19716 int c; /* not used */
19717 void *cookie;
19718 int indent; /* not used */
19719{
Bram Moolenaar33570922005-01-25 22:26:29 +000019720 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019721 ufunc_T *fp = fcp->func;
19722 char_u *retval;
19723 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724
19725 /* If breakpoints have been added/deleted need to check for it. */
19726 if (fcp->dbg_tick != debug_tick)
19727 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019728 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 sourcing_lnum);
19730 fcp->dbg_tick = debug_tick;
19731 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019732#ifdef FEAT_PROFILE
19733 if (do_profiling)
19734 func_line_end(cookie);
19735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736
Bram Moolenaar05159a02005-02-26 23:04:13 +000019737 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019738 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19739 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 retval = NULL;
19741 else
19742 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019743 /* Skip NULL lines (continuation lines). */
19744 while (fcp->linenr < gap->ga_len
19745 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19746 ++fcp->linenr;
19747 if (fcp->linenr >= gap->ga_len)
19748 retval = NULL;
19749 else
19750 {
19751 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19752 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019753#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019754 if (do_profiling)
19755 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019756#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 }
19759
19760 /* Did we encounter a breakpoint? */
19761 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19762 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019763 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019765 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 sourcing_lnum);
19767 fcp->dbg_tick = debug_tick;
19768 }
19769
19770 return retval;
19771}
19772
Bram Moolenaar05159a02005-02-26 23:04:13 +000019773#if defined(FEAT_PROFILE) || defined(PROTO)
19774/*
19775 * Called when starting to read a function line.
19776 * "sourcing_lnum" must be correct!
19777 * When skipping lines it may not actually be executed, but we won't find out
19778 * until later and we need to store the time now.
19779 */
19780 void
19781func_line_start(cookie)
19782 void *cookie;
19783{
19784 funccall_T *fcp = (funccall_T *)cookie;
19785 ufunc_T *fp = fcp->func;
19786
19787 if (fp->uf_profiling && sourcing_lnum >= 1
19788 && sourcing_lnum <= fp->uf_lines.ga_len)
19789 {
19790 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019791 /* Skip continuation lines. */
19792 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19793 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019794 fp->uf_tml_execed = FALSE;
19795 profile_start(&fp->uf_tml_start);
19796 profile_zero(&fp->uf_tml_children);
19797 profile_get_wait(&fp->uf_tml_wait);
19798 }
19799}
19800
19801/*
19802 * Called when actually executing a function line.
19803 */
19804 void
19805func_line_exec(cookie)
19806 void *cookie;
19807{
19808 funccall_T *fcp = (funccall_T *)cookie;
19809 ufunc_T *fp = fcp->func;
19810
19811 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19812 fp->uf_tml_execed = TRUE;
19813}
19814
19815/*
19816 * Called when done with a function line.
19817 */
19818 void
19819func_line_end(cookie)
19820 void *cookie;
19821{
19822 funccall_T *fcp = (funccall_T *)cookie;
19823 ufunc_T *fp = fcp->func;
19824
19825 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19826 {
19827 if (fp->uf_tml_execed)
19828 {
19829 ++fp->uf_tml_count[fp->uf_tml_idx];
19830 profile_end(&fp->uf_tml_start);
19831 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019832 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019833 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19834 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019835 }
19836 fp->uf_tml_idx = -1;
19837 }
19838}
19839#endif
19840
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841/*
19842 * Return TRUE if the currently active function should be ended, because a
19843 * return was encountered or an error occured. Used inside a ":while".
19844 */
19845 int
19846func_has_ended(cookie)
19847 void *cookie;
19848{
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850
19851 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19852 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019853 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 || fcp->returned);
19855}
19856
19857/*
19858 * return TRUE if cookie indicates a function which "abort"s on errors.
19859 */
19860 int
19861func_has_abort(cookie)
19862 void *cookie;
19863{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019864 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865}
19866
19867#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19868typedef enum
19869{
19870 VAR_FLAVOUR_DEFAULT,
19871 VAR_FLAVOUR_SESSION,
19872 VAR_FLAVOUR_VIMINFO
19873} var_flavour_T;
19874
19875static var_flavour_T var_flavour __ARGS((char_u *varname));
19876
19877 static var_flavour_T
19878var_flavour(varname)
19879 char_u *varname;
19880{
19881 char_u *p = varname;
19882
19883 if (ASCII_ISUPPER(*p))
19884 {
19885 while (*(++p))
19886 if (ASCII_ISLOWER(*p))
19887 return VAR_FLAVOUR_SESSION;
19888 return VAR_FLAVOUR_VIMINFO;
19889 }
19890 else
19891 return VAR_FLAVOUR_DEFAULT;
19892}
19893#endif
19894
19895#if defined(FEAT_VIMINFO) || defined(PROTO)
19896/*
19897 * Restore global vars that start with a capital from the viminfo file
19898 */
19899 int
19900read_viminfo_varlist(virp, writing)
19901 vir_T *virp;
19902 int writing;
19903{
19904 char_u *tab;
19905 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019906 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907
19908 if (!writing && (find_viminfo_parameter('!') != NULL))
19909 {
19910 tab = vim_strchr(virp->vir_line + 1, '\t');
19911 if (tab != NULL)
19912 {
19913 *tab++ = '\0'; /* isolate the variable name */
19914 if (*tab == 'S') /* string var */
19915 is_string = TRUE;
19916
19917 tab = vim_strchr(tab, '\t');
19918 if (tab != NULL)
19919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 if (is_string)
19921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019922 tv.v_type = VAR_STRING;
19923 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 }
19926 else
19927 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019928 tv.v_type = VAR_NUMBER;
19929 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019931 set_var(virp->vir_line + 1, &tv, FALSE);
19932 if (is_string)
19933 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 }
19935 }
19936 }
19937
19938 return viminfo_readline(virp);
19939}
19940
19941/*
19942 * Write global vars that start with a capital to the viminfo file
19943 */
19944 void
19945write_viminfo_varlist(fp)
19946 FILE *fp;
19947{
Bram Moolenaar33570922005-01-25 22:26:29 +000019948 hashitem_T *hi;
19949 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019950 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019951 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019952 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019953 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019954 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955
19956 if (find_viminfo_parameter('!') == NULL)
19957 return;
19958
19959 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019960
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 todo = globvarht.ht_used;
19962 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019964 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019966 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019967 this_var = HI2DI(hi);
19968 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019970 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019971 {
19972 case VAR_STRING: s = "STR"; break;
19973 case VAR_NUMBER: s = "NUM"; break;
19974 default: continue;
19975 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019976 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019977 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019978 if (p != NULL)
19979 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019980 vim_free(tofree);
19981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982 }
19983 }
19984}
19985#endif
19986
19987#if defined(FEAT_SESSION) || defined(PROTO)
19988 int
19989store_session_globals(fd)
19990 FILE *fd;
19991{
Bram Moolenaar33570922005-01-25 22:26:29 +000019992 hashitem_T *hi;
19993 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019994 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 char_u *p, *t;
19996
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 todo = globvarht.ht_used;
19998 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020000 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020002 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020003 this_var = HI2DI(hi);
20004 if ((this_var->di_tv.v_type == VAR_NUMBER
20005 || this_var->di_tv.v_type == VAR_STRING)
20006 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020007 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020008 /* Escape special characters with a backslash. Turn a LF and
20009 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020010 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020011 (char_u *)"\\\"\n\r");
20012 if (p == NULL) /* out of memory */
20013 break;
20014 for (t = p; *t != NUL; ++t)
20015 if (*t == '\n')
20016 *t = 'n';
20017 else if (*t == '\r')
20018 *t = 'r';
20019 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020020 this_var->di_key,
20021 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20022 : ' ',
20023 p,
20024 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20025 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020026 || put_eol(fd) == FAIL)
20027 {
20028 vim_free(p);
20029 return FAIL;
20030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 vim_free(p);
20032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 }
20034 }
20035 return OK;
20036}
20037#endif
20038
Bram Moolenaar661b1822005-07-28 22:36:45 +000020039/*
20040 * Display script name where an item was last set.
20041 * Should only be invoked when 'verbose' is non-zero.
20042 */
20043 void
20044last_set_msg(scriptID)
20045 scid_T scriptID;
20046{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020047 char_u *p;
20048
Bram Moolenaar661b1822005-07-28 22:36:45 +000020049 if (scriptID != 0)
20050 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020051 p = home_replace_save(NULL, get_scriptname(scriptID));
20052 if (p != NULL)
20053 {
20054 verbose_enter();
20055 MSG_PUTS(_("\n\tLast set from "));
20056 MSG_PUTS(p);
20057 vim_free(p);
20058 verbose_leave();
20059 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020060 }
20061}
20062
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063#endif /* FEAT_EVAL */
20064
20065#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20066
20067
20068#ifdef WIN3264
20069/*
20070 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20071 */
20072static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20073static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20074static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20075
20076/*
20077 * Get the short pathname of a file.
20078 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20079 */
20080 static int
20081get_short_pathname(fnamep, bufp, fnamelen)
20082 char_u **fnamep;
20083 char_u **bufp;
20084 int *fnamelen;
20085{
20086 int l,len;
20087 char_u *newbuf;
20088
20089 len = *fnamelen;
20090
20091 l = GetShortPathName(*fnamep, *fnamep, len);
20092 if (l > len - 1)
20093 {
20094 /* If that doesn't work (not enough space), then save the string
20095 * and try again with a new buffer big enough
20096 */
20097 newbuf = vim_strnsave(*fnamep, l);
20098 if (newbuf == NULL)
20099 return 0;
20100
20101 vim_free(*bufp);
20102 *fnamep = *bufp = newbuf;
20103
20104 l = GetShortPathName(*fnamep,*fnamep,l+1);
20105
20106 /* Really should always succeed, as the buffer is big enough */
20107 }
20108
20109 *fnamelen = l;
20110 return 1;
20111}
20112
20113/*
20114 * Create a short path name. Returns the length of the buffer it needs.
20115 * Doesn't copy over the end of the buffer passed in.
20116 */
20117 static int
20118shortpath_for_invalid_fname(fname, bufp, fnamelen)
20119 char_u **fname;
20120 char_u **bufp;
20121 int *fnamelen;
20122{
20123 char_u *s, *p, *pbuf2, *pbuf3;
20124 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020125 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126
20127 /* Make a copy */
20128 len2 = *fnamelen;
20129 pbuf2 = vim_strnsave(*fname, len2);
20130 pbuf3 = NULL;
20131
20132 s = pbuf2 + len2 - 1; /* Find the end */
20133 slen = 1;
20134 plen = len2;
20135
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020136 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 {
20138 --s;
20139 ++slen;
20140 --plen;
20141 }
20142
20143 do
20144 {
20145 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020146 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 {
20148 --s;
20149 ++slen;
20150 --plen;
20151 }
20152 if (s <= pbuf2)
20153 break;
20154
20155 /* Remeber the character that is about to be blatted */
20156 ch = *s;
20157 *s = 0; /* get_short_pathname requires a null-terminated string */
20158
20159 /* Try it in situ */
20160 p = pbuf2;
20161 if (!get_short_pathname(&p, &pbuf3, &plen))
20162 {
20163 vim_free(pbuf2);
20164 return -1;
20165 }
20166 *s = ch; /* Preserve the string */
20167 } while (plen == 0);
20168
20169 if (plen > 0)
20170 {
20171 /* Remeber the length of the new string. */
20172 *fnamelen = len = plen + slen;
20173 vim_free(*bufp);
20174 if (len > len2)
20175 {
20176 /* If there's not enough space in the currently allocated string,
20177 * then copy it to a buffer big enough.
20178 */
20179 *fname= *bufp = vim_strnsave(p, len);
20180 if (*fname == NULL)
20181 return -1;
20182 }
20183 else
20184 {
20185 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20186 *fname = *bufp = pbuf2;
20187 if (p != pbuf2)
20188 strncpy(*fname, p, plen);
20189 pbuf2 = NULL;
20190 }
20191 /* Concat the next bit */
20192 strncpy(*fname + plen, s, slen);
20193 (*fname)[len] = '\0';
20194 }
20195 vim_free(pbuf3);
20196 vim_free(pbuf2);
20197 return 0;
20198}
20199
20200/*
20201 * Get a pathname for a partial path.
20202 */
20203 static int
20204shortpath_for_partial(fnamep, bufp, fnamelen)
20205 char_u **fnamep;
20206 char_u **bufp;
20207 int *fnamelen;
20208{
20209 int sepcount, len, tflen;
20210 char_u *p;
20211 char_u *pbuf, *tfname;
20212 int hasTilde;
20213
20214 /* Count up the path seperators from the RHS.. so we know which part
20215 * of the path to return.
20216 */
20217 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020218 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 if (vim_ispathsep(*p))
20220 ++sepcount;
20221
20222 /* Need full path first (use expand_env() to remove a "~/") */
20223 hasTilde = (**fnamep == '~');
20224 if (hasTilde)
20225 pbuf = tfname = expand_env_save(*fnamep);
20226 else
20227 pbuf = tfname = FullName_save(*fnamep, FALSE);
20228
20229 len = tflen = STRLEN(tfname);
20230
20231 if (!get_short_pathname(&tfname, &pbuf, &len))
20232 return -1;
20233
20234 if (len == 0)
20235 {
20236 /* Don't have a valid filename, so shorten the rest of the
20237 * path if we can. This CAN give us invalid 8.3 filenames, but
20238 * there's not a lot of point in guessing what it might be.
20239 */
20240 len = tflen;
20241 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20242 return -1;
20243 }
20244
20245 /* Count the paths backward to find the beginning of the desired string. */
20246 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020247 {
20248#ifdef FEAT_MBYTE
20249 if (has_mbyte)
20250 p -= mb_head_off(tfname, p);
20251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 if (vim_ispathsep(*p))
20253 {
20254 if (sepcount == 0 || (hasTilde && sepcount == 1))
20255 break;
20256 else
20257 sepcount --;
20258 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 if (hasTilde)
20261 {
20262 --p;
20263 if (p >= tfname)
20264 *p = '~';
20265 else
20266 return -1;
20267 }
20268 else
20269 ++p;
20270
20271 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20272 vim_free(*bufp);
20273 *fnamelen = (int)STRLEN(p);
20274 *bufp = pbuf;
20275 *fnamep = p;
20276
20277 return 0;
20278}
20279#endif /* WIN3264 */
20280
20281/*
20282 * Adjust a filename, according to a string of modifiers.
20283 * *fnamep must be NUL terminated when called. When returning, the length is
20284 * determined by *fnamelen.
20285 * Returns valid flags.
20286 * When there is an error, *fnamep is set to NULL.
20287 */
20288 int
20289modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20290 char_u *src; /* string with modifiers */
20291 int *usedlen; /* characters after src that are used */
20292 char_u **fnamep; /* file name so far */
20293 char_u **bufp; /* buffer for allocated file name or NULL */
20294 int *fnamelen; /* length of fnamep */
20295{
20296 int valid = 0;
20297 char_u *tail;
20298 char_u *s, *p, *pbuf;
20299 char_u dirname[MAXPATHL];
20300 int c;
20301 int has_fullname = 0;
20302#ifdef WIN3264
20303 int has_shortname = 0;
20304#endif
20305
20306repeat:
20307 /* ":p" - full path/file_name */
20308 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20309 {
20310 has_fullname = 1;
20311
20312 valid |= VALID_PATH;
20313 *usedlen += 2;
20314
20315 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20316 if ((*fnamep)[0] == '~'
20317#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20318 && ((*fnamep)[1] == '/'
20319# ifdef BACKSLASH_IN_FILENAME
20320 || (*fnamep)[1] == '\\'
20321# endif
20322 || (*fnamep)[1] == NUL)
20323
20324#endif
20325 )
20326 {
20327 *fnamep = expand_env_save(*fnamep);
20328 vim_free(*bufp); /* free any allocated file name */
20329 *bufp = *fnamep;
20330 if (*fnamep == NULL)
20331 return -1;
20332 }
20333
20334 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020335 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 {
20337 if (vim_ispathsep(*p)
20338 && p[1] == '.'
20339 && (p[2] == NUL
20340 || vim_ispathsep(p[2])
20341 || (p[2] == '.'
20342 && (p[3] == NUL || vim_ispathsep(p[3])))))
20343 break;
20344 }
20345
20346 /* FullName_save() is slow, don't use it when not needed. */
20347 if (*p != NUL || !vim_isAbsName(*fnamep))
20348 {
20349 *fnamep = FullName_save(*fnamep, *p != NUL);
20350 vim_free(*bufp); /* free any allocated file name */
20351 *bufp = *fnamep;
20352 if (*fnamep == NULL)
20353 return -1;
20354 }
20355
20356 /* Append a path separator to a directory. */
20357 if (mch_isdir(*fnamep))
20358 {
20359 /* Make room for one or two extra characters. */
20360 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20361 vim_free(*bufp); /* free any allocated file name */
20362 *bufp = *fnamep;
20363 if (*fnamep == NULL)
20364 return -1;
20365 add_pathsep(*fnamep);
20366 }
20367 }
20368
20369 /* ":." - path relative to the current directory */
20370 /* ":~" - path relative to the home directory */
20371 /* ":8" - shortname path - postponed till after */
20372 while (src[*usedlen] == ':'
20373 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20374 {
20375 *usedlen += 2;
20376 if (c == '8')
20377 {
20378#ifdef WIN3264
20379 has_shortname = 1; /* Postpone this. */
20380#endif
20381 continue;
20382 }
20383 pbuf = NULL;
20384 /* Need full path first (use expand_env() to remove a "~/") */
20385 if (!has_fullname)
20386 {
20387 if (c == '.' && **fnamep == '~')
20388 p = pbuf = expand_env_save(*fnamep);
20389 else
20390 p = pbuf = FullName_save(*fnamep, FALSE);
20391 }
20392 else
20393 p = *fnamep;
20394
20395 has_fullname = 0;
20396
20397 if (p != NULL)
20398 {
20399 if (c == '.')
20400 {
20401 mch_dirname(dirname, MAXPATHL);
20402 s = shorten_fname(p, dirname);
20403 if (s != NULL)
20404 {
20405 *fnamep = s;
20406 if (pbuf != NULL)
20407 {
20408 vim_free(*bufp); /* free any allocated file name */
20409 *bufp = pbuf;
20410 pbuf = NULL;
20411 }
20412 }
20413 }
20414 else
20415 {
20416 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20417 /* Only replace it when it starts with '~' */
20418 if (*dirname == '~')
20419 {
20420 s = vim_strsave(dirname);
20421 if (s != NULL)
20422 {
20423 *fnamep = s;
20424 vim_free(*bufp);
20425 *bufp = s;
20426 }
20427 }
20428 }
20429 vim_free(pbuf);
20430 }
20431 }
20432
20433 tail = gettail(*fnamep);
20434 *fnamelen = (int)STRLEN(*fnamep);
20435
20436 /* ":h" - head, remove "/file_name", can be repeated */
20437 /* Don't remove the first "/" or "c:\" */
20438 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20439 {
20440 valid |= VALID_HEAD;
20441 *usedlen += 2;
20442 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020443 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 --tail;
20445 *fnamelen = (int)(tail - *fnamep);
20446#ifdef VMS
20447 if (*fnamelen > 0)
20448 *fnamelen += 1; /* the path separator is part of the path */
20449#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020450 while (tail > s && !after_pathsep(s, tail))
20451 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 }
20453
20454 /* ":8" - shortname */
20455 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20456 {
20457 *usedlen += 2;
20458#ifdef WIN3264
20459 has_shortname = 1;
20460#endif
20461 }
20462
20463#ifdef WIN3264
20464 /* Check shortname after we have done 'heads' and before we do 'tails'
20465 */
20466 if (has_shortname)
20467 {
20468 pbuf = NULL;
20469 /* Copy the string if it is shortened by :h */
20470 if (*fnamelen < (int)STRLEN(*fnamep))
20471 {
20472 p = vim_strnsave(*fnamep, *fnamelen);
20473 if (p == 0)
20474 return -1;
20475 vim_free(*bufp);
20476 *bufp = *fnamep = p;
20477 }
20478
20479 /* Split into two implementations - makes it easier. First is where
20480 * there isn't a full name already, second is where there is.
20481 */
20482 if (!has_fullname && !vim_isAbsName(*fnamep))
20483 {
20484 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20485 return -1;
20486 }
20487 else
20488 {
20489 int l;
20490
20491 /* Simple case, already have the full-name
20492 * Nearly always shorter, so try first time. */
20493 l = *fnamelen;
20494 if (!get_short_pathname(fnamep, bufp, &l))
20495 return -1;
20496
20497 if (l == 0)
20498 {
20499 /* Couldn't find the filename.. search the paths.
20500 */
20501 l = *fnamelen;
20502 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20503 return -1;
20504 }
20505 *fnamelen = l;
20506 }
20507 }
20508#endif /* WIN3264 */
20509
20510 /* ":t" - tail, just the basename */
20511 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20512 {
20513 *usedlen += 2;
20514 *fnamelen -= (int)(tail - *fnamep);
20515 *fnamep = tail;
20516 }
20517
20518 /* ":e" - extension, can be repeated */
20519 /* ":r" - root, without extension, can be repeated */
20520 while (src[*usedlen] == ':'
20521 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20522 {
20523 /* find a '.' in the tail:
20524 * - for second :e: before the current fname
20525 * - otherwise: The last '.'
20526 */
20527 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20528 s = *fnamep - 2;
20529 else
20530 s = *fnamep + *fnamelen - 1;
20531 for ( ; s > tail; --s)
20532 if (s[0] == '.')
20533 break;
20534 if (src[*usedlen + 1] == 'e') /* :e */
20535 {
20536 if (s > tail)
20537 {
20538 *fnamelen += (int)(*fnamep - (s + 1));
20539 *fnamep = s + 1;
20540#ifdef VMS
20541 /* cut version from the extension */
20542 s = *fnamep + *fnamelen - 1;
20543 for ( ; s > *fnamep; --s)
20544 if (s[0] == ';')
20545 break;
20546 if (s > *fnamep)
20547 *fnamelen = s - *fnamep;
20548#endif
20549 }
20550 else if (*fnamep <= tail)
20551 *fnamelen = 0;
20552 }
20553 else /* :r */
20554 {
20555 if (s > tail) /* remove one extension */
20556 *fnamelen = (int)(s - *fnamep);
20557 }
20558 *usedlen += 2;
20559 }
20560
20561 /* ":s?pat?foo?" - substitute */
20562 /* ":gs?pat?foo?" - global substitute */
20563 if (src[*usedlen] == ':'
20564 && (src[*usedlen + 1] == 's'
20565 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20566 {
20567 char_u *str;
20568 char_u *pat;
20569 char_u *sub;
20570 int sep;
20571 char_u *flags;
20572 int didit = FALSE;
20573
20574 flags = (char_u *)"";
20575 s = src + *usedlen + 2;
20576 if (src[*usedlen + 1] == 'g')
20577 {
20578 flags = (char_u *)"g";
20579 ++s;
20580 }
20581
20582 sep = *s++;
20583 if (sep)
20584 {
20585 /* find end of pattern */
20586 p = vim_strchr(s, sep);
20587 if (p != NULL)
20588 {
20589 pat = vim_strnsave(s, (int)(p - s));
20590 if (pat != NULL)
20591 {
20592 s = p + 1;
20593 /* find end of substitution */
20594 p = vim_strchr(s, sep);
20595 if (p != NULL)
20596 {
20597 sub = vim_strnsave(s, (int)(p - s));
20598 str = vim_strnsave(*fnamep, *fnamelen);
20599 if (sub != NULL && str != NULL)
20600 {
20601 *usedlen = (int)(p + 1 - src);
20602 s = do_string_sub(str, pat, sub, flags);
20603 if (s != NULL)
20604 {
20605 *fnamep = s;
20606 *fnamelen = (int)STRLEN(s);
20607 vim_free(*bufp);
20608 *bufp = s;
20609 didit = TRUE;
20610 }
20611 }
20612 vim_free(sub);
20613 vim_free(str);
20614 }
20615 vim_free(pat);
20616 }
20617 }
20618 /* after using ":s", repeat all the modifiers */
20619 if (didit)
20620 goto repeat;
20621 }
20622 }
20623
20624 return valid;
20625}
20626
20627/*
20628 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20629 * "flags" can be "g" to do a global substitute.
20630 * Returns an allocated string, NULL for error.
20631 */
20632 char_u *
20633do_string_sub(str, pat, sub, flags)
20634 char_u *str;
20635 char_u *pat;
20636 char_u *sub;
20637 char_u *flags;
20638{
20639 int sublen;
20640 regmatch_T regmatch;
20641 int i;
20642 int do_all;
20643 char_u *tail;
20644 garray_T ga;
20645 char_u *ret;
20646 char_u *save_cpo;
20647
20648 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20649 save_cpo = p_cpo;
20650 p_cpo = (char_u *)"";
20651
20652 ga_init2(&ga, 1, 200);
20653
20654 do_all = (flags[0] == 'g');
20655
20656 regmatch.rm_ic = p_ic;
20657 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20658 if (regmatch.regprog != NULL)
20659 {
20660 tail = str;
20661 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20662 {
20663 /*
20664 * Get some space for a temporary buffer to do the substitution
20665 * into. It will contain:
20666 * - The text up to where the match is.
20667 * - The substituted text.
20668 * - The text after the match.
20669 */
20670 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20671 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20672 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20673 {
20674 ga_clear(&ga);
20675 break;
20676 }
20677
20678 /* copy the text up to where the match is */
20679 i = (int)(regmatch.startp[0] - tail);
20680 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20681 /* add the substituted text */
20682 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20683 + ga.ga_len + i, TRUE, TRUE, FALSE);
20684 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 /* avoid getting stuck on a match with an empty string */
20686 if (tail == regmatch.endp[0])
20687 {
20688 if (*tail == NUL)
20689 break;
20690 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20691 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 }
20693 else
20694 {
20695 tail = regmatch.endp[0];
20696 if (*tail == NUL)
20697 break;
20698 }
20699 if (!do_all)
20700 break;
20701 }
20702
20703 if (ga.ga_data != NULL)
20704 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20705
20706 vim_free(regmatch.regprog);
20707 }
20708
20709 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20710 ga_clear(&ga);
20711 p_cpo = save_cpo;
20712
20713 return ret;
20714}
20715
20716#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */