blob: e80407e68934f58c8885c82af942f3f9b1c576c4 [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 Moolenaare580b0c2006-03-21 21:33:03 +0000586static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000599static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000601static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000608static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000609static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000610static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000615static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000619static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620#ifdef HAVE_STRFTIME
621static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
623static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000635static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000636static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000637static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000638static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000639static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000641static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000655static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000660static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
661static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static int get_env_len __ARGS((char_u **arg));
663static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000664static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000665static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
666#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
667#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
668 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000669static 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 +0000670static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000671static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000672static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
673static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static typval_T *alloc_tv __ARGS((void));
675static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void init_tv __ARGS((typval_T *varp));
677static long get_tv_number __ARGS((typval_T *varp));
678static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000679static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static char_u *get_tv_string __ARGS((typval_T *varp));
681static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000682static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000684static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
686static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
687static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
688static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
689static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
690static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
691static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000692static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000694static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
696static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
697static int eval_fname_script __ARGS((char_u *p));
698static int eval_fname_sid __ARGS((char_u *p));
699static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static ufunc_T *find_func __ARGS((char_u *name));
701static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000702static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703#ifdef FEAT_PROFILE
704static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000705static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
706static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
707static int
708# ifdef __BORLANDC__
709 _RTLENTRYF
710# endif
711 prof_total_cmp __ARGS((const void *s1, const void *s2));
712static int
713# ifdef __BORLANDC__
714 _RTLENTRYF
715# endif
716 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000717#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000718static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000719static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000720static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void func_free __ARGS((ufunc_T *fp));
722static void func_unref __ARGS((char_u *name));
723static void func_ref __ARGS((char_u *name));
724static 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));
725static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000726static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000727static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000728static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000730/* Character used as separated in autoload function/variable names. */
731#define AUTOLOAD_CHAR '#'
732
Bram Moolenaar33570922005-01-25 22:26:29 +0000733/*
734 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000735 */
736 void
737eval_init()
738{
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 int i;
740 struct vimvar *p;
741
742 init_var_dict(&globvardict, &globvars_var);
743 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000744 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000745 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000746
747 for (i = 0; i < VV_LEN; ++i)
748 {
749 p = &vimvars[i];
750 STRCPY(p->vv_di.di_key, p->vv_name);
751 if (p->vv_flags & VV_RO)
752 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
753 else if (p->vv_flags & VV_RO_SBX)
754 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
755 else
756 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757
758 /* add to v: scope dict, unless the value is not always available */
759 if (p->vv_type != VAR_UNKNOWN)
760 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000761 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000762 /* add to compat scope dict */
763 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000764 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000765}
766
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000767#if defined(EXITFREE) || defined(PROTO)
768 void
769eval_clear()
770{
771 int i;
772 struct vimvar *p;
773
774 for (i = 0; i < VV_LEN; ++i)
775 {
776 p = &vimvars[i];
777 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000778 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000779 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000780 p->vv_di.di_tv.vval.v_string = NULL;
781 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000782 }
783 hash_clear(&vimvarht);
784 hash_clear(&compat_hashtab);
785
786 /* script-local variables */
787 for (i = 1; i <= ga_scripts.ga_len; ++i)
788 vars_clear(&SCRIPT_VARS(i));
789 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000790 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000791
792 /* global variables */
793 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000794
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000795 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000796 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000797 hash_clear(&func_hashtab);
798
799 /* unreferenced lists and dicts */
800 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000801}
802#endif
803
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 * Return the name of the executed function.
806 */
807 char_u *
808func_name(cookie)
809 void *cookie;
810{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000811 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812}
813
814/*
815 * Return the address holding the next breakpoint line for a funccall cookie.
816 */
817 linenr_T *
818func_breakpoint(cookie)
819 void *cookie;
820{
Bram Moolenaar33570922005-01-25 22:26:29 +0000821 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
825 * Return the address holding the debug tick for a funccall cookie.
826 */
827 int *
828func_dbg_tick(cookie)
829 void *cookie;
830{
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832}
833
834/*
835 * Return the nesting level for a funccall cookie.
836 */
837 int
838func_level(cookie)
839 void *cookie;
840{
Bram Moolenaar33570922005-01-25 22:26:29 +0000841 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000845funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847/*
848 * Return TRUE when a function was ended by a ":return" command.
849 */
850 int
851current_func_returned()
852{
853 return current_funccal->returned;
854}
855
856
857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 * Set an internal variable to a string value. Creates the variable if it does
859 * not already exist.
860 */
861 void
862set_internal_string_var(name, value)
863 char_u *name;
864 char_u *value;
865{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000866 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 val = vim_strsave(value);
870 if (val != NULL)
871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000873 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000875 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878 }
879}
880
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000881static lval_T *redir_lval = NULL;
882static char_u *redir_endp = NULL;
883static char_u *redir_varname = NULL;
884
885/*
886 * Start recording command output to a variable
887 * Returns OK if successfully completed the setup. FAIL otherwise.
888 */
889 int
890var_redir_start(name, append)
891 char_u *name;
892 int append; /* append to an existing variable */
893{
894 int save_emsg;
895 int err;
896 typval_T tv;
897
898 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000899 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900 {
901 EMSG(_(e_invarg));
902 return FAIL;
903 }
904
905 redir_varname = vim_strsave(name);
906 if (redir_varname == NULL)
907 return FAIL;
908
909 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
910 if (redir_lval == NULL)
911 {
912 var_redir_stop();
913 return FAIL;
914 }
915
916 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000917 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
918 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
920 {
921 if (redir_endp != NULL && *redir_endp != NUL)
922 /* Trailing characters are present after the variable name */
923 EMSG(_(e_trailing));
924 else
925 EMSG(_(e_invarg));
926 var_redir_stop();
927 return FAIL;
928 }
929
930 /* check if we can write to the variable: set it to or append an empty
931 * string */
932 save_emsg = did_emsg;
933 did_emsg = FALSE;
934 tv.v_type = VAR_STRING;
935 tv.vval.v_string = (char_u *)"";
936 if (append)
937 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
938 else
939 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
940 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000941 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 if (err)
943 {
944 var_redir_stop();
945 return FAIL;
946 }
947 if (redir_lval->ll_newkey != NULL)
948 {
949 /* Dictionary item was created, don't do it again. */
950 vim_free(redir_lval->ll_newkey);
951 redir_lval->ll_newkey = NULL;
952 }
953
954 return OK;
955}
956
957/*
958 * Append "value[len]" to the variable set by var_redir_start().
959 */
960 void
961var_redir_str(value, len)
962 char_u *value;
963 int len;
964{
965 char_u *val;
966 typval_T tv;
967 int save_emsg;
968 int err;
969
970 if (redir_lval == NULL)
971 return;
972
973 if (len == -1)
974 /* Append the entire string */
975 val = vim_strsave(value);
976 else
977 /* Append only the specified number of characters */
978 val = vim_strnsave(value, len);
979 if (val == NULL)
980 return;
981
982 tv.v_type = VAR_STRING;
983 tv.vval.v_string = val;
984
985 save_emsg = did_emsg;
986 did_emsg = FALSE;
987 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
988 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000989 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000990 if (err)
991 var_redir_stop();
992
993 vim_free(tv.vval.v_string);
994}
995
996/*
997 * Stop redirecting command output to a variable.
998 */
999 void
1000var_redir_stop()
1001{
1002 if (redir_lval != NULL)
1003 {
1004 clear_lval(redir_lval);
1005 vim_free(redir_lval);
1006 redir_lval = NULL;
1007 }
1008 vim_free(redir_varname);
1009 redir_varname = NULL;
1010}
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012# if defined(FEAT_MBYTE) || defined(PROTO)
1013 int
1014eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1015 char_u *enc_from;
1016 char_u *enc_to;
1017 char_u *fname_from;
1018 char_u *fname_to;
1019{
1020 int err = FALSE;
1021
1022 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1023 set_vim_var_string(VV_CC_TO, enc_to, -1);
1024 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1025 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1026 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1027 err = TRUE;
1028 set_vim_var_string(VV_CC_FROM, NULL, -1);
1029 set_vim_var_string(VV_CC_TO, NULL, -1);
1030 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1031 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1032
1033 if (err)
1034 return FAIL;
1035 return OK;
1036}
1037# endif
1038
1039# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1040 int
1041eval_printexpr(fname, args)
1042 char_u *fname;
1043 char_u *args;
1044{
1045 int err = FALSE;
1046
1047 set_vim_var_string(VV_FNAME_IN, fname, -1);
1048 set_vim_var_string(VV_CMDARG, args, -1);
1049 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1050 err = TRUE;
1051 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1052 set_vim_var_string(VV_CMDARG, NULL, -1);
1053
1054 if (err)
1055 {
1056 mch_remove(fname);
1057 return FAIL;
1058 }
1059 return OK;
1060}
1061# endif
1062
1063# if defined(FEAT_DIFF) || defined(PROTO)
1064 void
1065eval_diff(origfile, newfile, outfile)
1066 char_u *origfile;
1067 char_u *newfile;
1068 char_u *outfile;
1069{
1070 int err = FALSE;
1071
1072 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1073 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1074 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1075 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1076 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1077 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1078 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1079}
1080
1081 void
1082eval_patch(origfile, difffile, outfile)
1083 char_u *origfile;
1084 char_u *difffile;
1085 char_u *outfile;
1086{
1087 int err;
1088
1089 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1090 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1091 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1092 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1093 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1094 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1095 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1096}
1097# endif
1098
1099/*
1100 * Top level evaluation function, returning a boolean.
1101 * Sets "error" to TRUE if there was an error.
1102 * Return TRUE or FALSE.
1103 */
1104 int
1105eval_to_bool(arg, error, nextcmd, skip)
1106 char_u *arg;
1107 int *error;
1108 char_u **nextcmd;
1109 int skip; /* only parse, don't execute */
1110{
Bram Moolenaar33570922005-01-25 22:26:29 +00001111 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 int retval = FALSE;
1113
1114 if (skip)
1115 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else
1119 {
1120 *error = FALSE;
1121 if (!skip)
1122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001123 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 }
1127 if (skip)
1128 --emsg_skip;
1129
1130 return retval;
1131}
1132
1133/*
1134 * Top level evaluation function, returning a string. If "skip" is TRUE,
1135 * only parsing to "nextcmd" is done, without reporting errors. Return
1136 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1137 */
1138 char_u *
1139eval_to_string_skip(arg, nextcmd, skip)
1140 char_u *arg;
1141 char_u **nextcmd;
1142 int skip; /* only parse, don't execute */
1143{
Bram Moolenaar33570922005-01-25 22:26:29 +00001144 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 char_u *retval;
1146
1147 if (skip)
1148 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 retval = NULL;
1151 else
1152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001153 retval = vim_strsave(get_tv_string(&tv));
1154 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 if (skip)
1157 --emsg_skip;
1158
1159 return retval;
1160}
1161
1162/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001163 * Skip over an expression at "*pp".
1164 * Return FAIL for an error, OK otherwise.
1165 */
1166 int
1167skip_expr(pp)
1168 char_u **pp;
1169{
Bram Moolenaar33570922005-01-25 22:26:29 +00001170 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001171
1172 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001173 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001174}
1175
1176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 * Top level evaluation function, returning a string.
1178 * Return pointer to allocated memory, or NULL for failure.
1179 */
1180 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001181eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *arg;
1183 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001184 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
Bram Moolenaar33570922005-01-25 22:26:29 +00001186 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001188 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001190 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 retval = NULL;
1192 else
1193 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001194 if (dolist && tv.v_type == VAR_LIST)
1195 {
1196 ga_init2(&ga, (int)sizeof(char), 80);
1197 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1198 ga_append(&ga, NUL);
1199 retval = (char_u *)ga.ga_data;
1200 }
1201 else
1202 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 return retval;
1207}
1208
1209/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001210 * Call eval_to_string() without using current local variables and using
1211 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 */
1213 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001214eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 char_u *arg;
1216 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001217 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218{
1219 char_u *retval;
1220 void *save_funccalp;
1221
1222 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001223 if (use_sandbox)
1224 ++sandbox;
1225 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001226 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001227 if (use_sandbox)
1228 --sandbox;
1229 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 restore_funccal(save_funccalp);
1231 return retval;
1232}
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234/*
1235 * Top level evaluation function, returning a number.
1236 * Evaluates "expr" silently.
1237 * Returns -1 for an error.
1238 */
1239 int
1240eval_to_number(expr)
1241 char_u *expr;
1242{
Bram Moolenaar33570922005-01-25 22:26:29 +00001243 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001245 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 ++emsg_off;
1248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 retval = -1;
1251 else
1252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001253 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 --emsg_off;
1257
1258 return retval;
1259}
1260
Bram Moolenaara40058a2005-07-11 22:42:07 +00001261/*
1262 * Prepare v: variable "idx" to be used.
1263 * Save the current typeval in "save_tv".
1264 * When not used yet add the variable to the v: hashtable.
1265 */
1266 static void
1267prepare_vimvar(idx, save_tv)
1268 int idx;
1269 typval_T *save_tv;
1270{
1271 *save_tv = vimvars[idx].vv_tv;
1272 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1273 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1274}
1275
1276/*
1277 * Restore v: variable "idx" to typeval "save_tv".
1278 * When no longer defined, remove the variable from the v: hashtable.
1279 */
1280 static void
1281restore_vimvar(idx, save_tv)
1282 int idx;
1283 typval_T *save_tv;
1284{
1285 hashitem_T *hi;
1286
1287 clear_tv(&vimvars[idx].vv_tv);
1288 vimvars[idx].vv_tv = *save_tv;
1289 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1290 {
1291 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1292 if (HASHITEM_EMPTY(hi))
1293 EMSG2(_(e_intern2), "restore_vimvar()");
1294 else
1295 hash_remove(&vimvarht, hi);
1296 }
1297}
1298
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001299#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001300/*
1301 * Evaluate an expression to a list with suggestions.
1302 * For the "expr:" part of 'spellsuggest'.
1303 */
1304 list_T *
1305eval_spell_expr(badword, expr)
1306 char_u *badword;
1307 char_u *expr;
1308{
1309 typval_T save_val;
1310 typval_T rettv;
1311 list_T *list = NULL;
1312 char_u *p = skipwhite(expr);
1313
1314 /* Set "v:val" to the bad word. */
1315 prepare_vimvar(VV_VAL, &save_val);
1316 vimvars[VV_VAL].vv_type = VAR_STRING;
1317 vimvars[VV_VAL].vv_str = badword;
1318 if (p_verbose == 0)
1319 ++emsg_off;
1320
1321 if (eval1(&p, &rettv, TRUE) == OK)
1322 {
1323 if (rettv.v_type != VAR_LIST)
1324 clear_tv(&rettv);
1325 else
1326 list = rettv.vval.v_list;
1327 }
1328
1329 if (p_verbose == 0)
1330 --emsg_off;
1331 vimvars[VV_VAL].vv_str = NULL;
1332 restore_vimvar(VV_VAL, &save_val);
1333
1334 return list;
1335}
1336
1337/*
1338 * "list" is supposed to contain two items: a word and a number. Return the
1339 * word in "pp" and the number as the return value.
1340 * Return -1 if anything isn't right.
1341 * Used to get the good word and score from the eval_spell_expr() result.
1342 */
1343 int
1344get_spellword(list, pp)
1345 list_T *list;
1346 char_u **pp;
1347{
1348 listitem_T *li;
1349
1350 li = list->lv_first;
1351 if (li == NULL)
1352 return -1;
1353 *pp = get_tv_string(&li->li_tv);
1354
1355 li = li->li_next;
1356 if (li == NULL)
1357 return -1;
1358 return get_tv_number(&li->li_tv);
1359}
1360#endif
1361
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001362/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001363 * Top level evaluation function.
1364 * Returns an allocated typval_T with the result.
1365 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001366 */
1367 typval_T *
1368eval_expr(arg, nextcmd)
1369 char_u *arg;
1370 char_u **nextcmd;
1371{
1372 typval_T *tv;
1373
1374 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001375 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001376 {
1377 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001378 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001379 }
1380
1381 return tv;
1382}
1383
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1386/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001387 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001389 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001391 static int
1392call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 char_u *func;
1394 int argc;
1395 char_u **argv;
1396 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 long n;
1401 int len;
1402 int i;
1403 int doesrange;
1404 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001405 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
1411 for (i = 0; i < argc; i++)
1412 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001413 /* Pass a NULL or empty argument as an empty string */
1414 if (argv[i] == NULL || *argv[i] == NUL)
1415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001416 argvars[i].v_type = VAR_STRING;
1417 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001418 continue;
1419 }
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 /* Recognize a number argument, the others must be strings. */
1422 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1423 if (len != 0 && len == (int)STRLEN(argv[i]))
1424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001425 argvars[i].v_type = VAR_NUMBER;
1426 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428 else
1429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001430 argvars[i].v_type = VAR_STRING;
1431 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 }
1434
1435 if (safe)
1436 {
1437 save_funccalp = save_funccal();
1438 ++sandbox;
1439 }
1440
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001441 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1442 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001444 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (safe)
1446 {
1447 --sandbox;
1448 restore_funccal(save_funccalp);
1449 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001450 vim_free(argvars);
1451
1452 if (ret == FAIL)
1453 clear_tv(rettv);
1454
1455 return ret;
1456}
1457
1458/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001459 * Call vimL function "func" and return the result as a string.
1460 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001461 * Uses argv[argc] for the function arguments.
1462 */
1463 void *
1464call_func_retstr(func, argc, argv, safe)
1465 char_u *func;
1466 int argc;
1467 char_u **argv;
1468 int safe; /* use the sandbox */
1469{
1470 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001471 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001472
1473 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1474 return NULL;
1475
1476 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return retval;
1479}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001480
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001481#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001482/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001483 * Call vimL function "func" and return the result as a number.
1484 * Returns -1 when calling the function fails.
1485 * Uses argv[argc] for the function arguments.
1486 */
1487 long
1488call_func_retnr(func, argc, argv, safe)
1489 char_u *func;
1490 int argc;
1491 char_u **argv;
1492 int safe; /* use the sandbox */
1493{
1494 typval_T rettv;
1495 long retval;
1496
1497 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1498 return -1;
1499
1500 retval = get_tv_number_chk(&rettv, NULL);
1501 clear_tv(&rettv);
1502 return retval;
1503}
1504#endif
1505
1506/*
1507 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001508 * Uses argv[argc] for the function arguments.
1509 */
1510 void *
1511call_func_retlist(func, argc, argv, safe)
1512 char_u *func;
1513 int argc;
1514 char_u **argv;
1515 int safe; /* use the sandbox */
1516{
1517 typval_T rettv;
1518
1519 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1520 return NULL;
1521
1522 if (rettv.v_type != VAR_LIST)
1523 {
1524 clear_tv(&rettv);
1525 return NULL;
1526 }
1527
1528 return rettv.vval.v_list;
1529}
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531#endif
1532
1533/*
1534 * Save the current function call pointer, and set it to NULL.
1535 * Used when executing autocommands and for ":source".
1536 */
1537 void *
1538save_funccal()
1539{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001540 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 current_funccal = NULL;
1543 return (void *)fc;
1544}
1545
1546 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001547restore_funccal(vfc)
1548 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001550 funccall_T *fc = (funccall_T *)vfc;
1551
1552 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553}
1554
Bram Moolenaar05159a02005-02-26 23:04:13 +00001555#if defined(FEAT_PROFILE) || defined(PROTO)
1556/*
1557 * Prepare profiling for entering a child or something else that is not
1558 * counted for the script/function itself.
1559 * Should always be called in pair with prof_child_exit().
1560 */
1561 void
1562prof_child_enter(tm)
1563 proftime_T *tm; /* place to store waittime */
1564{
1565 funccall_T *fc = current_funccal;
1566
1567 if (fc != NULL && fc->func->uf_profiling)
1568 profile_start(&fc->prof_child);
1569 script_prof_save(tm);
1570}
1571
1572/*
1573 * Take care of time spent in a child.
1574 * Should always be called after prof_child_enter().
1575 */
1576 void
1577prof_child_exit(tm)
1578 proftime_T *tm; /* where waittime was stored */
1579{
1580 funccall_T *fc = current_funccal;
1581
1582 if (fc != NULL && fc->func->uf_profiling)
1583 {
1584 profile_end(&fc->prof_child);
1585 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1586 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1587 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1588 }
1589 script_prof_restore(tm);
1590}
1591#endif
1592
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594#ifdef FEAT_FOLDING
1595/*
1596 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1597 * it in "*cp". Doesn't give error messages.
1598 */
1599 int
1600eval_foldexpr(arg, cp)
1601 char_u *arg;
1602 int *cp;
1603{
Bram Moolenaar33570922005-01-25 22:26:29 +00001604 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 int retval;
1606 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001607 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1608 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001611 if (use_sandbox)
1612 ++sandbox;
1613 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001615 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 retval = 0;
1617 else
1618 {
1619 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 if (tv.v_type == VAR_NUMBER)
1621 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001622 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 retval = 0;
1624 else
1625 {
1626 /* If the result is a string, check if there is a non-digit before
1627 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (!VIM_ISDIGIT(*s) && *s != '-')
1630 *cp = *s++;
1631 retval = atol((char *)s);
1632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 }
1635 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001636 if (use_sandbox)
1637 --sandbox;
1638 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 return retval;
1641}
1642#endif
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001645 * ":let" list all variable values
1646 * ":let var1 var2" list variable values
1647 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 * ":let var += expr" assignment command.
1649 * ":let var -= expr" assignment command.
1650 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 */
1653 void
1654ex_let(eap)
1655 exarg_T *eap;
1656{
1657 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001659 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001661 int var_count = 0;
1662 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001664 char_u *argend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaardb552d602006-03-23 22:59:57 +00001666 argend = skip_var_list(arg, &var_count, &semicolon);
1667 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001669 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1670 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001671 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001674 /*
1675 * ":let" without "=": list variables
1676 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 if (*arg == '[')
1678 EMSG(_(e_invarg));
1679 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001680 /* ":let var1 var2" */
1681 arg = list_arg_vars(eap, arg);
1682 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001685 list_glob_vars();
1686 list_buf_vars();
1687 list_win_vars();
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001688 list_script_vars();
1689 list_func_vars();
Bram Moolenaara7043832005-01-21 11:56:39 +00001690 list_vim_vars();
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 eap->nextcmd = check_nextcmd(arg);
1693 }
1694 else
1695 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001696 op[0] = '=';
1697 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001698 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 {
1700 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1701 op[0] = expr[-1]; /* +=, -= or .= */
1702 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 if (eap->skip)
1706 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (eap->skip)
1709 {
1710 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 --emsg_skip;
1713 }
1714 else if (i != FAIL)
1715 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721}
1722
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723/*
1724 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1725 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 * When "nextchars" is not NULL it points to a string with characters that
1727 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1728 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 * Returns OK or FAIL;
1730 */
1731 static int
1732ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1733 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001734 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735 int copy; /* copy values from "tv", don't move */
1736 int semicolon; /* from skip_var_list() */
1737 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001738 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739{
1740 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001741 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001743 listitem_T *item;
1744 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745
1746 if (*arg != '[')
1747 {
1748 /*
1749 * ":let var = expr" or ":for var in list"
1750 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752 return FAIL;
1753 return OK;
1754 }
1755
1756 /*
1757 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1758 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001759 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 {
1761 EMSG(_(e_listreq));
1762 return FAIL;
1763 }
1764
1765 i = list_len(l);
1766 if (semicolon == 0 && var_count < i)
1767 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001768 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 return FAIL;
1770 }
1771 if (var_count - semicolon > i)
1772 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001773 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 return FAIL;
1775 }
1776
1777 item = l->lv_first;
1778 while (*arg != ']')
1779 {
1780 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001781 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 item = item->li_next;
1783 if (arg == NULL)
1784 return FAIL;
1785
1786 arg = skipwhite(arg);
1787 if (*arg == ';')
1788 {
1789 /* Put the rest of the list (may be empty) in the var after ';'.
1790 * Create a new list for this. */
1791 l = list_alloc();
1792 if (l == NULL)
1793 return FAIL;
1794 while (item != NULL)
1795 {
1796 list_append_tv(l, &item->li_tv);
1797 item = item->li_next;
1798 }
1799
1800 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001801 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 ltv.vval.v_list = l;
1803 l->lv_refcount = 1;
1804
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001805 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1806 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807 clear_tv(&ltv);
1808 if (arg == NULL)
1809 return FAIL;
1810 break;
1811 }
1812 else if (*arg != ',' && *arg != ']')
1813 {
1814 EMSG2(_(e_intern2), "ex_let_vars()");
1815 return FAIL;
1816 }
1817 }
1818
1819 return OK;
1820}
1821
1822/*
1823 * Skip over assignable variable "var" or list of variables "[var, var]".
1824 * Used for ":let varvar = expr" and ":for varvar in expr".
1825 * For "[var, var]" increment "*var_count" for each variable.
1826 * for "[var, var; var]" set "semicolon".
1827 * Return NULL for an error.
1828 */
1829 static char_u *
1830skip_var_list(arg, var_count, semicolon)
1831 char_u *arg;
1832 int *var_count;
1833 int *semicolon;
1834{
1835 char_u *p, *s;
1836
1837 if (*arg == '[')
1838 {
1839 /* "[var, var]": find the matching ']'. */
1840 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001841 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 {
1843 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1844 s = skip_var_one(p);
1845 if (s == p)
1846 {
1847 EMSG2(_(e_invarg2), p);
1848 return NULL;
1849 }
1850 ++*var_count;
1851
1852 p = skipwhite(s);
1853 if (*p == ']')
1854 break;
1855 else if (*p == ';')
1856 {
1857 if (*semicolon == 1)
1858 {
1859 EMSG(_("Double ; in list of variables"));
1860 return NULL;
1861 }
1862 *semicolon = 1;
1863 }
1864 else if (*p != ',')
1865 {
1866 EMSG2(_(e_invarg2), p);
1867 return NULL;
1868 }
1869 }
1870 return p + 1;
1871 }
1872 else
1873 return skip_var_one(arg);
1874}
1875
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001876/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001877 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1878 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001879 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 static char_u *
1881skip_var_one(arg)
1882 char_u *arg;
1883{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001884 if (*arg == '@' && arg[1] != NUL)
1885 return arg + 2;
1886 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1887 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888}
1889
Bram Moolenaara7043832005-01-21 11:56:39 +00001890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 * List variables for hashtab "ht" with prefix "prefix".
1892 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001893 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001895list_hashtable_vars(ht, prefix, empty)
1896 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001897 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001899{
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 hashitem_T *hi;
1901 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001902 int todo;
1903
1904 todo = ht->ht_used;
1905 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1906 {
1907 if (!HASHITEM_EMPTY(hi))
1908 {
1909 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 di = HI2DI(hi);
1911 if (empty || di->di_tv.v_type != VAR_STRING
1912 || di->di_tv.vval.v_string != NULL)
1913 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001914 }
1915 }
1916}
1917
1918/*
1919 * List global variables.
1920 */
1921 static void
1922list_glob_vars()
1923{
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001925}
1926
1927/*
1928 * List buffer variables.
1929 */
1930 static void
1931list_buf_vars()
1932{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 char_u numbuf[NUMBUFLEN];
1934
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936
1937 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1938 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001939}
1940
1941/*
1942 * List window variables.
1943 */
1944 static void
1945list_win_vars()
1946{
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001948}
1949
1950/*
1951 * List Vim variables.
1952 */
1953 static void
1954list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955{
Bram Moolenaar33570922005-01-25 22:26:29 +00001956 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957}
1958
1959/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001960 * List script-local variables, if there is a script.
1961 */
1962 static void
1963list_script_vars()
1964{
1965 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
1966 list_hashtable_vars(&SCRIPT_VARS(current_SID), (char_u *)"s:", FALSE);
1967}
1968
1969/*
1970 * List function variables, if there is a function.
1971 */
1972 static void
1973list_func_vars()
1974{
1975 if (current_funccal != NULL)
1976 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
1977 (char_u *)"l:", FALSE);
1978}
1979
1980/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 * List variables in "arg".
1982 */
1983 static char_u *
1984list_arg_vars(eap, arg)
1985 exarg_T *eap;
1986 char_u *arg;
1987{
1988 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001989 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001991 char_u *name_start;
1992 char_u *arg_subsc;
1993 char_u *tofree;
1994 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995
1996 while (!ends_excmd(*arg) && !got_int)
1997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002000 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2002 {
2003 emsg_severe = TRUE;
2004 EMSG(_(e_trailing));
2005 break;
2006 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002009 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 /* get_name_len() takes care of expanding curly braces */
2011 name_start = name = arg;
2012 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2013 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002015 /* This is mainly to keep test 49 working: when expanding
2016 * curly braces fails overrule the exception error message. */
2017 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002019 emsg_severe = TRUE;
2020 EMSG2(_(e_invarg2), arg);
2021 break;
2022 }
2023 error = TRUE;
2024 }
2025 else
2026 {
2027 if (tofree != NULL)
2028 name = tofree;
2029 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 else
2032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002033 /* handle d.key, l[idx], f(expr) */
2034 arg_subsc = arg;
2035 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002036 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002038 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002039 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002040 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002042 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 case 'g': list_glob_vars(); break;
2044 case 'b': list_buf_vars(); break;
2045 case 'w': list_win_vars(); break;
2046 case 'v': list_vim_vars(); break;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002047 case 's': list_script_vars(); break;
2048 case 'l': list_func_vars(); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 default:
2050 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002051 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002052 }
2053 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054 {
2055 char_u numbuf[NUMBUFLEN];
2056 char_u *tf;
2057 int c;
2058 char_u *s;
2059
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002060 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002061 c = *arg;
2062 *arg = NUL;
2063 list_one_var_a((char_u *)"",
2064 arg == arg_subsc ? name : name_start,
2065 tv.v_type, s == NULL ? (char_u *)"" : s);
2066 *arg = c;
2067 vim_free(tf);
2068 }
2069 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 }
2072 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073
2074 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076
2077 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 }
2079
2080 return arg;
2081}
2082
2083/*
2084 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2085 * Returns a pointer to the char just after the var name.
2086 * Returns NULL if there is an error.
2087 */
2088 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095{
2096 int c1;
2097 char_u *name;
2098 char_u *p;
2099 char_u *arg_end = NULL;
2100 int len;
2101 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103
2104 /*
2105 * ":let $VAR = expr": Set environment variable.
2106 */
2107 if (*arg == '$')
2108 {
2109 /* Find the end of the name. */
2110 ++arg;
2111 name = arg;
2112 len = get_env_len(&arg);
2113 if (len == 0)
2114 EMSG2(_(e_invarg2), name - 1);
2115 else
2116 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002117 if (op != NULL && (*op == '+' || *op == '-'))
2118 EMSG2(_(e_letwrong), op);
2119 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002120 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 EMSG(_(e_letunexp));
2122 else
2123 {
2124 c1 = name[len];
2125 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002126 p = get_tv_string_chk(tv);
2127 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002128 {
2129 int mustfree = FALSE;
2130 char_u *s = vim_getenv(name, &mustfree);
2131
2132 if (s != NULL)
2133 {
2134 p = tofree = concat_str(s, p);
2135 if (mustfree)
2136 vim_free(s);
2137 }
2138 }
2139 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002141 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002142 if (STRICMP(name, "HOME") == 0)
2143 init_homedir();
2144 else if (didset_vim && STRICMP(name, "VIM") == 0)
2145 didset_vim = FALSE;
2146 else if (didset_vimruntime
2147 && STRICMP(name, "VIMRUNTIME") == 0)
2148 didset_vimruntime = FALSE;
2149 arg_end = arg;
2150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002151 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153 }
2154 }
2155 }
2156
2157 /*
2158 * ":let &option = expr": Set option value.
2159 * ":let &l:option = expr": Set local option value.
2160 * ":let &g:option = expr": Set global option value.
2161 */
2162 else if (*arg == '&')
2163 {
2164 /* Find the end of the name. */
2165 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166 if (p == NULL || (endchars != NULL
2167 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 EMSG(_(e_letunexp));
2169 else
2170 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 long n;
2172 int opt_type;
2173 long numval;
2174 char_u *stringval = NULL;
2175 char_u *s;
2176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 c1 = *p;
2178 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002179
2180 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002181 s = get_tv_string_chk(tv); /* != NULL if number or string */
2182 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002183 {
2184 opt_type = get_option_value(arg, &numval,
2185 &stringval, opt_flags);
2186 if ((opt_type == 1 && *op == '.')
2187 || (opt_type == 0 && *op != '.'))
2188 EMSG2(_(e_letwrong), op);
2189 else
2190 {
2191 if (opt_type == 1) /* number */
2192 {
2193 if (*op == '+')
2194 n = numval + n;
2195 else
2196 n = numval - n;
2197 }
2198 else if (opt_type == 0 && stringval != NULL) /* string */
2199 {
2200 s = concat_str(stringval, s);
2201 vim_free(stringval);
2202 stringval = s;
2203 }
2204 }
2205 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002206 if (s != NULL)
2207 {
2208 set_option_value(arg, n, s, opt_flags);
2209 arg_end = p;
2210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
2214 }
2215
2216 /*
2217 * ":let @r = expr": Set register contents.
2218 */
2219 else if (*arg == '@')
2220 {
2221 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002222 if (op != NULL && (*op == '+' || *op == '-'))
2223 EMSG2(_(e_letwrong), op);
2224 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002225 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 EMSG(_(e_letunexp));
2227 else
2228 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002229 char_u *tofree = NULL;
2230 char_u *s;
2231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002232 p = get_tv_string_chk(tv);
2233 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002234 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002235 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002236 if (s != NULL)
2237 {
2238 p = tofree = concat_str(s, p);
2239 vim_free(s);
2240 }
2241 }
2242 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002244 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002245 arg_end = arg + 1;
2246 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002247 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 }
2249 }
2250
2251 /*
2252 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002257 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002259 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2263 EMSG(_(e_letunexp));
2264 else
2265 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002266 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 arg_end = p;
2268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002270 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
2272
2273 else
2274 EMSG2(_(e_invarg2), arg);
2275
2276 return arg_end;
2277}
2278
2279/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002280 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2281 */
2282 static int
2283check_changedtick(arg)
2284 char_u *arg;
2285{
2286 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2287 {
2288 EMSG2(_(e_readonlyvar), arg);
2289 return TRUE;
2290 }
2291 return FALSE;
2292}
2293
2294/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 * Get an lval: variable, Dict item or List item that can be assigned a value
2296 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2297 * "name.key", "name.key[expr]" etc.
2298 * Indexing only works if "name" is an existing List or Dictionary.
2299 * "name" points to the start of the name.
2300 * If "rettv" is not NULL it points to the value to be assigned.
2301 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2302 * wrong; must end in space or cmd separator.
2303 *
2304 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 */
2308 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002309get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *rettv;
2312 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 int unlet;
2314 int skip;
2315 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002316 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002319 char_u *expr_start, *expr_end;
2320 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002321 dictitem_T *v;
2322 typval_T var1;
2323 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002324 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002325 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002326 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002328 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002331 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332
2333 if (skip)
2334 {
2335 /* When skipping just find the end of the name. */
2336 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002337 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002338 }
2339
2340 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002341 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (expr_start != NULL)
2343 {
2344 /* Don't expand the name when we already know there is an error. */
2345 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2346 && *p != '[' && *p != '.')
2347 {
2348 EMSG(_(e_trailing));
2349 return NULL;
2350 }
2351
2352 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2353 if (lp->ll_exp_name == NULL)
2354 {
2355 /* Report an invalid expression in braces, unless the
2356 * expression evaluation has been cancelled due to an
2357 * aborting error, an interrupt, or an exception. */
2358 if (!aborting() && !quiet)
2359 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002360 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002361 EMSG2(_(e_invarg2), name);
2362 return NULL;
2363 }
2364 }
2365 lp->ll_name = lp->ll_exp_name;
2366 }
2367 else
2368 lp->ll_name = name;
2369
2370 /* Without [idx] or .key we are done. */
2371 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2372 return p;
2373
2374 cc = *p;
2375 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002376 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (v == NULL && !quiet)
2378 EMSG2(_(e_undefvar), lp->ll_name);
2379 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 if (v == NULL)
2381 return NULL;
2382
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 /*
2384 * Loop until no more [idx] or .key is following.
2385 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002386 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2390 && !(lp->ll_tv->v_type == VAR_DICT
2391 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 if (!quiet)
2394 EMSG(_("E689: Can only index a List or Dictionary"));
2395 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 if (!quiet)
2400 EMSG(_("E708: [:] must come last"));
2401 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002403
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 len = -1;
2405 if (*p == '.')
2406 {
2407 key = p + 1;
2408 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2409 ;
2410 if (len == 0)
2411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (!quiet)
2413 EMSG(_(e_emptykey));
2414 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 }
2416 p = key + len;
2417 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002418 else
2419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002420 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002421 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 if (*p == ':')
2423 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002424 else
2425 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002426 empty1 = FALSE;
2427 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 if (get_tv_string_chk(&var1) == NULL)
2430 {
2431 /* not a number or string */
2432 clear_tv(&var1);
2433 return NULL;
2434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002435 }
2436
2437 /* Optionally get the second index [ :expr]. */
2438 if (*p == ':')
2439 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002440 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002441 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002444 if (!empty1)
2445 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002447 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 if (rettv != NULL && (rettv->v_type != VAR_LIST
2449 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002450 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 if (!quiet)
2452 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 if (!empty1)
2454 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 }
2457 p = skipwhite(p + 1);
2458 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002460 else
2461 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002465 if (!empty1)
2466 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 if (get_tv_string_chk(&var2) == NULL)
2470 {
2471 /* not a number or string */
2472 if (!empty1)
2473 clear_tv(&var1);
2474 clear_tv(&var2);
2475 return NULL;
2476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002479 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002482
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
2486 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 if (!empty1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 }
2493
2494 /* Skip to past ']'. */
2495 ++p;
2496 }
2497
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 {
2500 if (len == -1)
2501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 if (*key == NUL)
2505 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 if (!quiet)
2507 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 }
2511 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 lp->ll_list = NULL;
2513 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002514 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002517 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002522 if (len == -1)
2523 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 }
2526 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 if (len == -1)
2531 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 p = NULL;
2534 break;
2535 }
2536 if (len == -1)
2537 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 }
2540 else
2541 {
2542 /*
2543 * Get the number and item for the only or first index of the List.
2544 */
2545 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 else
2548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002549 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 clear_tv(&var1);
2551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002552 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 lp->ll_list = lp->ll_tv->vval.v_list;
2554 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2555 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (!quiet)
2558 EMSGN(_(e_listidx), lp->ll_n1);
2559 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 }
2563
2564 /*
2565 * May need to find the item or absolute index for the second
2566 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * When no index given: "lp->ll_empty2" is TRUE.
2568 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002572 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002577 if (ni == NULL)
2578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 if (!quiet)
2580 EMSGN(_(e_listidx), lp->ll_n2);
2581 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584 }
2585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2587 if (lp->ll_n1 < 0)
2588 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2589 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002590 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (!quiet)
2592 EMSGN(_(e_listidx), lp->ll_n2);
2593 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002594 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002595 }
2596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 }
2600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 return p;
2602}
2603
2604/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002605 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 */
2607 static void
2608clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002609 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610{
2611 vim_free(lp->ll_exp_name);
2612 vim_free(lp->ll_newkey);
2613}
2614
2615/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002616 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002618 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 */
2620 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002626 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627{
2628 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 listitem_T *ri;
2630 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631
2632 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 cc = *endp;
2637 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002638 if (op != NULL && *op != '=')
2639 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002640 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002642 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002643 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2644 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002645 {
2646 if (tv_op(&tv, rettv, op) == OK)
2647 set_var(lp->ll_name, &tv, FALSE);
2648 clear_tv(&tv);
2649 }
2650 }
2651 else
2652 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002656 else if (tv_check_lock(lp->ll_newkey == NULL
2657 ? lp->ll_tv->v_lock
2658 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2659 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 else if (lp->ll_range)
2661 {
2662 /*
2663 * Assign the List values to the list items.
2664 */
2665 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002667 if (op != NULL && *op != '=')
2668 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2669 else
2670 {
2671 clear_tv(&lp->ll_li->li_tv);
2672 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 ri = ri->li_next;
2675 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2676 break;
2677 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002680 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 ri = NULL;
2683 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_li = lp->ll_li->li_next;
2687 ++lp->ll_n1;
2688 }
2689 if (ri != NULL)
2690 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002691 else if (lp->ll_empty2
2692 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 : lp->ll_n1 != lp->ll_n2)
2694 EMSG(_("E711: List value has not enough items"));
2695 }
2696 else
2697 {
2698 /*
2699 * Assign to a List or Dictionary item.
2700 */
2701 if (lp->ll_newkey != NULL)
2702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 if (op != NULL && *op != '=')
2704 {
2705 EMSG2(_(e_letwrong), op);
2706 return;
2707 }
2708
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002710 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (di == NULL)
2712 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002713 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2714 {
2715 vim_free(di);
2716 return;
2717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 else if (op != NULL && *op != '=')
2721 {
2722 tv_op(lp->ll_tv, rettv, op);
2723 return;
2724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002725 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 /*
2729 * Assign the value to the variable or list item.
2730 */
2731 if (copy)
2732 copy_tv(rettv, lp->ll_tv);
2733 else
2734 {
2735 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002736 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002738 }
2739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002740}
2741
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002743 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2744 * Returns OK or FAIL.
2745 */
2746 static int
2747tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002748 typval_T *tv1;
2749 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002750 char_u *op;
2751{
2752 long n;
2753 char_u numbuf[NUMBUFLEN];
2754 char_u *s;
2755
2756 /* Can't do anything with a Funcref or a Dict on the right. */
2757 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2758 {
2759 switch (tv1->v_type)
2760 {
2761 case VAR_DICT:
2762 case VAR_FUNC:
2763 break;
2764
2765 case VAR_LIST:
2766 if (*op != '+' || tv2->v_type != VAR_LIST)
2767 break;
2768 /* List += List */
2769 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2770 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2771 return OK;
2772
2773 case VAR_NUMBER:
2774 case VAR_STRING:
2775 if (tv2->v_type == VAR_LIST)
2776 break;
2777 if (*op == '+' || *op == '-')
2778 {
2779 /* nr += nr or nr -= nr*/
2780 n = get_tv_number(tv1);
2781 if (*op == '+')
2782 n += get_tv_number(tv2);
2783 else
2784 n -= get_tv_number(tv2);
2785 clear_tv(tv1);
2786 tv1->v_type = VAR_NUMBER;
2787 tv1->vval.v_number = n;
2788 }
2789 else
2790 {
2791 /* str .= str */
2792 s = get_tv_string(tv1);
2793 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2794 clear_tv(tv1);
2795 tv1->v_type = VAR_STRING;
2796 tv1->vval.v_string = s;
2797 }
2798 return OK;
2799 }
2800 }
2801
2802 EMSG2(_(e_letwrong), op);
2803 return FAIL;
2804}
2805
2806/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002807 * Add a watcher to a list.
2808 */
2809 static void
2810list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002811 list_T *l;
2812 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002813{
2814 lw->lw_next = l->lv_watch;
2815 l->lv_watch = lw;
2816}
2817
2818/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002819 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002820 * No warning when it isn't found...
2821 */
2822 static void
2823list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 list_T *l;
2825 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002826{
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002828
2829 lwp = &l->lv_watch;
2830 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2831 {
2832 if (lw == lwrem)
2833 {
2834 *lwp = lw->lw_next;
2835 break;
2836 }
2837 lwp = &lw->lw_next;
2838 }
2839}
2840
2841/*
2842 * Just before removing an item from a list: advance watchers to the next
2843 * item.
2844 */
2845 static void
2846list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002847 list_T *l;
2848 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002849{
Bram Moolenaar33570922005-01-25 22:26:29 +00002850 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002851
2852 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2853 if (lw->lw_item == item)
2854 lw->lw_item = item->li_next;
2855}
2856
2857/*
2858 * Evaluate the expression used in a ":for var in expr" command.
2859 * "arg" points to "var".
2860 * Set "*errp" to TRUE for an error, FALSE otherwise;
2861 * Return a pointer that holds the info. Null when there is an error.
2862 */
2863 void *
2864eval_for_line(arg, errp, nextcmdp, skip)
2865 char_u *arg;
2866 int *errp;
2867 char_u **nextcmdp;
2868 int skip;
2869{
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002871 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 typval_T tv;
2873 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874
2875 *errp = TRUE; /* default: there is an error */
2876
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002878 if (fi == NULL)
2879 return NULL;
2880
2881 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2882 if (expr == NULL)
2883 return fi;
2884
2885 expr = skipwhite(expr);
2886 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2887 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002888 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 return fi;
2890 }
2891
2892 if (skip)
2893 ++emsg_skip;
2894 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2895 {
2896 *errp = FALSE;
2897 if (!skip)
2898 {
2899 l = tv.vval.v_list;
2900 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002901 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002902 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002903 clear_tv(&tv);
2904 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 else
2906 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002907 /* No need to increment the refcount, it's already set for the
2908 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002909 fi->fi_list = l;
2910 list_add_watch(l, &fi->fi_lw);
2911 fi->fi_lw.lw_item = l->lv_first;
2912 }
2913 }
2914 }
2915 if (skip)
2916 --emsg_skip;
2917
2918 return fi;
2919}
2920
2921/*
2922 * Use the first item in a ":for" list. Advance to the next.
2923 * Assign the values to the variable (list). "arg" points to the first one.
2924 * Return TRUE when a valid item was found, FALSE when at end of list or
2925 * something wrong.
2926 */
2927 int
2928next_for_item(fi_void, arg)
2929 void *fi_void;
2930 char_u *arg;
2931{
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002933 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002934 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935
2936 item = fi->fi_lw.lw_item;
2937 if (item == NULL)
2938 result = FALSE;
2939 else
2940 {
2941 fi->fi_lw.lw_item = item->li_next;
2942 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2943 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2944 }
2945 return result;
2946}
2947
2948/*
2949 * Free the structure used to store info used by ":for".
2950 */
2951 void
2952free_for_info(fi_void)
2953 void *fi_void;
2954{
Bram Moolenaar33570922005-01-25 22:26:29 +00002955 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002956
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002957 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002958 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002959 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002960 list_unref(fi->fi_list);
2961 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002962 vim_free(fi);
2963}
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2966
2967 void
2968set_context_for_expression(xp, arg, cmdidx)
2969 expand_T *xp;
2970 char_u *arg;
2971 cmdidx_T cmdidx;
2972{
2973 int got_eq = FALSE;
2974 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002975 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977 if (cmdidx == CMD_let)
2978 {
2979 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002980 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002981 {
2982 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002983 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002984 {
2985 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002987 if (vim_iswhite(*p))
2988 break;
2989 }
2990 return;
2991 }
2992 }
2993 else
2994 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2995 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 while ((xp->xp_pattern = vim_strpbrk(arg,
2997 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2998 {
2999 c = *xp->xp_pattern;
3000 if (c == '&')
3001 {
3002 c = xp->xp_pattern[1];
3003 if (c == '&')
3004 {
3005 ++xp->xp_pattern;
3006 xp->xp_context = cmdidx != CMD_let || got_eq
3007 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3008 }
3009 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003012 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3013 xp->xp_pattern += 2;
3014
3015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017 else if (c == '$')
3018 {
3019 /* environment variable */
3020 xp->xp_context = EXPAND_ENV_VARS;
3021 }
3022 else if (c == '=')
3023 {
3024 got_eq = TRUE;
3025 xp->xp_context = EXPAND_EXPRESSION;
3026 }
3027 else if (c == '<'
3028 && xp->xp_context == EXPAND_FUNCTIONS
3029 && vim_strchr(xp->xp_pattern, '(') == NULL)
3030 {
3031 /* Function name can start with "<SNR>" */
3032 break;
3033 }
3034 else if (cmdidx != CMD_let || got_eq)
3035 {
3036 if (c == '"') /* string */
3037 {
3038 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3039 if (c == '\\' && xp->xp_pattern[1] != NUL)
3040 ++xp->xp_pattern;
3041 xp->xp_context = EXPAND_NOTHING;
3042 }
3043 else if (c == '\'') /* literal string */
3044 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003045 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3047 /* skip */ ;
3048 xp->xp_context = EXPAND_NOTHING;
3049 }
3050 else if (c == '|')
3051 {
3052 if (xp->xp_pattern[1] == '|')
3053 {
3054 ++xp->xp_pattern;
3055 xp->xp_context = EXPAND_EXPRESSION;
3056 }
3057 else
3058 xp->xp_context = EXPAND_COMMANDS;
3059 }
3060 else
3061 xp->xp_context = EXPAND_EXPRESSION;
3062 }
3063 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064 /* Doesn't look like something valid, expand as an expression
3065 * anyway. */
3066 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 arg = xp->xp_pattern;
3068 if (*arg != NUL)
3069 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3070 /* skip */ ;
3071 }
3072 xp->xp_pattern = arg;
3073}
3074
3075#endif /* FEAT_CMDL_COMPL */
3076
3077/*
3078 * ":1,25call func(arg1, arg2)" function call.
3079 */
3080 void
3081ex_call(eap)
3082 exarg_T *eap;
3083{
3084 char_u *arg = eap->arg;
3085 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 linenr_T lnum;
3091 int doesrange;
3092 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3096 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 if (tofree == NULL)
3098 return;
3099
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003100 /* Increase refcount on dictionary, it could get deleted when evaluating
3101 * the arguments. */
3102 if (fudi.fd_dict != NULL)
3103 ++fudi.fd_dict->dv_refcount;
3104
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3106 len = STRLEN(tofree);
3107 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
Bram Moolenaar532c7802005-01-27 14:44:31 +00003109 /* Skip white space to allow ":call func ()". Not good, but required for
3110 * backward compatibility. */
3111 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003112 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 if (*startarg != '(')
3115 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003116 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 goto end;
3118 }
3119
3120 /*
3121 * When skipping, evaluate the function once, to find the end of the
3122 * arguments.
3123 * When the function takes a range, this is discovered after the first
3124 * call, and the loop is broken.
3125 */
3126 if (eap->skip)
3127 {
3128 ++emsg_skip;
3129 lnum = eap->line2; /* do it once, also with an invalid range */
3130 }
3131 else
3132 lnum = eap->line1;
3133 for ( ; lnum <= eap->line2; ++lnum)
3134 {
3135 if (!eap->skip && eap->addr_count > 0)
3136 {
3137 curwin->w_cursor.lnum = lnum;
3138 curwin->w_cursor.col = 0;
3139 }
3140 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003142 eap->line1, eap->line2, &doesrange,
3143 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 failed = TRUE;
3146 break;
3147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003148 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (doesrange || eap->skip)
3150 break;
3151 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003152 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003153 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003154 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (aborting())
3156 break;
3157 }
3158 if (eap->skip)
3159 --emsg_skip;
3160
3161 if (!failed)
3162 {
3163 /* Check for trailing illegal characters and a following command. */
3164 if (!ends_excmd(*arg))
3165 {
3166 emsg_severe = TRUE;
3167 EMSG(_(e_trailing));
3168 }
3169 else
3170 eap->nextcmd = check_nextcmd(arg);
3171 }
3172
3173end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003174 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178/*
3179 * ":unlet[!] var1 ... " command.
3180 */
3181 void
3182ex_unlet(eap)
3183 exarg_T *eap;
3184{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003185 ex_unletlock(eap, eap->arg, 0);
3186}
3187
3188/*
3189 * ":lockvar" and ":unlockvar" commands
3190 */
3191 void
3192ex_lockvar(eap)
3193 exarg_T *eap;
3194{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003196 int deep = 2;
3197
3198 if (eap->forceit)
3199 deep = -1;
3200 else if (vim_isdigit(*arg))
3201 {
3202 deep = getdigits(&arg);
3203 arg = skipwhite(arg);
3204 }
3205
3206 ex_unletlock(eap, arg, deep);
3207}
3208
3209/*
3210 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3211 */
3212 static void
3213ex_unletlock(eap, argstart, deep)
3214 exarg_T *eap;
3215 char_u *argstart;
3216 int deep;
3217{
3218 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003221 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 do
3224 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003225 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003226 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3227 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003228 if (lv.ll_name == NULL)
3229 error = TRUE; /* error but continue parsing */
3230 if (name_end == NULL || (!vim_iswhite(*name_end)
3231 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233 if (name_end != NULL)
3234 {
3235 emsg_severe = TRUE;
3236 EMSG(_(e_trailing));
3237 }
3238 if (!(eap->skip || error))
3239 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 break;
3241 }
3242
3243 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003244 {
3245 if (eap->cmdidx == CMD_unlet)
3246 {
3247 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3248 error = TRUE;
3249 }
3250 else
3251 {
3252 if (do_lock_var(&lv, name_end, deep,
3253 eap->cmdidx == CMD_lockvar) == FAIL)
3254 error = TRUE;
3255 }
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258 if (!eap->skip)
3259 clear_lval(&lv);
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 arg = skipwhite(name_end);
3262 } while (!ends_excmd(*arg));
3263
3264 eap->nextcmd = check_nextcmd(arg);
3265}
3266
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003268do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003270 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003271 int forceit;
3272{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003273 int ret = OK;
3274 int cc;
3275
3276 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003278 cc = *name_end;
3279 *name_end = NUL;
3280
3281 /* Normal name or expanded name. */
3282 if (check_changedtick(lp->ll_name))
3283 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003284 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003285 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003286 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003287 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003288 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3289 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003290 else if (lp->ll_range)
3291 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003293
3294 /* Delete a range of List items. */
3295 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3296 {
3297 li = lp->ll_li->li_next;
3298 listitem_remove(lp->ll_list, lp->ll_li);
3299 lp->ll_li = li;
3300 ++lp->ll_n1;
3301 }
3302 }
3303 else
3304 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003305 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003306 /* unlet a List item. */
3307 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003308 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003309 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003310 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003311 }
3312
3313 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003314}
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316/*
3317 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003318 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
3320 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003321do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003323 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 hashtab_T *ht;
3326 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003327 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 ht = find_var_ht(name, &varname);
3330 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 hi = hash_find(ht, varname);
3333 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003334 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003335 if (var_check_ro(HI2DI(hi)->di_flags, name))
3336 return FAIL;
3337 delete_var(ht, hi);
3338 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003341 if (forceit)
3342 return OK;
3343 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 return FAIL;
3345}
3346
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003347/*
3348 * Lock or unlock variable indicated by "lp".
3349 * "deep" is the levels to go (-1 for unlimited);
3350 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3351 */
3352 static int
3353do_lock_var(lp, name_end, deep, lock)
3354 lval_T *lp;
3355 char_u *name_end;
3356 int deep;
3357 int lock;
3358{
3359 int ret = OK;
3360 int cc;
3361 dictitem_T *di;
3362
3363 if (deep == 0) /* nothing to do */
3364 return OK;
3365
3366 if (lp->ll_tv == NULL)
3367 {
3368 cc = *name_end;
3369 *name_end = NUL;
3370
3371 /* Normal name or expanded name. */
3372 if (check_changedtick(lp->ll_name))
3373 ret = FAIL;
3374 else
3375 {
3376 di = find_var(lp->ll_name, NULL);
3377 if (di == NULL)
3378 ret = FAIL;
3379 else
3380 {
3381 if (lock)
3382 di->di_flags |= DI_FLAGS_LOCK;
3383 else
3384 di->di_flags &= ~DI_FLAGS_LOCK;
3385 item_lock(&di->di_tv, deep, lock);
3386 }
3387 }
3388 *name_end = cc;
3389 }
3390 else if (lp->ll_range)
3391 {
3392 listitem_T *li = lp->ll_li;
3393
3394 /* (un)lock a range of List items. */
3395 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3396 {
3397 item_lock(&li->li_tv, deep, lock);
3398 li = li->li_next;
3399 ++lp->ll_n1;
3400 }
3401 }
3402 else if (lp->ll_list != NULL)
3403 /* (un)lock a List item. */
3404 item_lock(&lp->ll_li->li_tv, deep, lock);
3405 else
3406 /* un(lock) a Dictionary item. */
3407 item_lock(&lp->ll_di->di_tv, deep, lock);
3408
3409 return ret;
3410}
3411
3412/*
3413 * Lock or unlock an item. "deep" is nr of levels to go.
3414 */
3415 static void
3416item_lock(tv, deep, lock)
3417 typval_T *tv;
3418 int deep;
3419 int lock;
3420{
3421 static int recurse = 0;
3422 list_T *l;
3423 listitem_T *li;
3424 dict_T *d;
3425 hashitem_T *hi;
3426 int todo;
3427
3428 if (recurse >= DICT_MAXNEST)
3429 {
3430 EMSG(_("E743: variable nested too deep for (un)lock"));
3431 return;
3432 }
3433 if (deep == 0)
3434 return;
3435 ++recurse;
3436
3437 /* lock/unlock the item itself */
3438 if (lock)
3439 tv->v_lock |= VAR_LOCKED;
3440 else
3441 tv->v_lock &= ~VAR_LOCKED;
3442
3443 switch (tv->v_type)
3444 {
3445 case VAR_LIST:
3446 if ((l = tv->vval.v_list) != NULL)
3447 {
3448 if (lock)
3449 l->lv_lock |= VAR_LOCKED;
3450 else
3451 l->lv_lock &= ~VAR_LOCKED;
3452 if (deep < 0 || deep > 1)
3453 /* recursive: lock/unlock the items the List contains */
3454 for (li = l->lv_first; li != NULL; li = li->li_next)
3455 item_lock(&li->li_tv, deep - 1, lock);
3456 }
3457 break;
3458 case VAR_DICT:
3459 if ((d = tv->vval.v_dict) != NULL)
3460 {
3461 if (lock)
3462 d->dv_lock |= VAR_LOCKED;
3463 else
3464 d->dv_lock &= ~VAR_LOCKED;
3465 if (deep < 0 || deep > 1)
3466 {
3467 /* recursive: lock/unlock the items the List contains */
3468 todo = d->dv_hashtab.ht_used;
3469 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3470 {
3471 if (!HASHITEM_EMPTY(hi))
3472 {
3473 --todo;
3474 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3475 }
3476 }
3477 }
3478 }
3479 }
3480 --recurse;
3481}
3482
Bram Moolenaara40058a2005-07-11 22:42:07 +00003483/*
3484 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3485 * it refers to a List or Dictionary that is locked.
3486 */
3487 static int
3488tv_islocked(tv)
3489 typval_T *tv;
3490{
3491 return (tv->v_lock & VAR_LOCKED)
3492 || (tv->v_type == VAR_LIST
3493 && tv->vval.v_list != NULL
3494 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3495 || (tv->v_type == VAR_DICT
3496 && tv->vval.v_dict != NULL
3497 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3498}
3499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3501/*
3502 * Delete all "menutrans_" variables.
3503 */
3504 void
3505del_menutrans_vars()
3506{
Bram Moolenaar33570922005-01-25 22:26:29 +00003507 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003508 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar33570922005-01-25 22:26:29 +00003510 hash_lock(&globvarht);
3511 todo = globvarht.ht_used;
3512 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003513 {
3514 if (!HASHITEM_EMPTY(hi))
3515 {
3516 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003517 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3518 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003519 }
3520 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003521 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523#endif
3524
3525#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3526
3527/*
3528 * Local string buffer for the next two functions to store a variable name
3529 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3530 * get_user_var_name().
3531 */
3532
3533static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3534
3535static char_u *varnamebuf = NULL;
3536static int varnamebuflen = 0;
3537
3538/*
3539 * Function to concatenate a prefix and a variable name.
3540 */
3541 static char_u *
3542cat_prefix_varname(prefix, name)
3543 int prefix;
3544 char_u *name;
3545{
3546 int len;
3547
3548 len = (int)STRLEN(name) + 3;
3549 if (len > varnamebuflen)
3550 {
3551 vim_free(varnamebuf);
3552 len += 10; /* some additional space */
3553 varnamebuf = alloc(len);
3554 if (varnamebuf == NULL)
3555 {
3556 varnamebuflen = 0;
3557 return NULL;
3558 }
3559 varnamebuflen = len;
3560 }
3561 *varnamebuf = prefix;
3562 varnamebuf[1] = ':';
3563 STRCPY(varnamebuf + 2, name);
3564 return varnamebuf;
3565}
3566
3567/*
3568 * Function given to ExpandGeneric() to obtain the list of user defined
3569 * (global/buffer/window/built-in) variable names.
3570 */
3571/*ARGSUSED*/
3572 char_u *
3573get_user_var_name(xp, idx)
3574 expand_T *xp;
3575 int idx;
3576{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003577 static long_u gdone;
3578 static long_u bdone;
3579 static long_u wdone;
3580 static int vidx;
3581 static hashitem_T *hi;
3582 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003585 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003586
3587 /* Global variables */
3588 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003590 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003591 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003592 else
3593 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003594 while (HASHITEM_EMPTY(hi))
3595 ++hi;
3596 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3597 return cat_prefix_varname('g', hi->hi_key);
3598 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003600
3601 /* b: variables */
3602 ht = &curbuf->b_vars.dv_hashtab;
3603 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003605 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003607 else
3608 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003609 while (HASHITEM_EMPTY(hi))
3610 ++hi;
3611 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003613 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003615 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return (char_u *)"b:changedtick";
3617 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003618
3619 /* w: variables */
3620 ht = &curwin->w_vars.dv_hashtab;
3621 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003623 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003624 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003625 else
3626 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003627 while (HASHITEM_EMPTY(hi))
3628 ++hi;
3629 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003631
3632 /* v: variables */
3633 if (vidx < VV_LEN)
3634 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 vim_free(varnamebuf);
3637 varnamebuf = NULL;
3638 varnamebuflen = 0;
3639 return NULL;
3640}
3641
3642#endif /* FEAT_CMDL_COMPL */
3643
3644/*
3645 * types for expressions.
3646 */
3647typedef enum
3648{
3649 TYPE_UNKNOWN = 0
3650 , TYPE_EQUAL /* == */
3651 , TYPE_NEQUAL /* != */
3652 , TYPE_GREATER /* > */
3653 , TYPE_GEQUAL /* >= */
3654 , TYPE_SMALLER /* < */
3655 , TYPE_SEQUAL /* <= */
3656 , TYPE_MATCH /* =~ */
3657 , TYPE_NOMATCH /* !~ */
3658} exptype_T;
3659
3660/*
3661 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003662 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3664 */
3665
3666/*
3667 * Handle zero level expression.
3668 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003669 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003670 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 * Return OK or FAIL.
3672 */
3673 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 char_u **nextcmd;
3678 int evaluate;
3679{
3680 int ret;
3681 char_u *p;
3682
3683 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (ret == FAIL || !ends_excmd(*p))
3686 {
3687 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 /*
3690 * Report the invalid expression unless the expression evaluation has
3691 * been cancelled due to an aborting error, an interrupt, or an
3692 * exception.
3693 */
3694 if (!aborting())
3695 EMSG2(_(e_invexpr2), arg);
3696 ret = FAIL;
3697 }
3698 if (nextcmd != NULL)
3699 *nextcmd = check_nextcmd(p);
3700
3701 return ret;
3702}
3703
3704/*
3705 * Handle top level expression:
3706 * expr1 ? expr0 : expr0
3707 *
3708 * "arg" must point to the first non-white of the expression.
3709 * "arg" is advanced to the next non-white after the recognized expression.
3710 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003711 * Note: "rettv.v_lock" is not set.
3712 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 * Return OK or FAIL.
3714 */
3715 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003716eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int evaluate;
3720{
3721 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003722 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
3724 /*
3725 * Get the first variable.
3726 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 return FAIL;
3729
3730 if ((*arg)[0] == '?')
3731 {
3732 result = FALSE;
3733 if (evaluate)
3734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 int error = FALSE;
3736
3737 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003739 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003740 if (error)
3741 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743
3744 /*
3745 * Get the second variable.
3746 */
3747 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003748 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 return FAIL;
3750
3751 /*
3752 * Check for the ":".
3753 */
3754 if ((*arg)[0] != ':')
3755 {
3756 EMSG(_("E109: Missing ':' after '?'"));
3757 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003758 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return FAIL;
3760 }
3761
3762 /*
3763 * Get the third variable.
3764 */
3765 *arg = skipwhite(*arg + 1);
3766 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3767 {
3768 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 return FAIL;
3771 }
3772 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003773 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
3776 return OK;
3777}
3778
3779/*
3780 * Handle first level expression:
3781 * expr2 || expr2 || expr2 logical OR
3782 *
3783 * "arg" must point to the first non-white of the expression.
3784 * "arg" is advanced to the next non-white after the recognized expression.
3785 *
3786 * Return OK or FAIL.
3787 */
3788 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003789eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 int evaluate;
3793{
Bram Moolenaar33570922005-01-25 22:26:29 +00003794 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 long result;
3796 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 /*
3800 * Get the first variable.
3801 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return FAIL;
3804
3805 /*
3806 * Repeat until there is no following "||".
3807 */
3808 first = TRUE;
3809 result = FALSE;
3810 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3811 {
3812 if (evaluate && first)
3813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003814 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003816 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003817 if (error)
3818 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 first = FALSE;
3820 }
3821
3822 /*
3823 * Get the second variable.
3824 */
3825 *arg = skipwhite(*arg + 2);
3826 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3827 return FAIL;
3828
3829 /*
3830 * Compute the result.
3831 */
3832 if (evaluate && !result)
3833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003836 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 if (error)
3838 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 if (evaluate)
3841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003842 rettv->v_type = VAR_NUMBER;
3843 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 }
3846
3847 return OK;
3848}
3849
3850/*
3851 * Handle second level expression:
3852 * expr3 && expr3 && expr3 logical AND
3853 *
3854 * "arg" must point to the first non-white of the expression.
3855 * "arg" is advanced to the next non-white after the recognized expression.
3856 *
3857 * Return OK or FAIL.
3858 */
3859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003860eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 int evaluate;
3864{
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 long result;
3867 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003868 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
3870 /*
3871 * Get the first variable.
3872 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003873 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 return FAIL;
3875
3876 /*
3877 * Repeat until there is no following "&&".
3878 */
3879 first = TRUE;
3880 result = TRUE;
3881 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3882 {
3883 if (evaluate && first)
3884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 if (error)
3889 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 first = FALSE;
3891 }
3892
3893 /*
3894 * Get the second variable.
3895 */
3896 *arg = skipwhite(*arg + 2);
3897 if (eval4(arg, &var2, evaluate && result) == FAIL)
3898 return FAIL;
3899
3900 /*
3901 * Compute the result.
3902 */
3903 if (evaluate && result)
3904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 if (error)
3909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 if (evaluate)
3912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 rettv->v_type = VAR_NUMBER;
3914 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916 }
3917
3918 return OK;
3919}
3920
3921/*
3922 * Handle third level expression:
3923 * var1 == var2
3924 * var1 =~ var2
3925 * var1 != var2
3926 * var1 !~ var2
3927 * var1 > var2
3928 * var1 >= var2
3929 * var1 < var2
3930 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003931 * var1 is var2
3932 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 *
3934 * "arg" must point to the first non-white of the expression.
3935 * "arg" is advanced to the next non-white after the recognized expression.
3936 *
3937 * Return OK or FAIL.
3938 */
3939 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003940eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 int evaluate;
3944{
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 char_u *p;
3947 int i;
3948 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003949 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int len = 2;
3951 long n1, n2;
3952 char_u *s1, *s2;
3953 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3954 regmatch_T regmatch;
3955 int ic;
3956 char_u *save_cpo;
3957
3958 /*
3959 * Get the first variable.
3960 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003961 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 return FAIL;
3963
3964 p = *arg;
3965 switch (p[0])
3966 {
3967 case '=': if (p[1] == '=')
3968 type = TYPE_EQUAL;
3969 else if (p[1] == '~')
3970 type = TYPE_MATCH;
3971 break;
3972 case '!': if (p[1] == '=')
3973 type = TYPE_NEQUAL;
3974 else if (p[1] == '~')
3975 type = TYPE_NOMATCH;
3976 break;
3977 case '>': if (p[1] != '=')
3978 {
3979 type = TYPE_GREATER;
3980 len = 1;
3981 }
3982 else
3983 type = TYPE_GEQUAL;
3984 break;
3985 case '<': if (p[1] != '=')
3986 {
3987 type = TYPE_SMALLER;
3988 len = 1;
3989 }
3990 else
3991 type = TYPE_SEQUAL;
3992 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003993 case 'i': if (p[1] == 's')
3994 {
3995 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3996 len = 5;
3997 if (!vim_isIDc(p[len]))
3998 {
3999 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4000 type_is = TRUE;
4001 }
4002 }
4003 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005
4006 /*
4007 * If there is a comparitive operator, use it.
4008 */
4009 if (type != TYPE_UNKNOWN)
4010 {
4011 /* extra question mark appended: ignore case */
4012 if (p[len] == '?')
4013 {
4014 ic = TRUE;
4015 ++len;
4016 }
4017 /* extra '#' appended: match case */
4018 else if (p[len] == '#')
4019 {
4020 ic = FALSE;
4021 ++len;
4022 }
4023 /* nothing appened: use 'ignorecase' */
4024 else
4025 ic = p_ic;
4026
4027 /*
4028 * Get the second variable.
4029 */
4030 *arg = skipwhite(p + len);
4031 if (eval5(arg, &var2, evaluate) == FAIL)
4032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return FAIL;
4035 }
4036
4037 if (evaluate)
4038 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 if (type_is && rettv->v_type != var2.v_type)
4040 {
4041 /* For "is" a different type always means FALSE, for "notis"
4042 * it means TRUE. */
4043 n1 = (type == TYPE_NEQUAL);
4044 }
4045 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4046 {
4047 if (type_is)
4048 {
4049 n1 = (rettv->v_type == var2.v_type
4050 && rettv->vval.v_list == var2.vval.v_list);
4051 if (type == TYPE_NEQUAL)
4052 n1 = !n1;
4053 }
4054 else if (rettv->v_type != var2.v_type
4055 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4056 {
4057 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004058 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004059 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004060 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 clear_tv(rettv);
4062 clear_tv(&var2);
4063 return FAIL;
4064 }
4065 else
4066 {
4067 /* Compare two Lists for being equal or unequal. */
4068 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4069 if (type == TYPE_NEQUAL)
4070 n1 = !n1;
4071 }
4072 }
4073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004074 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4075 {
4076 if (type_is)
4077 {
4078 n1 = (rettv->v_type == var2.v_type
4079 && rettv->vval.v_dict == var2.vval.v_dict);
4080 if (type == TYPE_NEQUAL)
4081 n1 = !n1;
4082 }
4083 else if (rettv->v_type != var2.v_type
4084 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4085 {
4086 if (rettv->v_type != var2.v_type)
4087 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4088 else
4089 EMSG(_("E736: Invalid operation for Dictionary"));
4090 clear_tv(rettv);
4091 clear_tv(&var2);
4092 return FAIL;
4093 }
4094 else
4095 {
4096 /* Compare two Dictionaries for being equal or unequal. */
4097 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4098 if (type == TYPE_NEQUAL)
4099 n1 = !n1;
4100 }
4101 }
4102
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004103 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4104 {
4105 if (rettv->v_type != var2.v_type
4106 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4107 {
4108 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004109 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004110 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004111 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004112 clear_tv(rettv);
4113 clear_tv(&var2);
4114 return FAIL;
4115 }
4116 else
4117 {
4118 /* Compare two Funcrefs for being equal or unequal. */
4119 if (rettv->vval.v_string == NULL
4120 || var2.vval.v_string == NULL)
4121 n1 = FALSE;
4122 else
4123 n1 = STRCMP(rettv->vval.v_string,
4124 var2.vval.v_string) == 0;
4125 if (type == TYPE_NEQUAL)
4126 n1 = !n1;
4127 }
4128 }
4129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 /*
4131 * If one of the two variables is a number, compare as a number.
4132 * When using "=~" or "!~", always compare as string.
4133 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004134 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 n1 = get_tv_number(rettv);
4138 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 switch (type)
4140 {
4141 case TYPE_EQUAL: n1 = (n1 == n2); break;
4142 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4143 case TYPE_GREATER: n1 = (n1 > n2); break;
4144 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4145 case TYPE_SMALLER: n1 = (n1 < n2); break;
4146 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4147 case TYPE_UNKNOWN:
4148 case TYPE_MATCH:
4149 case TYPE_NOMATCH: break; /* avoid gcc warning */
4150 }
4151 }
4152 else
4153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 s1 = get_tv_string_buf(rettv, buf1);
4155 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4157 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4158 else
4159 i = 0;
4160 n1 = FALSE;
4161 switch (type)
4162 {
4163 case TYPE_EQUAL: n1 = (i == 0); break;
4164 case TYPE_NEQUAL: n1 = (i != 0); break;
4165 case TYPE_GREATER: n1 = (i > 0); break;
4166 case TYPE_GEQUAL: n1 = (i >= 0); break;
4167 case TYPE_SMALLER: n1 = (i < 0); break;
4168 case TYPE_SEQUAL: n1 = (i <= 0); break;
4169
4170 case TYPE_MATCH:
4171 case TYPE_NOMATCH:
4172 /* avoid 'l' flag in 'cpoptions' */
4173 save_cpo = p_cpo;
4174 p_cpo = (char_u *)"";
4175 regmatch.regprog = vim_regcomp(s2,
4176 RE_MAGIC + RE_STRING);
4177 regmatch.rm_ic = ic;
4178 if (regmatch.regprog != NULL)
4179 {
4180 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4181 vim_free(regmatch.regprog);
4182 if (type == TYPE_NOMATCH)
4183 n1 = !n1;
4184 }
4185 p_cpo = save_cpo;
4186 break;
4187
4188 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4189 }
4190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
4192 clear_tv(&var2);
4193 rettv->v_type = VAR_NUMBER;
4194 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 }
4197
4198 return OK;
4199}
4200
4201/*
4202 * Handle fourth level expression:
4203 * + number addition
4204 * - number subtraction
4205 * . string concatenation
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
4219 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 int op;
4221 long n1, n2;
4222 char_u *s1, *s2;
4223 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4224 char_u *p;
4225
4226 /*
4227 * Get the first variable.
4228 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231
4232 /*
4233 * Repeat computing, until no '+', '-' or '.' is following.
4234 */
4235 for (;;)
4236 {
4237 op = **arg;
4238 if (op != '+' && op != '-' && op != '.')
4239 break;
4240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (op != '+' || rettv->v_type != VAR_LIST)
4242 {
4243 /* For "list + ...", an illegal use of the first operand as
4244 * a number cannot be determined before evaluating the 2nd
4245 * operand: if this is also a list, all is ok.
4246 * For "something . ...", "something - ..." or "non-list + ...",
4247 * we know that the first operand needs to be a string or number
4248 * without evaluating the 2nd operand. So check before to avoid
4249 * side effects after an error. */
4250 if (evaluate && get_tv_string_chk(rettv) == NULL)
4251 {
4252 clear_tv(rettv);
4253 return FAIL;
4254 }
4255 }
4256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 /*
4258 * Get the second variable.
4259 */
4260 *arg = skipwhite(*arg + 1);
4261 if (eval6(arg, &var2, evaluate) == FAIL)
4262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
4265 }
4266
4267 if (evaluate)
4268 {
4269 /*
4270 * Compute the result.
4271 */
4272 if (op == '.')
4273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4275 s2 = get_tv_string_buf_chk(&var2, buf2);
4276 if (s2 == NULL) /* type error ? */
4277 {
4278 clear_tv(rettv);
4279 clear_tv(&var2);
4280 return FAIL;
4281 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004282 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(rettv);
4284 rettv->v_type = VAR_STRING;
4285 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004287 else if (op == '+' && rettv->v_type == VAR_LIST
4288 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004289 {
4290 /* concatenate Lists */
4291 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4292 &var3) == FAIL)
4293 {
4294 clear_tv(rettv);
4295 clear_tv(&var2);
4296 return FAIL;
4297 }
4298 clear_tv(rettv);
4299 *rettv = var3;
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 else
4302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 int error = FALSE;
4304
4305 n1 = get_tv_number_chk(rettv, &error);
4306 if (error)
4307 {
4308 /* This can only happen for "list + non-list".
4309 * For "non-list + ..." or "something - ...", we returned
4310 * before evaluating the 2nd operand. */
4311 clear_tv(rettv);
4312 return FAIL;
4313 }
4314 n2 = get_tv_number_chk(&var2, &error);
4315 if (error)
4316 {
4317 clear_tv(rettv);
4318 clear_tv(&var2);
4319 return FAIL;
4320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (op == '+')
4323 n1 = n1 + n2;
4324 else
4325 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 rettv->v_type = VAR_NUMBER;
4327 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004329 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 }
4332 return OK;
4333}
4334
4335/*
4336 * Handle fifth level expression:
4337 * * number multiplication
4338 * / number division
4339 * % number modulo
4340 *
4341 * "arg" must point to the first non-white of the expression.
4342 * "arg" is advanced to the next non-white after the recognized expression.
4343 *
4344 * Return OK or FAIL.
4345 */
4346 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int evaluate;
4351{
Bram Moolenaar33570922005-01-25 22:26:29 +00004352 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 int op;
4354 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 /*
4358 * Get the first variable.
4359 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 return FAIL;
4362
4363 /*
4364 * Repeat computing, until no '*', '/' or '%' is following.
4365 */
4366 for (;;)
4367 {
4368 op = **arg;
4369 if (op != '*' && op != '/' && op != '%')
4370 break;
4371
4372 if (evaluate)
4373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004375 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376 if (error)
4377 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 else
4380 n1 = 0;
4381
4382 /*
4383 * Get the second variable.
4384 */
4385 *arg = skipwhite(*arg + 1);
4386 if (eval7(arg, &var2, evaluate) == FAIL)
4387 return FAIL;
4388
4389 if (evaluate)
4390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004391 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393 if (error)
4394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 /*
4397 * Compute the result.
4398 */
4399 if (op == '*')
4400 n1 = n1 * n2;
4401 else if (op == '/')
4402 {
4403 if (n2 == 0) /* give an error message? */
4404 n1 = 0x7fffffffL;
4405 else
4406 n1 = n1 / n2;
4407 }
4408 else
4409 {
4410 if (n2 == 0) /* give an error message? */
4411 n1 = 0;
4412 else
4413 n1 = n1 % n2;
4414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415 rettv->v_type = VAR_NUMBER;
4416 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 }
4419
4420 return OK;
4421}
4422
4423/*
4424 * Handle sixth level expression:
4425 * number number constant
4426 * "string" string contstant
4427 * 'string' literal string contstant
4428 * &option-name option value
4429 * @r register contents
4430 * identifier variable value
4431 * function() function call
4432 * $VAR environment variable
4433 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004434 * [expr, expr] List
4435 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 *
4437 * Also handle:
4438 * ! in front logical NOT
4439 * - in front unary minus
4440 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004441 * trailing [] subscript in String or List
4442 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 *
4444 * "arg" must point to the first non-white of the expression.
4445 * "arg" is advanced to the next non-white after the recognized expression.
4446 *
4447 * Return OK or FAIL.
4448 */
4449 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 int evaluate;
4454{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 long n;
4456 int len;
4457 char_u *s;
4458 int val;
4459 char_u *start_leader, *end_leader;
4460 int ret = OK;
4461 char_u *alias;
4462
4463 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004464 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 /*
4470 * Skip '!' and '-' characters. They are handled later.
4471 */
4472 start_leader = *arg;
4473 while (**arg == '!' || **arg == '-' || **arg == '+')
4474 *arg = skipwhite(*arg + 1);
4475 end_leader = *arg;
4476
4477 switch (**arg)
4478 {
4479 /*
4480 * Number constant.
4481 */
4482 case '0':
4483 case '1':
4484 case '2':
4485 case '3':
4486 case '4':
4487 case '5':
4488 case '6':
4489 case '7':
4490 case '8':
4491 case '9':
4492 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4493 *arg += len;
4494 if (evaluate)
4495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004496 rettv->v_type = VAR_NUMBER;
4497 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499 break;
4500
4501 /*
4502 * String constant: "string".
4503 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506
4507 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004510 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 break;
4512
4513 /*
4514 * List: [expr, expr]
4515 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004516 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 break;
4518
4519 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 * Dictionary: {key: val, key: val}
4521 */
4522 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4523 break;
4524
4525 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004526 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004528 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530
4531 /*
4532 * Environment variable: $VAR.
4533 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004534 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 break;
4536
4537 /*
4538 * Register contents: @r.
4539 */
4540 case '@': ++*arg;
4541 if (evaluate)
4542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004544 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
4546 if (**arg != NUL)
4547 ++*arg;
4548 break;
4549
4550 /*
4551 * nested expression: (expression).
4552 */
4553 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (**arg == ')')
4556 ++*arg;
4557 else if (ret == OK)
4558 {
4559 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004560 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 ret = FAIL;
4562 }
4563 break;
4564
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568
4569 if (ret == NOTDONE)
4570 {
4571 /*
4572 * Must be a variable or function name.
4573 * Can also be a curly-braces kind of name: {expr}.
4574 */
4575 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004576 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004577 if (alias != NULL)
4578 s = alias;
4579
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004580 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004581 ret = FAIL;
4582 else
4583 {
4584 if (**arg == '(') /* recursive! */
4585 {
4586 /* If "s" is the name of a variable of type VAR_FUNC
4587 * use its contents. */
4588 s = deref_func_name(s, &len);
4589
4590 /* Invoke the function. */
4591 ret = get_func_tv(s, len, rettv, arg,
4592 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004593 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 /* Stop the expression evaluation when immediately
4595 * aborting on error, or when an interrupt occurred or
4596 * an exception was thrown but not caught. */
4597 if (aborting())
4598 {
4599 if (ret == OK)
4600 clear_tv(rettv);
4601 ret = FAIL;
4602 }
4603 }
4604 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004605 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004606 else
4607 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 }
4609
4610 if (alias != NULL)
4611 vim_free(alias);
4612 }
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 *arg = skipwhite(*arg);
4615
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004616 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4617 * expr(expr). */
4618 if (ret == OK)
4619 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 /*
4622 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4623 */
4624 if (ret == OK && evaluate && end_leader > start_leader)
4625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 int error = FALSE;
4627
4628 val = get_tv_number_chk(rettv, &error);
4629 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 clear_tv(rettv);
4632 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004634 else
4635 {
4636 while (end_leader > start_leader)
4637 {
4638 --end_leader;
4639 if (*end_leader == '!')
4640 val = !val;
4641 else if (*end_leader == '-')
4642 val = -val;
4643 }
4644 clear_tv(rettv);
4645 rettv->v_type = VAR_NUMBER;
4646 rettv->vval.v_number = val;
4647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649
4650 return ret;
4651}
4652
4653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 * Evaluate an "[expr]" or "[expr:expr]" index.
4655 * "*arg" points to the '['.
4656 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4657 */
4658 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004659eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004663 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004664{
4665 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004666 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004667 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 long len = -1;
4669 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004675 if (verbose)
4676 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004677 return FAIL;
4678 }
4679
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004682 /*
4683 * dict.name
4684 */
4685 key = *arg + 1;
4686 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4687 ;
4688 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004689 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 }
4692 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004694 /*
4695 * something[idx]
4696 *
4697 * Get the (first) variable from inside the [].
4698 */
4699 *arg = skipwhite(*arg + 1);
4700 if (**arg == ':')
4701 empty1 = TRUE;
4702 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4703 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4705 {
4706 /* not a number or string */
4707 clear_tv(&var1);
4708 return FAIL;
4709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004710
4711 /*
4712 * Get the second variable from inside the [:].
4713 */
4714 if (**arg == ':')
4715 {
4716 range = TRUE;
4717 *arg = skipwhite(*arg + 1);
4718 if (**arg == ']')
4719 empty2 = TRUE;
4720 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 if (!empty1)
4723 clear_tv(&var1);
4724 return FAIL;
4725 }
4726 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4727 {
4728 /* not a number or string */
4729 if (!empty1)
4730 clear_tv(&var1);
4731 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004732 return FAIL;
4733 }
4734 }
4735
4736 /* Check for the ']'. */
4737 if (**arg != ']')
4738 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004739 if (verbose)
4740 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004741 clear_tv(&var1);
4742 if (range)
4743 clear_tv(&var2);
4744 return FAIL;
4745 }
4746 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004747 }
4748
4749 if (evaluate)
4750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 n1 = 0;
4752 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754 n1 = get_tv_number(&var1);
4755 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 }
4757 if (range)
4758 {
4759 if (empty2)
4760 n2 = -1;
4761 else
4762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 n2 = get_tv_number(&var2);
4764 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 }
4766 }
4767
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769 {
4770 case VAR_NUMBER:
4771 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004773 len = (long)STRLEN(s);
4774 if (range)
4775 {
4776 /* The resulting variable is a substring. If the indexes
4777 * are out of range the result is empty. */
4778 if (n1 < 0)
4779 {
4780 n1 = len + n1;
4781 if (n1 < 0)
4782 n1 = 0;
4783 }
4784 if (n2 < 0)
4785 n2 = len + n2;
4786 else if (n2 >= len)
4787 n2 = len;
4788 if (n1 >= len || n2 < 0 || n1 > n2)
4789 s = NULL;
4790 else
4791 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4792 }
4793 else
4794 {
4795 /* The resulting variable is a string of a single
4796 * character. If the index is too big or negative the
4797 * result is empty. */
4798 if (n1 >= len || n1 < 0)
4799 s = NULL;
4800 else
4801 s = vim_strnsave(s + n1, 1);
4802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 clear_tv(rettv);
4804 rettv->v_type = VAR_STRING;
4805 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806 break;
4807
4808 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 if (n1 < 0)
4811 n1 = len + n1;
4812 if (!empty1 && (n1 < 0 || n1 >= len))
4813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004814 if (verbose)
4815 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004816 return FAIL;
4817 }
4818 if (range)
4819 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004820 list_T *l;
4821 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822
4823 if (n2 < 0)
4824 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004825 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004827 if (verbose)
4828 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 return FAIL;
4830 }
4831 l = list_alloc();
4832 if (l == NULL)
4833 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 n1 <= n2; ++n1)
4836 {
4837 if (list_append_tv(l, &item->li_tv) == FAIL)
4838 {
4839 list_free(l);
4840 return FAIL;
4841 }
4842 item = item->li_next;
4843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 clear_tv(rettv);
4845 rettv->v_type = VAR_LIST;
4846 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004847 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 }
4849 else
4850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004853 clear_tv(rettv);
4854 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 }
4856 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857
4858 case VAR_DICT:
4859 if (range)
4860 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004861 if (verbose)
4862 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004863 if (len == -1)
4864 clear_tv(&var1);
4865 return FAIL;
4866 }
4867 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004868 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869
4870 if (len == -1)
4871 {
4872 key = get_tv_string(&var1);
4873 if (*key == NUL)
4874 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004875 if (verbose)
4876 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004877 clear_tv(&var1);
4878 return FAIL;
4879 }
4880 }
4881
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004882 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004884 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004885 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 if (len == -1)
4887 clear_tv(&var1);
4888 if (item == NULL)
4889 return FAIL;
4890
4891 copy_tv(&item->di_tv, &var1);
4892 clear_tv(rettv);
4893 *rettv = var1;
4894 }
4895 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004896 }
4897 }
4898
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004899 return OK;
4900}
4901
4902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 * Get an option value.
4904 * "arg" points to the '&' or '+' before the option name.
4905 * "arg" is advanced to character after the option name.
4906 * Return OK or FAIL.
4907 */
4908 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004911 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 int evaluate;
4913{
4914 char_u *option_end;
4915 long numval;
4916 char_u *stringval;
4917 int opt_type;
4918 int c;
4919 int working = (**arg == '+'); /* has("+option") */
4920 int ret = OK;
4921 int opt_flags;
4922
4923 /*
4924 * Isolate the option name and find its value.
4925 */
4926 option_end = find_option_end(arg, &opt_flags);
4927 if (option_end == NULL)
4928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 EMSG2(_("E112: Option name missing: %s"), *arg);
4931 return FAIL;
4932 }
4933
4934 if (!evaluate)
4935 {
4936 *arg = option_end;
4937 return OK;
4938 }
4939
4940 c = *option_end;
4941 *option_end = NUL;
4942 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004943 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 if (opt_type == -3) /* invalid name */
4946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004947 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 EMSG2(_("E113: Unknown option: %s"), *arg);
4949 ret = FAIL;
4950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004951 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
4953 if (opt_type == -2) /* hidden string option */
4954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 rettv->v_type = VAR_STRING;
4956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 else if (opt_type == -1) /* hidden number option */
4959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004960 rettv->v_type = VAR_NUMBER;
4961 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else if (opt_type == 1) /* number option */
4964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 rettv->v_type = VAR_NUMBER;
4966 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else /* string option */
4969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004970 rettv->v_type = VAR_STRING;
4971 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 }
4974 else if (working && (opt_type == -2 || opt_type == -1))
4975 ret = FAIL;
4976
4977 *option_end = c; /* put back for error messages */
4978 *arg = option_end;
4979
4980 return ret;
4981}
4982
4983/*
4984 * Allocate a variable for a string constant.
4985 * Return OK or FAIL.
4986 */
4987 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004988get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 int evaluate;
4992{
4993 char_u *p;
4994 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 int extra = 0;
4996
4997 /*
4998 * Find the end of the string, skipping backslashed characters.
4999 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005000 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 if (*p == '\\' && p[1] != NUL)
5003 {
5004 ++p;
5005 /* A "\<x>" form occupies at least 4 characters, and produces up
5006 * to 6 characters: reserve space for 2 extra */
5007 if (*p == '<')
5008 extra += 2;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011
5012 if (*p != '"')
5013 {
5014 EMSG2(_("E114: Missing quote: %s"), *arg);
5015 return FAIL;
5016 }
5017
5018 /* If only parsing, set *arg and return here */
5019 if (!evaluate)
5020 {
5021 *arg = p + 1;
5022 return OK;
5023 }
5024
5025 /*
5026 * Copy the string into allocated memory, handling backslashed
5027 * characters.
5028 */
5029 name = alloc((unsigned)(p - *arg + extra));
5030 if (name == NULL)
5031 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 rettv->v_type = VAR_STRING;
5033 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 if (*p == '\\')
5038 {
5039 switch (*++p)
5040 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 case 'b': *name++ = BS; ++p; break;
5042 case 'e': *name++ = ESC; ++p; break;
5043 case 'f': *name++ = FF; ++p; break;
5044 case 'n': *name++ = NL; ++p; break;
5045 case 'r': *name++ = CAR; ++p; break;
5046 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 case 'X': /* hex: "\x1", "\x12" */
5049 case 'x':
5050 case 'u': /* Unicode: "\u0023" */
5051 case 'U':
5052 if (vim_isxdigit(p[1]))
5053 {
5054 int n, nr;
5055 int c = toupper(*p);
5056
5057 if (c == 'X')
5058 n = 2;
5059 else
5060 n = 4;
5061 nr = 0;
5062 while (--n >= 0 && vim_isxdigit(p[1]))
5063 {
5064 ++p;
5065 nr = (nr << 4) + hex2nr(*p);
5066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068#ifdef FEAT_MBYTE
5069 /* For "\u" store the number according to
5070 * 'encoding'. */
5071 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 else
5074#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 break;
5078
5079 /* octal: "\1", "\12", "\123" */
5080 case '0':
5081 case '1':
5082 case '2':
5083 case '3':
5084 case '4':
5085 case '5':
5086 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 case '7': *name = *p++ - '0';
5088 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 *name = (*name << 3) + *p++ - '0';
5091 if (*p >= '0' && *p <= '7')
5092 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
5096
5097 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (extra != 0)
5100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 }
5104 /* FALLTHROUGH */
5105
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 break;
5108 }
5109 }
5110 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005114 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 *arg = p + 1;
5116
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 return OK;
5118}
5119
5120/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005121 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 * Return OK or FAIL.
5123 */
5124 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 int evaluate;
5129{
5130 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132 int reduce = 0;
5133
5134 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 */
5137 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5138 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005140 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005141 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005142 break;
5143 ++reduce;
5144 ++p;
5145 }
5146 }
5147
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005151 return FAIL;
5152 }
5153
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005155 if (!evaluate)
5156 {
5157 *arg = p + 1;
5158 return OK;
5159 }
5160
5161 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005163 */
5164 str = alloc((unsigned)((p - *arg) - reduce));
5165 if (str == NULL)
5166 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 rettv->v_type = VAR_STRING;
5168 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005169
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005171 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005172 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005173 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175 break;
5176 ++p;
5177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005179 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005181 *arg = p + 1;
5182
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005183 return OK;
5184}
5185
5186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 * Allocate a variable for a List and fill it from "*arg".
5188 * Return OK or FAIL.
5189 */
5190 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005193 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194 int evaluate;
5195{
Bram Moolenaar33570922005-01-25 22:26:29 +00005196 list_T *l = NULL;
5197 typval_T tv;
5198 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199
5200 if (evaluate)
5201 {
5202 l = list_alloc();
5203 if (l == NULL)
5204 return FAIL;
5205 }
5206
5207 *arg = skipwhite(*arg + 1);
5208 while (**arg != ']' && **arg != NUL)
5209 {
5210 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5211 goto failret;
5212 if (evaluate)
5213 {
5214 item = listitem_alloc();
5215 if (item != NULL)
5216 {
5217 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005218 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 list_append(l, item);
5220 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005221 else
5222 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223 }
5224
5225 if (**arg == ']')
5226 break;
5227 if (**arg != ',')
5228 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 goto failret;
5231 }
5232 *arg = skipwhite(*arg + 1);
5233 }
5234
5235 if (**arg != ']')
5236 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238failret:
5239 if (evaluate)
5240 list_free(l);
5241 return FAIL;
5242 }
5243
5244 *arg = skipwhite(*arg + 1);
5245 if (evaluate)
5246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005247 rettv->v_type = VAR_LIST;
5248 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 ++l->lv_refcount;
5250 }
5251
5252 return OK;
5253}
5254
5255/*
5256 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005257 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005259 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260list_alloc()
5261{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262 list_T *l;
5263
5264 l = (list_T *)alloc_clear(sizeof(list_T));
5265 if (l != NULL)
5266 {
5267 /* Prepend the list to the list of lists for garbage collection. */
5268 if (first_list != NULL)
5269 first_list->lv_used_prev = l;
5270 l->lv_used_prev = NULL;
5271 l->lv_used_next = first_list;
5272 first_list = l;
5273 }
5274 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275}
5276
5277/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005278 * Allocate an empty list for a return value.
5279 * Returns OK or FAIL.
5280 */
5281 static int
5282rettv_list_alloc(rettv)
5283 typval_T *rettv;
5284{
5285 list_T *l = list_alloc();
5286
5287 if (l == NULL)
5288 return FAIL;
5289
5290 rettv->vval.v_list = l;
5291 rettv->v_type = VAR_LIST;
5292 ++l->lv_refcount;
5293 return OK;
5294}
5295
5296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 * Unreference a list: decrement the reference count and free it when it
5298 * becomes zero.
5299 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005300 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005302 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005304 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5305 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306}
5307
5308/*
5309 * Free a list, including all items it points to.
5310 * Ignores the reference count.
5311 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005312 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005314 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315{
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317
Bram Moolenaard9fba312005-06-26 22:34:35 +00005318 /* Avoid that recursive reference to the list frees us again. */
5319 l->lv_refcount = DEL_REFCOUNT;
5320
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005321 /* Remove the list from the list of lists for garbage collection. */
5322 if (l->lv_used_prev == NULL)
5323 first_list = l->lv_used_next;
5324 else
5325 l->lv_used_prev->lv_used_next = l->lv_used_next;
5326 if (l->lv_used_next != NULL)
5327 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5328
Bram Moolenaard9fba312005-06-26 22:34:35 +00005329 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005331 /* Remove the item before deleting it. */
5332 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 listitem_free(item);
5334 }
5335 vim_free(l);
5336}
5337
5338/*
5339 * Allocate a list item.
5340 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005341 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342listitem_alloc()
5343{
Bram Moolenaar33570922005-01-25 22:26:29 +00005344 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345}
5346
5347/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005348 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 */
5350 static void
5351listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 vim_free(item);
5356}
5357
5358/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005359 * Remove a list item from a List and free it. Also clears the value.
5360 */
5361 static void
5362listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005363 list_T *l;
5364 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005365{
5366 list_remove(l, item, item);
5367 listitem_free(item);
5368}
5369
5370/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 * Get the number of items in a list.
5372 */
5373 static long
5374list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005375 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 if (l == NULL)
5378 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005379 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380}
5381
5382/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 * Return TRUE when two lists have exactly the same values.
5384 */
5385 static int
5386list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005387 list_T *l1;
5388 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005389 int ic; /* ignore case for strings */
5390{
Bram Moolenaar33570922005-01-25 22:26:29 +00005391 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005393 if (list_len(l1) != list_len(l2))
5394 return FALSE;
5395
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005396 for (item1 = l1->lv_first, item2 = l2->lv_first;
5397 item1 != NULL && item2 != NULL;
5398 item1 = item1->li_next, item2 = item2->li_next)
5399 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5400 return FALSE;
5401 return item1 == NULL && item2 == NULL;
5402}
5403
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005404#if defined(FEAT_PYTHON) || defined(PROTO)
5405/*
5406 * Return the dictitem that an entry in a hashtable points to.
5407 */
5408 dictitem_T *
5409dict_lookup(hi)
5410 hashitem_T *hi;
5411{
5412 return HI2DI(hi);
5413}
5414#endif
5415
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005416/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005417 * Return TRUE when two dictionaries have exactly the same key/values.
5418 */
5419 static int
5420dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 dict_T *d1;
5422 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005423 int ic; /* ignore case for strings */
5424{
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 hashitem_T *hi;
5426 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005427 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005428
5429 if (dict_len(d1) != dict_len(d2))
5430 return FALSE;
5431
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 todo = d1->dv_hashtab.ht_used;
5433 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005435 if (!HASHITEM_EMPTY(hi))
5436 {
5437 item2 = dict_find(d2, hi->hi_key, -1);
5438 if (item2 == NULL)
5439 return FALSE;
5440 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5441 return FALSE;
5442 --todo;
5443 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005444 }
5445 return TRUE;
5446}
5447
5448/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005449 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005450 * Compares the items just like "==" would compare them, but strings and
5451 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005452 */
5453 static int
5454tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 typval_T *tv1;
5456 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005457 int ic; /* ignore case */
5458{
5459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005460 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005461
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005462 if (tv1->v_type != tv2->v_type)
5463 return FALSE;
5464
5465 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005466 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005467 case VAR_LIST:
5468 /* recursive! */
5469 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5470
5471 case VAR_DICT:
5472 /* recursive! */
5473 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5474
5475 case VAR_FUNC:
5476 return (tv1->vval.v_string != NULL
5477 && tv2->vval.v_string != NULL
5478 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5479
5480 case VAR_NUMBER:
5481 return tv1->vval.v_number == tv2->vval.v_number;
5482
5483 case VAR_STRING:
5484 s1 = get_tv_string_buf(tv1, buf1);
5485 s2 = get_tv_string_buf(tv2, buf2);
5486 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005487 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005488
5489 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005490 return TRUE;
5491}
5492
5493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 * Locate item with index "n" in list "l" and return it.
5495 * A negative index is counted from the end; -1 is the last item.
5496 * Returns NULL when "n" is out of range.
5497 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 long n;
5502{
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 long idx;
5505
5506 if (l == NULL)
5507 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005508
5509 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005511 n = l->lv_len + n;
5512
5513 /* Check for index out of range. */
5514 if (n < 0 || n >= l->lv_len)
5515 return NULL;
5516
5517 /* When there is a cached index may start search from there. */
5518 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005520 if (n < l->lv_idx / 2)
5521 {
5522 /* closest to the start of the list */
5523 item = l->lv_first;
5524 idx = 0;
5525 }
5526 else if (n > (l->lv_idx + l->lv_len) / 2)
5527 {
5528 /* closest to the end of the list */
5529 item = l->lv_last;
5530 idx = l->lv_len - 1;
5531 }
5532 else
5533 {
5534 /* closest to the cached index */
5535 item = l->lv_idx_item;
5536 idx = l->lv_idx;
5537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 else
5540 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005541 if (n < l->lv_len / 2)
5542 {
5543 /* closest to the start of the list */
5544 item = l->lv_first;
5545 idx = 0;
5546 }
5547 else
5548 {
5549 /* closest to the end of the list */
5550 item = l->lv_last;
5551 idx = l->lv_len - 1;
5552 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005554
5555 while (n > idx)
5556 {
5557 /* search forward */
5558 item = item->li_next;
5559 ++idx;
5560 }
5561 while (n < idx)
5562 {
5563 /* search backward */
5564 item = item->li_prev;
5565 --idx;
5566 }
5567
5568 /* cache the used index */
5569 l->lv_idx = idx;
5570 l->lv_idx_item = item;
5571
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 return item;
5573}
5574
5575/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005576 * Get list item "l[idx]" as a number.
5577 */
5578 static long
5579list_find_nr(l, idx, errorp)
5580 list_T *l;
5581 long idx;
5582 int *errorp; /* set to TRUE when something wrong */
5583{
5584 listitem_T *li;
5585
5586 li = list_find(l, idx);
5587 if (li == NULL)
5588 {
5589 if (errorp != NULL)
5590 *errorp = TRUE;
5591 return -1L;
5592 }
5593 return get_tv_number_chk(&li->li_tv, errorp);
5594}
5595
5596/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005597 * Locate "item" list "l" and return its index.
5598 * Returns -1 when "item" is not in the list.
5599 */
5600 static long
5601list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 list_T *l;
5603 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005604{
5605 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005606 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005607
5608 if (l == NULL)
5609 return -1;
5610 idx = 0;
5611 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5612 ++idx;
5613 if (li == NULL)
5614 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005615 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005616}
5617
5618/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 * Append item "item" to the end of list "l".
5620 */
5621 static void
5622list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005623 list_T *l;
5624 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625{
5626 if (l->lv_last == NULL)
5627 {
5628 /* empty list */
5629 l->lv_first = item;
5630 l->lv_last = item;
5631 item->li_prev = NULL;
5632 }
5633 else
5634 {
5635 l->lv_last->li_next = item;
5636 item->li_prev = l->lv_last;
5637 l->lv_last = item;
5638 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005639 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 item->li_next = NULL;
5641}
5642
5643/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005644 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005645 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 */
5647 static int
5648list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005649 list_T *l;
5650 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005652 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653
Bram Moolenaar05159a02005-02-26 23:04:13 +00005654 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005656 copy_tv(tv, &li->li_tv);
5657 list_append(l, li);
5658 return OK;
5659}
5660
5661/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005662 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005663 * Return FAIL when out of memory.
5664 */
5665 int
5666list_append_dict(list, dict)
5667 list_T *list;
5668 dict_T *dict;
5669{
5670 listitem_T *li = listitem_alloc();
5671
5672 if (li == NULL)
5673 return FAIL;
5674 li->li_tv.v_type = VAR_DICT;
5675 li->li_tv.v_lock = 0;
5676 li->li_tv.vval.v_dict = dict;
5677 list_append(list, li);
5678 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 return OK;
5680}
5681
5682/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005683 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005684 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005685 * Returns FAIL when out of memory.
5686 */
5687 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005688list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005689 list_T *l;
5690 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005691 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005692{
5693 listitem_T *li = listitem_alloc();
5694
5695 if (li == NULL)
5696 return FAIL;
5697 list_append(l, li);
5698 li->li_tv.v_type = VAR_STRING;
5699 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005700 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5701 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005702 return FAIL;
5703 return OK;
5704}
5705
5706/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005707 * Append "n" to list "l".
5708 * Returns FAIL when out of memory.
5709 */
5710 static int
5711list_append_number(l, n)
5712 list_T *l;
5713 varnumber_T n;
5714{
5715 listitem_T *li;
5716
5717 li = listitem_alloc();
5718 if (li == NULL)
5719 return FAIL;
5720 li->li_tv.v_type = VAR_NUMBER;
5721 li->li_tv.v_lock = 0;
5722 li->li_tv.vval.v_number = n;
5723 list_append(l, li);
5724 return OK;
5725}
5726
5727/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005729 * If "item" is NULL append at the end.
5730 * Return FAIL when out of memory.
5731 */
5732 static int
5733list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 list_T *l;
5735 typval_T *tv;
5736 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005737{
Bram Moolenaar33570922005-01-25 22:26:29 +00005738 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005739
5740 if (ni == NULL)
5741 return FAIL;
5742 copy_tv(tv, &ni->li_tv);
5743 if (item == NULL)
5744 /* Append new item at end of list. */
5745 list_append(l, ni);
5746 else
5747 {
5748 /* Insert new item before existing item. */
5749 ni->li_prev = item->li_prev;
5750 ni->li_next = item;
5751 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005752 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005753 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005754 ++l->lv_idx;
5755 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005756 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005757 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005758 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005759 l->lv_idx_item = NULL;
5760 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005761 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005762 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005763 }
5764 return OK;
5765}
5766
5767/*
5768 * Extend "l1" with "l2".
5769 * If "bef" is NULL append at the end, otherwise insert before this item.
5770 * Returns FAIL when out of memory.
5771 */
5772 static int
5773list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005774 list_T *l1;
5775 list_T *l2;
5776 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005777{
Bram Moolenaar33570922005-01-25 22:26:29 +00005778 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005779
5780 for (item = l2->lv_first; item != NULL; item = item->li_next)
5781 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5782 return FAIL;
5783 return OK;
5784}
5785
5786/*
5787 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5788 * Return FAIL when out of memory.
5789 */
5790 static int
5791list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005792 list_T *l1;
5793 list_T *l2;
5794 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005795{
Bram Moolenaar33570922005-01-25 22:26:29 +00005796 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005797
5798 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005799 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005800 if (l == NULL)
5801 return FAIL;
5802 tv->v_type = VAR_LIST;
5803 tv->vval.v_list = l;
5804
5805 /* append all items from the second list */
5806 return list_extend(l, l2, NULL);
5807}
5808
5809/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005810 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005812 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 * Returns NULL when out of memory.
5814 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005815 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005816list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005819 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820{
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 list_T *copy;
5822 listitem_T *item;
5823 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824
5825 if (orig == NULL)
5826 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827
5828 copy = list_alloc();
5829 if (copy != NULL)
5830 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005831 if (copyID != 0)
5832 {
5833 /* Do this before adding the items, because one of the items may
5834 * refer back to this list. */
5835 orig->lv_copyID = copyID;
5836 orig->lv_copylist = copy;
5837 }
5838 for (item = orig->lv_first; item != NULL && !got_int;
5839 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 {
5841 ni = listitem_alloc();
5842 if (ni == NULL)
5843 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005844 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005845 {
5846 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5847 {
5848 vim_free(ni);
5849 break;
5850 }
5851 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005853 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 list_append(copy, ni);
5855 }
5856 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005857 if (item != NULL)
5858 {
5859 list_unref(copy);
5860 copy = NULL;
5861 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 }
5863
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 return copy;
5865}
5866
5867/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005868 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005869 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005871 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005872list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005873 list_T *l;
5874 listitem_T *item;
5875 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005876{
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005879 /* notify watchers */
5880 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005883 list_fix_watch(l, ip);
5884 if (ip == item2)
5885 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005887
5888 if (item2->li_next == NULL)
5889 l->lv_last = item->li_prev;
5890 else
5891 item2->li_next->li_prev = item->li_prev;
5892 if (item->li_prev == NULL)
5893 l->lv_first = item2->li_next;
5894 else
5895 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005896 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005897}
5898
5899/*
5900 * Return an allocated string with the string representation of a list.
5901 * May return NULL.
5902 */
5903 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005906 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907{
5908 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909
5910 if (tv->vval.v_list == NULL)
5911 return NULL;
5912 ga_init2(&ga, (int)sizeof(char), 80);
5913 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005914 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005915 {
5916 vim_free(ga.ga_data);
5917 return NULL;
5918 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919 ga_append(&ga, ']');
5920 ga_append(&ga, NUL);
5921 return (char_u *)ga.ga_data;
5922}
5923
5924/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 * Join list "l" into a string in "*gap", using separator "sep".
5926 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005929 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005930list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 char_u *sep;
5934 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005935 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936{
5937 int first = TRUE;
5938 char_u *tofree;
5939 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941 char_u *s;
5942
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005943 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
5945 if (first)
5946 first = FALSE;
5947 else
5948 ga_concat(gap, sep);
5949
5950 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005951 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005953 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 if (s != NULL)
5955 ga_concat(gap, s);
5956 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005957 if (s == NULL)
5958 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005959 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005961}
5962
5963/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005964 * Garbage collection for lists and dictionaries.
5965 *
5966 * We use reference counts to be able to free most items right away when they
5967 * are no longer used. But for composite items it's possible that it becomes
5968 * unused while the reference count is > 0: When there is a recursive
5969 * reference. Example:
5970 * :let l = [1, 2, 3]
5971 * :let d = {9: l}
5972 * :let l[1] = d
5973 *
5974 * Since this is quite unusual we handle this with garbage collection: every
5975 * once in a while find out which lists and dicts are not referenced from any
5976 * variable.
5977 *
5978 * Here is a good reference text about garbage collection (refers to Python
5979 * but it applies to all reference-counting mechanisms):
5980 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005981 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005982
5983/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984 * Do garbage collection for lists and dicts.
5985 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005986 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005987 int
5988garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005989{
5990 dict_T *dd;
5991 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005992 int copyID = ++current_copyID;
5993 buf_T *buf;
5994 win_T *wp;
5995 int i;
5996 funccall_T *fc;
5997 int did_free = FALSE;
5998
5999 /*
6000 * 1. Go through all accessible variables and mark all lists and dicts
6001 * with copyID.
6002 */
6003 /* script-local variables */
6004 for (i = 1; i <= ga_scripts.ga_len; ++i)
6005 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6006
6007 /* buffer-local variables */
6008 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6009 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6010
6011 /* window-local variables */
6012 FOR_ALL_WINDOWS(wp)
6013 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6014
6015 /* global variables */
6016 set_ref_in_ht(&globvarht, copyID);
6017
6018 /* function-local variables */
6019 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6020 {
6021 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6022 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6023 }
6024
6025 /*
6026 * 2. Go through the list of dicts and free items without the copyID.
6027 */
6028 for (dd = first_dict; dd != NULL; )
6029 if (dd->dv_copyID != copyID)
6030 {
6031 dict_free(dd);
6032 did_free = TRUE;
6033
6034 /* restart, next dict may also have been freed */
6035 dd = first_dict;
6036 }
6037 else
6038 dd = dd->dv_used_next;
6039
6040 /*
6041 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006042 * But don't free a list that has a watcher (used in a for loop), these
6043 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006044 */
6045 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006046 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006047 {
6048 list_free(ll);
6049 did_free = TRUE;
6050
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006051 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006052 ll = first_list;
6053 }
6054 else
6055 ll = ll->lv_used_next;
6056
6057 return did_free;
6058}
6059
6060/*
6061 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6062 */
6063 static void
6064set_ref_in_ht(ht, copyID)
6065 hashtab_T *ht;
6066 int copyID;
6067{
6068 int todo;
6069 hashitem_T *hi;
6070
6071 todo = ht->ht_used;
6072 for (hi = ht->ht_array; todo > 0; ++hi)
6073 if (!HASHITEM_EMPTY(hi))
6074 {
6075 --todo;
6076 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6077 }
6078}
6079
6080/*
6081 * Mark all lists and dicts referenced through list "l" with "copyID".
6082 */
6083 static void
6084set_ref_in_list(l, copyID)
6085 list_T *l;
6086 int copyID;
6087{
6088 listitem_T *li;
6089
6090 for (li = l->lv_first; li != NULL; li = li->li_next)
6091 set_ref_in_item(&li->li_tv, copyID);
6092}
6093
6094/*
6095 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6096 */
6097 static void
6098set_ref_in_item(tv, copyID)
6099 typval_T *tv;
6100 int copyID;
6101{
6102 dict_T *dd;
6103 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006104
6105 switch (tv->v_type)
6106 {
6107 case VAR_DICT:
6108 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006109 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006111 /* Didn't see this dict yet. */
6112 dd->dv_copyID = copyID;
6113 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006114 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006116
6117 case VAR_LIST:
6118 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006119 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006120 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* Didn't see this list yet. */
6122 ll->lv_copyID = copyID;
6123 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006124 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006125 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006126 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006127 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006128}
6129
6130/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006131 * Allocate an empty header for a dictionary.
6132 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006133 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006134dict_alloc()
6135{
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006140 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006141 /* Add the list to the hashtable for garbage collection. */
6142 if (first_dict != NULL)
6143 first_dict->dv_used_prev = d;
6144 d->dv_used_next = first_dict;
6145 d->dv_used_prev = NULL;
6146
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006148 d->dv_lock = 0;
6149 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006150 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006151 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006153}
6154
6155/*
6156 * Unreference a Dictionary: decrement the reference count and free it when it
6157 * becomes zero.
6158 */
6159 static void
6160dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006161 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006162{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006163 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6164 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006165}
6166
6167/*
6168 * Free a Dictionary, including all items it contains.
6169 * Ignores the reference count.
6170 */
6171 static void
6172dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006174{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006175 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006177 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006178
Bram Moolenaard9fba312005-06-26 22:34:35 +00006179 /* Avoid that recursive reference to the dict frees us again. */
6180 d->dv_refcount = DEL_REFCOUNT;
6181
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006182 /* Remove the dict from the list of dicts for garbage collection. */
6183 if (d->dv_used_prev == NULL)
6184 first_dict = d->dv_used_next;
6185 else
6186 d->dv_used_prev->dv_used_next = d->dv_used_next;
6187 if (d->dv_used_next != NULL)
6188 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6189
6190 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006191 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 todo = d->dv_hashtab.ht_used;
6193 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006195 if (!HASHITEM_EMPTY(hi))
6196 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006197 /* Remove the item before deleting it, just in case there is
6198 * something recursive causing trouble. */
6199 di = HI2DI(hi);
6200 hash_remove(&d->dv_hashtab, hi);
6201 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006202 --todo;
6203 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006204 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006205 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006206 vim_free(d);
6207}
6208
6209/*
6210 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211 * The "key" is copied to the new item.
6212 * Note that the value of the item "di_tv" still needs to be initialized!
6213 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006214 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006216dictitem_alloc(key)
6217 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006218{
Bram Moolenaar33570922005-01-25 22:26:29 +00006219 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006220
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006223 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006224 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006225 di->di_flags = 0;
6226 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006227 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006228}
6229
6230/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006231 * Make a copy of a Dictionary item.
6232 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006233 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006234dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006236{
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006238
Bram Moolenaar33570922005-01-25 22:26:29 +00006239 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006240 if (di != NULL)
6241 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006242 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006243 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006244 copy_tv(&org->di_tv, &di->di_tv);
6245 }
6246 return di;
6247}
6248
6249/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006250 * Remove item "item" from Dictionary "dict" and free it.
6251 */
6252 static void
6253dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006254 dict_T *dict;
6255 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006256{
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006258
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260 if (HASHITEM_EMPTY(hi))
6261 EMSG2(_(e_intern2), "dictitem_remove()");
6262 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006264 dictitem_free(item);
6265}
6266
6267/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268 * Free a dict item. Also clears the value.
6269 */
6270 static void
6271dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006273{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006274 clear_tv(&item->di_tv);
6275 vim_free(item);
6276}
6277
6278/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006279 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6280 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006281 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006282 * Returns NULL when out of memory.
6283 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006285dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006287 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006288 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006289{
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 dict_T *copy;
6291 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006292 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006294
6295 if (orig == NULL)
6296 return NULL;
6297
6298 copy = dict_alloc();
6299 if (copy != NULL)
6300 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006301 if (copyID != 0)
6302 {
6303 orig->dv_copyID = copyID;
6304 orig->dv_copydict = copy;
6305 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006307 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006309 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006310 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006311 --todo;
6312
6313 di = dictitem_alloc(hi->hi_key);
6314 if (di == NULL)
6315 break;
6316 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006317 {
6318 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6319 copyID) == FAIL)
6320 {
6321 vim_free(di);
6322 break;
6323 }
6324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 else
6326 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6327 if (dict_add(copy, di) == FAIL)
6328 {
6329 dictitem_free(di);
6330 break;
6331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006333 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006334
Bram Moolenaare9a41262005-01-15 22:18:47 +00006335 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006336 if (todo > 0)
6337 {
6338 dict_unref(copy);
6339 copy = NULL;
6340 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006341 }
6342
6343 return copy;
6344}
6345
6346/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006348 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006349 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006350 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 dict_T *d;
6353 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354{
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356}
6357
Bram Moolenaar8c711452005-01-14 21:53:12 +00006358/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006359 * Add a number or string entry to dictionary "d".
6360 * When "str" is NULL use number "nr", otherwise use "str".
6361 * Returns FAIL when out of memory and when key already exists.
6362 */
6363 int
6364dict_add_nr_str(d, key, nr, str)
6365 dict_T *d;
6366 char *key;
6367 long nr;
6368 char_u *str;
6369{
6370 dictitem_T *item;
6371
6372 item = dictitem_alloc((char_u *)key);
6373 if (item == NULL)
6374 return FAIL;
6375 item->di_tv.v_lock = 0;
6376 if (str == NULL)
6377 {
6378 item->di_tv.v_type = VAR_NUMBER;
6379 item->di_tv.vval.v_number = nr;
6380 }
6381 else
6382 {
6383 item->di_tv.v_type = VAR_STRING;
6384 item->di_tv.vval.v_string = vim_strsave(str);
6385 }
6386 if (dict_add(d, item) == FAIL)
6387 {
6388 dictitem_free(item);
6389 return FAIL;
6390 }
6391 return OK;
6392}
6393
6394/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395 * Get the number of items in a Dictionary.
6396 */
6397 static long
6398dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006400{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006401 if (d == NULL)
6402 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006404}
6405
6406/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006407 * Find item "key[len]" in Dictionary "d".
6408 * If "len" is negative use strlen(key).
6409 * Returns NULL when not found.
6410 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006412dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006414 char_u *key;
6415 int len;
6416{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006417#define AKEYLEN 200
6418 char_u buf[AKEYLEN];
6419 char_u *akey;
6420 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006423 if (len < 0)
6424 akey = key;
6425 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006426 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006427 tofree = akey = vim_strnsave(key, len);
6428 if (akey == NULL)
6429 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006430 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006431 else
6432 {
6433 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006434 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006435 akey = buf;
6436 }
6437
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006439 vim_free(tofree);
6440 if (HASHITEM_EMPTY(hi))
6441 return NULL;
6442 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006443}
6444
6445/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006446 * Get a string item from a dictionary.
6447 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006448 * Returns NULL if the entry doesn't exist or out of memory.
6449 */
6450 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006451get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006452 dict_T *d;
6453 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006454 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006455{
6456 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006457 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006458
6459 di = dict_find(d, key, -1);
6460 if (di == NULL)
6461 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006462 s = get_tv_string(&di->di_tv);
6463 if (save && s != NULL)
6464 s = vim_strsave(s);
6465 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006466}
6467
6468/*
6469 * Get a number item from a dictionary.
6470 * Returns 0 if the entry doesn't exist or out of memory.
6471 */
6472 long
6473get_dict_number(d, key)
6474 dict_T *d;
6475 char_u *key;
6476{
6477 dictitem_T *di;
6478
6479 di = dict_find(d, key, -1);
6480 if (di == NULL)
6481 return 0;
6482 return get_tv_number(&di->di_tv);
6483}
6484
6485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006486 * Return an allocated string with the string representation of a Dictionary.
6487 * May return NULL.
6488 */
6489 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006490dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006492 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006493{
6494 garray_T ga;
6495 int first = TRUE;
6496 char_u *tofree;
6497 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006503 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006504 return NULL;
6505 ga_init2(&ga, (int)sizeof(char), 80);
6506 ga_append(&ga, '{');
6507
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006511 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006513 --todo;
6514
6515 if (first)
6516 first = FALSE;
6517 else
6518 ga_concat(&ga, (char_u *)", ");
6519
6520 tofree = string_quote(hi->hi_key, FALSE);
6521 if (tofree != NULL)
6522 {
6523 ga_concat(&ga, tofree);
6524 vim_free(tofree);
6525 }
6526 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006527 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006528 if (s != NULL)
6529 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006530 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (s == NULL)
6532 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006535 if (todo > 0)
6536 {
6537 vim_free(ga.ga_data);
6538 return NULL;
6539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006540
6541 ga_append(&ga, '}');
6542 ga_append(&ga, NUL);
6543 return (char_u *)ga.ga_data;
6544}
6545
6546/*
6547 * Allocate a variable for a Dictionary and fill it from "*arg".
6548 * Return OK or FAIL. Returns NOTDONE for {expr}.
6549 */
6550 static int
6551get_dict_tv(arg, rettv, evaluate)
6552 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006554 int evaluate;
6555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 dict_T *d = NULL;
6557 typval_T tvkey;
6558 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006559 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006561 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006562 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006563
6564 /*
6565 * First check if it's not a curly-braces thing: {expr}.
6566 * Must do this without evaluating, otherwise a function may be called
6567 * twice. Unfortunately this means we need to call eval1() twice for the
6568 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006569 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006570 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 if (*start != '}')
6572 {
6573 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6574 return FAIL;
6575 if (*start == '}')
6576 return NOTDONE;
6577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006578
6579 if (evaluate)
6580 {
6581 d = dict_alloc();
6582 if (d == NULL)
6583 return FAIL;
6584 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006585 tvkey.v_type = VAR_UNKNOWN;
6586 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587
6588 *arg = skipwhite(*arg + 1);
6589 while (**arg != '}' && **arg != NUL)
6590 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006591 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006592 goto failret;
6593 if (**arg != ':')
6594 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006595 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006596 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006597 goto failret;
6598 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006599 key = get_tv_string_buf_chk(&tvkey, buf);
6600 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006602 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6603 if (key != NULL)
6604 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006605 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606 goto failret;
6607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006608
6609 *arg = skipwhite(*arg + 1);
6610 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006612 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006613 goto failret;
6614 }
6615 if (evaluate)
6616 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006617 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006618 if (item != NULL)
6619 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006620 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006621 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006622 clear_tv(&tv);
6623 goto failret;
6624 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006625 item = dictitem_alloc(key);
6626 clear_tv(&tvkey);
6627 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006630 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006631 if (dict_add(d, item) == FAIL)
6632 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006633 }
6634 }
6635
6636 if (**arg == '}')
6637 break;
6638 if (**arg != ',')
6639 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006640 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006641 goto failret;
6642 }
6643 *arg = skipwhite(*arg + 1);
6644 }
6645
6646 if (**arg != '}')
6647 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006648 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006649failret:
6650 if (evaluate)
6651 dict_free(d);
6652 return FAIL;
6653 }
6654
6655 *arg = skipwhite(*arg + 1);
6656 if (evaluate)
6657 {
6658 rettv->v_type = VAR_DICT;
6659 rettv->vval.v_dict = d;
6660 ++d->dv_refcount;
6661 }
6662
6663 return OK;
6664}
6665
Bram Moolenaar8c711452005-01-14 21:53:12 +00006666/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 * Return a string with the string representation of a variable.
6668 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006670 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006671 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 * May return NULL;
6673 */
6674 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006675echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006676 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006678 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006679 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006681 static int recurse = 0;
6682 char_u *r = NULL;
6683
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006685 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006686 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006687 *tofree = NULL;
6688 return NULL;
6689 }
6690 ++recurse;
6691
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 switch (tv->v_type)
6693 {
6694 case VAR_FUNC:
6695 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006696 r = tv->vval.v_string;
6697 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006698
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006700 if (tv->vval.v_list == NULL)
6701 {
6702 *tofree = NULL;
6703 r = NULL;
6704 }
6705 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6706 {
6707 *tofree = NULL;
6708 r = (char_u *)"[...]";
6709 }
6710 else
6711 {
6712 tv->vval.v_list->lv_copyID = copyID;
6713 *tofree = list2string(tv, copyID);
6714 r = *tofree;
6715 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006716 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006717
Bram Moolenaar8c711452005-01-14 21:53:12 +00006718 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006719 if (tv->vval.v_dict == NULL)
6720 {
6721 *tofree = NULL;
6722 r = NULL;
6723 }
6724 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6725 {
6726 *tofree = NULL;
6727 r = (char_u *)"{...}";
6728 }
6729 else
6730 {
6731 tv->vval.v_dict->dv_copyID = copyID;
6732 *tofree = dict2string(tv, copyID);
6733 r = *tofree;
6734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006735 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006737 case VAR_STRING:
6738 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 *tofree = NULL;
6740 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006741 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006742
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006743 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006745 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006747
6748 --recurse;
6749 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006750}
6751
6752/*
6753 * Return a string with the string representation of a variable.
6754 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6755 * "numbuf" is used for a number.
6756 * Puts quotes around strings, so that they can be parsed back by eval().
6757 * May return NULL;
6758 */
6759 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006760tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 char_u **tofree;
6763 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006764 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765{
6766 switch (tv->v_type)
6767 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 case VAR_FUNC:
6769 *tofree = string_quote(tv->vval.v_string, TRUE);
6770 return *tofree;
6771 case VAR_STRING:
6772 *tofree = string_quote(tv->vval.v_string, FALSE);
6773 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006775 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006776 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006777 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006779 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006781 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782}
6783
6784/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006785 * Return string "str" in ' quotes, doubling ' characters.
6786 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788 */
6789 static char_u *
6790string_quote(str, function)
6791 char_u *str;
6792 int function;
6793{
Bram Moolenaar33570922005-01-25 22:26:29 +00006794 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 char_u *p, *r, *s;
6796
Bram Moolenaar33570922005-01-25 22:26:29 +00006797 len = (function ? 13 : 3);
6798 if (str != NULL)
6799 {
6800 len += STRLEN(str);
6801 for (p = str; *p != NUL; mb_ptr_adv(p))
6802 if (*p == '\'')
6803 ++len;
6804 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 s = r = alloc(len);
6806 if (r != NULL)
6807 {
6808 if (function)
6809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006810 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 r += 10;
6812 }
6813 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006814 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006815 if (str != NULL)
6816 for (p = str; *p != NUL; )
6817 {
6818 if (*p == '\'')
6819 *r++ = '\'';
6820 MB_COPY_CHAR(p, r);
6821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 if (function)
6824 *r++ = ')';
6825 *r++ = NUL;
6826 }
6827 return s;
6828}
6829
6830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 * Get the value of an environment variable.
6832 * "arg" is pointing to the '$'. It is advanced to after the name.
6833 * If the environment variable was not set, silently assume it is empty.
6834 * Always return OK.
6835 */
6836 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006837get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 int evaluate;
6841{
6842 char_u *string = NULL;
6843 int len;
6844 int cc;
6845 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006846 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848 ++*arg;
6849 name = *arg;
6850 len = get_env_len(arg);
6851 if (evaluate)
6852 {
6853 if (len != 0)
6854 {
6855 cc = name[len];
6856 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006857 /* first try vim_getenv(), fast for normal environment vars */
6858 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006860 {
6861 if (!mustfree)
6862 string = vim_strsave(string);
6863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 else
6865 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006866 if (mustfree)
6867 vim_free(string);
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 /* next try expanding things like $VIM and ${HOME} */
6870 string = expand_env_save(name - 1);
6871 if (string != NULL && *string == '$')
6872 {
6873 vim_free(string);
6874 string = NULL;
6875 }
6876 }
6877 name[len] = cc;
6878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006879 rettv->v_type = VAR_STRING;
6880 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 }
6882
6883 return OK;
6884}
6885
6886/*
6887 * Array with names and number of arguments of all internal functions
6888 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6889 */
6890static struct fst
6891{
6892 char *f_name; /* function name */
6893 char f_min_argc; /* minimal number of arguments */
6894 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897} functions[] =
6898{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006899 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {"append", 2, 2, f_append},
6901 {"argc", 0, 0, f_argc},
6902 {"argidx", 0, 0, f_argidx},
6903 {"argv", 1, 1, f_argv},
6904 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006905 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {"bufexists", 1, 1, f_bufexists},
6907 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6908 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6909 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6910 {"buflisted", 1, 1, f_buflisted},
6911 {"bufloaded", 1, 1, f_bufloaded},
6912 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006913 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"bufwinnr", 1, 1, f_bufwinnr},
6915 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006916 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00006918 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {"char2nr", 1, 1, f_char2nr},
6920 {"cindent", 1, 1, f_cindent},
6921 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006922#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00006923 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006924 {"complete_add", 1, 1, f_complete_add},
6925 {"complete_check", 0, 0, f_complete_check},
6926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006928 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006929 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006931 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006932 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {"delete", 1, 1, f_delete},
6934 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006935 {"diff_filler", 1, 1, f_diff_filler},
6936 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006937 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006939 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {"eventhandler", 0, 0, f_eventhandler},
6941 {"executable", 1, 1, f_executable},
6942 {"exists", 1, 1, f_exists},
6943 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006944 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6946 {"filereadable", 1, 1, f_filereadable},
6947 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006948 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006949 {"finddir", 1, 3, f_finddir},
6950 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {"fnamemodify", 2, 2, f_fnamemodify},
6952 {"foldclosed", 1, 1, f_foldclosed},
6953 {"foldclosedend", 1, 1, f_foldclosedend},
6954 {"foldlevel", 1, 1, f_foldlevel},
6955 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006956 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006958 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006960 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006961 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {"getbufvar", 2, 2, f_getbufvar},
6963 {"getchar", 0, 1, f_getchar},
6964 {"getcharmod", 0, 0, f_getcharmod},
6965 {"getcmdline", 0, 0, f_getcmdline},
6966 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006967 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006969 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006970 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {"getfsize", 1, 1, f_getfsize},
6972 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006973 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006974 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006975 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006976 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006977 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006978 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {"getregtype", 0, 1, f_getregtype},
6980 {"getwinposx", 0, 0, f_getwinposx},
6981 {"getwinposy", 0, 0, f_getwinposy},
6982 {"getwinvar", 2, 2, f_getwinvar},
6983 {"glob", 1, 1, f_glob},
6984 {"globpath", 2, 2, f_globpath},
6985 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006986 {"has_key", 2, 2, f_has_key},
Bram Moolenaar2c932302006-03-18 21:42:09 +00006987 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6989 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6990 {"histadd", 2, 2, f_histadd},
6991 {"histdel", 1, 2, f_histdel},
6992 {"histget", 1, 2, f_histget},
6993 {"histnr", 1, 1, f_histnr},
6994 {"hlID", 1, 1, f_hlID},
6995 {"hlexists", 1, 1, f_hlexists},
6996 {"hostname", 0, 0, f_hostname},
6997 {"iconv", 3, 3, f_iconv},
6998 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007000 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007002 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"inputrestore", 0, 0, f_inputrestore},
7004 {"inputsave", 0, 0, f_inputsave},
7005 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007006 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007008 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007010 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007013 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {"libcall", 3, 3, f_libcall},
7015 {"libcallnr", 3, 3, f_libcallnr},
7016 {"line", 1, 1, f_line},
7017 {"line2byte", 1, 1, f_line2byte},
7018 {"lispindent", 1, 1, f_lispindent},
7019 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007020 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007021 {"maparg", 1, 3, f_maparg},
7022 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007023 {"match", 2, 4, f_match},
7024 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007025 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007026 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007027 {"max", 1, 1, f_max},
7028 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007029#ifdef vim_mkdir
7030 {"mkdir", 1, 3, f_mkdir},
7031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {"mode", 0, 0, f_mode},
7033 {"nextnonblank", 1, 1, f_nextnonblank},
7034 {"nr2char", 1, 1, f_nr2char},
7035 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007036 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007037 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007039 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007040 {"reltime", 0, 2, f_reltime},
7041 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"remote_expr", 2, 3, f_remote_expr},
7043 {"remote_foreground", 1, 1, f_remote_foreground},
7044 {"remote_peek", 1, 2, f_remote_peek},
7045 {"remote_read", 1, 1, f_remote_read},
7046 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007047 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007049 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007051 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007052 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007053 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007054 {"searchpair", 3, 6, f_searchpair},
7055 {"searchpairpos", 3, 6, f_searchpairpos},
7056 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {"server2client", 2, 2, f_server2client},
7058 {"serverlist", 0, 0, f_serverlist},
7059 {"setbufvar", 3, 3, f_setbufvar},
7060 {"setcmdpos", 1, 1, f_setcmdpos},
7061 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007062 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007063 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007064 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {"setreg", 2, 3, f_setreg},
7066 {"setwinvar", 3, 3, f_setwinvar},
7067 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007068 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007069 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007070 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007071 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007072 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007073 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#ifdef HAVE_STRFTIME
7075 {"strftime", 1, 2, f_strftime},
7076#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007078 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {"strlen", 1, 1, f_strlen},
7080 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007081 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {"strtrans", 1, 1, f_strtrans},
7083 {"submatch", 1, 1, f_submatch},
7084 {"substitute", 4, 4, f_substitute},
7085 {"synID", 3, 3, f_synID},
7086 {"synIDattr", 2, 3, f_synIDattr},
7087 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007088 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007089 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007090 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007091 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007092 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007093 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007095 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {"tolower", 1, 1, f_tolower},
7097 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007098 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {"virtcol", 1, 1, f_virtcol},
7102 {"visualmode", 0, 1, f_visualmode},
7103 {"winbufnr", 1, 1, f_winbufnr},
7104 {"wincol", 0, 0, f_wincol},
7105 {"winheight", 1, 1, f_winheight},
7106 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007107 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007109 {"winrestview", 1, 1, f_winrestview},
7110 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007112 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113};
7114
7115#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7116
7117/*
7118 * Function given to ExpandGeneric() to obtain the list of internal
7119 * or user defined function names.
7120 */
7121 char_u *
7122get_function_name(xp, idx)
7123 expand_T *xp;
7124 int idx;
7125{
7126 static int intidx = -1;
7127 char_u *name;
7128
7129 if (idx == 0)
7130 intidx = -1;
7131 if (intidx < 0)
7132 {
7133 name = get_user_func_name(xp, idx);
7134 if (name != NULL)
7135 return name;
7136 }
7137 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7138 {
7139 STRCPY(IObuff, functions[intidx].f_name);
7140 STRCAT(IObuff, "(");
7141 if (functions[intidx].f_max_argc == 0)
7142 STRCAT(IObuff, ")");
7143 return IObuff;
7144 }
7145
7146 return NULL;
7147}
7148
7149/*
7150 * Function given to ExpandGeneric() to obtain the list of internal or
7151 * user defined variable or function names.
7152 */
7153/*ARGSUSED*/
7154 char_u *
7155get_expr_name(xp, idx)
7156 expand_T *xp;
7157 int idx;
7158{
7159 static int intidx = -1;
7160 char_u *name;
7161
7162 if (idx == 0)
7163 intidx = -1;
7164 if (intidx < 0)
7165 {
7166 name = get_function_name(xp, idx);
7167 if (name != NULL)
7168 return name;
7169 }
7170 return get_user_var_name(xp, ++intidx);
7171}
7172
7173#endif /* FEAT_CMDL_COMPL */
7174
7175/*
7176 * Find internal function in table above.
7177 * Return index, or -1 if not found
7178 */
7179 static int
7180find_internal_func(name)
7181 char_u *name; /* name of the function */
7182{
7183 int first = 0;
7184 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7185 int cmp;
7186 int x;
7187
7188 /*
7189 * Find the function name in the table. Binary search.
7190 */
7191 while (first <= last)
7192 {
7193 x = first + ((unsigned)(last - first) >> 1);
7194 cmp = STRCMP(name, functions[x].f_name);
7195 if (cmp < 0)
7196 last = x - 1;
7197 else if (cmp > 0)
7198 first = x + 1;
7199 else
7200 return x;
7201 }
7202 return -1;
7203}
7204
7205/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7207 * name it contains, otherwise return "name".
7208 */
7209 static char_u *
7210deref_func_name(name, lenp)
7211 char_u *name;
7212 int *lenp;
7213{
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 int cc;
7216
7217 cc = name[*lenp];
7218 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007219 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007220 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007221 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007222 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007224 {
7225 *lenp = 0;
7226 return (char_u *)""; /* just in case */
7227 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 *lenp = STRLEN(v->di_tv.vval.v_string);
7229 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007230 }
7231
7232 return name;
7233}
7234
7235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 * Allocate a variable for the result of a function.
7237 * Return OK or FAIL.
7238 */
7239 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7241 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 char_u *name; /* name of the function */
7243 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 char_u **arg; /* argument, pointing to the '(' */
7246 linenr_T firstline; /* first line of range */
7247 linenr_T lastline; /* last line of range */
7248 int *doesrange; /* return: function handled range */
7249 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 char_u *argp;
7253 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 int argcount = 0; /* number of arguments found */
7256
7257 /*
7258 * Get the arguments.
7259 */
7260 argp = *arg;
7261 while (argcount < MAX_FUNC_ARGS)
7262 {
7263 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7264 if (*argp == ')' || *argp == ',' || *argp == NUL)
7265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7267 {
7268 ret = FAIL;
7269 break;
7270 }
7271 ++argcount;
7272 if (*argp != ',')
7273 break;
7274 }
7275 if (*argp == ')')
7276 ++argp;
7277 else
7278 ret = FAIL;
7279
7280 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007281 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 {
7285 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007286 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007288 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290
7291 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007292 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294 *arg = skipwhite(argp);
7295 return ret;
7296}
7297
7298
7299/*
7300 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007301 * Return OK when the function can't be called, FAIL otherwise.
7302 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 */
7304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007305call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 char_u *name; /* name of the function */
7308 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 linenr_T firstline; /* first line of range */
7313 linenr_T lastline; /* last line of range */
7314 int *doesrange; /* return: function handled range */
7315 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
7318 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319#define ERROR_UNKNOWN 0
7320#define ERROR_TOOMANY 1
7321#define ERROR_TOOFEW 2
7322#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007323#define ERROR_DICT 4
7324#define ERROR_NONE 5
7325#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 int error = ERROR_NONE;
7327 int i;
7328 int llen;
7329 ufunc_T *fp;
7330 int cc;
7331#define FLEN_FIXED 40
7332 char_u fname_buf[FLEN_FIXED + 1];
7333 char_u *fname;
7334
7335 /*
7336 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7337 * Change <SNR>123_name() to K_SNR 123_name().
7338 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7339 */
7340 cc = name[len];
7341 name[len] = NUL;
7342 llen = eval_fname_script(name);
7343 if (llen > 0)
7344 {
7345 fname_buf[0] = K_SPECIAL;
7346 fname_buf[1] = KS_EXTRA;
7347 fname_buf[2] = (int)KE_SNR;
7348 i = 3;
7349 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7350 {
7351 if (current_SID <= 0)
7352 error = ERROR_SCRIPT;
7353 else
7354 {
7355 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7356 i = (int)STRLEN(fname_buf);
7357 }
7358 }
7359 if (i + STRLEN(name + llen) < FLEN_FIXED)
7360 {
7361 STRCPY(fname_buf + i, name + llen);
7362 fname = fname_buf;
7363 }
7364 else
7365 {
7366 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7367 if (fname == NULL)
7368 error = ERROR_OTHER;
7369 else
7370 {
7371 mch_memmove(fname, fname_buf, (size_t)i);
7372 STRCPY(fname + i, name + llen);
7373 }
7374 }
7375 }
7376 else
7377 fname = name;
7378
7379 *doesrange = FALSE;
7380
7381
7382 /* execute the function if no errors detected and executing */
7383 if (evaluate && error == ERROR_NONE)
7384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007385 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 error = ERROR_UNKNOWN;
7387
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007388 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 {
7390 /*
7391 * User defined function.
7392 */
7393 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007396 /* Trigger FuncUndefined event, may load the function. */
7397 if (fp == NULL
7398 && apply_autocmds(EVENT_FUNCUNDEFINED,
7399 fname, fname, TRUE, NULL)
7400 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007402 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 fp = find_func(fname);
7404 }
7405#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007406 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007407 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007408 {
7409 /* loaded a package, search for the function again */
7410 fp = find_func(fname);
7411 }
7412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 if (fp != NULL)
7414 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007415 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007417 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007419 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007421 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 else
7424 {
7425 /*
7426 * Call the user function.
7427 * Save and restore search patterns, script variables and
7428 * redo buffer.
7429 */
7430 save_search_patterns();
7431 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007432 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007433 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007435 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7436 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7437 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007438 /* Function was unreferenced while being used, free it
7439 * now. */
7440 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 restoreRedobuff();
7442 restore_search_patterns();
7443 error = ERROR_NONE;
7444 }
7445 }
7446 }
7447 else
7448 {
7449 /*
7450 * Find the function name in the table, call its implementation.
7451 */
7452 i = find_internal_func(fname);
7453 if (i >= 0)
7454 {
7455 if (argcount < functions[i].f_min_argc)
7456 error = ERROR_TOOFEW;
7457 else if (argcount > functions[i].f_max_argc)
7458 error = ERROR_TOOMANY;
7459 else
7460 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007461 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 error = ERROR_NONE;
7464 }
7465 }
7466 }
7467 /*
7468 * The function call (or "FuncUndefined" autocommand sequence) might
7469 * have been aborted by an error, an interrupt, or an explicitly thrown
7470 * exception that has not been caught so far. This situation can be
7471 * tested for by calling aborting(). For an error in an internal
7472 * function or for the "E132" error in call_user_func(), however, the
7473 * throw point at which the "force_abort" flag (temporarily reset by
7474 * emsg()) is normally updated has not been reached yet. We need to
7475 * update that flag first to make aborting() reliable.
7476 */
7477 update_force_abort();
7478 }
7479 if (error == ERROR_NONE)
7480 ret = OK;
7481
7482 /*
7483 * Report an error unless the argument evaluation or function call has been
7484 * cancelled due to an aborting error, an interrupt, or an exception.
7485 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 if (!aborting())
7487 {
7488 switch (error)
7489 {
7490 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007491 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 break;
7493 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007494 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 break;
7496 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007497 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 name);
7499 break;
7500 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007501 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 name);
7503 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007505 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 name);
7507 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 }
7509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510
7511 name[len] = cc;
7512 if (fname != name && fname != fname_buf)
7513 vim_free(fname);
7514
7515 return ret;
7516}
7517
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007518/*
7519 * Give an error message with a function name. Handle <SNR> things.
7520 */
7521 static void
7522emsg_funcname(msg, name)
7523 char *msg;
7524 char_u *name;
7525{
7526 char_u *p;
7527
7528 if (*name == K_SPECIAL)
7529 p = concat_str((char_u *)"<SNR>", name + 3);
7530 else
7531 p = name;
7532 EMSG2(_(msg), p);
7533 if (p != name)
7534 vim_free(p);
7535}
7536
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537/*********************************************
7538 * Implementation of the built-in functions
7539 */
7540
7541/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007542 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 */
7544 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007545f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 typval_T *argvars;
7547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548{
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007552 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007554 if ((l = argvars[0].vval.v_list) != NULL
7555 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7556 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007558 }
7559 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007560 EMSG(_(e_listreq));
7561}
7562
7563/*
7564 * "append(lnum, string/list)" function
7565 */
7566 static void
7567f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 typval_T *argvars;
7569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007570{
7571 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007572 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 list_T *l = NULL;
7574 listitem_T *li = NULL;
7575 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007576 long added = 0;
7577
Bram Moolenaar0d660222005-01-07 21:51:51 +00007578 lnum = get_tv_lnum(argvars);
7579 if (lnum >= 0
7580 && lnum <= curbuf->b_ml.ml_line_count
7581 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007583 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007585 l = argvars[1].vval.v_list;
7586 if (l == NULL)
7587 return;
7588 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007590 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007591 for (;;)
7592 {
7593 if (l == NULL)
7594 tv = &argvars[1]; /* append a string */
7595 else if (li == NULL)
7596 break; /* end of list */
7597 else
7598 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007599 line = get_tv_string_chk(tv);
7600 if (line == NULL) /* type error */
7601 {
7602 rettv->vval.v_number = 1; /* Failed */
7603 break;
7604 }
7605 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007606 ++added;
7607 if (l == NULL)
7608 break;
7609 li = li->li_next;
7610 }
7611
7612 appended_lines_mark(lnum, added);
7613 if (curwin->w_cursor.lnum > lnum)
7614 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007616 else
7617 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618}
7619
7620/*
7621 * "argc()" function
7622 */
7623/* ARGSUSED */
7624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 typval_T *argvars;
7627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630}
7631
7632/*
7633 * "argidx()" function
7634 */
7635/* ARGSUSED */
7636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007637f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 typval_T *argvars;
7639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642}
7643
7644/*
7645 * "argv(nr)" function
7646 */
7647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007648f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 typval_T *argvars;
7650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
7652 int idx;
7653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007654 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007656 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658 rettv->vval.v_string = NULL;
7659 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660}
7661
7662/*
7663 * "browse(save, title, initdir, default)" function
7664 */
7665/* ARGSUSED */
7666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 typval_T *argvars;
7669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671#ifdef FEAT_BROWSE
7672 int save;
7673 char_u *title;
7674 char_u *initdir;
7675 char_u *defname;
7676 char_u buf[NUMBUFLEN];
7677 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007678 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007680 save = get_tv_number_chk(&argvars[0], &error);
7681 title = get_tv_string_chk(&argvars[1]);
7682 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7683 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007685 if (error || title == NULL || initdir == NULL || defname == NULL)
7686 rettv->vval.v_string = NULL;
7687 else
7688 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007689 do_browse(save ? BROWSE_SAVE : 0,
7690 title, defname, NULL, initdir, NULL, curbuf);
7691#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007692 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007693#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007695}
7696
7697/*
7698 * "browsedir(title, initdir)" function
7699 */
7700/* ARGSUSED */
7701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007702f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 typval_T *argvars;
7704 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007705{
7706#ifdef FEAT_BROWSE
7707 char_u *title;
7708 char_u *initdir;
7709 char_u buf[NUMBUFLEN];
7710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007711 title = get_tv_string_chk(&argvars[0]);
7712 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007714 if (title == NULL || initdir == NULL)
7715 rettv->vval.v_string = NULL;
7716 else
7717 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007718 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
Bram Moolenaar33570922005-01-25 22:26:29 +00007725static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007726
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727/*
7728 * Find a buffer by number or exact name.
7729 */
7730 static buf_T *
7731find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007732 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733{
7734 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007736 if (avar->v_type == VAR_NUMBER)
7737 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007738 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007740 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007741 if (buf == NULL)
7742 {
7743 /* No full path name match, try a match with a URL or a "nofile"
7744 * buffer, these don't use the full path. */
7745 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7746 if (buf->b_fname != NULL
7747 && (path_with_url(buf->b_fname)
7748#ifdef FEAT_QUICKFIX
7749 || bt_nofile(buf)
7750#endif
7751 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007753 break;
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756 return buf;
7757}
7758
7759/*
7760 * "bufexists(expr)" function
7761 */
7762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007763f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *argvars;
7765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768}
7769
7770/*
7771 * "buflisted(expr)" function
7772 */
7773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *argvars;
7776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 buf_T *buf;
7779
7780 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007781 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782}
7783
7784/*
7785 * "bufloaded(expr)" function
7786 */
7787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007788f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 typval_T *argvars;
7790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 buf_T *buf;
7793
7794 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
Bram Moolenaar33570922005-01-25 22:26:29 +00007798static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800/*
7801 * Get buffer by number or pattern.
7802 */
7803 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 int save_magic;
7809 char_u *save_cpo;
7810 buf_T *buf;
7811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 if (tv->v_type == VAR_NUMBER)
7813 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007814 if (tv->v_type != VAR_STRING)
7815 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (name == NULL || *name == NUL)
7817 return curbuf;
7818 if (name[0] == '$' && name[1] == NUL)
7819 return lastbuf;
7820
7821 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7822 save_magic = p_magic;
7823 p_magic = TRUE;
7824 save_cpo = p_cpo;
7825 p_cpo = (char_u *)"";
7826
7827 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7828 TRUE, FALSE));
7829
7830 p_magic = save_magic;
7831 p_cpo = save_cpo;
7832
7833 /* If not found, try expanding the name, like done for bufexists(). */
7834 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837 return buf;
7838}
7839
7840/*
7841 * "bufname(expr)" function
7842 */
7843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 buf_T *buf;
7849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007852 buf = get_buf_tv(&argvars[0]);
7853 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 --emsg_off;
7859}
7860
7861/*
7862 * "bufnr(expr)" function
7863 */
7864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 typval_T *argvars;
7867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007870 int error = FALSE;
7871 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007873 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007876 --emsg_off;
7877
7878 /* If the buffer isn't found and the second argument is not zero create a
7879 * new buffer. */
7880 if (buf == NULL
7881 && argvars[1].v_type != VAR_UNKNOWN
7882 && get_tv_number_chk(&argvars[1], &error) != 0
7883 && !error
7884 && (name = get_tv_string_chk(&argvars[0])) != NULL
7885 && !error)
7886 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7887
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007889 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892}
7893
7894/*
7895 * "bufwinnr(nr)" function
7896 */
7897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007898f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 typval_T *argvars;
7900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902#ifdef FEAT_WINDOWS
7903 win_T *wp;
7904 int winnr = 0;
7905#endif
7906 buf_T *buf;
7907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007908 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007910 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911#ifdef FEAT_WINDOWS
7912 for (wp = firstwin; wp; wp = wp->w_next)
7913 {
7914 ++winnr;
7915 if (wp->w_buffer == buf)
7916 break;
7917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007920 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921#endif
7922 --emsg_off;
7923}
7924
7925/*
7926 * "byte2line(byte)" function
7927 */
7928/*ARGSUSED*/
7929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 typval_T *argvars;
7932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933{
7934#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936#else
7937 long boff = 0;
7938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007939 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007941 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 (linenr_T)0, &boff);
7945#endif
7946}
7947
7948/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007949 * "byteidx()" function
7950 */
7951/*ARGSUSED*/
7952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007953f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 typval_T *argvars;
7955 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007956{
7957#ifdef FEAT_MBYTE
7958 char_u *t;
7959#endif
7960 char_u *str;
7961 long idx;
7962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007963 str = get_tv_string_chk(&argvars[0]);
7964 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007966 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007967 return;
7968
7969#ifdef FEAT_MBYTE
7970 t = str;
7971 for ( ; idx > 0; idx--)
7972 {
7973 if (*t == NUL) /* EOL reached */
7974 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007975 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007978#else
7979 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007980 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007981#endif
7982}
7983
7984/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007985 * "call(func, arglist)" function
7986 */
7987 static void
7988f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 typval_T *argvars;
7990 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991{
7992 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007994 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007995 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007996 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007997 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007998
7999 rettv->vval.v_number = 0;
8000 if (argvars[1].v_type != VAR_LIST)
8001 {
8002 EMSG(_(e_listreq));
8003 return;
8004 }
8005 if (argvars[1].vval.v_list == NULL)
8006 return;
8007
8008 if (argvars[0].v_type == VAR_FUNC)
8009 func = argvars[0].vval.v_string;
8010 else
8011 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008012 if (*func == NUL)
8013 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008014
Bram Moolenaare9a41262005-01-15 22:18:47 +00008015 if (argvars[2].v_type != VAR_UNKNOWN)
8016 {
8017 if (argvars[2].v_type != VAR_DICT)
8018 {
8019 EMSG(_(e_dictreq));
8020 return;
8021 }
8022 selfdict = argvars[2].vval.v_dict;
8023 }
8024
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008025 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8026 item = item->li_next)
8027 {
8028 if (argc == MAX_FUNC_ARGS)
8029 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008030 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008031 break;
8032 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008033 /* Make a copy of each argument. This is needed to be able to set
8034 * v_lock to VAR_FIXED in the copy without changing the original list.
8035 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036 copy_tv(&item->li_tv, &argv[argc++]);
8037 }
8038
8039 if (item == NULL)
8040 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008041 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8042 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008043
8044 /* Free the arguments. */
8045 while (argc > 0)
8046 clear_tv(&argv[--argc]);
8047}
8048
8049/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008050 * "changenr()" function
8051 */
8052/*ARGSUSED*/
8053 static void
8054f_changenr(argvars, rettv)
8055 typval_T *argvars;
8056 typval_T *rettv;
8057{
8058 rettv->vval.v_number = curbuf->b_u_seq_cur;
8059}
8060
8061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 * "char2nr(string)" function
8063 */
8064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 typval_T *argvars;
8067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069#ifdef FEAT_MBYTE
8070 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008071 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 else
8073#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008074 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075}
8076
8077/*
8078 * "cindent(lnum)" function
8079 */
8080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 typval_T *argvars;
8083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085#ifdef FEAT_CINDENT
8086 pos_T pos;
8087 linenr_T lnum;
8088
8089 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8092 {
8093 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 curwin->w_cursor = pos;
8096 }
8097 else
8098#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
8101
8102/*
8103 * "col(string)" function
8104 */
8105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008106f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008107 typval_T *argvars;
8108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109{
8110 colnr_T col = 0;
8111 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008112 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008114 fp = var2fpos(&argvars[0], FALSE, &fnum);
8115 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {
8117 if (fp->col == MAXCOL)
8118 {
8119 /* '> can be MAXCOL, get the length of the line then */
8120 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8121 col = STRLEN(ml_get(fp->lnum)) + 1;
8122 else
8123 col = MAXCOL;
8124 }
8125 else
8126 {
8127 col = fp->col + 1;
8128#ifdef FEAT_VIRTUALEDIT
8129 /* col(".") when the cursor is on the NUL at the end of the line
8130 * because of "coladd" can be seen as an extra column. */
8131 if (virtual_active() && fp == &curwin->w_cursor)
8132 {
8133 char_u *p = ml_get_cursor();
8134
8135 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8136 curwin->w_virtcol - curwin->w_cursor.coladd))
8137 {
8138# ifdef FEAT_MBYTE
8139 int l;
8140
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008141 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 col += l;
8143# else
8144 if (*p != NUL && p[1] == NUL)
8145 ++col;
8146# endif
8147 }
8148 }
8149#endif
8150 }
8151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
Bram Moolenaar572cb562005-08-05 21:35:02 +00008155#if defined(FEAT_INS_EXPAND)
8156/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008157 * "complete()" function
8158 */
8159/*ARGSUSED*/
8160 static void
8161f_complete(argvars, rettv)
8162 typval_T *argvars;
8163 typval_T *rettv;
8164{
8165 int startcol;
8166
8167 if ((State & INSERT) == 0)
8168 {
8169 EMSG(_("E785: complete() can only be used in Insert mode"));
8170 return;
8171 }
8172 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8173 {
8174 EMSG(_(e_invarg));
8175 return;
8176 }
8177
8178 startcol = get_tv_number_chk(&argvars[0], NULL);
8179 if (startcol <= 0)
8180 return;
8181
8182 set_completion(startcol - 1, argvars[1].vval.v_list);
8183}
8184
8185/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008186 * "complete_add()" function
8187 */
8188/*ARGSUSED*/
8189 static void
8190f_complete_add(argvars, rettv)
8191 typval_T *argvars;
8192 typval_T *rettv;
8193{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008194 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008195}
8196
8197/*
8198 * "complete_check()" function
8199 */
8200/*ARGSUSED*/
8201 static void
8202f_complete_check(argvars, rettv)
8203 typval_T *argvars;
8204 typval_T *rettv;
8205{
8206 int saved = RedrawingDisabled;
8207
8208 RedrawingDisabled = 0;
8209 ins_compl_check_keys(0);
8210 rettv->vval.v_number = compl_interrupted;
8211 RedrawingDisabled = saved;
8212}
8213#endif
8214
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215/*
8216 * "confirm(message, buttons[, default [, type]])" function
8217 */
8218/*ARGSUSED*/
8219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008220f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 typval_T *argvars;
8222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
8224#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8225 char_u *message;
8226 char_u *buttons = NULL;
8227 char_u buf[NUMBUFLEN];
8228 char_u buf2[NUMBUFLEN];
8229 int def = 1;
8230 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008231 char_u *typestr;
8232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008234 message = get_tv_string_chk(&argvars[0]);
8235 if (message == NULL)
8236 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008237 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008239 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8240 if (buttons == NULL)
8241 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008244 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008245 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008247 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8248 if (typestr == NULL)
8249 error = TRUE;
8250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252 switch (TOUPPER_ASC(*typestr))
8253 {
8254 case 'E': type = VIM_ERROR; break;
8255 case 'Q': type = VIM_QUESTION; break;
8256 case 'I': type = VIM_INFO; break;
8257 case 'W': type = VIM_WARNING; break;
8258 case 'G': type = VIM_GENERIC; break;
8259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
8261 }
8262 }
8263 }
8264
8265 if (buttons == NULL || *buttons == NUL)
8266 buttons = (char_u *)_("&Ok");
8267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008268 if (error)
8269 rettv->vval.v_number = 0;
8270 else
8271 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 def, NULL);
8273#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008274 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275#endif
8276}
8277
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278/*
8279 * "copy()" function
8280 */
8281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008282f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 typval_T *argvars;
8284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008286 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
8289/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008290 * "count()" function
8291 */
8292 static void
8293f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008294 typval_T *argvars;
8295 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008296{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008297 long n = 0;
8298 int ic = FALSE;
8299
Bram Moolenaare9a41262005-01-15 22:18:47 +00008300 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008301 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008302 listitem_T *li;
8303 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008304 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008305
Bram Moolenaare9a41262005-01-15 22:18:47 +00008306 if ((l = argvars[0].vval.v_list) != NULL)
8307 {
8308 li = l->lv_first;
8309 if (argvars[2].v_type != VAR_UNKNOWN)
8310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 int error = FALSE;
8312
8313 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008314 if (argvars[3].v_type != VAR_UNKNOWN)
8315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316 idx = get_tv_number_chk(&argvars[3], &error);
8317 if (!error)
8318 {
8319 li = list_find(l, idx);
8320 if (li == NULL)
8321 EMSGN(_(e_listidx), idx);
8322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008324 if (error)
8325 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008326 }
8327
8328 for ( ; li != NULL; li = li->li_next)
8329 if (tv_equal(&li->li_tv, &argvars[1], ic))
8330 ++n;
8331 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008332 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333 else if (argvars[0].v_type == VAR_DICT)
8334 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 int todo;
8336 dict_T *d;
8337 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008338
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008339 if ((d = argvars[0].vval.v_dict) != NULL)
8340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008341 int error = FALSE;
8342
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343 if (argvars[2].v_type != VAR_UNKNOWN)
8344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008345 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008346 if (argvars[3].v_type != VAR_UNKNOWN)
8347 EMSG(_(e_invarg));
8348 }
8349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008350 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008352 {
8353 if (!HASHITEM_EMPTY(hi))
8354 {
8355 --todo;
8356 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8357 ++n;
8358 }
8359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008360 }
8361 }
8362 else
8363 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008364 rettv->vval.v_number = n;
8365}
8366
8367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8369 *
8370 * Checks the existence of a cscope connection.
8371 */
8372/*ARGSUSED*/
8373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008374f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 typval_T *argvars;
8376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377{
8378#ifdef FEAT_CSCOPE
8379 int num = 0;
8380 char_u *dbpath = NULL;
8381 char_u *prepend = NULL;
8382 char_u buf[NUMBUFLEN];
8383
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008384 if (argvars[0].v_type != VAR_UNKNOWN
8385 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 num = (int)get_tv_number(&argvars[0]);
8388 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 }
8392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008395 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396#endif
8397}
8398
8399/*
8400 * "cursor(lnum, col)" function
8401 *
8402 * Moves the cursor to the specified line and column
8403 */
8404/*ARGSUSED*/
8405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008407 typval_T *argvars;
8408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008411#ifdef FEAT_VIRTUALEDIT
8412 long coladd = 0;
8413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
Bram Moolenaara5525202006-03-02 22:52:09 +00008415 if (argvars[1].v_type == VAR_UNKNOWN)
8416 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008417 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008418
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008419 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008420 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008421 line = pos.lnum;
8422 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008423#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008424 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008425#endif
8426 }
8427 else
8428 {
8429 line = get_tv_lnum(argvars);
8430 col = get_tv_number_chk(&argvars[1], NULL);
8431#ifdef FEAT_VIRTUALEDIT
8432 if (argvars[2].v_type != VAR_UNKNOWN)
8433 coladd = get_tv_number_chk(&argvars[2], NULL);
8434#endif
8435 }
8436 if (line < 0 || col < 0
8437#ifdef FEAT_VIRTUALEDIT
8438 || coladd < 0
8439#endif
8440 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 if (line > 0)
8443 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 if (col > 0)
8445 curwin->w_cursor.col = col - 1;
8446#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008447 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448#endif
8449
8450 /* Make sure the cursor is in a valid position. */
8451 check_cursor();
8452#ifdef FEAT_MBYTE
8453 /* Correct cursor for multi-byte character. */
8454 if (has_mbyte)
8455 mb_adjust_cursor();
8456#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008457
8458 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459}
8460
8461/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008462 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 */
8464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008465f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 typval_T *argvars;
8467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008469 int noref = 0;
8470
8471 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008473 if (noref < 0 || noref > 1)
8474 EMSG(_(e_invarg));
8475 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008476 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477}
8478
8479/*
8480 * "delete()" function
8481 */
8482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008484 typval_T *argvars;
8485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491}
8492
8493/*
8494 * "did_filetype()" function
8495 */
8496/*ARGSUSED*/
8497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008499 typval_T *argvars;
8500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506#endif
8507}
8508
8509/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008510 * "diff_filler()" function
8511 */
8512/*ARGSUSED*/
8513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 typval_T *argvars;
8516 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008517{
8518#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008520#endif
8521}
8522
8523/*
8524 * "diff_hlID()" function
8525 */
8526/*ARGSUSED*/
8527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008528f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 typval_T *argvars;
8530 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008531{
8532#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008534 static linenr_T prev_lnum = 0;
8535 static int changedtick = 0;
8536 static int fnum = 0;
8537 static int change_start = 0;
8538 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008539 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008540 int filler_lines;
8541 int col;
8542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008543 if (lnum < 0) /* ignore type error in {lnum} arg */
8544 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008545 if (lnum != prev_lnum
8546 || changedtick != curbuf->b_changedtick
8547 || fnum != curbuf->b_fnum)
8548 {
8549 /* New line, buffer, change: need to get the values. */
8550 filler_lines = diff_check(curwin, lnum);
8551 if (filler_lines < 0)
8552 {
8553 if (filler_lines == -1)
8554 {
8555 change_start = MAXCOL;
8556 change_end = -1;
8557 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8558 hlID = HLF_ADD; /* added line */
8559 else
8560 hlID = HLF_CHD; /* changed line */
8561 }
8562 else
8563 hlID = HLF_ADD; /* added line */
8564 }
8565 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008566 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008567 prev_lnum = lnum;
8568 changedtick = curbuf->b_changedtick;
8569 fnum = curbuf->b_fnum;
8570 }
8571
8572 if (hlID == HLF_CHD || hlID == HLF_TXD)
8573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008574 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008575 if (col >= change_start && col <= change_end)
8576 hlID = HLF_TXD; /* changed text */
8577 else
8578 hlID = HLF_CHD; /* changed line */
8579 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008580 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008581#endif
8582}
8583
8584/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008585 * "empty({expr})" function
8586 */
8587 static void
8588f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008589 typval_T *argvars;
8590 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008591{
8592 int n;
8593
8594 switch (argvars[0].v_type)
8595 {
8596 case VAR_STRING:
8597 case VAR_FUNC:
8598 n = argvars[0].vval.v_string == NULL
8599 || *argvars[0].vval.v_string == NUL;
8600 break;
8601 case VAR_NUMBER:
8602 n = argvars[0].vval.v_number == 0;
8603 break;
8604 case VAR_LIST:
8605 n = argvars[0].vval.v_list == NULL
8606 || argvars[0].vval.v_list->lv_first == NULL;
8607 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008608 case VAR_DICT:
8609 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008611 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008612 default:
8613 EMSG2(_(e_intern2), "f_empty()");
8614 n = 0;
8615 }
8616
8617 rettv->vval.v_number = n;
8618}
8619
8620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 * "escape({string}, {chars})" function
8622 */
8623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008625 typval_T *argvars;
8626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 char_u buf[NUMBUFLEN];
8629
Bram Moolenaar758711c2005-02-02 23:11:38 +00008630 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8631 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633}
8634
8635/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008636 * "eval()" function
8637 */
8638/*ARGSUSED*/
8639 static void
8640f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 typval_T *argvars;
8642 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008643{
8644 char_u *s;
8645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 s = get_tv_string_chk(&argvars[0]);
8647 if (s != NULL)
8648 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008650 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8651 {
8652 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008653 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008654 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008655 else if (*s != NUL)
8656 EMSG(_(e_trailing));
8657}
8658
8659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 * "eventhandler()" function
8661 */
8662/*ARGSUSED*/
8663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008664f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 typval_T *argvars;
8666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669}
8670
8671/*
8672 * "executable()" function
8673 */
8674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 typval_T *argvars;
8677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682/*
8683 * "exists()" function
8684 */
8685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008686f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 typval_T *argvars;
8688 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689{
8690 char_u *p;
8691 char_u *name;
8692 int n = FALSE;
8693 int len = 0;
8694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 if (*p == '$') /* environment variable */
8697 {
8698 /* first try "normal" environment variables (fast) */
8699 if (mch_getenv(p + 1) != NULL)
8700 n = TRUE;
8701 else
8702 {
8703 /* try expanding things like $VIM and ${HOME} */
8704 p = expand_env_save(p);
8705 if (p != NULL && *p != '$')
8706 n = TRUE;
8707 vim_free(p);
8708 }
8709 }
8710 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 else if (*p == '*') /* internal or user defined function */
8713 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 }
8716 else if (*p == ':')
8717 {
8718 n = cmd_exists(p + 1);
8719 }
8720 else if (*p == '#')
8721 {
8722#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008723 if (p[1] == '#')
8724 n = autocmd_supported(p + 2);
8725 else
8726 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727#endif
8728 }
8729 else /* internal variable */
8730 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008731 char_u *tofree;
8732 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008734 /* get_name_len() takes care of expanding curly braces */
8735 name = p;
8736 len = get_name_len(&p, &tofree, TRUE, FALSE);
8737 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008739 if (tofree != NULL)
8740 name = tofree;
8741 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8742 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008744 /* handle d.key, l[idx], f(expr) */
8745 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8746 if (n)
8747 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 }
8749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008751 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 }
8753
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
8758 * "expand()" function
8759 */
8760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 char_u *s;
8766 int len;
8767 char_u *errormsg;
8768 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8769 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008770 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772 rettv->v_type = VAR_STRING;
8773 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 if (*s == '%' || *s == '#' || *s == '<')
8775 {
8776 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 --emsg_off;
8779 }
8780 else
8781 {
8782 /* When the optional second argument is non-zero, don't remove matches
8783 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 if (argvars[1].v_type != VAR_UNKNOWN
8785 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008787 if (!error)
8788 {
8789 ExpandInit(&xpc);
8790 xpc.xp_context = EXPAND_FILES;
8791 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8792 ExpandCleanup(&xpc);
8793 }
8794 else
8795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 }
8797}
8798
8799/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008800 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008801 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008802 */
8803 static void
8804f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008805 typval_T *argvars;
8806 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008807{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008808 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008809 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008810 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 list_T *l1, *l2;
8812 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008813 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008815
Bram Moolenaare9a41262005-01-15 22:18:47 +00008816 l1 = argvars[0].vval.v_list;
8817 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008818 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8819 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 {
8821 if (argvars[2].v_type != VAR_UNKNOWN)
8822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 before = get_tv_number_chk(&argvars[2], &error);
8824 if (error)
8825 return; /* type error; errmsg already given */
8826
Bram Moolenaar758711c2005-02-02 23:11:38 +00008827 if (before == l1->lv_len)
8828 item = NULL;
8829 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008830 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008831 item = list_find(l1, before);
8832 if (item == NULL)
8833 {
8834 EMSGN(_(e_listidx), before);
8835 return;
8836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008837 }
8838 }
8839 else
8840 item = NULL;
8841 list_extend(l1, l2, item);
8842
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 copy_tv(&argvars[0], rettv);
8844 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008845 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008846 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8847 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 dict_T *d1, *d2;
8849 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008850 char_u *action;
8851 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008853 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854
8855 d1 = argvars[0].vval.v_dict;
8856 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008857 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8858 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008859 {
8860 /* Check the third argument. */
8861 if (argvars[2].v_type != VAR_UNKNOWN)
8862 {
8863 static char *(av[]) = {"keep", "force", "error"};
8864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008865 action = get_tv_string_chk(&argvars[2]);
8866 if (action == NULL)
8867 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008868 for (i = 0; i < 3; ++i)
8869 if (STRCMP(action, av[i]) == 0)
8870 break;
8871 if (i == 3)
8872 {
8873 EMSGN(_(e_invarg2), action);
8874 return;
8875 }
8876 }
8877 else
8878 action = (char_u *)"force";
8879
8880 /* Go over all entries in the second dict and add them to the
8881 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 todo = d2->dv_hashtab.ht_used;
8883 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008885 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008886 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008887 --todo;
8888 di1 = dict_find(d1, hi2->hi_key, -1);
8889 if (di1 == NULL)
8890 {
8891 di1 = dictitem_copy(HI2DI(hi2));
8892 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8893 dictitem_free(di1);
8894 }
8895 else if (*action == 'e')
8896 {
8897 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8898 break;
8899 }
8900 else if (*action == 'f')
8901 {
8902 clear_tv(&di1->di_tv);
8903 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008905 }
8906 }
8907
Bram Moolenaare9a41262005-01-15 22:18:47 +00008908 copy_tv(&argvars[0], rettv);
8909 }
8910 }
8911 else
8912 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008913}
8914
8915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 * "filereadable()" function
8917 */
8918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 typval_T *argvars;
8921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 FILE *fd;
8924 char_u *p;
8925 int n;
8926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8929 {
8930 n = TRUE;
8931 fclose(fd);
8932 }
8933 else
8934 n = FALSE;
8935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937}
8938
8939/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008940 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 * rights to write into.
8942 */
8943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *argvars;
8946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008948 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949}
8950
Bram Moolenaar33570922005-01-25 22:26:29 +00008951static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008952
8953 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008954findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 typval_T *argvars;
8956 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008957 int dir;
8958{
8959#ifdef FEAT_SEARCHPATH
8960 char_u *fname;
8961 char_u *fresult = NULL;
8962 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8963 char_u *p;
8964 char_u pathbuf[NUMBUFLEN];
8965 int count = 1;
8966 int first = TRUE;
8967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008969
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008970 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8973 if (p == NULL)
8974 count = -1; /* error */
8975 else
8976 {
8977 if (*p != NUL)
8978 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008980 if (argvars[2].v_type != VAR_UNKNOWN)
8981 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8982 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008983 }
8984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 do
8988 {
8989 vim_free(fresult);
8990 fresult = find_file_in_path_option(first ? fname : NULL,
8991 first ? (int)STRLEN(fname) : 0,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008992 0, first, path, dir, NULL,
8993 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994 first = FALSE;
8995 } while (--count > 0 && fresult != NULL);
8996 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008999#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009001#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009003}
9004
Bram Moolenaar33570922005-01-25 22:26:29 +00009005static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9006static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009007
9008/*
9009 * Implementation of map() and filter().
9010 */
9011 static void
9012filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 typval_T *argvars;
9014 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009015 int map;
9016{
9017 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009018 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 listitem_T *li, *nli;
9020 list_T *l = NULL;
9021 dictitem_T *di;
9022 hashtab_T *ht;
9023 hashitem_T *hi;
9024 dict_T *d = NULL;
9025 typval_T save_val;
9026 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009027 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009028 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009029 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009030 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009031
9032 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009033 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009034 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009035 if ((l = argvars[0].vval.v_list) == NULL
9036 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 return;
9038 }
9039 else if (argvars[0].v_type == VAR_DICT)
9040 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009041 if ((d = argvars[0].vval.v_dict) == NULL
9042 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 return;
9044 }
9045 else
9046 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009047 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048 return;
9049 }
9050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 expr = get_tv_string_buf_chk(&argvars[1], buf);
9052 /* On type errors, the preceding call has already displayed an error
9053 * message. Avoid a misleading error message for an empty string that
9054 * was not passed as argument. */
9055 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009057 prepare_vimvar(VV_VAL, &save_val);
9058 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009059
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009060 /* We reset "did_emsg" to be able to detect whether an error
9061 * occurred during evaluation of the expression. */
9062 save_did_emsg = did_emsg;
9063 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 prepare_vimvar(VV_KEY, &save_key);
9068 vimvars[VV_KEY].vv_type = VAR_STRING;
9069
9070 ht = &d->dv_hashtab;
9071 hash_lock(ht);
9072 todo = ht->ht_used;
9073 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 if (!HASHITEM_EMPTY(hi))
9076 {
9077 --todo;
9078 di = HI2DI(hi);
9079 if (tv_check_lock(di->di_tv.v_lock, msg))
9080 break;
9081 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009082 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009083 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009084 break;
9085 if (!map && rem)
9086 dictitem_remove(d, di);
9087 clear_tv(&vimvars[VV_KEY].vv_tv);
9088 }
9089 }
9090 hash_unlock(ht);
9091
9092 restore_vimvar(VV_KEY, &save_key);
9093 }
9094 else
9095 {
9096 for (li = l->lv_first; li != NULL; li = nli)
9097 {
9098 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009099 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009100 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009101 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009102 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009103 break;
9104 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009106 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009107 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009109 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009110
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009111 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009112 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009113
9114 copy_tv(&argvars[0], rettv);
9115}
9116
9117 static int
9118filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009119 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009120 char_u *expr;
9121 int map;
9122 int *remp;
9123{
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009125 char_u *s;
9126
Bram Moolenaar33570922005-01-25 22:26:29 +00009127 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009128 s = expr;
9129 if (eval1(&s, &rettv, TRUE) == FAIL)
9130 return FAIL;
9131 if (*s != NUL) /* check for trailing chars after expr */
9132 {
9133 EMSG2(_(e_invexpr2), s);
9134 return FAIL;
9135 }
9136 if (map)
9137 {
9138 /* map(): replace the list item value */
9139 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009140 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009141 *tv = rettv;
9142 }
9143 else
9144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 int error = FALSE;
9146
Bram Moolenaare9a41262005-01-15 22:18:47 +00009147 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009148 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009149 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 /* On type error, nothing has been removed; return FAIL to stop the
9151 * loop. The error message was given by get_tv_number_chk(). */
9152 if (error)
9153 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009154 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009156 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009157}
9158
9159/*
9160 * "filter()" function
9161 */
9162 static void
9163f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009166{
9167 filter_map(argvars, rettv, FALSE);
9168}
9169
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009170/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009171 * "finddir({fname}[, {path}[, {count}]])" function
9172 */
9173 static void
9174f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009175 typval_T *argvars;
9176 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009177{
9178 findfilendir(argvars, rettv, TRUE);
9179}
9180
9181/*
9182 * "findfile({fname}[, {path}[, {count}]])" function
9183 */
9184 static void
9185f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009186 typval_T *argvars;
9187 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009188{
9189 findfilendir(argvars, rettv, FALSE);
9190}
9191
9192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 * "fnamemodify({fname}, {mods})" function
9194 */
9195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009197 typval_T *argvars;
9198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 char_u *fname;
9201 char_u *mods;
9202 int usedlen = 0;
9203 int len;
9204 char_u *fbuf = NULL;
9205 char_u buf[NUMBUFLEN];
9206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 fname = get_tv_string_chk(&argvars[0]);
9208 mods = get_tv_string_buf_chk(&argvars[1], buf);
9209 if (fname == NULL || mods == NULL)
9210 fname = NULL;
9211 else
9212 {
9213 len = (int)STRLEN(fname);
9214 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009221 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 vim_free(fbuf);
9223}
9224
Bram Moolenaar33570922005-01-25 22:26:29 +00009225static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
9227/*
9228 * "foldclosed()" function
9229 */
9230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 typval_T *argvars;
9233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 int end;
9235{
9236#ifdef FEAT_FOLDING
9237 linenr_T lnum;
9238 linenr_T first, last;
9239
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9242 {
9243 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9244 {
9245 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009246 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009248 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 return;
9250 }
9251 }
9252#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254}
9255
9256/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009257 * "foldclosed()" function
9258 */
9259 static void
9260f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009261 typval_T *argvars;
9262 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009263{
9264 foldclosed_both(argvars, rettv, FALSE);
9265}
9266
9267/*
9268 * "foldclosedend()" function
9269 */
9270 static void
9271f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009272 typval_T *argvars;
9273 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009274{
9275 foldclosed_both(argvars, rettv, TRUE);
9276}
9277
9278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 * "foldlevel()" function
9280 */
9281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009283 typval_T *argvars;
9284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286#ifdef FEAT_FOLDING
9287 linenr_T lnum;
9288
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 else
9293#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295}
9296
9297/*
9298 * "foldtext()" function
9299 */
9300/*ARGSUSED*/
9301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009302f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009303 typval_T *argvars;
9304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306#ifdef FEAT_FOLDING
9307 linenr_T lnum;
9308 char_u *s;
9309 char_u *r;
9310 int len;
9311 char *txt;
9312#endif
9313
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009314 rettv->v_type = VAR_STRING;
9315 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009317 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9318 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9319 <= curbuf->b_ml.ml_line_count
9320 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 {
9322 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009323 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9324 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
9326 if (!linewhite(lnum))
9327 break;
9328 ++lnum;
9329 }
9330
9331 /* Find interesting text in this line. */
9332 s = skipwhite(ml_get(lnum));
9333 /* skip C comment-start */
9334 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009337 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009338 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009339 {
9340 s = skipwhite(ml_get(lnum + 1));
9341 if (*s == '*')
9342 s = skipwhite(s + 1);
9343 }
9344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 txt = _("+-%s%3ld lines: ");
9346 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 + 20 /* for %3ld */
9349 + STRLEN(s))); /* concatenated */
9350 if (r != NULL)
9351 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009352 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9353 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9354 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 len = (int)STRLEN(r);
9356 STRCAT(r, s);
9357 /* remove 'foldmarker' and 'commentstring' */
9358 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009359 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 }
9361 }
9362#endif
9363}
9364
9365/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009366 * "foldtextresult(lnum)" function
9367 */
9368/*ARGSUSED*/
9369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009371 typval_T *argvars;
9372 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009373{
9374#ifdef FEAT_FOLDING
9375 linenr_T lnum;
9376 char_u *text;
9377 char_u buf[51];
9378 foldinfo_T foldinfo;
9379 int fold_count;
9380#endif
9381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382 rettv->v_type = VAR_STRING;
9383 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009384#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009385 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 /* treat illegal types and illegal string values for {lnum} the same */
9387 if (lnum < 0)
9388 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009389 fold_count = foldedCount(curwin, lnum, &foldinfo);
9390 if (fold_count > 0)
9391 {
9392 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9393 &foldinfo, buf);
9394 if (text == buf)
9395 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009397 }
9398#endif
9399}
9400
9401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 * "foreground()" function
9403 */
9404/*ARGSUSED*/
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009407 typval_T *argvars;
9408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411#ifdef FEAT_GUI
9412 if (gui.in_use)
9413 gui_mch_set_foreground();
9414#else
9415# ifdef WIN32
9416 win32_set_foreground();
9417# endif
9418#endif
9419}
9420
9421/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009422 * "function()" function
9423 */
9424/*ARGSUSED*/
9425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009427 typval_T *argvars;
9428 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009429{
9430 char_u *s;
9431
Bram Moolenaara7043832005-01-21 11:56:39 +00009432 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009434 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009435 EMSG2(_(e_invarg2), s);
9436 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009437 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009438 else
9439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->vval.v_string = vim_strsave(s);
9441 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009442 }
9443}
9444
9445/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009446 * "garbagecollect()" function
9447 */
9448/*ARGSUSED*/
9449 static void
9450f_garbagecollect(argvars, rettv)
9451 typval_T *argvars;
9452 typval_T *rettv;
9453{
9454 garbage_collect();
9455}
9456
9457/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009458 * "get()" function
9459 */
9460 static void
9461f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009462 typval_T *argvars;
9463 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009464{
Bram Moolenaar33570922005-01-25 22:26:29 +00009465 listitem_T *li;
9466 list_T *l;
9467 dictitem_T *di;
9468 dict_T *d;
9469 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009470
Bram Moolenaare9a41262005-01-15 22:18:47 +00009471 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009472 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009473 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 int error = FALSE;
9476
9477 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9478 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009479 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009480 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009482 else if (argvars[0].v_type == VAR_DICT)
9483 {
9484 if ((d = argvars[0].vval.v_dict) != NULL)
9485 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009486 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009487 if (di != NULL)
9488 tv = &di->di_tv;
9489 }
9490 }
9491 else
9492 EMSG2(_(e_listdictarg), "get()");
9493
9494 if (tv == NULL)
9495 {
9496 if (argvars[2].v_type == VAR_UNKNOWN)
9497 rettv->vval.v_number = 0;
9498 else
9499 copy_tv(&argvars[2], rettv);
9500 }
9501 else
9502 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009503}
9504
Bram Moolenaar342337a2005-07-21 21:11:17 +00009505static 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 +00009506
9507/*
9508 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009509 * Return a range (from start to end) of lines in rettv from the specified
9510 * buffer.
9511 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009512 */
9513 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009514get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009515 buf_T *buf;
9516 linenr_T start;
9517 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009518 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009519 typval_T *rettv;
9520{
9521 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009522
Bram Moolenaar342337a2005-07-21 21:11:17 +00009523 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009524 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009525 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009526 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009527 }
9528 else
9529 rettv->vval.v_number = 0;
9530
9531 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9532 return;
9533
9534 if (!retlist)
9535 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009536 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9537 p = ml_get_buf(buf, start, FALSE);
9538 else
9539 p = (char_u *)"";
9540
9541 rettv->v_type = VAR_STRING;
9542 rettv->vval.v_string = vim_strsave(p);
9543 }
9544 else
9545 {
9546 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009547 return;
9548
9549 if (start < 1)
9550 start = 1;
9551 if (end > buf->b_ml.ml_line_count)
9552 end = buf->b_ml.ml_line_count;
9553 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009554 if (list_append_string(rettv->vval.v_list,
9555 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009556 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009557 }
9558}
9559
9560/*
9561 * "getbufline()" function
9562 */
9563 static void
9564f_getbufline(argvars, rettv)
9565 typval_T *argvars;
9566 typval_T *rettv;
9567{
9568 linenr_T lnum;
9569 linenr_T end;
9570 buf_T *buf;
9571
9572 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9573 ++emsg_off;
9574 buf = get_buf_tv(&argvars[0]);
9575 --emsg_off;
9576
Bram Moolenaar661b1822005-07-28 22:36:45 +00009577 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009578 if (argvars[2].v_type == VAR_UNKNOWN)
9579 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009580 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009581 end = get_tv_lnum_buf(&argvars[2], buf);
9582
Bram Moolenaar342337a2005-07-21 21:11:17 +00009583 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009584}
9585
Bram Moolenaar0d660222005-01-07 21:51:51 +00009586/*
9587 * "getbufvar()" function
9588 */
9589 static void
9590f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009591 typval_T *argvars;
9592 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009593{
9594 buf_T *buf;
9595 buf_T *save_curbuf;
9596 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009599 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9600 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009601 ++emsg_off;
9602 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009603
9604 rettv->v_type = VAR_STRING;
9605 rettv->vval.v_string = NULL;
9606
9607 if (buf != NULL && varname != NULL)
9608 {
9609 if (*varname == '&') /* buffer-local-option */
9610 {
9611 /* set curbuf to be our buf, temporarily */
9612 save_curbuf = curbuf;
9613 curbuf = buf;
9614
9615 get_option_tv(&varname, rettv, TRUE);
9616
9617 /* restore previous notion of curbuf */
9618 curbuf = save_curbuf;
9619 }
9620 else
9621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 if (*varname == NUL)
9623 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9624 * scope prefix before the NUL byte is required by
9625 * find_var_in_ht(). */
9626 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009627 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009628 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009629 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009631 }
9632 }
9633
9634 --emsg_off;
9635}
9636
9637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 * "getchar()" function
9639 */
9640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 typval_T *argvars;
9643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647
9648 ++no_mapping;
9649 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009650 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 /* getchar(): blocking wait. */
9652 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009653 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 /* getchar(1): only check if char avail */
9655 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 else if (error || vpeekc() == NUL)
9657 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 n = 0;
9659 else
9660 /* getchar(0) and char avail: return char */
9661 n = safe_vgetc();
9662 --no_mapping;
9663 --allow_keys;
9664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 if (IS_SPECIAL(n) || mod_mask != 0)
9667 {
9668 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9669 int i = 0;
9670
9671 /* Turn a special key into three bytes, plus modifier. */
9672 if (mod_mask != 0)
9673 {
9674 temp[i++] = K_SPECIAL;
9675 temp[i++] = KS_MODIFIER;
9676 temp[i++] = mod_mask;
9677 }
9678 if (IS_SPECIAL(n))
9679 {
9680 temp[i++] = K_SPECIAL;
9681 temp[i++] = K_SECOND(n);
9682 temp[i++] = K_THIRD(n);
9683 }
9684#ifdef FEAT_MBYTE
9685 else if (has_mbyte)
9686 i += (*mb_char2bytes)(n, temp + i);
9687#endif
9688 else
9689 temp[i++] = n;
9690 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 rettv->v_type = VAR_STRING;
9692 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 }
9694}
9695
9696/*
9697 * "getcharmod()" function
9698 */
9699/*ARGSUSED*/
9700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009702 typval_T *argvars;
9703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706}
9707
9708/*
9709 * "getcmdline()" function
9710 */
9711/*ARGSUSED*/
9712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_getcmdline(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->v_type = VAR_STRING;
9718 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719}
9720
9721/*
9722 * "getcmdpos()" function
9723 */
9724/*ARGSUSED*/
9725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009727 typval_T *argvars;
9728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009734 * "getcmdtype()" function
9735 */
9736/*ARGSUSED*/
9737 static void
9738f_getcmdtype(argvars, rettv)
9739 typval_T *argvars;
9740 typval_T *rettv;
9741{
9742 rettv->v_type = VAR_STRING;
9743 rettv->vval.v_string = alloc(2);
9744 if (rettv->vval.v_string != NULL)
9745 {
9746 rettv->vval.v_string[0] = get_cmdline_type();
9747 rettv->vval.v_string[1] = NUL;
9748 }
9749}
9750
9751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 * "getcwd()" function
9753 */
9754/*ARGSUSED*/
9755 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009757 typval_T *argvars;
9758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760 char_u cwd[MAXPATHL];
9761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 else
9766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009769 if (rettv->vval.v_string != NULL)
9770 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771#endif
9772 }
9773}
9774
9775/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009776 * "getfontname()" function
9777 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009778/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009781 typval_T *argvars;
9782 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009784 rettv->v_type = VAR_STRING;
9785 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009786#ifdef FEAT_GUI
9787 if (gui.in_use)
9788 {
9789 GuiFont font;
9790 char_u *name = NULL;
9791
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009792 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009793 {
9794 /* Get the "Normal" font. Either the name saved by
9795 * hl_set_font_name() or from the font ID. */
9796 font = gui.norm_font;
9797 name = hl_get_font_name();
9798 }
9799 else
9800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009802 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9803 return;
9804 font = gui_mch_get_font(name, FALSE);
9805 if (font == NOFONT)
9806 return; /* Invalid font name, return empty string. */
9807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009809 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009810 gui_mch_free_font(font);
9811 }
9812#endif
9813}
9814
9815/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009816 * "getfperm({fname})" function
9817 */
9818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009820 typval_T *argvars;
9821 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009822{
9823 char_u *fname;
9824 struct stat st;
9825 char_u *perm = NULL;
9826 char_u flags[] = "rwx";
9827 int i;
9828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009832 if (mch_stat((char *)fname, &st) >= 0)
9833 {
9834 perm = vim_strsave((char_u *)"---------");
9835 if (perm != NULL)
9836 {
9837 for (i = 0; i < 9; i++)
9838 {
9839 if (st.st_mode & (1 << (8 - i)))
9840 perm[i] = flags[i % 3];
9841 }
9842 }
9843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009845}
9846
9847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 * "getfsize({fname})" function
9849 */
9850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009852 typval_T *argvars;
9853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854{
9855 char_u *fname;
9856 struct stat st;
9857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861
9862 if (mch_stat((char *)fname, &st) >= 0)
9863 {
9864 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 }
9869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871}
9872
9873/*
9874 * "getftime({fname})" function
9875 */
9876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009878 typval_T *argvars;
9879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880{
9881 char_u *fname;
9882 struct stat st;
9883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885
9886 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890}
9891
9892/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009893 * "getftype({fname})" function
9894 */
9895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009896f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 typval_T *argvars;
9898 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009899{
9900 char_u *fname;
9901 struct stat st;
9902 char_u *type = NULL;
9903 char *t;
9904
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009905 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009906
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009908 if (mch_lstat((char *)fname, &st) >= 0)
9909 {
9910#ifdef S_ISREG
9911 if (S_ISREG(st.st_mode))
9912 t = "file";
9913 else if (S_ISDIR(st.st_mode))
9914 t = "dir";
9915# ifdef S_ISLNK
9916 else if (S_ISLNK(st.st_mode))
9917 t = "link";
9918# endif
9919# ifdef S_ISBLK
9920 else if (S_ISBLK(st.st_mode))
9921 t = "bdev";
9922# endif
9923# ifdef S_ISCHR
9924 else if (S_ISCHR(st.st_mode))
9925 t = "cdev";
9926# endif
9927# ifdef S_ISFIFO
9928 else if (S_ISFIFO(st.st_mode))
9929 t = "fifo";
9930# endif
9931# ifdef S_ISSOCK
9932 else if (S_ISSOCK(st.st_mode))
9933 t = "fifo";
9934# endif
9935 else
9936 t = "other";
9937#else
9938# ifdef S_IFMT
9939 switch (st.st_mode & S_IFMT)
9940 {
9941 case S_IFREG: t = "file"; break;
9942 case S_IFDIR: t = "dir"; break;
9943# ifdef S_IFLNK
9944 case S_IFLNK: t = "link"; break;
9945# endif
9946# ifdef S_IFBLK
9947 case S_IFBLK: t = "bdev"; break;
9948# endif
9949# ifdef S_IFCHR
9950 case S_IFCHR: t = "cdev"; break;
9951# endif
9952# ifdef S_IFIFO
9953 case S_IFIFO: t = "fifo"; break;
9954# endif
9955# ifdef S_IFSOCK
9956 case S_IFSOCK: t = "socket"; break;
9957# endif
9958 default: t = "other";
9959 }
9960# else
9961 if (mch_isdir(fname))
9962 t = "dir";
9963 else
9964 t = "file";
9965# endif
9966#endif
9967 type = vim_strsave((char_u *)t);
9968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009970}
9971
9972/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009973 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009974 */
9975 static void
9976f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009977 typval_T *argvars;
9978 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009979{
9980 linenr_T lnum;
9981 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009982 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009983
9984 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009985 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009986 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009987 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009988 retlist = FALSE;
9989 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009990 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009991 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009992 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009993 retlist = TRUE;
9994 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009995
Bram Moolenaar342337a2005-07-21 21:11:17 +00009996 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009997}
9998
9999/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010000 * "getpos(string)" function
10001 */
10002 static void
10003f_getpos(argvars, rettv)
10004 typval_T *argvars;
10005 typval_T *rettv;
10006{
10007 pos_T *fp;
10008 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010009 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010010
10011 if (rettv_list_alloc(rettv) == OK)
10012 {
10013 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010014 fp = var2fpos(&argvars[0], TRUE, &fnum);
10015 if (fnum != -1)
10016 list_append_number(l, (varnumber_T)fnum);
10017 else
10018 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010019 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10020 : (varnumber_T)0);
10021 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
10022 : (varnumber_T)0);
10023 list_append_number(l,
10024#ifdef FEAT_VIRTUALEDIT
10025 (fp != NULL) ? (varnumber_T)fp->coladd :
10026#endif
10027 (varnumber_T)0);
10028 }
10029 else
10030 rettv->vval.v_number = FALSE;
10031}
10032
10033/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010034 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010035 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010036/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010037 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010038f_getqflist(argvars, rettv)
10039 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010040 typval_T *rettv;
10041{
10042#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010043 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010044#endif
10045
10046 rettv->vval.v_number = FALSE;
10047#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010048 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010049 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010050 wp = NULL;
10051 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10052 {
10053 wp = find_win_by_nr(&argvars[0]);
10054 if (wp == NULL)
10055 return;
10056 }
10057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010058 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010059 }
10060#endif
10061}
10062
10063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 * "getreg()" function
10065 */
10066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010068 typval_T *argvars;
10069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070{
10071 char_u *strregname;
10072 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010073 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010076 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 strregname = get_tv_string_chk(&argvars[0]);
10079 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010080 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010084 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 regname = (strregname == NULL ? '"' : *strregname);
10086 if (regname == 0)
10087 regname = '"';
10088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 rettv->vval.v_string = error ? NULL :
10091 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092}
10093
10094/*
10095 * "getregtype()" function
10096 */
10097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010099 typval_T *argvars;
10100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 char_u *strregname;
10103 int regname;
10104 char_u buf[NUMBUFLEN + 2];
10105 long reglen = 0;
10106
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010107 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010108 {
10109 strregname = get_tv_string_chk(&argvars[0]);
10110 if (strregname == NULL) /* type error; errmsg already given */
10111 {
10112 rettv->v_type = VAR_STRING;
10113 rettv->vval.v_string = NULL;
10114 return;
10115 }
10116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 else
10118 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010119 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
10121 regname = (strregname == NULL ? '"' : *strregname);
10122 if (regname == 0)
10123 regname = '"';
10124
10125 buf[0] = NUL;
10126 buf[1] = NUL;
10127 switch (get_reg_type(regname, &reglen))
10128 {
10129 case MLINE: buf[0] = 'V'; break;
10130 case MCHAR: buf[0] = 'v'; break;
10131#ifdef FEAT_VISUAL
10132 case MBLOCK:
10133 buf[0] = Ctrl_V;
10134 sprintf((char *)buf + 1, "%ld", reglen + 1);
10135 break;
10136#endif
10137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010138 rettv->v_type = VAR_STRING;
10139 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140}
10141
10142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 * "getwinposx()" function
10144 */
10145/*ARGSUSED*/
10146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010148 typval_T *argvars;
10149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152#ifdef FEAT_GUI
10153 if (gui.in_use)
10154 {
10155 int x, y;
10156
10157 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010158 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 }
10160#endif
10161}
10162
10163/*
10164 * "getwinposy()" function
10165 */
10166/*ARGSUSED*/
10167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010169 typval_T *argvars;
10170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173#ifdef FEAT_GUI
10174 if (gui.in_use)
10175 {
10176 int x, y;
10177
10178 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 }
10181#endif
10182}
10183
Bram Moolenaara40058a2005-07-11 22:42:07 +000010184 static win_T *
10185find_win_by_nr(vp)
10186 typval_T *vp;
10187{
10188#ifdef FEAT_WINDOWS
10189 win_T *wp;
10190#endif
10191 int nr;
10192
10193 nr = get_tv_number_chk(vp, NULL);
10194
10195#ifdef FEAT_WINDOWS
10196 if (nr < 0)
10197 return NULL;
10198 if (nr == 0)
10199 return curwin;
10200
10201 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10202 if (--nr <= 0)
10203 break;
10204 return wp;
10205#else
10206 if (nr == 0 || nr == 1)
10207 return curwin;
10208 return NULL;
10209#endif
10210}
10211
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212/*
10213 * "getwinvar()" function
10214 */
10215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010217 typval_T *argvars;
10218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219{
10220 win_T *win, *oldcurwin;
10221 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 varname = get_tv_string_chk(&argvars[1]);
10226 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 rettv->v_type = VAR_STRING;
10229 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
10231 if (win != NULL && varname != NULL)
10232 {
10233 if (*varname == '&') /* window-local-option */
10234 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010235 /* Set curwin to be our win, temporarily. Also set curbuf, so
10236 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 oldcurwin = curwin;
10238 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010239 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242
10243 /* restore previous notion of curwin */
10244 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010245 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 }
10247 else
10248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 if (*varname == NUL)
10250 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10251 * scope prefix before the NUL byte is required by
10252 * find_var_in_ht(). */
10253 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010255 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010257 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 }
10259 }
10260
10261 --emsg_off;
10262}
10263
10264/*
10265 * "glob()" function
10266 */
10267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010269 typval_T *argvars;
10270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271{
10272 expand_T xpc;
10273
10274 ExpandInit(&xpc);
10275 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010276 rettv->v_type = VAR_STRING;
10277 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10279 ExpandCleanup(&xpc);
10280}
10281
10282/*
10283 * "globpath()" function
10284 */
10285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010286f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 typval_T *argvars;
10288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289{
10290 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010294 if (file == NULL)
10295 rettv->vval.v_string = NULL;
10296 else
10297 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298}
10299
10300/*
10301 * "has()" function
10302 */
10303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010305 typval_T *argvars;
10306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307{
10308 int i;
10309 char_u *name;
10310 int n = FALSE;
10311 static char *(has_list[]) =
10312 {
10313#ifdef AMIGA
10314 "amiga",
10315# ifdef FEAT_ARP
10316 "arp",
10317# endif
10318#endif
10319#ifdef __BEOS__
10320 "beos",
10321#endif
10322#ifdef MSDOS
10323# ifdef DJGPP
10324 "dos32",
10325# else
10326 "dos16",
10327# endif
10328#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010329#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 "mac",
10331#endif
10332#if defined(MACOS_X_UNIX)
10333 "macunix",
10334#endif
10335#ifdef OS2
10336 "os2",
10337#endif
10338#ifdef __QNX__
10339 "qnx",
10340#endif
10341#ifdef RISCOS
10342 "riscos",
10343#endif
10344#ifdef UNIX
10345 "unix",
10346#endif
10347#ifdef VMS
10348 "vms",
10349#endif
10350#ifdef WIN16
10351 "win16",
10352#endif
10353#ifdef WIN32
10354 "win32",
10355#endif
10356#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10357 "win32unix",
10358#endif
10359#ifdef WIN64
10360 "win64",
10361#endif
10362#ifdef EBCDIC
10363 "ebcdic",
10364#endif
10365#ifndef CASE_INSENSITIVE_FILENAME
10366 "fname_case",
10367#endif
10368#ifdef FEAT_ARABIC
10369 "arabic",
10370#endif
10371#ifdef FEAT_AUTOCMD
10372 "autocmd",
10373#endif
10374#ifdef FEAT_BEVAL
10375 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010376# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10377 "balloon_multiline",
10378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379#endif
10380#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10381 "builtin_terms",
10382# ifdef ALL_BUILTIN_TCAPS
10383 "all_builtin_terms",
10384# endif
10385#endif
10386#ifdef FEAT_BYTEOFF
10387 "byte_offset",
10388#endif
10389#ifdef FEAT_CINDENT
10390 "cindent",
10391#endif
10392#ifdef FEAT_CLIENTSERVER
10393 "clientserver",
10394#endif
10395#ifdef FEAT_CLIPBOARD
10396 "clipboard",
10397#endif
10398#ifdef FEAT_CMDL_COMPL
10399 "cmdline_compl",
10400#endif
10401#ifdef FEAT_CMDHIST
10402 "cmdline_hist",
10403#endif
10404#ifdef FEAT_COMMENTS
10405 "comments",
10406#endif
10407#ifdef FEAT_CRYPT
10408 "cryptv",
10409#endif
10410#ifdef FEAT_CSCOPE
10411 "cscope",
10412#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010413#ifdef CURSOR_SHAPE
10414 "cursorshape",
10415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416#ifdef DEBUG
10417 "debug",
10418#endif
10419#ifdef FEAT_CON_DIALOG
10420 "dialog_con",
10421#endif
10422#ifdef FEAT_GUI_DIALOG
10423 "dialog_gui",
10424#endif
10425#ifdef FEAT_DIFF
10426 "diff",
10427#endif
10428#ifdef FEAT_DIGRAPHS
10429 "digraphs",
10430#endif
10431#ifdef FEAT_DND
10432 "dnd",
10433#endif
10434#ifdef FEAT_EMACS_TAGS
10435 "emacs_tags",
10436#endif
10437 "eval", /* always present, of course! */
10438#ifdef FEAT_EX_EXTRA
10439 "ex_extra",
10440#endif
10441#ifdef FEAT_SEARCH_EXTRA
10442 "extra_search",
10443#endif
10444#ifdef FEAT_FKMAP
10445 "farsi",
10446#endif
10447#ifdef FEAT_SEARCHPATH
10448 "file_in_path",
10449#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010450#if defined(UNIX) && !defined(USE_SYSTEM)
10451 "filterpipe",
10452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453#ifdef FEAT_FIND_ID
10454 "find_in_path",
10455#endif
10456#ifdef FEAT_FOLDING
10457 "folding",
10458#endif
10459#ifdef FEAT_FOOTER
10460 "footer",
10461#endif
10462#if !defined(USE_SYSTEM) && defined(UNIX)
10463 "fork",
10464#endif
10465#ifdef FEAT_GETTEXT
10466 "gettext",
10467#endif
10468#ifdef FEAT_GUI
10469 "gui",
10470#endif
10471#ifdef FEAT_GUI_ATHENA
10472# ifdef FEAT_GUI_NEXTAW
10473 "gui_neXtaw",
10474# else
10475 "gui_athena",
10476# endif
10477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478#ifdef FEAT_GUI_GTK
10479 "gui_gtk",
10480# ifdef HAVE_GTK2
10481 "gui_gtk2",
10482# endif
10483#endif
10484#ifdef FEAT_GUI_MAC
10485 "gui_mac",
10486#endif
10487#ifdef FEAT_GUI_MOTIF
10488 "gui_motif",
10489#endif
10490#ifdef FEAT_GUI_PHOTON
10491 "gui_photon",
10492#endif
10493#ifdef FEAT_GUI_W16
10494 "gui_win16",
10495#endif
10496#ifdef FEAT_GUI_W32
10497 "gui_win32",
10498#endif
10499#ifdef FEAT_HANGULIN
10500 "hangul_input",
10501#endif
10502#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10503 "iconv",
10504#endif
10505#ifdef FEAT_INS_EXPAND
10506 "insert_expand",
10507#endif
10508#ifdef FEAT_JUMPLIST
10509 "jumplist",
10510#endif
10511#ifdef FEAT_KEYMAP
10512 "keymap",
10513#endif
10514#ifdef FEAT_LANGMAP
10515 "langmap",
10516#endif
10517#ifdef FEAT_LIBCALL
10518 "libcall",
10519#endif
10520#ifdef FEAT_LINEBREAK
10521 "linebreak",
10522#endif
10523#ifdef FEAT_LISP
10524 "lispindent",
10525#endif
10526#ifdef FEAT_LISTCMDS
10527 "listcmds",
10528#endif
10529#ifdef FEAT_LOCALMAP
10530 "localmap",
10531#endif
10532#ifdef FEAT_MENU
10533 "menu",
10534#endif
10535#ifdef FEAT_SESSION
10536 "mksession",
10537#endif
10538#ifdef FEAT_MODIFY_FNAME
10539 "modify_fname",
10540#endif
10541#ifdef FEAT_MOUSE
10542 "mouse",
10543#endif
10544#ifdef FEAT_MOUSESHAPE
10545 "mouseshape",
10546#endif
10547#if defined(UNIX) || defined(VMS)
10548# ifdef FEAT_MOUSE_DEC
10549 "mouse_dec",
10550# endif
10551# ifdef FEAT_MOUSE_GPM
10552 "mouse_gpm",
10553# endif
10554# ifdef FEAT_MOUSE_JSB
10555 "mouse_jsbterm",
10556# endif
10557# ifdef FEAT_MOUSE_NET
10558 "mouse_netterm",
10559# endif
10560# ifdef FEAT_MOUSE_PTERM
10561 "mouse_pterm",
10562# endif
10563# ifdef FEAT_MOUSE_XTERM
10564 "mouse_xterm",
10565# endif
10566#endif
10567#ifdef FEAT_MBYTE
10568 "multi_byte",
10569#endif
10570#ifdef FEAT_MBYTE_IME
10571 "multi_byte_ime",
10572#endif
10573#ifdef FEAT_MULTI_LANG
10574 "multi_lang",
10575#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010576#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010577#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010578 "mzscheme",
10579#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581#ifdef FEAT_OLE
10582 "ole",
10583#endif
10584#ifdef FEAT_OSFILETYPE
10585 "osfiletype",
10586#endif
10587#ifdef FEAT_PATH_EXTRA
10588 "path_extra",
10589#endif
10590#ifdef FEAT_PERL
10591#ifndef DYNAMIC_PERL
10592 "perl",
10593#endif
10594#endif
10595#ifdef FEAT_PYTHON
10596#ifndef DYNAMIC_PYTHON
10597 "python",
10598#endif
10599#endif
10600#ifdef FEAT_POSTSCRIPT
10601 "postscript",
10602#endif
10603#ifdef FEAT_PRINTER
10604 "printer",
10605#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010606#ifdef FEAT_PROFILE
10607 "profile",
10608#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000010609#ifdef FEAT_RELTIME
10610 "reltime",
10611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612#ifdef FEAT_QUICKFIX
10613 "quickfix",
10614#endif
10615#ifdef FEAT_RIGHTLEFT
10616 "rightleft",
10617#endif
10618#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10619 "ruby",
10620#endif
10621#ifdef FEAT_SCROLLBIND
10622 "scrollbind",
10623#endif
10624#ifdef FEAT_CMDL_INFO
10625 "showcmd",
10626 "cmdline_info",
10627#endif
10628#ifdef FEAT_SIGNS
10629 "signs",
10630#endif
10631#ifdef FEAT_SMARTINDENT
10632 "smartindent",
10633#endif
10634#ifdef FEAT_SNIFF
10635 "sniff",
10636#endif
10637#ifdef FEAT_STL_OPT
10638 "statusline",
10639#endif
10640#ifdef FEAT_SUN_WORKSHOP
10641 "sun_workshop",
10642#endif
10643#ifdef FEAT_NETBEANS_INTG
10644 "netbeans_intg",
10645#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000010646#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010647 "spell",
10648#endif
10649#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 "syntax",
10651#endif
10652#if defined(USE_SYSTEM) || !defined(UNIX)
10653 "system",
10654#endif
10655#ifdef FEAT_TAG_BINS
10656 "tag_binary",
10657#endif
10658#ifdef FEAT_TAG_OLDSTATIC
10659 "tag_old_static",
10660#endif
10661#ifdef FEAT_TAG_ANYWHITE
10662 "tag_any_white",
10663#endif
10664#ifdef FEAT_TCL
10665# ifndef DYNAMIC_TCL
10666 "tcl",
10667# endif
10668#endif
10669#ifdef TERMINFO
10670 "terminfo",
10671#endif
10672#ifdef FEAT_TERMRESPONSE
10673 "termresponse",
10674#endif
10675#ifdef FEAT_TEXTOBJ
10676 "textobjects",
10677#endif
10678#ifdef HAVE_TGETENT
10679 "tgetent",
10680#endif
10681#ifdef FEAT_TITLE
10682 "title",
10683#endif
10684#ifdef FEAT_TOOLBAR
10685 "toolbar",
10686#endif
10687#ifdef FEAT_USR_CMDS
10688 "user-commands", /* was accidentally included in 5.4 */
10689 "user_commands",
10690#endif
10691#ifdef FEAT_VIMINFO
10692 "viminfo",
10693#endif
10694#ifdef FEAT_VERTSPLIT
10695 "vertsplit",
10696#endif
10697#ifdef FEAT_VIRTUALEDIT
10698 "virtualedit",
10699#endif
10700#ifdef FEAT_VISUAL
10701 "visual",
10702#endif
10703#ifdef FEAT_VISUALEXTRA
10704 "visualextra",
10705#endif
10706#ifdef FEAT_VREPLACE
10707 "vreplace",
10708#endif
10709#ifdef FEAT_WILDIGN
10710 "wildignore",
10711#endif
10712#ifdef FEAT_WILDMENU
10713 "wildmenu",
10714#endif
10715#ifdef FEAT_WINDOWS
10716 "windows",
10717#endif
10718#ifdef FEAT_WAK
10719 "winaltkeys",
10720#endif
10721#ifdef FEAT_WRITEBACKUP
10722 "writebackup",
10723#endif
10724#ifdef FEAT_XIM
10725 "xim",
10726#endif
10727#ifdef FEAT_XFONTSET
10728 "xfontset",
10729#endif
10730#ifdef USE_XSMP
10731 "xsmp",
10732#endif
10733#ifdef USE_XSMP_INTERACT
10734 "xsmp_interact",
10735#endif
10736#ifdef FEAT_XCLIPBOARD
10737 "xterm_clipboard",
10738#endif
10739#ifdef FEAT_XTERM_SAVE
10740 "xterm_save",
10741#endif
10742#if defined(UNIX) && defined(FEAT_X11)
10743 "X11",
10744#endif
10745 NULL
10746 };
10747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 for (i = 0; has_list[i] != NULL; ++i)
10750 if (STRICMP(name, has_list[i]) == 0)
10751 {
10752 n = TRUE;
10753 break;
10754 }
10755
10756 if (n == FALSE)
10757 {
10758 if (STRNICMP(name, "patch", 5) == 0)
10759 n = has_patch(atoi((char *)name + 5));
10760 else if (STRICMP(name, "vim_starting") == 0)
10761 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010762#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10763 else if (STRICMP(name, "balloon_multiline") == 0)
10764 n = multiline_balloon_available();
10765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766#ifdef DYNAMIC_TCL
10767 else if (STRICMP(name, "tcl") == 0)
10768 n = tcl_enabled(FALSE);
10769#endif
10770#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10771 else if (STRICMP(name, "iconv") == 0)
10772 n = iconv_enabled(FALSE);
10773#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010774#ifdef DYNAMIC_MZSCHEME
10775 else if (STRICMP(name, "mzscheme") == 0)
10776 n = mzscheme_enabled(FALSE);
10777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#ifdef DYNAMIC_RUBY
10779 else if (STRICMP(name, "ruby") == 0)
10780 n = ruby_enabled(FALSE);
10781#endif
10782#ifdef DYNAMIC_PYTHON
10783 else if (STRICMP(name, "python") == 0)
10784 n = python_enabled(FALSE);
10785#endif
10786#ifdef DYNAMIC_PERL
10787 else if (STRICMP(name, "perl") == 0)
10788 n = perl_enabled(FALSE);
10789#endif
10790#ifdef FEAT_GUI
10791 else if (STRICMP(name, "gui_running") == 0)
10792 n = (gui.in_use || gui.starting);
10793# ifdef FEAT_GUI_W32
10794 else if (STRICMP(name, "gui_win32s") == 0)
10795 n = gui_is_win32s();
10796# endif
10797# ifdef FEAT_BROWSE
10798 else if (STRICMP(name, "browse") == 0)
10799 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10800# endif
10801#endif
10802#ifdef FEAT_SYN_HL
10803 else if (STRICMP(name, "syntax_items") == 0)
10804 n = syntax_present(curbuf);
10805#endif
10806#if defined(WIN3264)
10807 else if (STRICMP(name, "win95") == 0)
10808 n = mch_windows95();
10809#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010810#ifdef FEAT_NETBEANS_INTG
10811 else if (STRICMP(name, "netbeans_enabled") == 0)
10812 n = usingNetbeans;
10813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 }
10815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817}
10818
10819/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010820 * "has_key()" function
10821 */
10822 static void
10823f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 typval_T *argvars;
10825 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826{
10827 rettv->vval.v_number = 0;
10828 if (argvars[0].v_type != VAR_DICT)
10829 {
10830 EMSG(_(e_dictreq));
10831 return;
10832 }
10833 if (argvars[0].vval.v_dict == NULL)
10834 return;
10835
10836 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010837 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838}
10839
10840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 * "hasmapto()" function
10842 */
10843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010845 typval_T *argvars;
10846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
10848 char_u *name;
10849 char_u *mode;
10850 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000010851 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010854 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 mode = (char_u *)"nvo";
10856 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000010857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010858 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000010859 if (argvars[2].v_type != VAR_UNKNOWN)
10860 abbr = get_tv_number(&argvars[2]);
10861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862
Bram Moolenaar2c932302006-03-18 21:42:09 +000010863 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867}
10868
10869/*
10870 * "histadd()" function
10871 */
10872/*ARGSUSED*/
10873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010875 typval_T *argvars;
10876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877{
10878#ifdef FEAT_CMDHIST
10879 int histype;
10880 char_u *str;
10881 char_u buf[NUMBUFLEN];
10882#endif
10883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 if (check_restricted() || check_secure())
10886 return;
10887#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010888 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10889 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 if (histype >= 0)
10891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 if (*str != NUL)
10894 {
10895 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 return;
10898 }
10899 }
10900#endif
10901}
10902
10903/*
10904 * "histdel()" function
10905 */
10906/*ARGSUSED*/
10907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010909 typval_T *argvars;
10910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
10912#ifdef FEAT_CMDHIST
10913 int n;
10914 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10918 if (str == NULL)
10919 n = 0;
10920 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010923 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010925 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 else
10928 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 get_tv_string_buf(&argvars[1], buf));
10931 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#endif
10935}
10936
10937/*
10938 * "histget()" function
10939 */
10940/*ARGSUSED*/
10941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010943 typval_T *argvars;
10944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
10946#ifdef FEAT_CMDHIST
10947 int type;
10948 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010949 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010951 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10952 if (str == NULL)
10953 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 {
10956 type = get_histtype(str);
10957 if (argvars[1].v_type == VAR_UNKNOWN)
10958 idx = get_history_idx(type);
10959 else
10960 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10961 /* -1 on type error */
10962 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968}
10969
10970/*
10971 * "histnr()" function
10972 */
10973/*ARGSUSED*/
10974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *argvars;
10977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978{
10979 int i;
10980
10981#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010982 char_u *history = get_tv_string_chk(&argvars[0]);
10983
10984 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 if (i >= HIST_CMD && i < HIST_COUNT)
10986 i = get_history_idx(i);
10987 else
10988#endif
10989 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991}
10992
10993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 * "highlightID(name)" function
10995 */
10996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010997f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010998 typval_T *argvars;
10999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002}
11003
11004/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005 * "highlight_exists()" function
11006 */
11007 static void
11008f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 typval_T *argvars;
11010 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011011{
11012 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11013}
11014
11015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 * "hostname()" function
11017 */
11018/*ARGSUSED*/
11019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011021 typval_T *argvars;
11022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
11024 char_u hostname[256];
11025
11026 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->v_type = VAR_STRING;
11028 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029}
11030
11031/*
11032 * iconv() function
11033 */
11034/*ARGSUSED*/
11035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 typval_T *argvars;
11038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
11040#ifdef FEAT_MBYTE
11041 char_u buf1[NUMBUFLEN];
11042 char_u buf2[NUMBUFLEN];
11043 char_u *from, *to, *str;
11044 vimconv_T vimconv;
11045#endif
11046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 rettv->v_type = VAR_STRING;
11048 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049
11050#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 str = get_tv_string(&argvars[0]);
11052 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11053 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 vimconv.vc_type = CONV_NONE;
11055 convert_setup(&vimconv, from, to);
11056
11057 /* If the encodings are equal, no conversion needed. */
11058 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062
11063 convert_setup(&vimconv, NULL, NULL);
11064 vim_free(from);
11065 vim_free(to);
11066#endif
11067}
11068
11069/*
11070 * "indent()" function
11071 */
11072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011074 typval_T *argvars;
11075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
11077 linenr_T lnum;
11078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084}
11085
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011086/*
11087 * "index()" function
11088 */
11089 static void
11090f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011091 typval_T *argvars;
11092 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011093{
Bram Moolenaar33570922005-01-25 22:26:29 +000011094 list_T *l;
11095 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011096 long idx = 0;
11097 int ic = FALSE;
11098
11099 rettv->vval.v_number = -1;
11100 if (argvars[0].v_type != VAR_LIST)
11101 {
11102 EMSG(_(e_listreq));
11103 return;
11104 }
11105 l = argvars[0].vval.v_list;
11106 if (l != NULL)
11107 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011108 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 int error = FALSE;
11112
Bram Moolenaar758711c2005-02-02 23:11:38 +000011113 /* Start at specified item. Use the cached index that list_find()
11114 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011116 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011117 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118 ic = get_tv_number_chk(&argvars[3], &error);
11119 if (error)
11120 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011121 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011122
Bram Moolenaar758711c2005-02-02 23:11:38 +000011123 for ( ; item != NULL; item = item->li_next, ++idx)
11124 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011125 {
11126 rettv->vval.v_number = idx;
11127 break;
11128 }
11129 }
11130}
11131
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132static int inputsecret_flag = 0;
11133
11134/*
11135 * "input()" function
11136 * Also handles inputsecret() when inputsecret is set.
11137 */
11138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011140 typval_T *argvars;
11141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 char_u *p = NULL;
11145 int c;
11146 char_u buf[NUMBUFLEN];
11147 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011149 int xp_type = EXPAND_NOTHING;
11150 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153
11154#ifdef NO_CONSOLE_INPUT
11155 /* While starting up, there is no place to enter text. */
11156 if (no_console_input())
11157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 return;
11160 }
11161#endif
11162
11163 cmd_silent = FALSE; /* Want to see the prompt. */
11164 if (prompt != NULL)
11165 {
11166 /* Only the part of the message after the last NL is considered as
11167 * prompt for the command line */
11168 p = vim_strrchr(prompt, '\n');
11169 if (p == NULL)
11170 p = prompt;
11171 else
11172 {
11173 ++p;
11174 c = *p;
11175 *p = NUL;
11176 msg_start();
11177 msg_clr_eos();
11178 msg_puts_attr(prompt, echo_attr);
11179 msg_didout = FALSE;
11180 msg_starthere();
11181 *p = c;
11182 }
11183 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 if (argvars[1].v_type != VAR_UNKNOWN)
11186 {
11187 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11188 if (defstr != NULL)
11189 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190
Bram Moolenaar4463f292005-09-25 22:20:24 +000011191 if (argvars[2].v_type != VAR_UNKNOWN)
11192 {
11193 char_u *xp_name;
11194 int xp_namelen;
11195 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011196
Bram Moolenaar4463f292005-09-25 22:20:24 +000011197 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011198
Bram Moolenaar4463f292005-09-25 22:20:24 +000011199 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11200 if (xp_name == NULL)
11201 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011202
Bram Moolenaar4463f292005-09-25 22:20:24 +000011203 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011204
Bram Moolenaar4463f292005-09-25 22:20:24 +000011205 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11206 &xp_arg) == FAIL)
11207 return;
11208 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011209 }
11210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 if (defstr != NULL)
11212 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011213 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11214 xp_type, xp_arg);
11215
11216 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 /* since the user typed this, no need to wait for return */
11219 need_wait_return = FALSE;
11220 msg_didout = FALSE;
11221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 cmd_silent = cmd_silent_save;
11223}
11224
11225/*
11226 * "inputdialog()" function
11227 */
11228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011230 typval_T *argvars;
11231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232{
11233#if defined(FEAT_GUI_TEXTDIALOG)
11234 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11235 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11236 {
11237 char_u *message;
11238 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 message = get_tv_string_chk(&argvars[0]);
11242 if (argvars[1].v_type != VAR_UNKNOWN
11243 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011244 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 else
11246 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 if (message != NULL && defstr != NULL
11248 && do_dialog(VIM_QUESTION, NULL, message,
11249 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 else
11252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253 if (message != NULL && defstr != NULL
11254 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011255 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_string = vim_strsave(
11257 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011261 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 }
11263 else
11264#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011265 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266}
11267
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011268/*
11269 * "inputlist()" function
11270 */
11271 static void
11272f_inputlist(argvars, rettv)
11273 typval_T *argvars;
11274 typval_T *rettv;
11275{
11276 listitem_T *li;
11277 int selected;
11278 int mouse_used;
11279
11280 rettv->vval.v_number = 0;
11281#ifdef NO_CONSOLE_INPUT
11282 /* While starting up, there is no place to enter text. */
11283 if (no_console_input())
11284 return;
11285#endif
11286 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11287 {
11288 EMSG2(_(e_listarg), "inputlist()");
11289 return;
11290 }
11291
11292 msg_start();
11293 lines_left = Rows; /* avoid more prompt */
11294 msg_scroll = TRUE;
11295 msg_clr_eos();
11296
11297 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11298 {
11299 msg_puts(get_tv_string(&li->li_tv));
11300 msg_putchar('\n');
11301 }
11302
11303 /* Ask for choice. */
11304 selected = prompt_for_number(&mouse_used);
11305 if (mouse_used)
11306 selected -= lines_left;
11307
11308 rettv->vval.v_number = selected;
11309}
11310
11311
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11313
11314/*
11315 * "inputrestore()" function
11316 */
11317/*ARGSUSED*/
11318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011320 typval_T *argvars;
11321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
11323 if (ga_userinput.ga_len > 0)
11324 {
11325 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11327 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 }
11330 else if (p_verbose > 1)
11331 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011332 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 }
11335}
11336
11337/*
11338 * "inputsave()" function
11339 */
11340/*ARGSUSED*/
11341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 typval_T *argvars;
11344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345{
11346 /* Add an entry to the stack of typehead storage. */
11347 if (ga_grow(&ga_userinput, 1) == OK)
11348 {
11349 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11350 + ga_userinput.ga_len);
11351 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 }
11354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356}
11357
11358/*
11359 * "inputsecret()" function
11360 */
11361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *argvars;
11364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
11366 ++cmdline_star;
11367 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 --cmdline_star;
11370 --inputsecret_flag;
11371}
11372
11373/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011374 * "insert()" function
11375 */
11376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011378 typval_T *argvars;
11379 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011380{
11381 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011382 listitem_T *item;
11383 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011385
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011386 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011387 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011388 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011389 else if ((l = argvars[0].vval.v_list) != NULL
11390 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011391 {
11392 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 before = get_tv_number_chk(&argvars[2], &error);
11394 if (error)
11395 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011396
Bram Moolenaar758711c2005-02-02 23:11:38 +000011397 if (before == l->lv_len)
11398 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011399 else
11400 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011401 item = list_find(l, before);
11402 if (item == NULL)
11403 {
11404 EMSGN(_(e_listidx), before);
11405 l = NULL;
11406 }
11407 }
11408 if (l != NULL)
11409 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011410 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011411 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011412 }
11413 }
11414}
11415
11416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 * "isdirectory()" function
11418 */
11419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011421 typval_T *argvars;
11422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425}
11426
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011427/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011428 * "islocked()" function
11429 */
11430 static void
11431f_islocked(argvars, rettv)
11432 typval_T *argvars;
11433 typval_T *rettv;
11434{
11435 lval_T lv;
11436 char_u *end;
11437 dictitem_T *di;
11438
11439 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011440 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11441 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011442 if (end != NULL && lv.ll_name != NULL)
11443 {
11444 if (*end != NUL)
11445 EMSG(_(e_trailing));
11446 else
11447 {
11448 if (lv.ll_tv == NULL)
11449 {
11450 if (check_changedtick(lv.ll_name))
11451 rettv->vval.v_number = 1; /* always locked */
11452 else
11453 {
11454 di = find_var(lv.ll_name, NULL);
11455 if (di != NULL)
11456 {
11457 /* Consider a variable locked when:
11458 * 1. the variable itself is locked
11459 * 2. the value of the variable is locked.
11460 * 3. the List or Dict value is locked.
11461 */
11462 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11463 || tv_islocked(&di->di_tv));
11464 }
11465 }
11466 }
11467 else if (lv.ll_range)
11468 EMSG(_("E745: Range not allowed"));
11469 else if (lv.ll_newkey != NULL)
11470 EMSG2(_(e_dictkey), lv.ll_newkey);
11471 else if (lv.ll_list != NULL)
11472 /* List item. */
11473 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11474 else
11475 /* Dictionary item. */
11476 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11477 }
11478 }
11479
11480 clear_lval(&lv);
11481}
11482
Bram Moolenaar33570922005-01-25 22:26:29 +000011483static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011484
11485/*
11486 * Turn a dict into a list:
11487 * "what" == 0: list of keys
11488 * "what" == 1: list of values
11489 * "what" == 2: list of items
11490 */
11491 static void
11492dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 typval_T *argvars;
11494 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011495 int what;
11496{
Bram Moolenaar33570922005-01-25 22:26:29 +000011497 list_T *l2;
11498 dictitem_T *di;
11499 hashitem_T *hi;
11500 listitem_T *li;
11501 listitem_T *li2;
11502 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011503 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011504
11505 rettv->vval.v_number = 0;
11506 if (argvars[0].v_type != VAR_DICT)
11507 {
11508 EMSG(_(e_dictreq));
11509 return;
11510 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011511 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011512 return;
11513
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011514 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011515 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011516
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 todo = d->dv_hashtab.ht_used;
11518 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011520 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011521 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011522 --todo;
11523 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011524
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011525 li = listitem_alloc();
11526 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011527 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011528 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011529
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011530 if (what == 0)
11531 {
11532 /* keys() */
11533 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011534 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011535 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11536 }
11537 else if (what == 1)
11538 {
11539 /* values() */
11540 copy_tv(&di->di_tv, &li->li_tv);
11541 }
11542 else
11543 {
11544 /* items() */
11545 l2 = list_alloc();
11546 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011547 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011548 li->li_tv.vval.v_list = l2;
11549 if (l2 == NULL)
11550 break;
11551 ++l2->lv_refcount;
11552
11553 li2 = listitem_alloc();
11554 if (li2 == NULL)
11555 break;
11556 list_append(l2, li2);
11557 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011558 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011559 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11560
11561 li2 = listitem_alloc();
11562 if (li2 == NULL)
11563 break;
11564 list_append(l2, li2);
11565 copy_tv(&di->di_tv, &li2->li_tv);
11566 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011567 }
11568 }
11569}
11570
11571/*
11572 * "items(dict)" function
11573 */
11574 static void
11575f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011576 typval_T *argvars;
11577 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011578{
11579 dict_list(argvars, rettv, 2);
11580}
11581
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011583 * "join()" function
11584 */
11585 static void
11586f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011587 typval_T *argvars;
11588 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011589{
11590 garray_T ga;
11591 char_u *sep;
11592
11593 rettv->vval.v_number = 0;
11594 if (argvars[0].v_type != VAR_LIST)
11595 {
11596 EMSG(_(e_listreq));
11597 return;
11598 }
11599 if (argvars[0].vval.v_list == NULL)
11600 return;
11601 if (argvars[1].v_type == VAR_UNKNOWN)
11602 sep = (char_u *)" ";
11603 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011605
11606 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011607
11608 if (sep != NULL)
11609 {
11610 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011611 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 ga_append(&ga, NUL);
11613 rettv->vval.v_string = (char_u *)ga.ga_data;
11614 }
11615 else
11616 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011617}
11618
11619/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011620 * "keys()" function
11621 */
11622 static void
11623f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011624 typval_T *argvars;
11625 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011626{
11627 dict_list(argvars, rettv, 0);
11628}
11629
11630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 * "last_buffer_nr()" function.
11632 */
11633/*ARGSUSED*/
11634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011636 typval_T *argvars;
11637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638{
11639 int n = 0;
11640 buf_T *buf;
11641
11642 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11643 if (n < buf->b_fnum)
11644 n = buf->b_fnum;
11645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011646 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011647}
11648
11649/*
11650 * "len()" function
11651 */
11652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011654 typval_T *argvars;
11655 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011656{
11657 switch (argvars[0].v_type)
11658 {
11659 case VAR_STRING:
11660 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_number = (varnumber_T)STRLEN(
11662 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011663 break;
11664 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011666 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011667 case VAR_DICT:
11668 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11669 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011670 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011671 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011672 break;
11673 }
11674}
11675
Bram Moolenaar33570922005-01-25 22:26:29 +000011676static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011677
11678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011680 typval_T *argvars;
11681 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011682 int type;
11683{
11684#ifdef FEAT_LIBCALL
11685 char_u *string_in;
11686 char_u **string_result;
11687 int nr_result;
11688#endif
11689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011691 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011695
11696 if (check_restricted() || check_secure())
11697 return;
11698
11699#ifdef FEAT_LIBCALL
11700 /* The first two args must be strings, otherwise its meaningless */
11701 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11702 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011703 string_in = NULL;
11704 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011705 string_in = argvars[2].vval.v_string;
11706 if (type == VAR_NUMBER)
11707 string_result = NULL;
11708 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011710 if (mch_libcall(argvars[0].vval.v_string,
11711 argvars[1].vval.v_string,
11712 string_in,
11713 argvars[2].vval.v_number,
11714 string_result,
11715 &nr_result) == OK
11716 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011718 }
11719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720}
11721
11722/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011723 * "libcall()" function
11724 */
11725 static void
11726f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011727 typval_T *argvars;
11728 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011729{
11730 libcall_common(argvars, rettv, VAR_STRING);
11731}
11732
11733/*
11734 * "libcallnr()" function
11735 */
11736 static void
11737f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011738 typval_T *argvars;
11739 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011740{
11741 libcall_common(argvars, rettv, VAR_NUMBER);
11742}
11743
11744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 * "line(string)" function
11746 */
11747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011749 typval_T *argvars;
11750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751{
11752 linenr_T lnum = 0;
11753 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011754 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011756 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 if (fp != NULL)
11758 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760}
11761
11762/*
11763 * "line2byte(lnum)" function
11764 */
11765/*ARGSUSED*/
11766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011768 typval_T *argvars;
11769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770{
11771#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773#else
11774 linenr_T lnum;
11775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011780 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11781 if (rettv->vval.v_number >= 0)
11782 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783#endif
11784}
11785
11786/*
11787 * "lispindent(lnum)" function
11788 */
11789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011791 typval_T *argvars;
11792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793{
11794#ifdef FEAT_LISP
11795 pos_T pos;
11796 linenr_T lnum;
11797
11798 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11801 {
11802 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 curwin->w_cursor = pos;
11805 }
11806 else
11807#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809}
11810
11811/*
11812 * "localtime()" function
11813 */
11814/*ARGSUSED*/
11815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011817 typval_T *argvars;
11818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011820 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821}
11822
Bram Moolenaar33570922005-01-25 22:26:29 +000011823static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824
11825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011827 typval_T *argvars;
11828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829 int exact;
11830{
11831 char_u *keys;
11832 char_u *which;
11833 char_u buf[NUMBUFLEN];
11834 char_u *keys_buf = NULL;
11835 char_u *rhs;
11836 int mode;
11837 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000011838 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839
11840 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 rettv->v_type = VAR_STRING;
11842 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011844 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 if (*keys == NUL)
11846 return;
11847
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011848 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000011849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011851 if (argvars[2].v_type != VAR_UNKNOWN)
11852 abbr = get_tv_number(&argvars[2]);
11853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 else
11855 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 if (which == NULL)
11857 return;
11858
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 mode = get_map_mode(&which, 0);
11860
11861 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011862 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 vim_free(keys_buf);
11864 if (rhs != NULL)
11865 {
11866 ga_init(&ga);
11867 ga.ga_itemsize = 1;
11868 ga.ga_growsize = 40;
11869
11870 while (*rhs != NUL)
11871 ga_concat(&ga, str2special(&rhs, FALSE));
11872
11873 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011874 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 }
11876}
11877
11878/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011879 * "map()" function
11880 */
11881 static void
11882f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011883 typval_T *argvars;
11884 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011885{
11886 filter_map(argvars, rettv, TRUE);
11887}
11888
11889/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011890 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 */
11892 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011893f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011894 typval_T *argvars;
11895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898}
11899
11900/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011901 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 */
11903 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011905 typval_T *argvars;
11906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011908 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909}
11910
Bram Moolenaar33570922005-01-25 22:26:29 +000011911static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912
11913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 typval_T *argvars;
11916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 int type;
11918{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011919 char_u *str = NULL;
11920 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 char_u *pat;
11922 regmatch_T regmatch;
11923 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011924 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 char_u *save_cpo;
11926 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011927 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011928 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011929 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011930 list_T *l = NULL;
11931 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011932 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011933 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934
11935 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11936 save_cpo = p_cpo;
11937 p_cpo = (char_u *)"";
11938
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011939 rettv->vval.v_number = -1;
11940 if (type == 3)
11941 {
11942 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011943 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011944 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011945 }
11946 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011948 rettv->v_type = VAR_STRING;
11949 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011952 if (argvars[0].v_type == VAR_LIST)
11953 {
11954 if ((l = argvars[0].vval.v_list) == NULL)
11955 goto theend;
11956 li = l->lv_first;
11957 }
11958 else
11959 expr = str = get_tv_string(&argvars[0]);
11960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011961 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11962 if (pat == NULL)
11963 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011964
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011965 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 int error = FALSE;
11968
11969 start = get_tv_number_chk(&argvars[2], &error);
11970 if (error)
11971 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011972 if (l != NULL)
11973 {
11974 li = list_find(l, start);
11975 if (li == NULL)
11976 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011977 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011978 }
11979 else
11980 {
11981 if (start < 0)
11982 start = 0;
11983 if (start > (long)STRLEN(str))
11984 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011985 /* When "count" argument is there ignore matches before "start",
11986 * otherwise skip part of the string. Differs when pattern is "^"
11987 * or "\<". */
11988 if (argvars[3].v_type != VAR_UNKNOWN)
11989 startcol = start;
11990 else
11991 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011992 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011993
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011994 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 nth = get_tv_number_chk(&argvars[3], &error);
11996 if (error)
11997 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011998 }
11999
12000 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12001 if (regmatch.regprog != NULL)
12002 {
12003 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012004
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012005 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012006 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012007 if (l != NULL)
12008 {
12009 if (li == NULL)
12010 {
12011 match = FALSE;
12012 break;
12013 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012014 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012015 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012016 if (str == NULL)
12017 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012018 }
12019
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012020 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012021
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012022 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012023 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012024 if (l == NULL && !match)
12025 break;
12026
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012027 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012028 if (l != NULL)
12029 {
12030 li = li->li_next;
12031 ++idx;
12032 }
12033 else
12034 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012035#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012036 startcol = regmatch.startp[0]
12037 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012038#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012039 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012040#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012041 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012042 }
12043
12044 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012046 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012048 int i;
12049
12050 /* return list with matched string and submatches */
12051 for (i = 0; i < NSUBEXP; ++i)
12052 {
12053 if (regmatch.endp[i] == NULL)
12054 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012055 if (list_append_string(rettv->vval.v_list,
12056 regmatch.startp[i],
12057 (int)(regmatch.endp[i] - regmatch.startp[i]))
12058 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012059 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012060 }
12061 }
12062 else if (type == 2)
12063 {
12064 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012065 if (l != NULL)
12066 copy_tv(&li->li_tv, rettv);
12067 else
12068 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012070 }
12071 else if (l != NULL)
12072 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 else
12074 {
12075 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012076 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 (varnumber_T)(regmatch.startp[0] - str);
12078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012079 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 }
12083 }
12084 vim_free(regmatch.regprog);
12085 }
12086
12087theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012088 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 p_cpo = save_cpo;
12090}
12091
12092/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012093 * "match()" function
12094 */
12095 static void
12096f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012097 typval_T *argvars;
12098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099{
12100 find_some_match(argvars, rettv, 1);
12101}
12102
12103/*
12104 * "matchend()" function
12105 */
12106 static void
12107f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012108 typval_T *argvars;
12109 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012110{
12111 find_some_match(argvars, rettv, 0);
12112}
12113
12114/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012115 * "matchlist()" function
12116 */
12117 static void
12118f_matchlist(argvars, rettv)
12119 typval_T *argvars;
12120 typval_T *rettv;
12121{
12122 find_some_match(argvars, rettv, 3);
12123}
12124
12125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126 * "matchstr()" function
12127 */
12128 static void
12129f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012130 typval_T *argvars;
12131 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132{
12133 find_some_match(argvars, rettv, 2);
12134}
12135
Bram Moolenaar33570922005-01-25 22:26:29 +000012136static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012137
12138 static void
12139max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 typval_T *argvars;
12141 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012142 int domax;
12143{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012144 long n = 0;
12145 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012147
12148 if (argvars[0].v_type == VAR_LIST)
12149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012150 list_T *l;
12151 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012152
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012153 l = argvars[0].vval.v_list;
12154 if (l != NULL)
12155 {
12156 li = l->lv_first;
12157 if (li != NULL)
12158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012159 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012160 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012161 {
12162 li = li->li_next;
12163 if (li == NULL)
12164 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012165 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012166 if (domax ? i > n : i < n)
12167 n = i;
12168 }
12169 }
12170 }
12171 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012172 else if (argvars[0].v_type == VAR_DICT)
12173 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012174 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012175 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012176 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012177 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012178
12179 d = argvars[0].vval.v_dict;
12180 if (d != NULL)
12181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012182 todo = d->dv_hashtab.ht_used;
12183 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012185 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012186 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012187 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012189 if (first)
12190 {
12191 n = i;
12192 first = FALSE;
12193 }
12194 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012195 n = i;
12196 }
12197 }
12198 }
12199 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012200 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012201 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012202 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012203}
12204
12205/*
12206 * "max()" function
12207 */
12208 static void
12209f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012210 typval_T *argvars;
12211 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012212{
12213 max_min(argvars, rettv, TRUE);
12214}
12215
12216/*
12217 * "min()" function
12218 */
12219 static void
12220f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012221 typval_T *argvars;
12222 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012223{
12224 max_min(argvars, rettv, FALSE);
12225}
12226
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012227static int mkdir_recurse __ARGS((char_u *dir, int prot));
12228
12229/*
12230 * Create the directory in which "dir" is located, and higher levels when
12231 * needed.
12232 */
12233 static int
12234mkdir_recurse(dir, prot)
12235 char_u *dir;
12236 int prot;
12237{
12238 char_u *p;
12239 char_u *updir;
12240 int r = FAIL;
12241
12242 /* Get end of directory name in "dir".
12243 * We're done when it's "/" or "c:/". */
12244 p = gettail_sep(dir);
12245 if (p <= get_past_head(dir))
12246 return OK;
12247
12248 /* If the directory exists we're done. Otherwise: create it.*/
12249 updir = vim_strnsave(dir, (int)(p - dir));
12250 if (updir == NULL)
12251 return FAIL;
12252 if (mch_isdir(updir))
12253 r = OK;
12254 else if (mkdir_recurse(updir, prot) == OK)
12255 r = vim_mkdir_emsg(updir, prot);
12256 vim_free(updir);
12257 return r;
12258}
12259
12260#ifdef vim_mkdir
12261/*
12262 * "mkdir()" function
12263 */
12264 static void
12265f_mkdir(argvars, rettv)
12266 typval_T *argvars;
12267 typval_T *rettv;
12268{
12269 char_u *dir;
12270 char_u buf[NUMBUFLEN];
12271 int prot = 0755;
12272
12273 rettv->vval.v_number = FAIL;
12274 if (check_restricted() || check_secure())
12275 return;
12276
12277 dir = get_tv_string_buf(&argvars[0], buf);
12278 if (argvars[1].v_type != VAR_UNKNOWN)
12279 {
12280 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012281 prot = get_tv_number_chk(&argvars[2], NULL);
12282 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012283 mkdir_recurse(dir, prot);
12284 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012285 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012286}
12287#endif
12288
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 * "mode()" function
12291 */
12292/*ARGSUSED*/
12293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012295 typval_T *argvars;
12296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297{
12298 char_u buf[2];
12299
12300#ifdef FEAT_VISUAL
12301 if (VIsual_active)
12302 {
12303 if (VIsual_select)
12304 buf[0] = VIsual_mode + 's' - 'v';
12305 else
12306 buf[0] = VIsual_mode;
12307 }
12308 else
12309#endif
12310 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12311 buf[0] = 'r';
12312 else if (State & INSERT)
12313 {
12314 if (State & REPLACE_FLAG)
12315 buf[0] = 'R';
12316 else
12317 buf[0] = 'i';
12318 }
12319 else if (State & CMDLINE)
12320 buf[0] = 'c';
12321 else
12322 buf[0] = 'n';
12323
12324 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 rettv->vval.v_string = vim_strsave(buf);
12326 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
12328
12329/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330 * "nextnonblank()" function
12331 */
12332 static void
12333f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012334 typval_T *argvars;
12335 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336{
12337 linenr_T lnum;
12338
12339 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012342 {
12343 lnum = 0;
12344 break;
12345 }
12346 if (*skipwhite(ml_get(lnum)) != NUL)
12347 break;
12348 }
12349 rettv->vval.v_number = lnum;
12350}
12351
12352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 * "nr2char()" function
12354 */
12355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012356f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012357 typval_T *argvars;
12358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359{
12360 char_u buf[NUMBUFLEN];
12361
12362#ifdef FEAT_MBYTE
12363 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 else
12366#endif
12367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 buf[1] = NUL;
12370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->v_type = VAR_STRING;
12372 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012373}
12374
12375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012376 * "prevnonblank()" function
12377 */
12378 static void
12379f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012380 typval_T *argvars;
12381 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012382{
12383 linenr_T lnum;
12384
12385 lnum = get_tv_lnum(argvars);
12386 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12387 lnum = 0;
12388 else
12389 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12390 --lnum;
12391 rettv->vval.v_number = lnum;
12392}
12393
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012394#ifdef HAVE_STDARG_H
12395/* This dummy va_list is here because:
12396 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12397 * - locally in the function results in a "used before set" warning
12398 * - using va_start() to initialize it gives "function with fixed args" error */
12399static va_list ap;
12400#endif
12401
Bram Moolenaar8c711452005-01-14 21:53:12 +000012402/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012403 * "printf()" function
12404 */
12405 static void
12406f_printf(argvars, rettv)
12407 typval_T *argvars;
12408 typval_T *rettv;
12409{
12410 rettv->v_type = VAR_STRING;
12411 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012412#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012413 {
12414 char_u buf[NUMBUFLEN];
12415 int len;
12416 char_u *s;
12417 int saved_did_emsg = did_emsg;
12418 char *fmt;
12419
12420 /* Get the required length, allocate the buffer and do it for real. */
12421 did_emsg = FALSE;
12422 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012423 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012424 if (!did_emsg)
12425 {
12426 s = alloc(len + 1);
12427 if (s != NULL)
12428 {
12429 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012430 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012431 }
12432 }
12433 did_emsg |= saved_did_emsg;
12434 }
12435#endif
12436}
12437
12438/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012439 * "pumvisible()" function
12440 */
12441/*ARGSUSED*/
12442 static void
12443f_pumvisible(argvars, rettv)
12444 typval_T *argvars;
12445 typval_T *rettv;
12446{
12447 rettv->vval.v_number = 0;
12448#ifdef FEAT_INS_EXPAND
12449 if (pum_visible())
12450 rettv->vval.v_number = 1;
12451#endif
12452}
12453
12454/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012455 * "range()" function
12456 */
12457 static void
12458f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012459 typval_T *argvars;
12460 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012461{
12462 long start;
12463 long end;
12464 long stride = 1;
12465 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012466 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012469 if (argvars[1].v_type == VAR_UNKNOWN)
12470 {
12471 end = start - 1;
12472 start = 0;
12473 }
12474 else
12475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012476 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012478 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012479 }
12480
12481 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012482 if (error)
12483 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012484 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012485 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012486 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012487 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012488 else
12489 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012490 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012491 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012492 if (list_append_number(rettv->vval.v_list,
12493 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012494 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012495 }
12496}
12497
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012498/*
12499 * "readfile()" function
12500 */
12501 static void
12502f_readfile(argvars, rettv)
12503 typval_T *argvars;
12504 typval_T *rettv;
12505{
12506 int binary = FALSE;
12507 char_u *fname;
12508 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012509 listitem_T *li;
12510#define FREAD_SIZE 200 /* optimized for text lines */
12511 char_u buf[FREAD_SIZE];
12512 int readlen; /* size of last fread() */
12513 int buflen; /* nr of valid chars in buf[] */
12514 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12515 int tolist; /* first byte in buf[] still to be put in list */
12516 int chop; /* how many CR to chop off */
12517 char_u *prev = NULL; /* previously read bytes, if any */
12518 int prevlen = 0; /* length of "prev" if not NULL */
12519 char_u *s;
12520 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012521 long maxline = MAXLNUM;
12522 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012523
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012524 if (argvars[1].v_type != VAR_UNKNOWN)
12525 {
12526 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12527 binary = TRUE;
12528 if (argvars[2].v_type != VAR_UNKNOWN)
12529 maxline = get_tv_number(&argvars[2]);
12530 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012532 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012533 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012534
12535 /* Always open the file in binary mode, library functions have a mind of
12536 * their own about CR-LF conversion. */
12537 fname = get_tv_string(&argvars[0]);
12538 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12539 {
12540 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12541 return;
12542 }
12543
12544 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012545 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012546 {
12547 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12548 buflen = filtd + readlen;
12549 tolist = 0;
12550 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12551 {
12552 if (buf[filtd] == '\n' || readlen <= 0)
12553 {
12554 /* Only when in binary mode add an empty list item when the
12555 * last line ends in a '\n'. */
12556 if (!binary && readlen == 0 && filtd == 0)
12557 break;
12558
12559 /* Found end-of-line or end-of-file: add a text line to the
12560 * list. */
12561 chop = 0;
12562 if (!binary)
12563 while (filtd - chop - 1 >= tolist
12564 && buf[filtd - chop - 1] == '\r')
12565 ++chop;
12566 len = filtd - tolist - chop;
12567 if (prev == NULL)
12568 s = vim_strnsave(buf + tolist, len);
12569 else
12570 {
12571 s = alloc((unsigned)(prevlen + len + 1));
12572 if (s != NULL)
12573 {
12574 mch_memmove(s, prev, prevlen);
12575 vim_free(prev);
12576 prev = NULL;
12577 mch_memmove(s + prevlen, buf + tolist, len);
12578 s[prevlen + len] = NUL;
12579 }
12580 }
12581 tolist = filtd + 1;
12582
12583 li = listitem_alloc();
12584 if (li == NULL)
12585 {
12586 vim_free(s);
12587 break;
12588 }
12589 li->li_tv.v_type = VAR_STRING;
12590 li->li_tv.v_lock = 0;
12591 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012592 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012593
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012594 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012595 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012596 if (readlen <= 0)
12597 break;
12598 }
12599 else if (buf[filtd] == NUL)
12600 buf[filtd] = '\n';
12601 }
12602 if (readlen <= 0)
12603 break;
12604
12605 if (tolist == 0)
12606 {
12607 /* "buf" is full, need to move text to an allocated buffer */
12608 if (prev == NULL)
12609 {
12610 prev = vim_strnsave(buf, buflen);
12611 prevlen = buflen;
12612 }
12613 else
12614 {
12615 s = alloc((unsigned)(prevlen + buflen));
12616 if (s != NULL)
12617 {
12618 mch_memmove(s, prev, prevlen);
12619 mch_memmove(s + prevlen, buf, buflen);
12620 vim_free(prev);
12621 prev = s;
12622 prevlen += buflen;
12623 }
12624 }
12625 filtd = 0;
12626 }
12627 else
12628 {
12629 mch_memmove(buf, buf + tolist, buflen - tolist);
12630 filtd -= tolist;
12631 }
12632 }
12633
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012634 /*
12635 * For a negative line count use only the lines at the end of the file,
12636 * free the rest.
12637 */
12638 if (maxline < 0)
12639 while (cnt > -maxline)
12640 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012641 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012642 --cnt;
12643 }
12644
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012645 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012646 fclose(fd);
12647}
12648
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012649#if defined(FEAT_RELTIME)
12650static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
12651
12652/*
12653 * Convert a List to proftime_T.
12654 * Return FAIL when there is something wrong.
12655 */
12656 static int
12657list2proftime(arg, tm)
12658 typval_T *arg;
12659 proftime_T *tm;
12660{
12661 long n1, n2;
12662 int error = FALSE;
12663
12664 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
12665 || arg->vval.v_list->lv_len != 2)
12666 return FAIL;
12667 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
12668 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
12669# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012670 tm->HighPart = n1;
12671 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012672# else
12673 tm->tv_sec = n1;
12674 tm->tv_usec = n2;
12675# endif
12676 return error ? FAIL : OK;
12677}
12678#endif /* FEAT_RELTIME */
12679
12680/*
12681 * "reltime()" function
12682 */
12683 static void
12684f_reltime(argvars, rettv)
12685 typval_T *argvars;
12686 typval_T *rettv;
12687{
12688#ifdef FEAT_RELTIME
12689 proftime_T res;
12690 proftime_T start;
12691
12692 if (argvars[0].v_type == VAR_UNKNOWN)
12693 {
12694 /* No arguments: get current time. */
12695 profile_start(&res);
12696 }
12697 else if (argvars[1].v_type == VAR_UNKNOWN)
12698 {
12699 if (list2proftime(&argvars[0], &res) == FAIL)
12700 return;
12701 profile_end(&res);
12702 }
12703 else
12704 {
12705 /* Two arguments: compute the difference. */
12706 if (list2proftime(&argvars[0], &start) == FAIL
12707 || list2proftime(&argvars[1], &res) == FAIL)
12708 return;
12709 profile_sub(&res, &start);
12710 }
12711
12712 if (rettv_list_alloc(rettv) == OK)
12713 {
12714 long n1, n2;
12715
12716# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000012717 n1 = res.HighPart;
12718 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012719# else
12720 n1 = res.tv_sec;
12721 n2 = res.tv_usec;
12722# endif
12723 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
12724 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
12725 }
12726#endif
12727}
12728
12729/*
12730 * "reltimestr()" function
12731 */
12732 static void
12733f_reltimestr(argvars, rettv)
12734 typval_T *argvars;
12735 typval_T *rettv;
12736{
12737#ifdef FEAT_RELTIME
12738 proftime_T tm;
12739#endif
12740
12741 rettv->v_type = VAR_STRING;
12742 rettv->vval.v_string = NULL;
12743#ifdef FEAT_RELTIME
12744 if (list2proftime(&argvars[0], &tm) == OK)
12745 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
12746#endif
12747}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012748
Bram Moolenaar0d660222005-01-07 21:51:51 +000012749#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12750static void make_connection __ARGS((void));
12751static int check_connection __ARGS((void));
12752
12753 static void
12754make_connection()
12755{
12756 if (X_DISPLAY == NULL
12757# ifdef FEAT_GUI
12758 && !gui.in_use
12759# endif
12760 )
12761 {
12762 x_force_connect = TRUE;
12763 setup_term_clip();
12764 x_force_connect = FALSE;
12765 }
12766}
12767
12768 static int
12769check_connection()
12770{
12771 make_connection();
12772 if (X_DISPLAY == NULL)
12773 {
12774 EMSG(_("E240: No connection to Vim server"));
12775 return FAIL;
12776 }
12777 return OK;
12778}
12779#endif
12780
12781#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012782static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012783
12784 static void
12785remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012788 int expr;
12789{
12790 char_u *server_name;
12791 char_u *keys;
12792 char_u *r = NULL;
12793 char_u buf[NUMBUFLEN];
12794# ifdef WIN32
12795 HWND w;
12796# else
12797 Window w;
12798# endif
12799
12800 if (check_restricted() || check_secure())
12801 return;
12802
12803# ifdef FEAT_X11
12804 if (check_connection() == FAIL)
12805 return;
12806# endif
12807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012808 server_name = get_tv_string_chk(&argvars[0]);
12809 if (server_name == NULL)
12810 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012811 keys = get_tv_string_buf(&argvars[1], buf);
12812# ifdef WIN32
12813 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12814# else
12815 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12816 < 0)
12817# endif
12818 {
12819 if (r != NULL)
12820 EMSG(r); /* sending worked but evaluation failed */
12821 else
12822 EMSG2(_("E241: Unable to send to %s"), server_name);
12823 return;
12824 }
12825
12826 rettv->vval.v_string = r;
12827
12828 if (argvars[2].v_type != VAR_UNKNOWN)
12829 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012830 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012831 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012833
12834 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 v.di_tv.v_type = VAR_STRING;
12836 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012837 idvar = get_tv_string_chk(&argvars[2]);
12838 if (idvar != NULL)
12839 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012840 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012841 }
12842}
12843#endif
12844
12845/*
12846 * "remote_expr()" function
12847 */
12848/*ARGSUSED*/
12849 static void
12850f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012851 typval_T *argvars;
12852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853{
12854 rettv->v_type = VAR_STRING;
12855 rettv->vval.v_string = NULL;
12856#ifdef FEAT_CLIENTSERVER
12857 remote_common(argvars, rettv, TRUE);
12858#endif
12859}
12860
12861/*
12862 * "remote_foreground()" function
12863 */
12864/*ARGSUSED*/
12865 static void
12866f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012867 typval_T *argvars;
12868 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012869{
12870 rettv->vval.v_number = 0;
12871#ifdef FEAT_CLIENTSERVER
12872# ifdef WIN32
12873 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012874 {
12875 char_u *server_name = get_tv_string_chk(&argvars[0]);
12876
12877 if (server_name != NULL)
12878 serverForeground(server_name);
12879 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012880# else
12881 /* Send a foreground() expression to the server. */
12882 argvars[1].v_type = VAR_STRING;
12883 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12884 argvars[2].v_type = VAR_UNKNOWN;
12885 remote_common(argvars, rettv, TRUE);
12886 vim_free(argvars[1].vval.v_string);
12887# endif
12888#endif
12889}
12890
12891/*ARGSUSED*/
12892 static void
12893f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012894 typval_T *argvars;
12895 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012896{
12897#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012898 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012899 char_u *s = NULL;
12900# ifdef WIN32
12901 int n = 0;
12902# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012904
12905 if (check_restricted() || check_secure())
12906 {
12907 rettv->vval.v_number = -1;
12908 return;
12909 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012910 serverid = get_tv_string_chk(&argvars[0]);
12911 if (serverid == NULL)
12912 {
12913 rettv->vval.v_number = -1;
12914 return; /* type error; errmsg already given */
12915 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012916# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012917 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012918 if (n == 0)
12919 rettv->vval.v_number = -1;
12920 else
12921 {
12922 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12923 rettv->vval.v_number = (s != NULL);
12924 }
12925# else
12926 rettv->vval.v_number = 0;
12927 if (check_connection() == FAIL)
12928 return;
12929
12930 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012931 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012932# endif
12933
12934 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012936 char_u *retvar;
12937
Bram Moolenaar33570922005-01-25 22:26:29 +000012938 v.di_tv.v_type = VAR_STRING;
12939 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012940 retvar = get_tv_string_chk(&argvars[1]);
12941 if (retvar != NULL)
12942 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012944 }
12945#else
12946 rettv->vval.v_number = -1;
12947#endif
12948}
12949
12950/*ARGSUSED*/
12951 static void
12952f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012953 typval_T *argvars;
12954 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012955{
12956 char_u *r = NULL;
12957
12958#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012959 char_u *serverid = get_tv_string_chk(&argvars[0]);
12960
12961 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012962 {
12963# ifdef WIN32
12964 /* The server's HWND is encoded in the 'id' parameter */
12965 int n = 0;
12966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012967 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012968 if (n != 0)
12969 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12970 if (r == NULL)
12971# else
12972 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012973 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974# endif
12975 EMSG(_("E277: Unable to read a server reply"));
12976 }
12977#endif
12978 rettv->v_type = VAR_STRING;
12979 rettv->vval.v_string = r;
12980}
12981
12982/*
12983 * "remote_send()" function
12984 */
12985/*ARGSUSED*/
12986 static void
12987f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012988 typval_T *argvars;
12989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012990{
12991 rettv->v_type = VAR_STRING;
12992 rettv->vval.v_string = NULL;
12993#ifdef FEAT_CLIENTSERVER
12994 remote_common(argvars, rettv, FALSE);
12995#endif
12996}
12997
12998/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012999 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013000 */
13001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013003 typval_T *argvars;
13004 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013005{
Bram Moolenaar33570922005-01-25 22:26:29 +000013006 list_T *l;
13007 listitem_T *item, *item2;
13008 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013009 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013010 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013011 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013012 dict_T *d;
13013 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013014
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013015 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013016 if (argvars[0].v_type == VAR_DICT)
13017 {
13018 if (argvars[2].v_type != VAR_UNKNOWN)
13019 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013020 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000013021 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013023 key = get_tv_string_chk(&argvars[1]);
13024 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013026 di = dict_find(d, key, -1);
13027 if (di == NULL)
13028 EMSG2(_(e_dictkey), key);
13029 else
13030 {
13031 *rettv = di->di_tv;
13032 init_tv(&di->di_tv);
13033 dictitem_remove(d, di);
13034 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013036 }
13037 }
13038 else if (argvars[0].v_type != VAR_LIST)
13039 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013040 else if ((l = argvars[0].vval.v_list) != NULL
13041 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 int error = FALSE;
13044
13045 idx = get_tv_number_chk(&argvars[1], &error);
13046 if (error)
13047 ; /* type error: do nothing, errmsg already given */
13048 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013049 EMSGN(_(e_listidx), idx);
13050 else
13051 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013052 if (argvars[2].v_type == VAR_UNKNOWN)
13053 {
13054 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013055 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013056 *rettv = item->li_tv;
13057 vim_free(item);
13058 }
13059 else
13060 {
13061 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 end = get_tv_number_chk(&argvars[2], &error);
13063 if (error)
13064 ; /* type error: do nothing */
13065 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013066 EMSGN(_(e_listidx), end);
13067 else
13068 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013069 int cnt = 0;
13070
13071 for (li = item; li != NULL; li = li->li_next)
13072 {
13073 ++cnt;
13074 if (li == item2)
13075 break;
13076 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013077 if (li == NULL) /* didn't find "item2" after "item" */
13078 EMSG(_(e_invrange));
13079 else
13080 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013081 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013082 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013083 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013084 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013085 l->lv_first = item;
13086 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013087 item->li_prev = NULL;
13088 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013089 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013090 }
13091 }
13092 }
13093 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013094 }
13095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096}
13097
13098/*
13099 * "rename({from}, {to})" function
13100 */
13101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013103 typval_T *argvars;
13104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105{
13106 char_u buf[NUMBUFLEN];
13107
13108 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13112 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113}
13114
13115/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013116 * "repeat()" function
13117 */
13118/*ARGSUSED*/
13119 static void
13120f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013121 typval_T *argvars;
13122 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013123{
13124 char_u *p;
13125 int n;
13126 int slen;
13127 int len;
13128 char_u *r;
13129 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013130
13131 n = get_tv_number(&argvars[1]);
13132 if (argvars[0].v_type == VAR_LIST)
13133 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013134 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013135 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013136 if (list_extend(rettv->vval.v_list,
13137 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013138 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013139 }
13140 else
13141 {
13142 p = get_tv_string(&argvars[0]);
13143 rettv->v_type = VAR_STRING;
13144 rettv->vval.v_string = NULL;
13145
13146 slen = (int)STRLEN(p);
13147 len = slen * n;
13148 if (len <= 0)
13149 return;
13150
13151 r = alloc(len + 1);
13152 if (r != NULL)
13153 {
13154 for (i = 0; i < n; i++)
13155 mch_memmove(r + i * slen, p, (size_t)slen);
13156 r[len] = NUL;
13157 }
13158
13159 rettv->vval.v_string = r;
13160 }
13161}
13162
13163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 * "resolve()" function
13165 */
13166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013168 typval_T *argvars;
13169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
13171 char_u *p;
13172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174#ifdef FEAT_SHORTCUT
13175 {
13176 char_u *v = NULL;
13177
13178 v = mch_resolve_shortcut(p);
13179 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 }
13184#else
13185# ifdef HAVE_READLINK
13186 {
13187 char_u buf[MAXPATHL + 1];
13188 char_u *cpy;
13189 int len;
13190 char_u *remain = NULL;
13191 char_u *q;
13192 int is_relative_to_current = FALSE;
13193 int has_trailing_pathsep = FALSE;
13194 int limit = 100;
13195
13196 p = vim_strsave(p);
13197
13198 if (p[0] == '.' && (vim_ispathsep(p[1])
13199 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13200 is_relative_to_current = TRUE;
13201
13202 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013203 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 has_trailing_pathsep = TRUE;
13205
13206 q = getnextcomp(p);
13207 if (*q != NUL)
13208 {
13209 /* Separate the first path component in "p", and keep the
13210 * remainder (beginning with the path separator). */
13211 remain = vim_strsave(q - 1);
13212 q[-1] = NUL;
13213 }
13214
13215 for (;;)
13216 {
13217 for (;;)
13218 {
13219 len = readlink((char *)p, (char *)buf, MAXPATHL);
13220 if (len <= 0)
13221 break;
13222 buf[len] = NUL;
13223
13224 if (limit-- == 0)
13225 {
13226 vim_free(p);
13227 vim_free(remain);
13228 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 goto fail;
13231 }
13232
13233 /* Ensure that the result will have a trailing path separator
13234 * if the argument has one. */
13235 if (remain == NULL && has_trailing_pathsep)
13236 add_pathsep(buf);
13237
13238 /* Separate the first path component in the link value and
13239 * concatenate the remainders. */
13240 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13241 if (*q != NUL)
13242 {
13243 if (remain == NULL)
13244 remain = vim_strsave(q - 1);
13245 else
13246 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013247 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 if (cpy != NULL)
13249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 vim_free(remain);
13251 remain = cpy;
13252 }
13253 }
13254 q[-1] = NUL;
13255 }
13256
13257 q = gettail(p);
13258 if (q > p && *q == NUL)
13259 {
13260 /* Ignore trailing path separator. */
13261 q[-1] = NUL;
13262 q = gettail(p);
13263 }
13264 if (q > p && !mch_isFullName(buf))
13265 {
13266 /* symlink is relative to directory of argument */
13267 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13268 if (cpy != NULL)
13269 {
13270 STRCPY(cpy, p);
13271 STRCPY(gettail(cpy), buf);
13272 vim_free(p);
13273 p = cpy;
13274 }
13275 }
13276 else
13277 {
13278 vim_free(p);
13279 p = vim_strsave(buf);
13280 }
13281 }
13282
13283 if (remain == NULL)
13284 break;
13285
13286 /* Append the first path component of "remain" to "p". */
13287 q = getnextcomp(remain + 1);
13288 len = q - remain - (*q != NUL);
13289 cpy = vim_strnsave(p, STRLEN(p) + len);
13290 if (cpy != NULL)
13291 {
13292 STRNCAT(cpy, remain, len);
13293 vim_free(p);
13294 p = cpy;
13295 }
13296 /* Shorten "remain". */
13297 if (*q != NUL)
13298 STRCPY(remain, q - 1);
13299 else
13300 {
13301 vim_free(remain);
13302 remain = NULL;
13303 }
13304 }
13305
13306 /* If the result is a relative path name, make it explicitly relative to
13307 * the current directory if and only if the argument had this form. */
13308 if (!vim_ispathsep(*p))
13309 {
13310 if (is_relative_to_current
13311 && *p != NUL
13312 && !(p[0] == '.'
13313 && (p[1] == NUL
13314 || vim_ispathsep(p[1])
13315 || (p[1] == '.'
13316 && (p[2] == NUL
13317 || vim_ispathsep(p[2]))))))
13318 {
13319 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013320 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 if (cpy != NULL)
13322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 vim_free(p);
13324 p = cpy;
13325 }
13326 }
13327 else if (!is_relative_to_current)
13328 {
13329 /* Strip leading "./". */
13330 q = p;
13331 while (q[0] == '.' && vim_ispathsep(q[1]))
13332 q += 2;
13333 if (q > p)
13334 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13335 }
13336 }
13337
13338 /* Ensure that the result will have no trailing path separator
13339 * if the argument had none. But keep "/" or "//". */
13340 if (!has_trailing_pathsep)
13341 {
13342 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013343 if (after_pathsep(p, q))
13344 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 }
13346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 }
13349# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351# endif
13352#endif
13353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355
13356#ifdef HAVE_READLINK
13357fail:
13358#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360}
13361
13362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013363 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 */
13365 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013366f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 typval_T *argvars;
13368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 list_T *l;
13371 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372
Bram Moolenaar0d660222005-01-07 21:51:51 +000013373 rettv->vval.v_number = 0;
13374 if (argvars[0].v_type != VAR_LIST)
13375 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013376 else if ((l = argvars[0].vval.v_list) != NULL
13377 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378 {
13379 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013380 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013381 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013382 while (li != NULL)
13383 {
13384 ni = li->li_prev;
13385 list_append(l, li);
13386 li = ni;
13387 }
13388 rettv->vval.v_list = l;
13389 rettv->v_type = VAR_LIST;
13390 ++l->lv_refcount;
13391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392}
13393
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013394#define SP_NOMOVE 0x01 /* don't move cursor */
13395#define SP_REPEAT 0x02 /* repeat to find outer pair */
13396#define SP_RETCOUNT 0x04 /* return matchcount */
13397#define SP_SETPCMARK 0x08 /* set previous context mark */
13398#define SP_START 0x10 /* accept match at start position */
13399#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13400#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013401
Bram Moolenaar33570922005-01-25 22:26:29 +000013402static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013403
13404/*
13405 * Get flags for a search function.
13406 * Possibly sets "p_ws".
13407 * Returns BACKWARD, FORWARD or zero (for an error).
13408 */
13409 static int
13410get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013411 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013412 int *flagsp;
13413{
13414 int dir = FORWARD;
13415 char_u *flags;
13416 char_u nbuf[NUMBUFLEN];
13417 int mask;
13418
13419 if (varp->v_type != VAR_UNKNOWN)
13420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421 flags = get_tv_string_buf_chk(varp, nbuf);
13422 if (flags == NULL)
13423 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013424 while (*flags != NUL)
13425 {
13426 switch (*flags)
13427 {
13428 case 'b': dir = BACKWARD; break;
13429 case 'w': p_ws = TRUE; break;
13430 case 'W': p_ws = FALSE; break;
13431 default: mask = 0;
13432 if (flagsp != NULL)
13433 switch (*flags)
13434 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013435 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013436 case 'e': mask = SP_END; break;
13437 case 'm': mask = SP_RETCOUNT; break;
13438 case 'n': mask = SP_NOMOVE; break;
13439 case 'p': mask = SP_SUBPAT; break;
13440 case 'r': mask = SP_REPEAT; break;
13441 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013442 }
13443 if (mask == 0)
13444 {
13445 EMSG2(_(e_invarg2), flags);
13446 dir = 0;
13447 }
13448 else
13449 *flagsp |= mask;
13450 }
13451 if (dir == 0)
13452 break;
13453 ++flags;
13454 }
13455 }
13456 return dir;
13457}
13458
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013460 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013462 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013463search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013465 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013466 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013468 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 char_u *pat;
13470 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013471 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 int save_p_ws = p_ws;
13473 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013474 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013475 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013476 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013477 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013480 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013481 if (dir == 0)
13482 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013483 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013484 if (flags & SP_START)
13485 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013486 if (flags & SP_END)
13487 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013488
13489 /* Optional extra argument: line number to stop searching. */
13490 if (argvars[1].v_type != VAR_UNKNOWN
13491 && argvars[2].v_type != VAR_UNKNOWN)
13492 {
13493 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13494 if (lnum_stop < 0)
13495 goto theend;
13496 }
13497
Bram Moolenaar231334e2005-07-25 20:46:57 +000013498 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013499 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013500 * Check to make sure only those flags are set.
13501 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13502 * flags cannot be set. Check for that condition also.
13503 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013504 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013505 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013507 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013508 goto theend;
13509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013511 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013512 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13513 options, RE_SEARCH, (linenr_T)lnum_stop);
13514 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013516 if (flags & SP_SUBPAT)
13517 retval = subpatnum;
13518 else
13519 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013520 if (flags & SP_SETPCMARK)
13521 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013523 if (match_pos != NULL)
13524 {
13525 /* Store the match cursor position */
13526 match_pos->lnum = pos.lnum;
13527 match_pos->col = pos.col + 1;
13528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 /* "/$" will put the cursor after the end of the line, may need to
13530 * correct that here */
13531 check_cursor();
13532 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013533
13534 /* If 'n' flag is used: restore cursor position. */
13535 if (flags & SP_NOMOVE)
13536 curwin->w_cursor = save_cursor;
13537theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013539
13540 return retval;
13541}
13542
13543/*
13544 * "search()" function
13545 */
13546 static void
13547f_search(argvars, rettv)
13548 typval_T *argvars;
13549 typval_T *rettv;
13550{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013551 int flags = 0;
13552
13553 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554}
13555
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013557 * "searchdecl()" function
13558 */
13559 static void
13560f_searchdecl(argvars, rettv)
13561 typval_T *argvars;
13562 typval_T *rettv;
13563{
13564 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013565 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013566 int error = FALSE;
13567 char_u *name;
13568
13569 rettv->vval.v_number = 1; /* default: FAIL */
13570
13571 name = get_tv_string_chk(&argvars[0]);
13572 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013573 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013574 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013575 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13576 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13577 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013578 if (!error && name != NULL)
13579 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013580 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013581}
13582
13583/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013584 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013586 static int
13587searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013588 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013589 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590{
13591 char_u *spat, *mpat, *epat;
13592 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 int dir;
13595 int flags = 0;
13596 char_u nbuf1[NUMBUFLEN];
13597 char_u nbuf2[NUMBUFLEN];
13598 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013599 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013600 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603 spat = get_tv_string_chk(&argvars[0]);
13604 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13605 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13606 if (spat == NULL || mpat == NULL || epat == NULL)
13607 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 /* Handle the optional fourth argument: flags */
13610 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013611 if (dir == 0)
13612 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013613
13614 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013615 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13616 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013617 if ((flags & (SP_END | SP_SUBPAT)) != 0
13618 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013619 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013620 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013621 goto theend;
13622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013624 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013625 if (argvars[3].v_type == VAR_UNKNOWN
13626 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 skip = (char_u *)"";
13628 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013631 if (argvars[5].v_type != VAR_UNKNOWN)
13632 {
13633 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13634 if (lnum_stop < 0)
13635 goto theend;
13636 }
13637 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013638 if (skip == NULL)
13639 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013641 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13642 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013643
13644theend:
13645 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013646
13647 return retval;
13648}
13649
13650/*
13651 * "searchpair()" function
13652 */
13653 static void
13654f_searchpair(argvars, rettv)
13655 typval_T *argvars;
13656 typval_T *rettv;
13657{
13658 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13659}
13660
13661/*
13662 * "searchpairpos()" function
13663 */
13664 static void
13665f_searchpairpos(argvars, rettv)
13666 typval_T *argvars;
13667 typval_T *rettv;
13668{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013669 pos_T match_pos;
13670 int lnum = 0;
13671 int col = 0;
13672
13673 rettv->vval.v_number = 0;
13674
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013675 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013676 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013677
13678 if (searchpair_cmn(argvars, &match_pos) > 0)
13679 {
13680 lnum = match_pos.lnum;
13681 col = match_pos.col;
13682 }
13683
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013684 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13685 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013686}
13687
13688/*
13689 * Search for a start/middle/end thing.
13690 * Used by searchpair(), see its documentation for the details.
13691 * Returns 0 or -1 for no match,
13692 */
13693 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013694do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013695 char_u *spat; /* start pattern */
13696 char_u *mpat; /* middle pattern */
13697 char_u *epat; /* end pattern */
13698 int dir; /* BACKWARD or FORWARD */
13699 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013700 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013701 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013702 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013703{
13704 char_u *save_cpo;
13705 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13706 long retval = 0;
13707 pos_T pos;
13708 pos_T firstpos;
13709 pos_T foundpos;
13710 pos_T save_cursor;
13711 pos_T save_pos;
13712 int n;
13713 int r;
13714 int nest = 1;
13715 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013716 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013717
13718 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13719 save_cpo = p_cpo;
13720 p_cpo = (char_u *)"";
13721
13722 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13723 * start/middle/end (pat3, for the top pair). */
13724 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13725 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13726 if (pat2 == NULL || pat3 == NULL)
13727 goto theend;
13728 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13729 if (*mpat == NUL)
13730 STRCPY(pat3, pat2);
13731 else
13732 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13733 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013734 if (flags & SP_START)
13735 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013736
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 save_cursor = curwin->w_cursor;
13738 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013739 clearpos(&firstpos);
13740 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 pat = pat3;
13742 for (;;)
13743 {
13744 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013745 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13747 /* didn't find it or found the first match again: FAIL */
13748 break;
13749
13750 if (firstpos.lnum == 0)
13751 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013752 if (equalpos(pos, foundpos))
13753 {
13754 /* Found the same position again. Can happen with a pattern that
13755 * has "\zs" at the end and searching backwards. Advance one
13756 * character and try again. */
13757 if (dir == BACKWARD)
13758 decl(&pos);
13759 else
13760 incl(&pos);
13761 }
13762 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763
13764 /* If the skip pattern matches, ignore this match. */
13765 if (*skip != NUL)
13766 {
13767 save_pos = curwin->w_cursor;
13768 curwin->w_cursor = pos;
13769 r = eval_to_bool(skip, &err, NULL, FALSE);
13770 curwin->w_cursor = save_pos;
13771 if (err)
13772 {
13773 /* Evaluating {skip} caused an error, break here. */
13774 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013775 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 break;
13777 }
13778 if (r)
13779 continue;
13780 }
13781
13782 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13783 {
13784 /* Found end when searching backwards or start when searching
13785 * forward: nested pair. */
13786 ++nest;
13787 pat = pat2; /* nested, don't search for middle */
13788 }
13789 else
13790 {
13791 /* Found end when searching forward or start when searching
13792 * backward: end of (nested) pair; or found middle in outer pair. */
13793 if (--nest == 1)
13794 pat = pat3; /* outer level, search for middle */
13795 }
13796
13797 if (nest == 0)
13798 {
13799 /* Found the match: return matchcount or line number. */
13800 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013801 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013803 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013804 if (flags & SP_SETPCMARK)
13805 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 curwin->w_cursor = pos;
13807 if (!(flags & SP_REPEAT))
13808 break;
13809 nest = 1; /* search for next unmatched */
13810 }
13811 }
13812
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013813 if (match_pos != NULL)
13814 {
13815 /* Store the match cursor position */
13816 match_pos->lnum = curwin->w_cursor.lnum;
13817 match_pos->col = curwin->w_cursor.col + 1;
13818 }
13819
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013821 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 curwin->w_cursor = save_cursor;
13823
13824theend:
13825 vim_free(pat2);
13826 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013828
13829 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830}
13831
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013832/*
13833 * "searchpos()" function
13834 */
13835 static void
13836f_searchpos(argvars, rettv)
13837 typval_T *argvars;
13838 typval_T *rettv;
13839{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013840 pos_T match_pos;
13841 int lnum = 0;
13842 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013843 int n;
13844 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013845
13846 rettv->vval.v_number = 0;
13847
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013848 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013849 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013850
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013851 n = search_cmn(argvars, &match_pos, &flags);
13852 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013853 {
13854 lnum = match_pos.lnum;
13855 col = match_pos.col;
13856 }
13857
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013858 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13859 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013860 if (flags & SP_SUBPAT)
13861 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013862}
13863
13864
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865/*ARGSUSED*/
13866 static void
13867f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013868 typval_T *argvars;
13869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871#ifdef FEAT_CLIENTSERVER
13872 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013873 char_u *server = get_tv_string_chk(&argvars[0]);
13874 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875
Bram Moolenaar0d660222005-01-07 21:51:51 +000013876 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 if (server == NULL || reply == NULL)
13878 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013879 if (check_restricted() || check_secure())
13880 return;
13881# ifdef FEAT_X11
13882 if (check_connection() == FAIL)
13883 return;
13884# endif
13885
13886 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013888 EMSG(_("E258: Unable to send to client"));
13889 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013891 rettv->vval.v_number = 0;
13892#else
13893 rettv->vval.v_number = -1;
13894#endif
13895}
13896
13897/*ARGSUSED*/
13898 static void
13899f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013900 typval_T *argvars;
13901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013902{
13903 char_u *r = NULL;
13904
13905#ifdef FEAT_CLIENTSERVER
13906# ifdef WIN32
13907 r = serverGetVimNames();
13908# else
13909 make_connection();
13910 if (X_DISPLAY != NULL)
13911 r = serverGetVimNames(X_DISPLAY);
13912# endif
13913#endif
13914 rettv->v_type = VAR_STRING;
13915 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916}
13917
13918/*
13919 * "setbufvar()" function
13920 */
13921/*ARGSUSED*/
13922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013924 typval_T *argvars;
13925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926{
13927 buf_T *buf;
13928#ifdef FEAT_AUTOCMD
13929 aco_save_T aco;
13930#else
13931 buf_T *save_curbuf;
13932#endif
13933 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013934 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 char_u nbuf[NUMBUFLEN];
13936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937 rettv->vval.v_number = 0;
13938
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 if (check_restricted() || check_secure())
13940 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013941 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13942 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013943 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 varp = &argvars[2];
13945
13946 if (buf != NULL && varname != NULL && varp != NULL)
13947 {
13948 /* set curbuf to be our buf, temporarily */
13949#ifdef FEAT_AUTOCMD
13950 aucmd_prepbuf(&aco, buf);
13951#else
13952 save_curbuf = curbuf;
13953 curbuf = buf;
13954#endif
13955
13956 if (*varname == '&')
13957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 long numval;
13959 char_u *strval;
13960 int error = FALSE;
13961
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013963 numval = get_tv_number_chk(varp, &error);
13964 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965 if (!error && strval != NULL)
13966 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 }
13968 else
13969 {
13970 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13971 if (bufvarname != NULL)
13972 {
13973 STRCPY(bufvarname, "b:");
13974 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013975 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 vim_free(bufvarname);
13977 }
13978 }
13979
13980 /* reset notion of buffer */
13981#ifdef FEAT_AUTOCMD
13982 aucmd_restbuf(&aco);
13983#else
13984 curbuf = save_curbuf;
13985#endif
13986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987}
13988
13989/*
13990 * "setcmdpos()" function
13991 */
13992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013994 typval_T *argvars;
13995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997 int pos = (int)get_tv_number(&argvars[0]) - 1;
13998
13999 if (pos >= 0)
14000 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001}
14002
14003/*
14004 * "setline()" function
14005 */
14006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014007f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014008 typval_T *argvars;
14009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010{
14011 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014012 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014013 list_T *l = NULL;
14014 listitem_T *li = NULL;
14015 long added = 0;
14016 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014018 lnum = get_tv_lnum(&argvars[0]);
14019 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014021 l = argvars[1].vval.v_list;
14022 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014024 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014025 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014026
14027 rettv->vval.v_number = 0; /* OK */
14028 for (;;)
14029 {
14030 if (l != NULL)
14031 {
14032 /* list argument, get next string */
14033 if (li == NULL)
14034 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014035 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014036 li = li->li_next;
14037 }
14038
14039 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014041 break;
14042 if (lnum <= curbuf->b_ml.ml_line_count)
14043 {
14044 /* existing line, replace it */
14045 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14046 {
14047 changed_bytes(lnum, 0);
14048 check_cursor_col();
14049 rettv->vval.v_number = 0; /* OK */
14050 }
14051 }
14052 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14053 {
14054 /* lnum is one past the last line, append the line */
14055 ++added;
14056 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14057 rettv->vval.v_number = 0; /* OK */
14058 }
14059
14060 if (l == NULL) /* only one string argument */
14061 break;
14062 ++lnum;
14063 }
14064
14065 if (added > 0)
14066 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067}
14068
14069/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014070 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014071 */
14072/*ARGSUSED*/
14073 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014074set_qf_ll_list(wp, list_arg, action_arg, rettv)
14075 win_T *wp;
14076 typval_T *list_arg;
14077 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014078 typval_T *rettv;
14079{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014080#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014081 char_u *act;
14082 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014083#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014084
Bram Moolenaar2641f772005-03-25 21:58:17 +000014085 rettv->vval.v_number = -1;
14086
14087#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014088 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014089 EMSG(_(e_listreq));
14090 else
14091 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014092 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014093
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014094 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014095 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014096 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 if (act == NULL)
14098 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014099 if (*act == 'a' || *act == 'r')
14100 action = *act;
14101 }
14102
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014103 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014104 rettv->vval.v_number = 0;
14105 }
14106#endif
14107}
14108
14109/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014110 * "setloclist()" function
14111 */
14112/*ARGSUSED*/
14113 static void
14114f_setloclist(argvars, rettv)
14115 typval_T *argvars;
14116 typval_T *rettv;
14117{
14118 win_T *win;
14119
14120 rettv->vval.v_number = -1;
14121
14122 win = find_win_by_nr(&argvars[0]);
14123 if (win != NULL)
14124 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14125}
14126
14127/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014128 * "setpos()" function
14129 */
14130/*ARGSUSED*/
14131 static void
14132f_setpos(argvars, rettv)
14133 typval_T *argvars;
14134 typval_T *rettv;
14135{
14136 pos_T pos;
14137 int fnum;
14138 char_u *name;
14139
14140 name = get_tv_string_chk(argvars);
14141 if (name != NULL)
14142 {
14143 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14144 {
14145 --pos.col;
14146 if (name[0] == '.') /* cursor */
14147 {
14148 if (fnum == curbuf->b_fnum)
14149 {
14150 curwin->w_cursor = pos;
14151 check_cursor();
14152 }
14153 else
14154 EMSG(_(e_invarg));
14155 }
14156 else if (name[0] == '\'') /* mark */
14157 (void)setmark_pos(name[1], &pos, fnum);
14158 else
14159 EMSG(_(e_invarg));
14160 }
14161 }
14162}
14163
14164/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014165 * "setqflist()" function
14166 */
14167/*ARGSUSED*/
14168 static void
14169f_setqflist(argvars, rettv)
14170 typval_T *argvars;
14171 typval_T *rettv;
14172{
14173 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14174}
14175
14176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 * "setreg()" function
14178 */
14179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014180f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014181 typval_T *argvars;
14182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183{
14184 int regname;
14185 char_u *strregname;
14186 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 int append;
14189 char_u yank_type;
14190 long block_len;
14191
14192 block_len = -1;
14193 yank_type = MAUTO;
14194 append = FALSE;
14195
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014197 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 if (strregname == NULL)
14200 return; /* type error; errmsg already given */
14201 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 if (regname == 0 || regname == '@')
14203 regname = '"';
14204 else if (regname == '=')
14205 return;
14206
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014207 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 stropt = get_tv_string_chk(&argvars[2]);
14210 if (stropt == NULL)
14211 return; /* type error */
14212 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 switch (*stropt)
14214 {
14215 case 'a': case 'A': /* append */
14216 append = TRUE;
14217 break;
14218 case 'v': case 'c': /* character-wise selection */
14219 yank_type = MCHAR;
14220 break;
14221 case 'V': case 'l': /* line-wise selection */
14222 yank_type = MLINE;
14223 break;
14224#ifdef FEAT_VISUAL
14225 case 'b': case Ctrl_V: /* block-wise selection */
14226 yank_type = MBLOCK;
14227 if (VIM_ISDIGIT(stropt[1]))
14228 {
14229 ++stropt;
14230 block_len = getdigits(&stropt) - 1;
14231 --stropt;
14232 }
14233 break;
14234#endif
14235 }
14236 }
14237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 strval = get_tv_string_chk(&argvars[1]);
14239 if (strval != NULL)
14240 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243}
14244
14245
14246/*
14247 * "setwinvar(expr)" function
14248 */
14249/*ARGSUSED*/
14250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014252 typval_T *argvars;
14253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254{
14255 win_T *win;
14256#ifdef FEAT_WINDOWS
14257 win_T *save_curwin;
14258#endif
14259 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014260 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 char_u nbuf[NUMBUFLEN];
14262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 rettv->vval.v_number = 0;
14264
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265 if (check_restricted() || check_secure())
14266 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 varp = &argvars[2];
14270
14271 if (win != NULL && varname != NULL && varp != NULL)
14272 {
14273#ifdef FEAT_WINDOWS
14274 /* set curwin to be our win, temporarily */
14275 save_curwin = curwin;
14276 curwin = win;
14277 curbuf = curwin->w_buffer;
14278#endif
14279
14280 if (*varname == '&')
14281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014282 long numval;
14283 char_u *strval;
14284 int error = FALSE;
14285
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014287 numval = get_tv_number_chk(varp, &error);
14288 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289 if (!error && strval != NULL)
14290 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 }
14292 else
14293 {
14294 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14295 if (winvarname != NULL)
14296 {
14297 STRCPY(winvarname, "w:");
14298 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014299 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 vim_free(winvarname);
14301 }
14302 }
14303
14304#ifdef FEAT_WINDOWS
14305 /* Restore current window, if it's still valid (autocomands can make
14306 * it invalid). */
14307 if (win_valid(save_curwin))
14308 {
14309 curwin = save_curwin;
14310 curbuf = curwin->w_buffer;
14311 }
14312#endif
14313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314}
14315
14316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014317 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 */
14319 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014320f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014321 typval_T *argvars;
14322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325
Bram Moolenaar0d660222005-01-07 21:51:51 +000014326 p = get_tv_string(&argvars[0]);
14327 rettv->vval.v_string = vim_strsave(p);
14328 simplify_filename(rettv->vval.v_string); /* simplify in place */
14329 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330}
14331
Bram Moolenaar0d660222005-01-07 21:51:51 +000014332static int
14333#ifdef __BORLANDC__
14334 _RTLENTRYF
14335#endif
14336 item_compare __ARGS((const void *s1, const void *s2));
14337static int
14338#ifdef __BORLANDC__
14339 _RTLENTRYF
14340#endif
14341 item_compare2 __ARGS((const void *s1, const void *s2));
14342
14343static int item_compare_ic;
14344static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014346#define ITEM_COMPARE_FAIL 999
14347
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014349 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014351 static int
14352#ifdef __BORLANDC__
14353_RTLENTRYF
14354#endif
14355item_compare(s1, s2)
14356 const void *s1;
14357 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014359 char_u *p1, *p2;
14360 char_u *tofree1, *tofree2;
14361 int res;
14362 char_u numbuf1[NUMBUFLEN];
14363 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014365 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14366 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367 if (item_compare_ic)
14368 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014370 res = STRCMP(p1, p2);
14371 vim_free(tofree1);
14372 vim_free(tofree2);
14373 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374}
14375
14376 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377#ifdef __BORLANDC__
14378_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014380item_compare2(s1, s2)
14381 const void *s1;
14382 const void *s2;
14383{
14384 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014385 typval_T rettv;
14386 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014387 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 /* shortcut after failure in previous call; compare all items equal */
14390 if (item_compare_func_err)
14391 return 0;
14392
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014393 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14394 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014395 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14396 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014397
14398 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14399 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014400 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014401 clear_tv(&argv[0]);
14402 clear_tv(&argv[1]);
14403
14404 if (res == FAIL)
14405 res = ITEM_COMPARE_FAIL;
14406 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 /* return value has wrong type */
14408 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14409 if (item_compare_func_err)
14410 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014411 clear_tv(&rettv);
14412 return res;
14413}
14414
14415/*
14416 * "sort({list})" function
14417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014419f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014420 typval_T *argvars;
14421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422{
Bram Moolenaar33570922005-01-25 22:26:29 +000014423 list_T *l;
14424 listitem_T *li;
14425 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426 long len;
14427 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428
Bram Moolenaar0d660222005-01-07 21:51:51 +000014429 rettv->vval.v_number = 0;
14430 if (argvars[0].v_type != VAR_LIST)
14431 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 else
14433 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014435 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436 return;
14437 rettv->vval.v_list = l;
14438 rettv->v_type = VAR_LIST;
14439 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 len = list_len(l);
14442 if (len <= 1)
14443 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445 item_compare_ic = FALSE;
14446 item_compare_func = NULL;
14447 if (argvars[1].v_type != VAR_UNKNOWN)
14448 {
14449 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014450 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014451 else
14452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 int error = FALSE;
14454
14455 i = get_tv_number_chk(&argvars[1], &error);
14456 if (error)
14457 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014458 if (i == 1)
14459 item_compare_ic = TRUE;
14460 else
14461 item_compare_func = get_tv_string(&argvars[1]);
14462 }
14463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014466 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014467 if (ptrs == NULL)
14468 return;
14469 i = 0;
14470 for (li = l->lv_first; li != NULL; li = li->li_next)
14471 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014473 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474 /* test the compare function */
14475 if (item_compare_func != NULL
14476 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14477 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014478 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480 {
14481 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014482 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014483 item_compare_func == NULL ? item_compare : item_compare2);
14484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 if (!item_compare_func_err)
14486 {
14487 /* Clear the List and append the items in the sorted order. */
14488 l->lv_first = l->lv_last = NULL;
14489 l->lv_len = 0;
14490 for (i = 0; i < len; ++i)
14491 list_append(l, ptrs[i]);
14492 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493 }
14494
14495 vim_free(ptrs);
14496 }
14497}
14498
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014499/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014500 * "soundfold({word})" function
14501 */
14502 static void
14503f_soundfold(argvars, rettv)
14504 typval_T *argvars;
14505 typval_T *rettv;
14506{
14507 char_u *s;
14508
14509 rettv->v_type = VAR_STRING;
14510 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014511#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014512 rettv->vval.v_string = eval_soundfold(s);
14513#else
14514 rettv->vval.v_string = vim_strsave(s);
14515#endif
14516}
14517
14518/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014519 * "spellbadword()" function
14520 */
14521/* ARGSUSED */
14522 static void
14523f_spellbadword(argvars, rettv)
14524 typval_T *argvars;
14525 typval_T *rettv;
14526{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014527 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014528 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014529 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014530
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014531 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014532 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014533
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014534#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014535 if (argvars[0].v_type == VAR_UNKNOWN)
14536 {
14537 /* Find the start and length of the badly spelled word. */
14538 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14539 if (len != 0)
14540 word = ml_get_cursor();
14541 }
14542 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14543 {
14544 char_u *str = get_tv_string_chk(&argvars[0]);
14545 int capcol = -1;
14546
14547 if (str != NULL)
14548 {
14549 /* Check the argument for spelling. */
14550 while (*str != NUL)
14551 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014552 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014553 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014554 {
14555 word = str;
14556 break;
14557 }
14558 str += len;
14559 }
14560 }
14561 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014562#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014564 list_append_string(rettv->vval.v_list, word, len);
14565 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014566 attr == HLF_SPB ? "bad" :
14567 attr == HLF_SPR ? "rare" :
14568 attr == HLF_SPL ? "local" :
14569 attr == HLF_SPC ? "caps" :
14570 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014571}
14572
14573/*
14574 * "spellsuggest()" function
14575 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014576/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014577 static void
14578f_spellsuggest(argvars, rettv)
14579 typval_T *argvars;
14580 typval_T *rettv;
14581{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014582#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014583 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014584 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014585 int maxcount;
14586 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014587 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014588 listitem_T *li;
14589 int need_capital = FALSE;
14590#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014591
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014592 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014593 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014594
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014595#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014596 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14597 {
14598 str = get_tv_string(&argvars[0]);
14599 if (argvars[1].v_type != VAR_UNKNOWN)
14600 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014601 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014602 if (maxcount <= 0)
14603 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014604 if (argvars[2].v_type != VAR_UNKNOWN)
14605 {
14606 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14607 if (typeerr)
14608 return;
14609 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014610 }
14611 else
14612 maxcount = 25;
14613
Bram Moolenaar4770d092006-01-12 23:22:24 +000014614 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014615
14616 for (i = 0; i < ga.ga_len; ++i)
14617 {
14618 str = ((char_u **)ga.ga_data)[i];
14619
14620 li = listitem_alloc();
14621 if (li == NULL)
14622 vim_free(str);
14623 else
14624 {
14625 li->li_tv.v_type = VAR_STRING;
14626 li->li_tv.v_lock = 0;
14627 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014628 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014629 }
14630 }
14631 ga_clear(&ga);
14632 }
14633#endif
14634}
14635
Bram Moolenaar0d660222005-01-07 21:51:51 +000014636 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014637f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014638 typval_T *argvars;
14639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014640{
14641 char_u *str;
14642 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014643 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 regmatch_T regmatch;
14645 char_u patbuf[NUMBUFLEN];
14646 char_u *save_cpo;
14647 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014649 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014651
14652 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14653 save_cpo = p_cpo;
14654 p_cpo = (char_u *)"";
14655
14656 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014657 if (argvars[1].v_type != VAR_UNKNOWN)
14658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014659 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14660 if (pat == NULL)
14661 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014662 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014663 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014664 }
14665 if (pat == NULL || *pat == NUL)
14666 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014667
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014668 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014670 if (typeerr)
14671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672
Bram Moolenaar0d660222005-01-07 21:51:51 +000014673 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14674 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014676 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014677 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014678 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014679 if (*str == NUL)
14680 match = FALSE; /* empty item at the end */
14681 else
14682 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683 if (match)
14684 end = regmatch.startp[0];
14685 else
14686 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014687 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14688 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014690 if (list_append_string(rettv->vval.v_list, str,
14691 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014693 }
14694 if (!match)
14695 break;
14696 /* Advance to just after the match. */
14697 if (regmatch.endp[0] > str)
14698 col = 0;
14699 else
14700 {
14701 /* Don't get stuck at the same match. */
14702#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014703 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014704#else
14705 col = 1;
14706#endif
14707 }
14708 str = regmatch.endp[0];
14709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
Bram Moolenaar0d660222005-01-07 21:51:51 +000014711 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713
Bram Moolenaar0d660222005-01-07 21:51:51 +000014714 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715}
14716
Bram Moolenaar2c932302006-03-18 21:42:09 +000014717/*
14718 * "str2nr()" function
14719 */
14720 static void
14721f_str2nr(argvars, rettv)
14722 typval_T *argvars;
14723 typval_T *rettv;
14724{
14725 int base = 10;
14726 char_u *p;
14727 long n;
14728
14729 if (argvars[1].v_type != VAR_UNKNOWN)
14730 {
14731 base = get_tv_number(&argvars[1]);
14732 if (base != 8 && base != 10 && base != 16)
14733 {
14734 EMSG(_(e_invarg));
14735 return;
14736 }
14737 }
14738
14739 p = skipwhite(get_tv_string(&argvars[0]));
14740 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
14741 rettv->vval.v_number = n;
14742}
14743
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744#ifdef HAVE_STRFTIME
14745/*
14746 * "strftime({format}[, {time}])" function
14747 */
14748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014750 typval_T *argvars;
14751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752{
14753 char_u result_buf[256];
14754 struct tm *curtime;
14755 time_t seconds;
14756 char_u *p;
14757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014761 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 seconds = time(NULL);
14763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 curtime = localtime(&seconds);
14766 /* MSVC returns NULL for an invalid value of seconds. */
14767 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 else
14770 {
14771# ifdef FEAT_MBYTE
14772 vimconv_T conv;
14773 char_u *enc;
14774
14775 conv.vc_type = CONV_NONE;
14776 enc = enc_locale();
14777 convert_setup(&conv, p_enc, enc);
14778 if (conv.vc_type != CONV_NONE)
14779 p = string_convert(&conv, p, NULL);
14780# endif
14781 if (p != NULL)
14782 (void)strftime((char *)result_buf, sizeof(result_buf),
14783 (char *)p, curtime);
14784 else
14785 result_buf[0] = NUL;
14786
14787# ifdef FEAT_MBYTE
14788 if (conv.vc_type != CONV_NONE)
14789 vim_free(p);
14790 convert_setup(&conv, enc, p_enc);
14791 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 else
14794# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796
14797# ifdef FEAT_MBYTE
14798 /* Release conversion descriptors */
14799 convert_setup(&conv, NULL, NULL);
14800 vim_free(enc);
14801# endif
14802 }
14803}
14804#endif
14805
14806/*
14807 * "stridx()" function
14808 */
14809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014810f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014811 typval_T *argvars;
14812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813{
14814 char_u buf[NUMBUFLEN];
14815 char_u *needle;
14816 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014817 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014819 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014821 needle = get_tv_string_chk(&argvars[1]);
14822 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014823 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014824 if (needle == NULL || haystack == NULL)
14825 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826
Bram Moolenaar33570922005-01-25 22:26:29 +000014827 if (argvars[2].v_type != VAR_UNKNOWN)
14828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014829 int error = FALSE;
14830
14831 start_idx = get_tv_number_chk(&argvars[2], &error);
14832 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014833 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014834 if (start_idx >= 0)
14835 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014836 }
14837
14838 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14839 if (pos != NULL)
14840 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841}
14842
14843/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014844 * "string()" function
14845 */
14846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014847f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014848 typval_T *argvars;
14849 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014850{
14851 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014852 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014855 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014856 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014857 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858}
14859
14860/*
14861 * "strlen()" function
14862 */
14863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014865 typval_T *argvars;
14866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 rettv->vval.v_number = (varnumber_T)(STRLEN(
14869 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870}
14871
14872/*
14873 * "strpart()" function
14874 */
14875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014877 typval_T *argvars;
14878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014879{
14880 char_u *p;
14881 int n;
14882 int len;
14883 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014884 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014886 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 slen = (int)STRLEN(p);
14888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014889 n = get_tv_number_chk(&argvars[1], &error);
14890 if (error)
14891 len = 0;
14892 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014893 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 else
14895 len = slen - n; /* default len: all bytes that are available. */
14896
14897 /*
14898 * Only return the overlap between the specified part and the actual
14899 * string.
14900 */
14901 if (n < 0)
14902 {
14903 len += n;
14904 n = 0;
14905 }
14906 else if (n > slen)
14907 n = slen;
14908 if (len < 0)
14909 len = 0;
14910 else if (n + len > slen)
14911 len = slen - n;
14912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->v_type = VAR_STRING;
14914 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915}
14916
14917/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014918 * "strridx()" function
14919 */
14920 static void
14921f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014922 typval_T *argvars;
14923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014924{
14925 char_u buf[NUMBUFLEN];
14926 char_u *needle;
14927 char_u *haystack;
14928 char_u *rest;
14929 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014930 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014932 needle = get_tv_string_chk(&argvars[1]);
14933 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014934 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014935
14936 rettv->vval.v_number = -1;
14937 if (needle == NULL || haystack == NULL)
14938 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014939 if (argvars[2].v_type != VAR_UNKNOWN)
14940 {
14941 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014942 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014943 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014944 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014945 }
14946 else
14947 end_idx = haystack_len;
14948
Bram Moolenaar0d660222005-01-07 21:51:51 +000014949 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014950 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014951 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014952 lastmatch = haystack + end_idx;
14953 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014955 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014956 for (rest = haystack; *rest != '\0'; ++rest)
14957 {
14958 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014959 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960 break;
14961 lastmatch = rest;
14962 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014963 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014964
14965 if (lastmatch == NULL)
14966 rettv->vval.v_number = -1;
14967 else
14968 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14969}
14970
14971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 * "strtrans()" function
14973 */
14974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014976 typval_T *argvars;
14977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 rettv->v_type = VAR_STRING;
14980 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981}
14982
14983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984 * "submatch()" function
14985 */
14986 static void
14987f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014988 typval_T *argvars;
14989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014990{
14991 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014992 rettv->vval.v_string =
14993 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994}
14995
14996/*
14997 * "substitute()" function
14998 */
14999 static void
15000f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015001 typval_T *argvars;
15002 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003{
15004 char_u patbuf[NUMBUFLEN];
15005 char_u subbuf[NUMBUFLEN];
15006 char_u flagsbuf[NUMBUFLEN];
15007
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015008 char_u *str = get_tv_string_chk(&argvars[0]);
15009 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15010 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15011 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15012
Bram Moolenaar0d660222005-01-07 21:51:51 +000015013 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015014 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15015 rettv->vval.v_string = NULL;
15016 else
15017 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018}
15019
15020/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015021 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022 */
15023/*ARGSUSED*/
15024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015025f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015026 typval_T *argvars;
15027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028{
15029 int id = 0;
15030#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015031 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 long col;
15033 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015034 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 lnum = get_tv_lnum(argvars); /* -1 on type error */
15037 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15038 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015040 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015041 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015042 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043#endif
15044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046}
15047
15048/*
15049 * "synIDattr(id, what [, mode])" function
15050 */
15051/*ARGSUSED*/
15052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015054 typval_T *argvars;
15055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056{
15057 char_u *p = NULL;
15058#ifdef FEAT_SYN_HL
15059 int id;
15060 char_u *what;
15061 char_u *mode;
15062 char_u modebuf[NUMBUFLEN];
15063 int modec;
15064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065 id = get_tv_number(&argvars[0]);
15066 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015067 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015069 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 modec = TOLOWER_ASC(mode[0]);
15071 if (modec != 't' && modec != 'c'
15072#ifdef FEAT_GUI
15073 && modec != 'g'
15074#endif
15075 )
15076 modec = 0; /* replace invalid with current */
15077 }
15078 else
15079 {
15080#ifdef FEAT_GUI
15081 if (gui.in_use)
15082 modec = 'g';
15083 else
15084#endif
15085 if (t_colors > 1)
15086 modec = 'c';
15087 else
15088 modec = 't';
15089 }
15090
15091
15092 switch (TOLOWER_ASC(what[0]))
15093 {
15094 case 'b':
15095 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15096 p = highlight_color(id, what, modec);
15097 else /* bold */
15098 p = highlight_has_attr(id, HL_BOLD, modec);
15099 break;
15100
15101 case 'f': /* fg[#] */
15102 p = highlight_color(id, what, modec);
15103 break;
15104
15105 case 'i':
15106 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15107 p = highlight_has_attr(id, HL_INVERSE, modec);
15108 else /* italic */
15109 p = highlight_has_attr(id, HL_ITALIC, modec);
15110 break;
15111
15112 case 'n': /* name */
15113 p = get_highlight_name(NULL, id - 1);
15114 break;
15115
15116 case 'r': /* reverse */
15117 p = highlight_has_attr(id, HL_INVERSE, modec);
15118 break;
15119
15120 case 's': /* standout */
15121 p = highlight_has_attr(id, HL_STANDOUT, modec);
15122 break;
15123
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015124 case 'u':
15125 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15126 /* underline */
15127 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15128 else
15129 /* undercurl */
15130 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 break;
15132 }
15133
15134 if (p != NULL)
15135 p = vim_strsave(p);
15136#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->v_type = VAR_STRING;
15138 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139}
15140
15141/*
15142 * "synIDtrans(id)" function
15143 */
15144/*ARGSUSED*/
15145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015146f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 typval_T *argvars;
15148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149{
15150 int id;
15151
15152#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015153 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154
15155 if (id > 0)
15156 id = syn_get_final_id(id);
15157 else
15158#endif
15159 id = 0;
15160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015161 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162}
15163
15164/*
15165 * "system()" function
15166 */
15167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015168f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 typval_T *argvars;
15170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015172 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015174 char_u *infile = NULL;
15175 char_u buf[NUMBUFLEN];
15176 int err = FALSE;
15177 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015179 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015180 {
15181 /*
15182 * Write the string to a temp file, to be used for input of the shell
15183 * command.
15184 */
15185 if ((infile = vim_tempname('i')) == NULL)
15186 {
15187 EMSG(_(e_notmp));
15188 return;
15189 }
15190
15191 fd = mch_fopen((char *)infile, WRITEBIN);
15192 if (fd == NULL)
15193 {
15194 EMSG2(_(e_notopen), infile);
15195 goto done;
15196 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 p = get_tv_string_buf_chk(&argvars[1], buf);
15198 if (p == NULL)
15199 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015200 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15201 err = TRUE;
15202 if (fclose(fd) != 0)
15203 err = TRUE;
15204 if (err)
15205 {
15206 EMSG(_("E677: Error writing temp file"));
15207 goto done;
15208 }
15209 }
15210
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015211 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15212 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015213
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214#ifdef USE_CR
15215 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015216 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 {
15218 char_u *s;
15219
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015220 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 {
15222 if (*s == CAR)
15223 *s = NL;
15224 }
15225 }
15226#else
15227# ifdef USE_CRNL
15228 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015229 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 {
15231 char_u *s, *d;
15232
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015233 d = res;
15234 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 {
15236 if (s[0] == CAR && s[1] == NL)
15237 ++s;
15238 *d++ = *s;
15239 }
15240 *d = NUL;
15241 }
15242# endif
15243#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015244
15245done:
15246 if (infile != NULL)
15247 {
15248 mch_remove(infile);
15249 vim_free(infile);
15250 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015251 rettv->v_type = VAR_STRING;
15252 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253}
15254
15255/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015256 * "tabpagebuflist()" function
15257 */
15258/* ARGSUSED */
15259 static void
15260f_tabpagebuflist(argvars, rettv)
15261 typval_T *argvars;
15262 typval_T *rettv;
15263{
15264#ifndef FEAT_WINDOWS
15265 rettv->vval.v_number = 0;
15266#else
15267 tabpage_T *tp;
15268 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015269
15270 if (argvars[0].v_type == VAR_UNKNOWN)
15271 wp = firstwin;
15272 else
15273 {
15274 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15275 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015276 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015277 }
15278 if (wp == NULL)
15279 rettv->vval.v_number = 0;
15280 else
15281 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015282 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015283 rettv->vval.v_number = 0;
15284 else
15285 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015286 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015287 if (list_append_number(rettv->vval.v_list,
15288 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015289 break;
15290 }
15291 }
15292#endif
15293}
15294
15295
15296/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015297 * "tabpagenr()" function
15298 */
15299/* ARGSUSED */
15300 static void
15301f_tabpagenr(argvars, rettv)
15302 typval_T *argvars;
15303 typval_T *rettv;
15304{
15305 int nr = 1;
15306#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015307 char_u *arg;
15308
15309 if (argvars[0].v_type != VAR_UNKNOWN)
15310 {
15311 arg = get_tv_string_chk(&argvars[0]);
15312 nr = 0;
15313 if (arg != NULL)
15314 {
15315 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015316 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015317 else
15318 EMSG2(_(e_invexpr2), arg);
15319 }
15320 }
15321 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015322 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015323#endif
15324 rettv->vval.v_number = nr;
15325}
15326
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015327
15328#ifdef FEAT_WINDOWS
15329static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15330
15331/*
15332 * Common code for tabpagewinnr() and winnr().
15333 */
15334 static int
15335get_winnr(tp, argvar)
15336 tabpage_T *tp;
15337 typval_T *argvar;
15338{
15339 win_T *twin;
15340 int nr = 1;
15341 win_T *wp;
15342 char_u *arg;
15343
15344 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15345 if (argvar->v_type != VAR_UNKNOWN)
15346 {
15347 arg = get_tv_string_chk(argvar);
15348 if (arg == NULL)
15349 nr = 0; /* type error; errmsg already given */
15350 else if (STRCMP(arg, "$") == 0)
15351 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15352 else if (STRCMP(arg, "#") == 0)
15353 {
15354 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15355 if (twin == NULL)
15356 nr = 0;
15357 }
15358 else
15359 {
15360 EMSG2(_(e_invexpr2), arg);
15361 nr = 0;
15362 }
15363 }
15364
15365 if (nr > 0)
15366 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15367 wp != twin; wp = wp->w_next)
15368 ++nr;
15369 return nr;
15370}
15371#endif
15372
15373/*
15374 * "tabpagewinnr()" function
15375 */
15376/* ARGSUSED */
15377 static void
15378f_tabpagewinnr(argvars, rettv)
15379 typval_T *argvars;
15380 typval_T *rettv;
15381{
15382 int nr = 1;
15383#ifdef FEAT_WINDOWS
15384 tabpage_T *tp;
15385
15386 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15387 if (tp == NULL)
15388 nr = 0;
15389 else
15390 nr = get_winnr(tp, &argvars[1]);
15391#endif
15392 rettv->vval.v_number = nr;
15393}
15394
15395
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015396/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015397 * "tagfiles()" function
15398 */
15399/*ARGSUSED*/
15400 static void
15401f_tagfiles(argvars, rettv)
15402 typval_T *argvars;
15403 typval_T *rettv;
15404{
15405 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015406 tagname_T tn;
15407 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015408
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015409 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015410 {
15411 rettv->vval.v_number = 0;
15412 return;
15413 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015414
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015415 for (first = TRUE; ; first = FALSE)
15416 if (get_tagfname(&tn, first, fname) == FAIL
15417 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015418 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015419 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015420}
15421
15422/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015423 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015424 */
15425 static void
15426f_taglist(argvars, rettv)
15427 typval_T *argvars;
15428 typval_T *rettv;
15429{
15430 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015431
15432 tag_pattern = get_tv_string(&argvars[0]);
15433
15434 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015435 if (*tag_pattern == NUL)
15436 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015437
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015438 if (rettv_list_alloc(rettv) == OK)
15439 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015440}
15441
15442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 * "tempname()" function
15444 */
15445/*ARGSUSED*/
15446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015448 typval_T *argvars;
15449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450{
15451 static int x = 'A';
15452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 rettv->v_type = VAR_STRING;
15454 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455
15456 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15457 * names. Skip 'I' and 'O', they are used for shell redirection. */
15458 do
15459 {
15460 if (x == 'Z')
15461 x = '0';
15462 else if (x == '9')
15463 x = 'A';
15464 else
15465 {
15466#ifdef EBCDIC
15467 if (x == 'I')
15468 x = 'J';
15469 else if (x == 'R')
15470 x = 'S';
15471 else
15472#endif
15473 ++x;
15474 }
15475 } while (x == 'I' || x == 'O');
15476}
15477
15478/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015479 * "test(list)" function: Just checking the walls...
15480 */
15481/*ARGSUSED*/
15482 static void
15483f_test(argvars, rettv)
15484 typval_T *argvars;
15485 typval_T *rettv;
15486{
15487 /* Used for unit testing. Change the code below to your liking. */
15488#if 0
15489 listitem_T *li;
15490 list_T *l;
15491 char_u *bad, *good;
15492
15493 if (argvars[0].v_type != VAR_LIST)
15494 return;
15495 l = argvars[0].vval.v_list;
15496 if (l == NULL)
15497 return;
15498 li = l->lv_first;
15499 if (li == NULL)
15500 return;
15501 bad = get_tv_string(&li->li_tv);
15502 li = li->li_next;
15503 if (li == NULL)
15504 return;
15505 good = get_tv_string(&li->li_tv);
15506 rettv->vval.v_number = test_edit_score(bad, good);
15507#endif
15508}
15509
15510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 * "tolower(string)" function
15512 */
15513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015515 typval_T *argvars;
15516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517{
15518 char_u *p;
15519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 p = vim_strsave(get_tv_string(&argvars[0]));
15521 rettv->v_type = VAR_STRING;
15522 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
15524 if (p != NULL)
15525 while (*p != NUL)
15526 {
15527#ifdef FEAT_MBYTE
15528 int l;
15529
15530 if (enc_utf8)
15531 {
15532 int c, lc;
15533
15534 c = utf_ptr2char(p);
15535 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015536 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 /* TODO: reallocate string when byte count changes. */
15538 if (utf_char2len(lc) == l)
15539 utf_char2bytes(lc, p);
15540 p += l;
15541 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015542 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 p += l; /* skip multi-byte character */
15544 else
15545#endif
15546 {
15547 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15548 ++p;
15549 }
15550 }
15551}
15552
15553/*
15554 * "toupper(string)" function
15555 */
15556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015558 typval_T *argvars;
15559 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015562 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563}
15564
15565/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015566 * "tr(string, fromstr, tostr)" function
15567 */
15568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015569f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015570 typval_T *argvars;
15571 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015572{
15573 char_u *instr;
15574 char_u *fromstr;
15575 char_u *tostr;
15576 char_u *p;
15577#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015578 int inlen;
15579 int fromlen;
15580 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015581 int idx;
15582 char_u *cpstr;
15583 int cplen;
15584 int first = TRUE;
15585#endif
15586 char_u buf[NUMBUFLEN];
15587 char_u buf2[NUMBUFLEN];
15588 garray_T ga;
15589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015591 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15592 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015593
15594 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595 rettv->v_type = VAR_STRING;
15596 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015597 if (fromstr == NULL || tostr == NULL)
15598 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015599 ga_init2(&ga, (int)sizeof(char), 80);
15600
15601#ifdef FEAT_MBYTE
15602 if (!has_mbyte)
15603#endif
15604 /* not multi-byte: fromstr and tostr must be the same length */
15605 if (STRLEN(fromstr) != STRLEN(tostr))
15606 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015607#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015608error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015609#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015610 EMSG2(_(e_invarg2), fromstr);
15611 ga_clear(&ga);
15612 return;
15613 }
15614
15615 /* fromstr and tostr have to contain the same number of chars */
15616 while (*instr != NUL)
15617 {
15618#ifdef FEAT_MBYTE
15619 if (has_mbyte)
15620 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015621 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015622 cpstr = instr;
15623 cplen = inlen;
15624 idx = 0;
15625 for (p = fromstr; *p != NUL; p += fromlen)
15626 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015627 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015628 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15629 {
15630 for (p = tostr; *p != NUL; p += tolen)
15631 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015632 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015633 if (idx-- == 0)
15634 {
15635 cplen = tolen;
15636 cpstr = p;
15637 break;
15638 }
15639 }
15640 if (*p == NUL) /* tostr is shorter than fromstr */
15641 goto error;
15642 break;
15643 }
15644 ++idx;
15645 }
15646
15647 if (first && cpstr == instr)
15648 {
15649 /* Check that fromstr and tostr have the same number of
15650 * (multi-byte) characters. Done only once when a character
15651 * of instr doesn't appear in fromstr. */
15652 first = FALSE;
15653 for (p = tostr; *p != NUL; p += tolen)
15654 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015655 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015656 --idx;
15657 }
15658 if (idx != 0)
15659 goto error;
15660 }
15661
15662 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015663 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015664 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015665
15666 instr += inlen;
15667 }
15668 else
15669#endif
15670 {
15671 /* When not using multi-byte chars we can do it faster. */
15672 p = vim_strchr(fromstr, *instr);
15673 if (p != NULL)
15674 ga_append(&ga, tostr[p - fromstr]);
15675 else
15676 ga_append(&ga, *instr);
15677 ++instr;
15678 }
15679 }
15680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015681 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015682}
15683
15684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 * "type(expr)" function
15686 */
15687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015688f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015689 typval_T *argvars;
15690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015692 int n;
15693
15694 switch (argvars[0].v_type)
15695 {
15696 case VAR_NUMBER: n = 0; break;
15697 case VAR_STRING: n = 1; break;
15698 case VAR_FUNC: n = 2; break;
15699 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015700 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015701 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15702 }
15703 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704}
15705
15706/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015707 * "values(dict)" function
15708 */
15709 static void
15710f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015711 typval_T *argvars;
15712 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015713{
15714 dict_list(argvars, rettv, 1);
15715}
15716
15717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 * "virtcol(string)" function
15719 */
15720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015722 typval_T *argvars;
15723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724{
15725 colnr_T vcol = 0;
15726 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015727 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015729 fp = var2fpos(&argvars[0], FALSE, &fnum);
15730 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15731 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 {
15733 getvvcol(curwin, fp, NULL, NULL, &vcol);
15734 ++vcol;
15735 }
15736
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015737 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015738}
15739
15740/*
15741 * "visualmode()" function
15742 */
15743/*ARGSUSED*/
15744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015745f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015746 typval_T *argvars;
15747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748{
15749#ifdef FEAT_VISUAL
15750 char_u str[2];
15751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 str[0] = curbuf->b_visual_mode_eval;
15754 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015755 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756
15757 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015758 if ((argvars[0].v_type == VAR_NUMBER
15759 && argvars[0].vval.v_number != 0)
15760 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 curbuf->b_visual_mode_eval = NUL;
15763#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015764 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765#endif
15766}
15767
15768/*
15769 * "winbufnr(nr)" function
15770 */
15771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015773 typval_T *argvars;
15774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775{
15776 win_T *wp;
15777
15778 wp = find_win_by_nr(&argvars[0]);
15779 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015782 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783}
15784
15785/*
15786 * "wincol()" function
15787 */
15788/*ARGSUSED*/
15789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015790f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015791 typval_T *argvars;
15792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793{
15794 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015795 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796}
15797
15798/*
15799 * "winheight(nr)" function
15800 */
15801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015803 typval_T *argvars;
15804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805{
15806 win_T *wp;
15807
15808 wp = find_win_by_nr(&argvars[0]);
15809 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015812 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813}
15814
15815/*
15816 * "winline()" function
15817 */
15818/*ARGSUSED*/
15819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015820f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015821 typval_T *argvars;
15822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823{
15824 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015825 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826}
15827
15828/*
15829 * "winnr()" function
15830 */
15831/* ARGSUSED */
15832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015833f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015834 typval_T *argvars;
15835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836{
15837 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015838
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015840 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015842 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843}
15844
15845/*
15846 * "winrestcmd()" function
15847 */
15848/* ARGSUSED */
15849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015850f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015851 typval_T *argvars;
15852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853{
15854#ifdef FEAT_WINDOWS
15855 win_T *wp;
15856 int winnr = 1;
15857 garray_T ga;
15858 char_u buf[50];
15859
15860 ga_init2(&ga, (int)sizeof(char), 70);
15861 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15862 {
15863 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15864 ga_concat(&ga, buf);
15865# ifdef FEAT_VERTSPLIT
15866 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15867 ga_concat(&ga, buf);
15868# endif
15869 ++winnr;
15870 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015871 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015873 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015877 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878}
15879
15880/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015881 * "winrestview()" function
15882 */
15883/* ARGSUSED */
15884 static void
15885f_winrestview(argvars, rettv)
15886 typval_T *argvars;
15887 typval_T *rettv;
15888{
15889 dict_T *dict;
15890
15891 if (argvars[0].v_type != VAR_DICT
15892 || (dict = argvars[0].vval.v_dict) == NULL)
15893 EMSG(_(e_invarg));
15894 else
15895 {
15896 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15897 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15898#ifdef FEAT_VIRTUALEDIT
15899 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15900#endif
15901 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015902 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015903
15904 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15905#ifdef FEAT_DIFF
15906 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15907#endif
15908 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15909 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15910
15911 check_cursor();
15912 changed_cline_bef_curs();
15913 invalidate_botline();
15914 redraw_later(VALID);
15915
15916 if (curwin->w_topline == 0)
15917 curwin->w_topline = 1;
15918 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15919 curwin->w_topline = curbuf->b_ml.ml_line_count;
15920#ifdef FEAT_DIFF
15921 check_topfill(curwin, TRUE);
15922#endif
15923 }
15924}
15925
15926/*
15927 * "winsaveview()" function
15928 */
15929/* ARGSUSED */
15930 static void
15931f_winsaveview(argvars, rettv)
15932 typval_T *argvars;
15933 typval_T *rettv;
15934{
15935 dict_T *dict;
15936
15937 dict = dict_alloc();
15938 if (dict == NULL)
15939 return;
15940 rettv->v_type = VAR_DICT;
15941 rettv->vval.v_dict = dict;
15942 ++dict->dv_refcount;
15943
15944 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15945 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15946#ifdef FEAT_VIRTUALEDIT
15947 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15948#endif
15949 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15950
15951 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15952#ifdef FEAT_DIFF
15953 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15954#endif
15955 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15956 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15957}
15958
15959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960 * "winwidth(nr)" function
15961 */
15962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015963f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015964 typval_T *argvars;
15965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966{
15967 win_T *wp;
15968
15969 wp = find_win_by_nr(&argvars[0]);
15970 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 else
15973#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015974 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015975#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015976 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977#endif
15978}
15979
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015981 * "writefile()" function
15982 */
15983 static void
15984f_writefile(argvars, rettv)
15985 typval_T *argvars;
15986 typval_T *rettv;
15987{
15988 int binary = FALSE;
15989 char_u *fname;
15990 FILE *fd;
15991 listitem_T *li;
15992 char_u *s;
15993 int ret = 0;
15994 int c;
15995
15996 if (argvars[0].v_type != VAR_LIST)
15997 {
15998 EMSG2(_(e_listarg), "writefile()");
15999 return;
16000 }
16001 if (argvars[0].vval.v_list == NULL)
16002 return;
16003
16004 if (argvars[2].v_type != VAR_UNKNOWN
16005 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16006 binary = TRUE;
16007
16008 /* Always open the file in binary mode, library functions have a mind of
16009 * their own about CR-LF conversion. */
16010 fname = get_tv_string(&argvars[1]);
16011 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16012 {
16013 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16014 ret = -1;
16015 }
16016 else
16017 {
16018 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16019 li = li->li_next)
16020 {
16021 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16022 {
16023 if (*s == '\n')
16024 c = putc(NUL, fd);
16025 else
16026 c = putc(*s, fd);
16027 if (c == EOF)
16028 {
16029 ret = -1;
16030 break;
16031 }
16032 }
16033 if (!binary || li->li_next != NULL)
16034 if (putc('\n', fd) == EOF)
16035 {
16036 ret = -1;
16037 break;
16038 }
16039 if (ret < 0)
16040 {
16041 EMSG(_(e_write));
16042 break;
16043 }
16044 }
16045 fclose(fd);
16046 }
16047
16048 rettv->vval.v_number = ret;
16049}
16050
16051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016053 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 */
16055 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016056var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016057 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016059 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016061 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016063 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064
Bram Moolenaara5525202006-03-02 22:52:09 +000016065 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016066 if (varp->v_type == VAR_LIST)
16067 {
16068 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016069 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016070 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016071
16072 l = varp->vval.v_list;
16073 if (l == NULL)
16074 return NULL;
16075
16076 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016077 pos.lnum = list_find_nr(l, 0L, &error);
16078 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016079 return NULL; /* invalid line number */
16080
16081 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016082 pos.col = list_find_nr(l, 1L, &error);
16083 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016084 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016085 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000016086 /* Accept a position up to the NUL after the line. */
16087 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016088 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016089 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016090
Bram Moolenaara5525202006-03-02 22:52:09 +000016091#ifdef FEAT_VIRTUALEDIT
16092 /* Get the virtual offset. Defaults to zero. */
16093 pos.coladd = list_find_nr(l, 2L, &error);
16094 if (error)
16095 pos.coladd = 0;
16096#endif
16097
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016098 return &pos;
16099 }
16100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016101 name = get_tv_string_chk(varp);
16102 if (name == NULL)
16103 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 if (name[0] == '.') /* cursor */
16105 return &curwin->w_cursor;
16106 if (name[0] == '\'') /* mark */
16107 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016108 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16110 return NULL;
16111 return pp;
16112 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016113
16114#ifdef FEAT_VIRTUALEDIT
16115 pos.coladd = 0;
16116#endif
16117
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016118 if (name[0] == 'w' && lnum)
16119 {
16120 pos.col = 0;
16121 if (name[1] == '0') /* "w0": first visible line */
16122 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016123 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016124 pos.lnum = curwin->w_topline;
16125 return &pos;
16126 }
16127 else if (name[1] == '$') /* "w$": last visible line */
16128 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016129 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016130 pos.lnum = curwin->w_botline - 1;
16131 return &pos;
16132 }
16133 }
16134 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 {
16136 if (lnum)
16137 {
16138 pos.lnum = curbuf->b_ml.ml_line_count;
16139 pos.col = 0;
16140 }
16141 else
16142 {
16143 pos.lnum = curwin->w_cursor.lnum;
16144 pos.col = (colnr_T)STRLEN(ml_get_curline());
16145 }
16146 return &pos;
16147 }
16148 return NULL;
16149}
16150
16151/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016152 * Convert list in "arg" into a position and optional file number.
16153 * When "fnump" is NULL there is no file number, only 3 items.
16154 * Note that the column is passed on as-is, the caller may want to decrement
16155 * it to use 1 for the first column.
16156 * Return FAIL when conversion is not possible, doesn't check the position for
16157 * validity.
16158 */
16159 static int
16160list2fpos(arg, posp, fnump)
16161 typval_T *arg;
16162 pos_T *posp;
16163 int *fnump;
16164{
16165 list_T *l = arg->vval.v_list;
16166 long i = 0;
16167 long n;
16168
16169 /* List must be: [fnum, lnum, col, coladd] */
16170 if (arg->v_type != VAR_LIST || l == NULL
16171 || l->lv_len != (fnump == NULL ? 3 : 4))
16172 return FAIL;
16173
16174 if (fnump != NULL)
16175 {
16176 n = list_find_nr(l, i++, NULL); /* fnum */
16177 if (n < 0)
16178 return FAIL;
16179 if (n == 0)
16180 n = curbuf->b_fnum; /* current buffer */
16181 *fnump = n;
16182 }
16183
16184 n = list_find_nr(l, i++, NULL); /* lnum */
16185 if (n < 0)
16186 return FAIL;
16187 posp->lnum = n;
16188
16189 n = list_find_nr(l, i++, NULL); /* col */
16190 if (n < 0)
16191 return FAIL;
16192 posp->col = n;
16193
16194#ifdef FEAT_VIRTUALEDIT
16195 n = list_find_nr(l, i, NULL);
16196 if (n < 0)
16197 return FAIL;
16198 posp->coladd = n;
16199#endif
16200
16201 return OK;
16202}
16203
16204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 * Get the length of an environment variable name.
16206 * Advance "arg" to the first character after the name.
16207 * Return 0 for error.
16208 */
16209 static int
16210get_env_len(arg)
16211 char_u **arg;
16212{
16213 char_u *p;
16214 int len;
16215
16216 for (p = *arg; vim_isIDc(*p); ++p)
16217 ;
16218 if (p == *arg) /* no name found */
16219 return 0;
16220
16221 len = (int)(p - *arg);
16222 *arg = p;
16223 return len;
16224}
16225
16226/*
16227 * Get the length of the name of a function or internal variable.
16228 * "arg" is advanced to the first non-white character after the name.
16229 * Return 0 if something is wrong.
16230 */
16231 static int
16232get_id_len(arg)
16233 char_u **arg;
16234{
16235 char_u *p;
16236 int len;
16237
16238 /* Find the end of the name. */
16239 for (p = *arg; eval_isnamec(*p); ++p)
16240 ;
16241 if (p == *arg) /* no name found */
16242 return 0;
16243
16244 len = (int)(p - *arg);
16245 *arg = skipwhite(p);
16246
16247 return len;
16248}
16249
16250/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016251 * Get the length of the name of a variable or function.
16252 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016254 * Return -1 if curly braces expansion failed.
16255 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 * If the name contains 'magic' {}'s, expand them and return the
16257 * expanded name in an allocated string via 'alias' - caller must free.
16258 */
16259 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016260get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 char_u **arg;
16262 char_u **alias;
16263 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016264 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265{
16266 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 char_u *p;
16268 char_u *expr_start;
16269 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270
16271 *alias = NULL; /* default to no alias */
16272
16273 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16274 && (*arg)[2] == (int)KE_SNR)
16275 {
16276 /* hard coded <SNR>, already translated */
16277 *arg += 3;
16278 return get_id_len(arg) + 3;
16279 }
16280 len = eval_fname_script(*arg);
16281 if (len > 0)
16282 {
16283 /* literal "<SID>", "s:" or "<SNR>" */
16284 *arg += len;
16285 }
16286
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016290 p = find_name_end(*arg, &expr_start, &expr_end,
16291 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 if (expr_start != NULL)
16293 {
16294 char_u *temp_string;
16295
16296 if (!evaluate)
16297 {
16298 len += (int)(p - *arg);
16299 *arg = skipwhite(p);
16300 return len;
16301 }
16302
16303 /*
16304 * Include any <SID> etc in the expanded string:
16305 * Thus the -len here.
16306 */
16307 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16308 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016309 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 *alias = temp_string;
16311 *arg = skipwhite(p);
16312 return (int)STRLEN(temp_string);
16313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314
16315 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016316 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 EMSG2(_(e_invexpr2), *arg);
16318
16319 return len;
16320}
16321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016322/*
16323 * Find the end of a variable or function name, taking care of magic braces.
16324 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16325 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016326 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016327 * Return a pointer to just after the name. Equal to "arg" if there is no
16328 * valid name.
16329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016331find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332 char_u *arg;
16333 char_u **expr_start;
16334 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016335 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016337 int mb_nest = 0;
16338 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 char_u *p;
16340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016341 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343 *expr_start = NULL;
16344 *expr_end = NULL;
16345 }
16346
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016347 /* Quick check for valid starting character. */
16348 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16349 return arg;
16350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016351 for (p = arg; *p != NUL
16352 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016353 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016354 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016356 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016358 if (*p == '\'')
16359 {
16360 /* skip over 'string' to avoid counting [ and ] inside it. */
16361 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16362 ;
16363 if (*p == NUL)
16364 break;
16365 }
16366 else if (*p == '"')
16367 {
16368 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16369 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16370 if (*p == '\\' && p[1] != NUL)
16371 ++p;
16372 if (*p == NUL)
16373 break;
16374 }
16375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016376 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016378 if (*p == '[')
16379 ++br_nest;
16380 else if (*p == ']')
16381 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016384 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016386 if (*p == '{')
16387 {
16388 mb_nest++;
16389 if (expr_start != NULL && *expr_start == NULL)
16390 *expr_start = p;
16391 }
16392 else if (*p == '}')
16393 {
16394 mb_nest--;
16395 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16396 *expr_end = p;
16397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399 }
16400
16401 return p;
16402}
16403
16404/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016405 * Expands out the 'magic' {}'s in a variable/function name.
16406 * Note that this can call itself recursively, to deal with
16407 * constructs like foo{bar}{baz}{bam}
16408 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16409 * "in_start" ^
16410 * "expr_start" ^
16411 * "expr_end" ^
16412 * "in_end" ^
16413 *
16414 * Returns a new allocated string, which the caller must free.
16415 * Returns NULL for failure.
16416 */
16417 static char_u *
16418make_expanded_name(in_start, expr_start, expr_end, in_end)
16419 char_u *in_start;
16420 char_u *expr_start;
16421 char_u *expr_end;
16422 char_u *in_end;
16423{
16424 char_u c1;
16425 char_u *retval = NULL;
16426 char_u *temp_result;
16427 char_u *nextcmd = NULL;
16428
16429 if (expr_end == NULL || in_end == NULL)
16430 return NULL;
16431 *expr_start = NUL;
16432 *expr_end = NUL;
16433 c1 = *in_end;
16434 *in_end = NUL;
16435
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016436 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016437 if (temp_result != NULL && nextcmd == NULL)
16438 {
16439 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16440 + (in_end - expr_end) + 1));
16441 if (retval != NULL)
16442 {
16443 STRCPY(retval, in_start);
16444 STRCAT(retval, temp_result);
16445 STRCAT(retval, expr_end + 1);
16446 }
16447 }
16448 vim_free(temp_result);
16449
16450 *in_end = c1; /* put char back for error messages */
16451 *expr_start = '{';
16452 *expr_end = '}';
16453
16454 if (retval != NULL)
16455 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016456 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016457 if (expr_start != NULL)
16458 {
16459 /* Further expansion! */
16460 temp_result = make_expanded_name(retval, expr_start,
16461 expr_end, temp_result);
16462 vim_free(retval);
16463 retval = temp_result;
16464 }
16465 }
16466
16467 return retval;
16468}
16469
16470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016472 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 */
16474 static int
16475eval_isnamec(c)
16476 int c;
16477{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016478 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16479}
16480
16481/*
16482 * Return TRUE if character "c" can be used as the first character in a
16483 * variable or function name (excluding '{' and '}').
16484 */
16485 static int
16486eval_isnamec1(c)
16487 int c;
16488{
16489 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490}
16491
16492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 * Set number v: variable to "val".
16494 */
16495 void
16496set_vim_var_nr(idx, val)
16497 int idx;
16498 long val;
16499{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016500 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016501}
16502
16503/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016504 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 */
16506 long
16507get_vim_var_nr(idx)
16508 int idx;
16509{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016510 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511}
16512
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016513#if defined(FEAT_AUTOCMD) || defined(PROTO)
16514/*
16515 * Get string v: variable value. Uses a static buffer, can only be used once.
16516 */
16517 char_u *
16518get_vim_var_str(idx)
16519 int idx;
16520{
16521 return get_tv_string(&vimvars[idx].vv_tv);
16522}
16523#endif
16524
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525/*
16526 * Set v:count, v:count1 and v:prevcount.
16527 */
16528 void
16529set_vcount(count, count1)
16530 long count;
16531 long count1;
16532{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016533 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16534 vimvars[VV_COUNT].vv_nr = count;
16535 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536}
16537
16538/*
16539 * Set string v: variable to a copy of "val".
16540 */
16541 void
16542set_vim_var_string(idx, val, len)
16543 int idx;
16544 char_u *val;
16545 int len; /* length of "val" to use or -1 (whole string) */
16546{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016547 /* Need to do this (at least) once, since we can't initialize a union.
16548 * Will always be invoked when "v:progname" is set. */
16549 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16550
Bram Moolenaare9a41262005-01-15 22:18:47 +000016551 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016553 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016555 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016557 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558}
16559
16560/*
16561 * Set v:register if needed.
16562 */
16563 void
16564set_reg_var(c)
16565 int c;
16566{
16567 char_u regname;
16568
16569 if (c == 0 || c == ' ')
16570 regname = '"';
16571 else
16572 regname = c;
16573 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016574 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 set_vim_var_string(VV_REG, &regname, 1);
16576}
16577
16578/*
16579 * Get or set v:exception. If "oldval" == NULL, return the current value.
16580 * Otherwise, restore the value to "oldval" and return NULL.
16581 * Must always be called in pairs to save and restore v:exception! Does not
16582 * take care of memory allocations.
16583 */
16584 char_u *
16585v_exception(oldval)
16586 char_u *oldval;
16587{
16588 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016589 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590
Bram Moolenaare9a41262005-01-15 22:18:47 +000016591 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 return NULL;
16593}
16594
16595/*
16596 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16597 * Otherwise, restore the value to "oldval" and return NULL.
16598 * Must always be called in pairs to save and restore v:throwpoint! Does not
16599 * take care of memory allocations.
16600 */
16601 char_u *
16602v_throwpoint(oldval)
16603 char_u *oldval;
16604{
16605 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016606 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607
Bram Moolenaare9a41262005-01-15 22:18:47 +000016608 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 return NULL;
16610}
16611
16612#if defined(FEAT_AUTOCMD) || defined(PROTO)
16613/*
16614 * Set v:cmdarg.
16615 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16616 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16617 * Must always be called in pairs!
16618 */
16619 char_u *
16620set_cmdarg(eap, oldarg)
16621 exarg_T *eap;
16622 char_u *oldarg;
16623{
16624 char_u *oldval;
16625 char_u *newval;
16626 unsigned len;
16627
Bram Moolenaare9a41262005-01-15 22:18:47 +000016628 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016629 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016631 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016632 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016633 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 }
16635
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016636 if (eap->force_bin == FORCE_BIN)
16637 len = 6;
16638 else if (eap->force_bin == FORCE_NOBIN)
16639 len = 8;
16640 else
16641 len = 0;
16642 if (eap->force_ff != 0)
16643 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16644# ifdef FEAT_MBYTE
16645 if (eap->force_enc != 0)
16646 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016647 if (eap->bad_char != 0)
16648 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016649# endif
16650
16651 newval = alloc(len + 1);
16652 if (newval == NULL)
16653 return NULL;
16654
16655 if (eap->force_bin == FORCE_BIN)
16656 sprintf((char *)newval, " ++bin");
16657 else if (eap->force_bin == FORCE_NOBIN)
16658 sprintf((char *)newval, " ++nobin");
16659 else
16660 *newval = NUL;
16661 if (eap->force_ff != 0)
16662 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16663 eap->cmd + eap->force_ff);
16664# ifdef FEAT_MBYTE
16665 if (eap->force_enc != 0)
16666 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16667 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016668 if (eap->bad_char != 0)
16669 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16670 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016671# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016672 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016673 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674}
16675#endif
16676
16677/*
16678 * Get the value of internal variable "name".
16679 * Return OK or FAIL.
16680 */
16681 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016682get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683 char_u *name;
16684 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016685 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016686 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687{
16688 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016689 typval_T *tv = NULL;
16690 typval_T atv;
16691 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693
16694 /* truncate the name, so that we can use strcmp() */
16695 cc = name[len];
16696 name[len] = NUL;
16697
16698 /*
16699 * Check for "b:changedtick".
16700 */
16701 if (STRCMP(name, "b:changedtick") == 0)
16702 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016703 atv.v_type = VAR_NUMBER;
16704 atv.vval.v_number = curbuf->b_changedtick;
16705 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016706 }
16707
16708 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 * Check for user-defined variables.
16710 */
16711 else
16712 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016713 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016715 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 }
16717
Bram Moolenaare9a41262005-01-15 22:18:47 +000016718 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016720 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016721 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 ret = FAIL;
16723 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016724 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016725 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726
16727 name[len] = cc;
16728
16729 return ret;
16730}
16731
16732/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016733 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16734 * Also handle function call with Funcref variable: func(expr)
16735 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16736 */
16737 static int
16738handle_subscript(arg, rettv, evaluate, verbose)
16739 char_u **arg;
16740 typval_T *rettv;
16741 int evaluate; /* do more than finding the end */
16742 int verbose; /* give error messages */
16743{
16744 int ret = OK;
16745 dict_T *selfdict = NULL;
16746 char_u *s;
16747 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016748 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016749
16750 while (ret == OK
16751 && (**arg == '['
16752 || (**arg == '.' && rettv->v_type == VAR_DICT)
16753 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16754 && !vim_iswhite(*(*arg - 1)))
16755 {
16756 if (**arg == '(')
16757 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016758 /* need to copy the funcref so that we can clear rettv */
16759 functv = *rettv;
16760 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016761
16762 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016763 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016764 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016765 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16766 &len, evaluate, selfdict);
16767
16768 /* Clear the funcref afterwards, so that deleting it while
16769 * evaluating the arguments is possible (see test55). */
16770 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016771
16772 /* Stop the expression evaluation when immediately aborting on
16773 * error, or when an interrupt occurred or an exception was thrown
16774 * but not caught. */
16775 if (aborting())
16776 {
16777 if (ret == OK)
16778 clear_tv(rettv);
16779 ret = FAIL;
16780 }
16781 dict_unref(selfdict);
16782 selfdict = NULL;
16783 }
16784 else /* **arg == '[' || **arg == '.' */
16785 {
16786 dict_unref(selfdict);
16787 if (rettv->v_type == VAR_DICT)
16788 {
16789 selfdict = rettv->vval.v_dict;
16790 if (selfdict != NULL)
16791 ++selfdict->dv_refcount;
16792 }
16793 else
16794 selfdict = NULL;
16795 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16796 {
16797 clear_tv(rettv);
16798 ret = FAIL;
16799 }
16800 }
16801 }
16802 dict_unref(selfdict);
16803 return ret;
16804}
16805
16806/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016807 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16808 * value).
16809 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016810 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016811alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016812{
Bram Moolenaar33570922005-01-25 22:26:29 +000016813 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016814}
16815
16816/*
16817 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 * The string "s" must have been allocated, it is consumed.
16819 * Return NULL for out of memory, the variable otherwise.
16820 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016821 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016822alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823 char_u *s;
16824{
Bram Moolenaar33570922005-01-25 22:26:29 +000016825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016827 rettv = alloc_tv();
16828 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016830 rettv->v_type = VAR_STRING;
16831 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 }
16833 else
16834 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016835 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016836}
16837
16838/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016839 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016841 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016842free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016843 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844{
16845 if (varp != NULL)
16846 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016847 switch (varp->v_type)
16848 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016849 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016850 func_unref(varp->vval.v_string);
16851 /*FALLTHROUGH*/
16852 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016853 vim_free(varp->vval.v_string);
16854 break;
16855 case VAR_LIST:
16856 list_unref(varp->vval.v_list);
16857 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016858 case VAR_DICT:
16859 dict_unref(varp->vval.v_dict);
16860 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016861 case VAR_NUMBER:
16862 case VAR_UNKNOWN:
16863 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016864 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016865 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016866 break;
16867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 vim_free(varp);
16869 }
16870}
16871
16872/*
16873 * Free the memory for a variable value and set the value to NULL or 0.
16874 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016875 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016876clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016877 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878{
16879 if (varp != NULL)
16880 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016881 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016883 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016884 func_unref(varp->vval.v_string);
16885 /*FALLTHROUGH*/
16886 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016887 vim_free(varp->vval.v_string);
16888 varp->vval.v_string = NULL;
16889 break;
16890 case VAR_LIST:
16891 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016892 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016893 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016894 case VAR_DICT:
16895 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016896 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016897 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016898 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016899 varp->vval.v_number = 0;
16900 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016901 case VAR_UNKNOWN:
16902 break;
16903 default:
16904 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016906 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 }
16908}
16909
16910/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016911 * Set the value of a variable to NULL without freeing items.
16912 */
16913 static void
16914init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016915 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016916{
16917 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016918 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016919}
16920
16921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 * Get the number value of a variable.
16923 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016924 * For incompatible types, return 0.
16925 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16926 * caller of incompatible types: it sets *denote to TRUE if "denote"
16927 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 */
16929 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016931 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016933 int error = FALSE;
16934
16935 return get_tv_number_chk(varp, &error); /* return 0L on error */
16936}
16937
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016938 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016939get_tv_number_chk(varp, denote)
16940 typval_T *varp;
16941 int *denote;
16942{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016943 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016945 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016947 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016949 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016950 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016951 break;
16952 case VAR_STRING:
16953 if (varp->vval.v_string != NULL)
16954 vim_str2nr(varp->vval.v_string, NULL, NULL,
16955 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016957 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016958 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016959 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016960 case VAR_DICT:
16961 EMSG(_("E728: Using a Dictionary as a number"));
16962 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016963 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016964 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016965 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016967 if (denote == NULL) /* useful for values that must be unsigned */
16968 n = -1;
16969 else
16970 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016971 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973
16974/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016975 * Get the lnum from the first argument.
16976 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016977 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 */
16979 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016980get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016981 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982{
Bram Moolenaar33570922005-01-25 22:26:29 +000016983 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 linenr_T lnum;
16985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 if (lnum == 0) /* no valid number, try using line() */
16988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016989 rettv.v_type = VAR_NUMBER;
16990 f_line(argvars, &rettv);
16991 lnum = rettv.vval.v_number;
16992 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 }
16994 return lnum;
16995}
16996
16997/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016998 * Get the lnum from the first argument.
16999 * Also accepts "$", then "buf" is used.
17000 * Returns 0 on error.
17001 */
17002 static linenr_T
17003get_tv_lnum_buf(argvars, buf)
17004 typval_T *argvars;
17005 buf_T *buf;
17006{
17007 if (argvars[0].v_type == VAR_STRING
17008 && argvars[0].vval.v_string != NULL
17009 && argvars[0].vval.v_string[0] == '$'
17010 && buf != NULL)
17011 return buf->b_ml.ml_line_count;
17012 return get_tv_number_chk(&argvars[0], NULL);
17013}
17014
17015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 * Get the string value of a variable.
17017 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017018 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17019 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 * If the String variable has never been set, return an empty string.
17021 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017022 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17023 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 */
17025 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017027 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028{
17029 static char_u mybuf[NUMBUFLEN];
17030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017031 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032}
17033
17034 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017035get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017036 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017037 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017039 char_u *res = get_tv_string_buf_chk(varp, buf);
17040
17041 return res != NULL ? res : (char_u *)"";
17042}
17043
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017044 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045get_tv_string_chk(varp)
17046 typval_T *varp;
17047{
17048 static char_u mybuf[NUMBUFLEN];
17049
17050 return get_tv_string_buf_chk(varp, mybuf);
17051}
17052
17053 static char_u *
17054get_tv_string_buf_chk(varp, buf)
17055 typval_T *varp;
17056 char_u *buf;
17057{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017058 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017060 case VAR_NUMBER:
17061 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17062 return buf;
17063 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017064 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017065 break;
17066 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017067 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017068 break;
17069 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017070 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017071 break;
17072 case VAR_STRING:
17073 if (varp->vval.v_string != NULL)
17074 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017076 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017077 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081}
17082
17083/*
17084 * Find variable "name" in the list of variables.
17085 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017086 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017087 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017091find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017096 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097
Bram Moolenaara7043832005-01-21 11:56:39 +000017098 ht = find_var_ht(name, &varname);
17099 if (htp != NULL)
17100 *htp = ht;
17101 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017103 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104}
17105
17106/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017107 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017108 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017110 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017111find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017112 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017113 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017114 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017115{
Bram Moolenaar33570922005-01-25 22:26:29 +000017116 hashitem_T *hi;
17117
17118 if (*varname == NUL)
17119 {
17120 /* Must be something like "s:", otherwise "ht" would be NULL. */
17121 switch (varname[-2])
17122 {
17123 case 's': return &SCRIPT_SV(current_SID).sv_var;
17124 case 'g': return &globvars_var;
17125 case 'v': return &vimvars_var;
17126 case 'b': return &curbuf->b_bufvar;
17127 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017128 case 'l': return current_funccal == NULL
17129 ? NULL : &current_funccal->l_vars_var;
17130 case 'a': return current_funccal == NULL
17131 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 }
17133 return NULL;
17134 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017135
17136 hi = hash_find(ht, varname);
17137 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017138 {
17139 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017140 * worked find the variable again. Don't auto-load a script if it was
17141 * loaded already, otherwise it would be loaded every time when
17142 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017143 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017144 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017145 hi = hash_find(ht, varname);
17146 if (HASHITEM_EMPTY(hi))
17147 return NULL;
17148 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017150}
17151
17152/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017153 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017154 * Set "varname" to the start of name without ':'.
17155 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017157find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 char_u *name;
17159 char_u **varname;
17160{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017161 hashitem_T *hi;
17162
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 if (name[1] != ':')
17164 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017165 /* The name must not start with a colon or #. */
17166 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 return NULL;
17168 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017169
17170 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017171 hi = hash_find(&compat_hashtab, name);
17172 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017173 return &compat_hashtab;
17174
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017176 return &globvarht; /* global variable */
17177 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 }
17179 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017180 if (*name == 'g') /* global variable */
17181 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017182 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17183 */
17184 if (vim_strchr(name + 2, ':') != NULL
17185 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017186 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017190 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000017191 if (*name == 'v') /* v: variable */
17192 return &vimvarht;
17193 if (*name == 'a' && current_funccal != NULL) /* function argument */
17194 return &current_funccal->l_avars.dv_hashtab;
17195 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17196 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 if (*name == 's' /* script variable */
17198 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17199 return &SCRIPT_VARS(current_SID);
17200 return NULL;
17201}
17202
17203/*
17204 * Get the string value of a (global/local) variable.
17205 * Returns NULL when it doesn't exist.
17206 */
17207 char_u *
17208get_var_value(name)
17209 char_u *name;
17210{
Bram Moolenaar33570922005-01-25 22:26:29 +000017211 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212
Bram Moolenaara7043832005-01-21 11:56:39 +000017213 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 if (v == NULL)
17215 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217}
17218
17219/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017220 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 * sourcing this script and when executing functions defined in the script.
17222 */
17223 void
17224new_script_vars(id)
17225 scid_T id;
17226{
Bram Moolenaara7043832005-01-21 11:56:39 +000017227 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 hashtab_T *ht;
17229 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017230
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17232 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017233 /* Re-allocating ga_data means that an ht_array pointing to
17234 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017235 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017236 for (i = 1; i <= ga_scripts.ga_len; ++i)
17237 {
17238 ht = &SCRIPT_VARS(i);
17239 if (ht->ht_mask == HT_INIT_SIZE - 1)
17240 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017241 sv = &SCRIPT_SV(i);
17242 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017243 }
17244
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 while (ga_scripts.ga_len < id)
17246 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017247 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17248 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 }
17251 }
17252}
17253
17254/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017255 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17256 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 */
17258 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017259init_var_dict(dict, dict_var)
17260 dict_T *dict;
17261 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262{
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 hash_init(&dict->dv_hashtab);
17264 dict->dv_refcount = 99999;
17265 dict_var->di_tv.vval.v_dict = dict;
17266 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017267 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017268 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17269 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270}
17271
17272/*
17273 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017274 * Frees all allocated variables and the value they contain.
17275 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 */
17277 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017278vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017279 hashtab_T *ht;
17280{
17281 vars_clear_ext(ht, TRUE);
17282}
17283
17284/*
17285 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17286 */
17287 static void
17288vars_clear_ext(ht, free_val)
17289 hashtab_T *ht;
17290 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291{
Bram Moolenaara7043832005-01-21 11:56:39 +000017292 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017293 hashitem_T *hi;
17294 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295
Bram Moolenaar33570922005-01-25 22:26:29 +000017296 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017297 todo = ht->ht_used;
17298 for (hi = ht->ht_array; todo > 0; ++hi)
17299 {
17300 if (!HASHITEM_EMPTY(hi))
17301 {
17302 --todo;
17303
Bram Moolenaar33570922005-01-25 22:26:29 +000017304 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017305 * ht_array might change then. hash_clear() takes care of it
17306 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017307 v = HI2DI(hi);
17308 if (free_val)
17309 clear_tv(&v->di_tv);
17310 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17311 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017312 }
17313 }
17314 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017315 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316}
17317
Bram Moolenaara7043832005-01-21 11:56:39 +000017318/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 * Delete a variable from hashtab "ht" at item "hi".
17320 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017323delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017324 hashtab_T *ht;
17325 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326{
Bram Moolenaar33570922005-01-25 22:26:29 +000017327 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017328
17329 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017330 clear_tv(&di->di_tv);
17331 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332}
17333
17334/*
17335 * List the value of one internal variable.
17336 */
17337 static void
17338list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017339 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 char_u *prefix;
17341{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017342 char_u *tofree;
17343 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017344 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017345
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017346 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017347 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017348 s == NULL ? (char_u *)"" : s);
17349 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350}
17351
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 static void
17353list_one_var_a(prefix, name, type, string)
17354 char_u *prefix;
17355 char_u *name;
17356 int type;
17357 char_u *string;
17358{
17359 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17360 if (name != NULL) /* "a:" vars don't have a name stored */
17361 msg_puts(name);
17362 msg_putchar(' ');
17363 msg_advance(22);
17364 if (type == VAR_NUMBER)
17365 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017366 else if (type == VAR_FUNC)
17367 msg_putchar('*');
17368 else if (type == VAR_LIST)
17369 {
17370 msg_putchar('[');
17371 if (*string == '[')
17372 ++string;
17373 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017374 else if (type == VAR_DICT)
17375 {
17376 msg_putchar('{');
17377 if (*string == '{')
17378 ++string;
17379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 else
17381 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017382
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017384
17385 if (type == VAR_FUNC)
17386 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387}
17388
17389/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 * If the variable already exists, the value is updated.
17392 * Otherwise the variable is created.
17393 */
17394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017395set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017398 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399{
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017402 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017403 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017405 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017406 {
17407 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17408 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17409 ? name[2] : name[0]))
17410 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017411 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017412 return;
17413 }
17414 if (function_exists(name))
17415 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017416 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017417 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017418 return;
17419 }
17420 }
17421
Bram Moolenaara7043832005-01-21 11:56:39 +000017422 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017423 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017424 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017425 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017426 return;
17427 }
17428
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017429 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017430 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017433 if (var_check_ro(v->di_flags, name)
17434 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 return;
17436 if (v->di_tv.v_type != tv->v_type
17437 && !((v->di_tv.v_type == VAR_STRING
17438 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017439 && (tv->v_type == VAR_STRING
17440 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017441 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017442 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017443 return;
17444 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017445
17446 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017447 * Handle setting internal v: variables separately: we don't change
17448 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017449 */
17450 if (ht == &vimvarht)
17451 {
17452 if (v->di_tv.v_type == VAR_STRING)
17453 {
17454 vim_free(v->di_tv.vval.v_string);
17455 if (copy || tv->v_type != VAR_STRING)
17456 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17457 else
17458 {
17459 /* Take over the string to avoid an extra alloc/free. */
17460 v->di_tv.vval.v_string = tv->vval.v_string;
17461 tv->vval.v_string = NULL;
17462 }
17463 }
17464 else if (v->di_tv.v_type != VAR_NUMBER)
17465 EMSG2(_(e_intern2), "set_var()");
17466 else
17467 v->di_tv.vval.v_number = get_tv_number(tv);
17468 return;
17469 }
17470
17471 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 }
17473 else /* add a new variable */
17474 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017475 /* Make sure the variable name is valid. */
17476 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017477 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17478 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017479 {
17480 EMSG2(_(e_illvar), varname);
17481 return;
17482 }
17483
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017484 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17485 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017486 if (v == NULL)
17487 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017488 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017491 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 return;
17493 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017494 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017498 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017499 else
17500 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017502 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017505}
17506
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017507/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017508 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17509 * Also give an error message.
17510 */
17511 static int
17512var_check_ro(flags, name)
17513 int flags;
17514 char_u *name;
17515{
17516 if (flags & DI_FLAGS_RO)
17517 {
17518 EMSG2(_(e_readonlyvar), name);
17519 return TRUE;
17520 }
17521 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17522 {
17523 EMSG2(_(e_readonlysbx), name);
17524 return TRUE;
17525 }
17526 return FALSE;
17527}
17528
17529/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017530 * Return TRUE if typeval "tv" is set to be locked (immutable).
17531 * Also give an error message, using "name".
17532 */
17533 static int
17534tv_check_lock(lock, name)
17535 int lock;
17536 char_u *name;
17537{
17538 if (lock & VAR_LOCKED)
17539 {
17540 EMSG2(_("E741: Value is locked: %s"),
17541 name == NULL ? (char_u *)_("Unknown") : name);
17542 return TRUE;
17543 }
17544 if (lock & VAR_FIXED)
17545 {
17546 EMSG2(_("E742: Cannot change value of %s"),
17547 name == NULL ? (char_u *)_("Unknown") : name);
17548 return TRUE;
17549 }
17550 return FALSE;
17551}
17552
17553/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017554 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017555 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017556 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017559copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 typval_T *from;
17561 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017563 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017564 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017565 switch (from->v_type)
17566 {
17567 case VAR_NUMBER:
17568 to->vval.v_number = from->vval.v_number;
17569 break;
17570 case VAR_STRING:
17571 case VAR_FUNC:
17572 if (from->vval.v_string == NULL)
17573 to->vval.v_string = NULL;
17574 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017576 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017577 if (from->v_type == VAR_FUNC)
17578 func_ref(to->vval.v_string);
17579 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017580 break;
17581 case VAR_LIST:
17582 if (from->vval.v_list == NULL)
17583 to->vval.v_list = NULL;
17584 else
17585 {
17586 to->vval.v_list = from->vval.v_list;
17587 ++to->vval.v_list->lv_refcount;
17588 }
17589 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017590 case VAR_DICT:
17591 if (from->vval.v_dict == NULL)
17592 to->vval.v_dict = NULL;
17593 else
17594 {
17595 to->vval.v_dict = from->vval.v_dict;
17596 ++to->vval.v_dict->dv_refcount;
17597 }
17598 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017599 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017600 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017601 break;
17602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603}
17604
17605/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017606 * Make a copy of an item.
17607 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017608 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17609 * reference to an already copied list/dict can be used.
17610 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017611 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017612 static int
17613item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 typval_T *from;
17615 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017616 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017617 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017618{
17619 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017620 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017621
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017623 {
17624 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017625 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017626 }
17627 ++recurse;
17628
17629 switch (from->v_type)
17630 {
17631 case VAR_NUMBER:
17632 case VAR_STRING:
17633 case VAR_FUNC:
17634 copy_tv(from, to);
17635 break;
17636 case VAR_LIST:
17637 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017638 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017639 if (from->vval.v_list == NULL)
17640 to->vval.v_list = NULL;
17641 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17642 {
17643 /* use the copy made earlier */
17644 to->vval.v_list = from->vval.v_list->lv_copylist;
17645 ++to->vval.v_list->lv_refcount;
17646 }
17647 else
17648 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17649 if (to->vval.v_list == NULL)
17650 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017651 break;
17652 case VAR_DICT:
17653 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017654 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017655 if (from->vval.v_dict == NULL)
17656 to->vval.v_dict = NULL;
17657 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17658 {
17659 /* use the copy made earlier */
17660 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17661 ++to->vval.v_dict->dv_refcount;
17662 }
17663 else
17664 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17665 if (to->vval.v_dict == NULL)
17666 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017667 break;
17668 default:
17669 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017670 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017671 }
17672 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017673 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017674}
17675
17676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 * ":echo expr1 ..." print each argument separated with a space, add a
17678 * newline at the end.
17679 * ":echon expr1 ..." print each argument plain.
17680 */
17681 void
17682ex_echo(eap)
17683 exarg_T *eap;
17684{
17685 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017686 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017687 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 char_u *p;
17689 int needclr = TRUE;
17690 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017691 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692
17693 if (eap->skip)
17694 ++emsg_skip;
17695 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17696 {
17697 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017698 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 {
17700 /*
17701 * Report the invalid expression unless the expression evaluation
17702 * has been cancelled due to an aborting error, an interrupt, or an
17703 * exception.
17704 */
17705 if (!aborting())
17706 EMSG2(_(e_invexpr2), p);
17707 break;
17708 }
17709 if (!eap->skip)
17710 {
17711 if (atstart)
17712 {
17713 atstart = FALSE;
17714 /* Call msg_start() after eval1(), evaluating the expression
17715 * may cause a message to appear. */
17716 if (eap->cmdidx == CMD_echo)
17717 msg_start();
17718 }
17719 else if (eap->cmdidx == CMD_echo)
17720 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017721 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017722 if (p != NULL)
17723 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017725 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017727 if (*p != TAB && needclr)
17728 {
17729 /* remove any text still there from the command */
17730 msg_clr_eos();
17731 needclr = FALSE;
17732 }
17733 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 }
17735 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017736 {
17737#ifdef FEAT_MBYTE
17738 if (has_mbyte)
17739 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017740 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017741
17742 (void)msg_outtrans_len_attr(p, i, echo_attr);
17743 p += i - 1;
17744 }
17745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017747 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017750 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017752 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 arg = skipwhite(arg);
17754 }
17755 eap->nextcmd = check_nextcmd(arg);
17756
17757 if (eap->skip)
17758 --emsg_skip;
17759 else
17760 {
17761 /* remove text that may still be there from the command */
17762 if (needclr)
17763 msg_clr_eos();
17764 if (eap->cmdidx == CMD_echo)
17765 msg_end();
17766 }
17767}
17768
17769/*
17770 * ":echohl {name}".
17771 */
17772 void
17773ex_echohl(eap)
17774 exarg_T *eap;
17775{
17776 int id;
17777
17778 id = syn_name2id(eap->arg);
17779 if (id == 0)
17780 echo_attr = 0;
17781 else
17782 echo_attr = syn_id2attr(id);
17783}
17784
17785/*
17786 * ":execute expr1 ..." execute the result of an expression.
17787 * ":echomsg expr1 ..." Print a message
17788 * ":echoerr expr1 ..." Print an error
17789 * Each gets spaces around each argument and a newline at the end for
17790 * echo commands
17791 */
17792 void
17793ex_execute(eap)
17794 exarg_T *eap;
17795{
17796 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017797 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 int ret = OK;
17799 char_u *p;
17800 garray_T ga;
17801 int len;
17802 int save_did_emsg;
17803
17804 ga_init2(&ga, 1, 80);
17805
17806 if (eap->skip)
17807 ++emsg_skip;
17808 while (*arg != NUL && *arg != '|' && *arg != '\n')
17809 {
17810 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017811 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 {
17813 /*
17814 * Report the invalid expression unless the expression evaluation
17815 * has been cancelled due to an aborting error, an interrupt, or an
17816 * exception.
17817 */
17818 if (!aborting())
17819 EMSG2(_(e_invexpr2), p);
17820 ret = FAIL;
17821 break;
17822 }
17823
17824 if (!eap->skip)
17825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 len = (int)STRLEN(p);
17828 if (ga_grow(&ga, len + 2) == FAIL)
17829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017830 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 ret = FAIL;
17832 break;
17833 }
17834 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 ga.ga_len += len;
17838 }
17839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017840 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 arg = skipwhite(arg);
17842 }
17843
17844 if (ret != FAIL && ga.ga_data != NULL)
17845 {
17846 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017847 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017849 out_flush();
17850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 else if (eap->cmdidx == CMD_echoerr)
17852 {
17853 /* We don't want to abort following commands, restore did_emsg. */
17854 save_did_emsg = did_emsg;
17855 EMSG((char_u *)ga.ga_data);
17856 if (!force_abort)
17857 did_emsg = save_did_emsg;
17858 }
17859 else if (eap->cmdidx == CMD_execute)
17860 do_cmdline((char_u *)ga.ga_data,
17861 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17862 }
17863
17864 ga_clear(&ga);
17865
17866 if (eap->skip)
17867 --emsg_skip;
17868
17869 eap->nextcmd = check_nextcmd(arg);
17870}
17871
17872/*
17873 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17874 * "arg" points to the "&" or '+' when called, to "option" when returning.
17875 * Returns NULL when no option name found. Otherwise pointer to the char
17876 * after the option name.
17877 */
17878 static char_u *
17879find_option_end(arg, opt_flags)
17880 char_u **arg;
17881 int *opt_flags;
17882{
17883 char_u *p = *arg;
17884
17885 ++p;
17886 if (*p == 'g' && p[1] == ':')
17887 {
17888 *opt_flags = OPT_GLOBAL;
17889 p += 2;
17890 }
17891 else if (*p == 'l' && p[1] == ':')
17892 {
17893 *opt_flags = OPT_LOCAL;
17894 p += 2;
17895 }
17896 else
17897 *opt_flags = 0;
17898
17899 if (!ASCII_ISALPHA(*p))
17900 return NULL;
17901 *arg = p;
17902
17903 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17904 p += 4; /* termcap option */
17905 else
17906 while (ASCII_ISALPHA(*p))
17907 ++p;
17908 return p;
17909}
17910
17911/*
17912 * ":function"
17913 */
17914 void
17915ex_function(eap)
17916 exarg_T *eap;
17917{
17918 char_u *theline;
17919 int j;
17920 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 char_u *name = NULL;
17923 char_u *p;
17924 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017925 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 garray_T newargs;
17927 garray_T newlines;
17928 int varargs = FALSE;
17929 int mustend = FALSE;
17930 int flags = 0;
17931 ufunc_T *fp;
17932 int indent;
17933 int nesting;
17934 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017935 dictitem_T *v;
17936 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017937 static int func_nr = 0; /* number for nameless function */
17938 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017939 hashtab_T *ht;
17940 int todo;
17941 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017942 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943
17944 /*
17945 * ":function" without argument: list functions.
17946 */
17947 if (ends_excmd(*eap->arg))
17948 {
17949 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017950 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017951 todo = func_hashtab.ht_used;
17952 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017953 {
17954 if (!HASHITEM_EMPTY(hi))
17955 {
17956 --todo;
17957 fp = HI2UF(hi);
17958 if (!isdigit(*fp->uf_name))
17959 list_func_head(fp, FALSE);
17960 }
17961 }
17962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 eap->nextcmd = check_nextcmd(eap->arg);
17964 return;
17965 }
17966
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017967 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017968 * ":function /pat": list functions matching pattern.
17969 */
17970 if (*eap->arg == '/')
17971 {
17972 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17973 if (!eap->skip)
17974 {
17975 regmatch_T regmatch;
17976
17977 c = *p;
17978 *p = NUL;
17979 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17980 *p = c;
17981 if (regmatch.regprog != NULL)
17982 {
17983 regmatch.rm_ic = p_ic;
17984
17985 todo = func_hashtab.ht_used;
17986 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17987 {
17988 if (!HASHITEM_EMPTY(hi))
17989 {
17990 --todo;
17991 fp = HI2UF(hi);
17992 if (!isdigit(*fp->uf_name)
17993 && vim_regexec(&regmatch, fp->uf_name, 0))
17994 list_func_head(fp, FALSE);
17995 }
17996 }
17997 }
17998 }
17999 if (*p == '/')
18000 ++p;
18001 eap->nextcmd = check_nextcmd(p);
18002 return;
18003 }
18004
18005 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018006 * Get the function name. There are these situations:
18007 * func normal function name
18008 * "name" == func, "fudi.fd_dict" == NULL
18009 * dict.func new dictionary entry
18010 * "name" == NULL, "fudi.fd_dict" set,
18011 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18012 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018013 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018014 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18015 * dict.func existing dict entry that's not a Funcref
18016 * "name" == NULL, "fudi.fd_dict" set,
18017 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018020 name = trans_function_name(&p, eap->skip, 0, &fudi);
18021 paren = (vim_strchr(p, '(') != NULL);
18022 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 {
18024 /*
18025 * Return on an invalid expression in braces, unless the expression
18026 * evaluation has been cancelled due to an aborting error, an
18027 * interrupt, or an exception.
18028 */
18029 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018030 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018031 if (!eap->skip && fudi.fd_newkey != NULL)
18032 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018033 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 else
18037 eap->skip = TRUE;
18038 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018039
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 /* An error in a function call during evaluation of an expression in magic
18041 * braces should not cause the function not to be defined. */
18042 saved_did_emsg = did_emsg;
18043 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044
18045 /*
18046 * ":function func" with only function name: list function.
18047 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018048 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 {
18050 if (!ends_excmd(*skipwhite(p)))
18051 {
18052 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018053 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 }
18055 eap->nextcmd = check_nextcmd(p);
18056 if (eap->nextcmd != NULL)
18057 *p = NUL;
18058 if (!eap->skip && !got_int)
18059 {
18060 fp = find_func(name);
18061 if (fp != NULL)
18062 {
18063 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018064 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018066 if (FUNCLINE(fp, j) == NULL)
18067 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 msg_putchar('\n');
18069 msg_outnum((long)(j + 1));
18070 if (j < 9)
18071 msg_putchar(' ');
18072 if (j < 99)
18073 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018074 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 out_flush(); /* show a line at a time */
18076 ui_breakcheck();
18077 }
18078 if (!got_int)
18079 {
18080 msg_putchar('\n');
18081 msg_puts((char_u *)" endfunction");
18082 }
18083 }
18084 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018085 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018087 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 }
18089
18090 /*
18091 * ":function name(arg1, arg2)" Define function.
18092 */
18093 p = skipwhite(p);
18094 if (*p != '(')
18095 {
18096 if (!eap->skip)
18097 {
18098 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018099 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 }
18101 /* attempt to continue by skipping some text */
18102 if (vim_strchr(p, '(') != NULL)
18103 p = vim_strchr(p, '(');
18104 }
18105 p = skipwhite(p + 1);
18106
18107 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18108 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18109
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018110 if (!eap->skip)
18111 {
18112 /* Check the name of the function. */
18113 if (name != NULL)
18114 arg = name;
18115 else
18116 arg = fudi.fd_newkey;
18117 if (arg != NULL)
18118 {
18119 if (*arg == K_SPECIAL)
18120 j = 3;
18121 else
18122 j = 0;
18123 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18124 : eval_isnamec(arg[j])))
18125 ++j;
18126 if (arg[j] != NUL)
18127 emsg_funcname(_(e_invarg2), arg);
18128 }
18129 }
18130
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 /*
18132 * Isolate the arguments: "arg1, arg2, ...)"
18133 */
18134 while (*p != ')')
18135 {
18136 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18137 {
18138 varargs = TRUE;
18139 p += 3;
18140 mustend = TRUE;
18141 }
18142 else
18143 {
18144 arg = p;
18145 while (ASCII_ISALNUM(*p) || *p == '_')
18146 ++p;
18147 if (arg == p || isdigit(*arg)
18148 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18149 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18150 {
18151 if (!eap->skip)
18152 EMSG2(_("E125: Illegal argument: %s"), arg);
18153 break;
18154 }
18155 if (ga_grow(&newargs, 1) == FAIL)
18156 goto erret;
18157 c = *p;
18158 *p = NUL;
18159 arg = vim_strsave(arg);
18160 if (arg == NULL)
18161 goto erret;
18162 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18163 *p = c;
18164 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 if (*p == ',')
18166 ++p;
18167 else
18168 mustend = TRUE;
18169 }
18170 p = skipwhite(p);
18171 if (mustend && *p != ')')
18172 {
18173 if (!eap->skip)
18174 EMSG2(_(e_invarg2), eap->arg);
18175 break;
18176 }
18177 }
18178 ++p; /* skip the ')' */
18179
Bram Moolenaare9a41262005-01-15 22:18:47 +000018180 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 for (;;)
18182 {
18183 p = skipwhite(p);
18184 if (STRNCMP(p, "range", 5) == 0)
18185 {
18186 flags |= FC_RANGE;
18187 p += 5;
18188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018189 else if (STRNCMP(p, "dict", 4) == 0)
18190 {
18191 flags |= FC_DICT;
18192 p += 4;
18193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 else if (STRNCMP(p, "abort", 5) == 0)
18195 {
18196 flags |= FC_ABORT;
18197 p += 5;
18198 }
18199 else
18200 break;
18201 }
18202
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018203 /* When there is a line break use what follows for the function body.
18204 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18205 if (*p == '\n')
18206 line_arg = p + 1;
18207 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 EMSG(_(e_trailing));
18209
18210 /*
18211 * Read the body of the function, until ":endfunction" is found.
18212 */
18213 if (KeyTyped)
18214 {
18215 /* Check if the function already exists, don't let the user type the
18216 * whole function before telling him it doesn't work! For a script we
18217 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018218 if (!eap->skip && !eap->forceit)
18219 {
18220 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18221 EMSG(_(e_funcdict));
18222 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018223 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018226 if (!eap->skip && did_emsg)
18227 goto erret;
18228
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 msg_putchar('\n'); /* don't overwrite the function name */
18230 cmdline_row = msg_row;
18231 }
18232
18233 indent = 2;
18234 nesting = 0;
18235 for (;;)
18236 {
18237 msg_scroll = TRUE;
18238 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018239 sourcing_lnum_off = sourcing_lnum;
18240
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018241 if (line_arg != NULL)
18242 {
18243 /* Use eap->arg, split up in parts by line breaks. */
18244 theline = line_arg;
18245 p = vim_strchr(theline, '\n');
18246 if (p == NULL)
18247 line_arg += STRLEN(line_arg);
18248 else
18249 {
18250 *p = NUL;
18251 line_arg = p + 1;
18252 }
18253 }
18254 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 theline = getcmdline(':', 0L, indent);
18256 else
18257 theline = eap->getline(':', eap->cookie, indent);
18258 if (KeyTyped)
18259 lines_left = Rows - 1;
18260 if (theline == NULL)
18261 {
18262 EMSG(_("E126: Missing :endfunction"));
18263 goto erret;
18264 }
18265
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018266 /* Detect line continuation: sourcing_lnum increased more than one. */
18267 if (sourcing_lnum > sourcing_lnum_off + 1)
18268 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18269 else
18270 sourcing_lnum_off = 0;
18271
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 if (skip_until != NULL)
18273 {
18274 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18275 * don't check for ":endfunc". */
18276 if (STRCMP(theline, skip_until) == 0)
18277 {
18278 vim_free(skip_until);
18279 skip_until = NULL;
18280 }
18281 }
18282 else
18283 {
18284 /* skip ':' and blanks*/
18285 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18286 ;
18287
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018288 /* Check for "endfunction". */
18289 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018291 if (line_arg == NULL)
18292 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 break;
18294 }
18295
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018296 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 * at "end". */
18298 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18299 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018300 else if (STRNCMP(p, "if", 2) == 0
18301 || STRNCMP(p, "wh", 2) == 0
18302 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 || STRNCMP(p, "try", 3) == 0)
18304 indent += 2;
18305
18306 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018307 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018309 if (*p == '!')
18310 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 p += eval_fname_script(p);
18312 if (ASCII_ISALPHA(*p))
18313 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018314 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 if (*skipwhite(p) == '(')
18316 {
18317 ++nesting;
18318 indent += 2;
18319 }
18320 }
18321 }
18322
18323 /* Check for ":append" or ":insert". */
18324 p = skip_range(p, NULL);
18325 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18326 || (p[0] == 'i'
18327 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18328 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18329 skip_until = vim_strsave((char_u *)".");
18330
18331 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18332 arg = skipwhite(skiptowhite(p));
18333 if (arg[0] == '<' && arg[1] =='<'
18334 && ((p[0] == 'p' && p[1] == 'y'
18335 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18336 || (p[0] == 'p' && p[1] == 'e'
18337 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18338 || (p[0] == 't' && p[1] == 'c'
18339 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18340 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18341 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018342 || (p[0] == 'm' && p[1] == 'z'
18343 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 ))
18345 {
18346 /* ":python <<" continues until a dot, like ":append" */
18347 p = skipwhite(arg + 2);
18348 if (*p == NUL)
18349 skip_until = vim_strsave((char_u *)".");
18350 else
18351 skip_until = vim_strsave(p);
18352 }
18353 }
18354
18355 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018356 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018357 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018358 if (line_arg == NULL)
18359 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018361 }
18362
18363 /* Copy the line to newly allocated memory. get_one_sourceline()
18364 * allocates 250 bytes per line, this saves 80% on average. The cost
18365 * is an extra alloc/free. */
18366 p = vim_strsave(theline);
18367 if (p != NULL)
18368 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018369 if (line_arg == NULL)
18370 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018371 theline = p;
18372 }
18373
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018374 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18375
18376 /* Add NULL lines for continuation lines, so that the line count is
18377 * equal to the index in the growarray. */
18378 while (sourcing_lnum_off-- > 0)
18379 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018380
18381 /* Check for end of eap->arg. */
18382 if (line_arg != NULL && *line_arg == NUL)
18383 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 }
18385
18386 /* Don't define the function when skipping commands or when an error was
18387 * detected. */
18388 if (eap->skip || did_emsg)
18389 goto erret;
18390
18391 /*
18392 * If there are no errors, add the function
18393 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018394 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018395 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018396 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018397 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018398 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018399 emsg_funcname("E707: Function name conflicts with variable: %s",
18400 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018401 goto erret;
18402 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018403
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018404 fp = find_func(name);
18405 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018407 if (!eap->forceit)
18408 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018409 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018410 goto erret;
18411 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018412 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018413 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018414 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018415 name);
18416 goto erret;
18417 }
18418 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018419 ga_clear_strings(&(fp->uf_args));
18420 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018421 vim_free(name);
18422 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 }
18425 else
18426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018427 char numbuf[20];
18428
18429 fp = NULL;
18430 if (fudi.fd_newkey == NULL && !eap->forceit)
18431 {
18432 EMSG(_(e_funcdict));
18433 goto erret;
18434 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018435 if (fudi.fd_di == NULL)
18436 {
18437 /* Can't add a function to a locked dictionary */
18438 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18439 goto erret;
18440 }
18441 /* Can't change an existing function if it is locked */
18442 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18443 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018444
18445 /* Give the function a sequential number. Can only be used with a
18446 * Funcref! */
18447 vim_free(name);
18448 sprintf(numbuf, "%d", ++func_nr);
18449 name = vim_strsave((char_u *)numbuf);
18450 if (name == NULL)
18451 goto erret;
18452 }
18453
18454 if (fp == NULL)
18455 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018456 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018457 {
18458 int slen, plen;
18459 char_u *scriptname;
18460
18461 /* Check that the autoload name matches the script name. */
18462 j = FAIL;
18463 if (sourcing_name != NULL)
18464 {
18465 scriptname = autoload_name(name);
18466 if (scriptname != NULL)
18467 {
18468 p = vim_strchr(scriptname, '/');
18469 plen = STRLEN(p);
18470 slen = STRLEN(sourcing_name);
18471 if (slen > plen && fnamecmp(p,
18472 sourcing_name + slen - plen) == 0)
18473 j = OK;
18474 vim_free(scriptname);
18475 }
18476 }
18477 if (j == FAIL)
18478 {
18479 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18480 goto erret;
18481 }
18482 }
18483
18484 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 if (fp == NULL)
18486 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018487
18488 if (fudi.fd_dict != NULL)
18489 {
18490 if (fudi.fd_di == NULL)
18491 {
18492 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018493 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018494 if (fudi.fd_di == NULL)
18495 {
18496 vim_free(fp);
18497 goto erret;
18498 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018499 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18500 {
18501 vim_free(fudi.fd_di);
18502 goto erret;
18503 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018504 }
18505 else
18506 /* overwrite existing dict entry */
18507 clear_tv(&fudi.fd_di->di_tv);
18508 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018509 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018510 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018511 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018512 }
18513
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018515 STRCPY(fp->uf_name, name);
18516 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018518 fp->uf_args = newargs;
18519 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018520#ifdef FEAT_PROFILE
18521 fp->uf_tml_count = NULL;
18522 fp->uf_tml_total = NULL;
18523 fp->uf_tml_self = NULL;
18524 fp->uf_profiling = FALSE;
18525 if (prof_def_func())
18526 func_do_profile(fp);
18527#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018528 fp->uf_varargs = varargs;
18529 fp->uf_flags = flags;
18530 fp->uf_calls = 0;
18531 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018532 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533
18534erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 ga_clear_strings(&newargs);
18536 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018537ret_free:
18538 vim_free(skip_until);
18539 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542}
18543
18544/*
18545 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018546 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018548 * flags:
18549 * TFN_INT: internal function name OK
18550 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 * Advances "pp" to just after the function name (if no error).
18552 */
18553 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018554trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 char_u **pp;
18556 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018557 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018558 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018560 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 char_u *start;
18562 char_u *end;
18563 int lead;
18564 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018566 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018567
18568 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018569 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018571
18572 /* Check for hard coded <SNR>: already translated function ID (from a user
18573 * command). */
18574 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18575 && (*pp)[2] == (int)KE_SNR)
18576 {
18577 *pp += 3;
18578 len = get_id_len(pp) + 3;
18579 return vim_strnsave(start, len);
18580 }
18581
18582 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18583 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018585 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018587
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018588 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18589 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018590 if (end == start)
18591 {
18592 if (!skip)
18593 EMSG(_("E129: Function name required"));
18594 goto theend;
18595 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018596 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018597 {
18598 /*
18599 * Report an invalid expression in braces, unless the expression
18600 * evaluation has been cancelled due to an aborting error, an
18601 * interrupt, or an exception.
18602 */
18603 if (!aborting())
18604 {
18605 if (end != NULL)
18606 EMSG2(_(e_invarg2), start);
18607 }
18608 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018609 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018610 goto theend;
18611 }
18612
18613 if (lv.ll_tv != NULL)
18614 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018615 if (fdp != NULL)
18616 {
18617 fdp->fd_dict = lv.ll_dict;
18618 fdp->fd_newkey = lv.ll_newkey;
18619 lv.ll_newkey = NULL;
18620 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018622 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18623 {
18624 name = vim_strsave(lv.ll_tv->vval.v_string);
18625 *pp = end;
18626 }
18627 else
18628 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018629 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18630 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018631 EMSG(_(e_funcref));
18632 else
18633 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018634 name = NULL;
18635 }
18636 goto theend;
18637 }
18638
18639 if (lv.ll_name == NULL)
18640 {
18641 /* Error found, but continue after the function name. */
18642 *pp = end;
18643 goto theend;
18644 }
18645
18646 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018648 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018649 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18650 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18651 {
18652 /* When there was "s:" already or the name expanded to get a
18653 * leading "s:" then remove it. */
18654 lv.ll_name += 2;
18655 len -= 2;
18656 lead = 2;
18657 }
18658 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018659 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018660 {
18661 if (lead == 2) /* skip over "s:" */
18662 lv.ll_name += 2;
18663 len = (int)(end - lv.ll_name);
18664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018665
18666 /*
18667 * Copy the function name to allocated memory.
18668 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18669 * Accept <SNR>123_name() outside a script.
18670 */
18671 if (skip)
18672 lead = 0; /* do nothing */
18673 else if (lead > 0)
18674 {
18675 lead = 3;
18676 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18677 {
18678 if (current_SID <= 0)
18679 {
18680 EMSG(_(e_usingsid));
18681 goto theend;
18682 }
18683 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18684 lead += (int)STRLEN(sid_buf);
18685 }
18686 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018687 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018688 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018689 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018690 goto theend;
18691 }
18692 name = alloc((unsigned)(len + lead + 1));
18693 if (name != NULL)
18694 {
18695 if (lead > 0)
18696 {
18697 name[0] = K_SPECIAL;
18698 name[1] = KS_EXTRA;
18699 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018700 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018701 STRCPY(name + 3, sid_buf);
18702 }
18703 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18704 name[len + lead] = NUL;
18705 }
18706 *pp = end;
18707
18708theend:
18709 clear_lval(&lv);
18710 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711}
18712
18713/*
18714 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18715 * Return 2 if "p" starts with "s:".
18716 * Return 0 otherwise.
18717 */
18718 static int
18719eval_fname_script(p)
18720 char_u *p;
18721{
18722 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18723 || STRNICMP(p + 1, "SNR>", 4) == 0))
18724 return 5;
18725 if (p[0] == 's' && p[1] == ':')
18726 return 2;
18727 return 0;
18728}
18729
18730/*
18731 * Return TRUE if "p" starts with "<SID>" or "s:".
18732 * Only works if eval_fname_script() returned non-zero for "p"!
18733 */
18734 static int
18735eval_fname_sid(p)
18736 char_u *p;
18737{
18738 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18739}
18740
18741/*
18742 * List the head of the function: "name(arg1, arg2)".
18743 */
18744 static void
18745list_func_head(fp, indent)
18746 ufunc_T *fp;
18747 int indent;
18748{
18749 int j;
18750
18751 msg_start();
18752 if (indent)
18753 MSG_PUTS(" ");
18754 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018755 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 {
18757 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018758 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 }
18760 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018761 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018763 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 {
18765 if (j)
18766 MSG_PUTS(", ");
18767 msg_puts(FUNCARG(fp, j));
18768 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018769 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 {
18771 if (j)
18772 MSG_PUTS(", ");
18773 MSG_PUTS("...");
18774 }
18775 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018776 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018777 if (p_verbose > 0)
18778 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779}
18780
18781/*
18782 * Find a function by name, return pointer to it in ufuncs.
18783 * Return NULL for unknown function.
18784 */
18785 static ufunc_T *
18786find_func(name)
18787 char_u *name;
18788{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018789 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018791 hi = hash_find(&func_hashtab, name);
18792 if (!HASHITEM_EMPTY(hi))
18793 return HI2UF(hi);
18794 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795}
18796
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018797#if defined(EXITFREE) || defined(PROTO)
18798 void
18799free_all_functions()
18800{
18801 hashitem_T *hi;
18802
18803 /* Need to start all over every time, because func_free() may change the
18804 * hash table. */
18805 while (func_hashtab.ht_used > 0)
18806 for (hi = func_hashtab.ht_array; ; ++hi)
18807 if (!HASHITEM_EMPTY(hi))
18808 {
18809 func_free(HI2UF(hi));
18810 break;
18811 }
18812}
18813#endif
18814
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018815/*
18816 * Return TRUE if a function "name" exists.
18817 */
18818 static int
18819function_exists(name)
18820 char_u *name;
18821{
18822 char_u *p = name;
18823 int n = FALSE;
18824
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018825 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018826 if (p != NULL)
18827 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018828 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018829 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018830 else
18831 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018832 vim_free(p);
18833 }
18834 return n;
18835}
18836
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018837/*
18838 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018839 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018840 */
18841 static int
18842builtin_function(name)
18843 char_u *name;
18844{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018845 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18846 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018847}
18848
Bram Moolenaar05159a02005-02-26 23:04:13 +000018849#if defined(FEAT_PROFILE) || defined(PROTO)
18850/*
18851 * Start profiling function "fp".
18852 */
18853 static void
18854func_do_profile(fp)
18855 ufunc_T *fp;
18856{
18857 fp->uf_tm_count = 0;
18858 profile_zero(&fp->uf_tm_self);
18859 profile_zero(&fp->uf_tm_total);
18860 if (fp->uf_tml_count == NULL)
18861 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18862 (sizeof(int) * fp->uf_lines.ga_len));
18863 if (fp->uf_tml_total == NULL)
18864 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18865 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18866 if (fp->uf_tml_self == NULL)
18867 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18868 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18869 fp->uf_tml_idx = -1;
18870 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18871 || fp->uf_tml_self == NULL)
18872 return; /* out of memory */
18873
18874 fp->uf_profiling = TRUE;
18875}
18876
18877/*
18878 * Dump the profiling results for all functions in file "fd".
18879 */
18880 void
18881func_dump_profile(fd)
18882 FILE *fd;
18883{
18884 hashitem_T *hi;
18885 int todo;
18886 ufunc_T *fp;
18887 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018888 ufunc_T **sorttab;
18889 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018890
18891 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018892 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18893
Bram Moolenaar05159a02005-02-26 23:04:13 +000018894 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18895 {
18896 if (!HASHITEM_EMPTY(hi))
18897 {
18898 --todo;
18899 fp = HI2UF(hi);
18900 if (fp->uf_profiling)
18901 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018902 if (sorttab != NULL)
18903 sorttab[st_len++] = fp;
18904
Bram Moolenaar05159a02005-02-26 23:04:13 +000018905 if (fp->uf_name[0] == K_SPECIAL)
18906 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18907 else
18908 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18909 if (fp->uf_tm_count == 1)
18910 fprintf(fd, "Called 1 time\n");
18911 else
18912 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18913 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18914 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18915 fprintf(fd, "\n");
18916 fprintf(fd, "count total (s) self (s)\n");
18917
18918 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18919 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018920 if (FUNCLINE(fp, i) == NULL)
18921 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018922 prof_func_line(fd, fp->uf_tml_count[i],
18923 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018924 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18925 }
18926 fprintf(fd, "\n");
18927 }
18928 }
18929 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018930
18931 if (sorttab != NULL && st_len > 0)
18932 {
18933 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18934 prof_total_cmp);
18935 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18936 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18937 prof_self_cmp);
18938 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018940}
Bram Moolenaar73830342005-02-28 22:48:19 +000018941
18942 static void
18943prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18944 FILE *fd;
18945 ufunc_T **sorttab;
18946 int st_len;
18947 char *title;
18948 int prefer_self; /* when equal print only self time */
18949{
18950 int i;
18951 ufunc_T *fp;
18952
18953 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18954 fprintf(fd, "count total (s) self (s) function\n");
18955 for (i = 0; i < 20 && i < st_len; ++i)
18956 {
18957 fp = sorttab[i];
18958 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18959 prefer_self);
18960 if (fp->uf_name[0] == K_SPECIAL)
18961 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18962 else
18963 fprintf(fd, " %s()\n", fp->uf_name);
18964 }
18965 fprintf(fd, "\n");
18966}
18967
18968/*
18969 * Print the count and times for one function or function line.
18970 */
18971 static void
18972prof_func_line(fd, count, total, self, prefer_self)
18973 FILE *fd;
18974 int count;
18975 proftime_T *total;
18976 proftime_T *self;
18977 int prefer_self; /* when equal print only self time */
18978{
18979 if (count > 0)
18980 {
18981 fprintf(fd, "%5d ", count);
18982 if (prefer_self && profile_equal(total, self))
18983 fprintf(fd, " ");
18984 else
18985 fprintf(fd, "%s ", profile_msg(total));
18986 if (!prefer_self && profile_equal(total, self))
18987 fprintf(fd, " ");
18988 else
18989 fprintf(fd, "%s ", profile_msg(self));
18990 }
18991 else
18992 fprintf(fd, " ");
18993}
18994
18995/*
18996 * Compare function for total time sorting.
18997 */
18998 static int
18999#ifdef __BORLANDC__
19000_RTLENTRYF
19001#endif
19002prof_total_cmp(s1, s2)
19003 const void *s1;
19004 const void *s2;
19005{
19006 ufunc_T *p1, *p2;
19007
19008 p1 = *(ufunc_T **)s1;
19009 p2 = *(ufunc_T **)s2;
19010 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19011}
19012
19013/*
19014 * Compare function for self time sorting.
19015 */
19016 static int
19017#ifdef __BORLANDC__
19018_RTLENTRYF
19019#endif
19020prof_self_cmp(s1, s2)
19021 const void *s1;
19022 const void *s2;
19023{
19024 ufunc_T *p1, *p2;
19025
19026 p1 = *(ufunc_T **)s1;
19027 p2 = *(ufunc_T **)s2;
19028 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19029}
19030
Bram Moolenaar05159a02005-02-26 23:04:13 +000019031#endif
19032
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019033/* The names of packages that once were loaded is remembered. */
19034static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19035
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019036/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019037 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019038 * Return TRUE if a package was loaded.
19039 */
19040 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019041script_autoload(name, reload)
19042 char_u *name;
19043 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019044{
19045 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019046 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019047 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019048 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019049
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019050 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019051 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019052 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019053 return FALSE;
19054
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019055 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019056
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019057 /* Find the name in the list of previously loaded package names. Skip
19058 * "autoload/", it's always the same. */
19059 for (i = 0; i < ga_loaded.ga_len; ++i)
19060 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19061 break;
19062 if (!reload && i < ga_loaded.ga_len)
19063 ret = FALSE; /* was loaded already */
19064 else
19065 {
19066 /* Remember the name if it wasn't loaded already. */
19067 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19068 {
19069 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19070 tofree = NULL;
19071 }
19072
19073 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019074 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019075 ret = TRUE;
19076 }
19077
19078 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019079 return ret;
19080}
19081
19082/*
19083 * Return the autoload script name for a function or variable name.
19084 * Returns NULL when out of memory.
19085 */
19086 static char_u *
19087autoload_name(name)
19088 char_u *name;
19089{
19090 char_u *p;
19091 char_u *scriptname;
19092
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019093 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019094 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19095 if (scriptname == NULL)
19096 return FALSE;
19097 STRCPY(scriptname, "autoload/");
19098 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019099 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019100 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019101 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019102 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019103 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019104}
19105
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19107
19108/*
19109 * Function given to ExpandGeneric() to obtain the list of user defined
19110 * function names.
19111 */
19112 char_u *
19113get_user_func_name(xp, idx)
19114 expand_T *xp;
19115 int idx;
19116{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019117 static long_u done;
19118 static hashitem_T *hi;
19119 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120
19121 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019123 done = 0;
19124 hi = func_hashtab.ht_array;
19125 }
19126 if (done < func_hashtab.ht_used)
19127 {
19128 if (done++ > 0)
19129 ++hi;
19130 while (HASHITEM_EMPTY(hi))
19131 ++hi;
19132 fp = HI2UF(hi);
19133
19134 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19135 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136
19137 cat_func_name(IObuff, fp);
19138 if (xp->xp_context != EXPAND_USER_FUNC)
19139 {
19140 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019141 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 STRCAT(IObuff, ")");
19143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144 return IObuff;
19145 }
19146 return NULL;
19147}
19148
19149#endif /* FEAT_CMDL_COMPL */
19150
19151/*
19152 * Copy the function name of "fp" to buffer "buf".
19153 * "buf" must be able to hold the function name plus three bytes.
19154 * Takes care of script-local function names.
19155 */
19156 static void
19157cat_func_name(buf, fp)
19158 char_u *buf;
19159 ufunc_T *fp;
19160{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019161 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 {
19163 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019164 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 }
19166 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019167 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168}
19169
19170/*
19171 * ":delfunction {name}"
19172 */
19173 void
19174ex_delfunction(eap)
19175 exarg_T *eap;
19176{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019177 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 char_u *p;
19179 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181
19182 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019183 name = trans_function_name(&p, eap->skip, 0, &fudi);
19184 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019186 {
19187 if (fudi.fd_dict != NULL && !eap->skip)
19188 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191 if (!ends_excmd(*skipwhite(p)))
19192 {
19193 vim_free(name);
19194 EMSG(_(e_trailing));
19195 return;
19196 }
19197 eap->nextcmd = check_nextcmd(p);
19198 if (eap->nextcmd != NULL)
19199 *p = NUL;
19200
19201 if (!eap->skip)
19202 fp = find_func(name);
19203 vim_free(name);
19204
19205 if (!eap->skip)
19206 {
19207 if (fp == NULL)
19208 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019209 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 return;
19211 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019212 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213 {
19214 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19215 return;
19216 }
19217
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019218 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019220 /* Delete the dict item that refers to the function, it will
19221 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019222 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019224 else
19225 func_free(fp);
19226 }
19227}
19228
19229/*
19230 * Free a function and remove it from the list of functions.
19231 */
19232 static void
19233func_free(fp)
19234 ufunc_T *fp;
19235{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019236 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019237
19238 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019239 ga_clear_strings(&(fp->uf_args));
19240 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019241#ifdef FEAT_PROFILE
19242 vim_free(fp->uf_tml_count);
19243 vim_free(fp->uf_tml_total);
19244 vim_free(fp->uf_tml_self);
19245#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019246
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019247 /* remove the function from the function hashtable */
19248 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19249 if (HASHITEM_EMPTY(hi))
19250 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019251 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019252 hash_remove(&func_hashtab, hi);
19253
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019254 vim_free(fp);
19255}
19256
19257/*
19258 * Unreference a Function: decrement the reference count and free it when it
19259 * becomes zero. Only for numbered functions.
19260 */
19261 static void
19262func_unref(name)
19263 char_u *name;
19264{
19265 ufunc_T *fp;
19266
19267 if (name != NULL && isdigit(*name))
19268 {
19269 fp = find_func(name);
19270 if (fp == NULL)
19271 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019272 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019273 {
19274 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019275 * when "uf_calls" becomes zero. */
19276 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019277 func_free(fp);
19278 }
19279 }
19280}
19281
19282/*
19283 * Count a reference to a Function.
19284 */
19285 static void
19286func_ref(name)
19287 char_u *name;
19288{
19289 ufunc_T *fp;
19290
19291 if (name != NULL && isdigit(*name))
19292 {
19293 fp = find_func(name);
19294 if (fp == NULL)
19295 EMSG2(_(e_intern2), "func_ref()");
19296 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019297 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 }
19299}
19300
19301/*
19302 * Call a user function.
19303 */
19304 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019305call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 ufunc_T *fp; /* pointer to function */
19307 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019308 typval_T *argvars; /* arguments */
19309 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 linenr_T firstline; /* first line of range */
19311 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019312 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313{
Bram Moolenaar33570922005-01-25 22:26:29 +000019314 char_u *save_sourcing_name;
19315 linenr_T save_sourcing_lnum;
19316 scid_T save_current_SID;
19317 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019318 int save_did_emsg;
19319 static int depth = 0;
19320 dictitem_T *v;
19321 int fixvar_idx = 0; /* index in fixvar[] */
19322 int i;
19323 int ai;
19324 char_u numbuf[NUMBUFLEN];
19325 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019326#ifdef FEAT_PROFILE
19327 proftime_T wait_start;
19328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329
19330 /* If depth of calling is getting too high, don't execute the function */
19331 if (depth >= p_mfd)
19332 {
19333 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019334 rettv->v_type = VAR_NUMBER;
19335 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 return;
19337 }
19338 ++depth;
19339
19340 line_breakcheck(); /* check for CTRL-C hit */
19341
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019342 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019343 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019345 fc.rettv = rettv;
19346 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 fc.linenr = 0;
19348 fc.returned = FALSE;
19349 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019351 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 fc.dbg_tick = debug_tick;
19353
Bram Moolenaar33570922005-01-25 22:26:29 +000019354 /*
19355 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19356 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19357 * each argument variable and saves a lot of time.
19358 */
19359 /*
19360 * Init l: variables.
19361 */
19362 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019363 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019364 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019365 /* Set l:self to "selfdict". Use "name" to avoid a warning from
19366 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019367 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000019368 name = v->di_key;
19369 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000019370 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19371 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19372 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019373 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019374 v->di_tv.vval.v_dict = selfdict;
19375 ++selfdict->dv_refcount;
19376 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019377
Bram Moolenaar33570922005-01-25 22:26:29 +000019378 /*
19379 * Init a: variables.
19380 * Set a:0 to "argcount".
19381 * Set a:000 to a list with room for the "..." arguments.
19382 */
19383 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19384 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019385 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019386 v = &fc.fixvar[fixvar_idx++].var;
19387 STRCPY(v->di_key, "000");
19388 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19389 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19390 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019391 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019392 v->di_tv.vval.v_list = &fc.l_varlist;
19393 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19394 fc.l_varlist.lv_refcount = 99999;
19395
19396 /*
19397 * Set a:firstline to "firstline" and a:lastline to "lastline".
19398 * Set a:name to named arguments.
19399 * Set a:N to the "..." arguments.
19400 */
19401 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19402 (varnumber_T)firstline);
19403 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19404 (varnumber_T)lastline);
19405 for (i = 0; i < argcount; ++i)
19406 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019407 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019408 if (ai < 0)
19409 /* named argument a:name */
19410 name = FUNCARG(fp, i);
19411 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019412 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019413 /* "..." argument a:1, a:2, etc. */
19414 sprintf((char *)numbuf, "%d", ai + 1);
19415 name = numbuf;
19416 }
19417 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19418 {
19419 v = &fc.fixvar[fixvar_idx++].var;
19420 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19421 }
19422 else
19423 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019424 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19425 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 if (v == NULL)
19427 break;
19428 v->di_flags = DI_FLAGS_RO;
19429 }
19430 STRCPY(v->di_key, name);
19431 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19432
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019433 /* Note: the values are copied directly to avoid alloc/free.
19434 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019435 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019436 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019437
19438 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19439 {
19440 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19441 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019442 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019443 }
19444 }
19445
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 /* Don't redraw while executing the function. */
19447 ++RedrawingDisabled;
19448 save_sourcing_name = sourcing_name;
19449 save_sourcing_lnum = sourcing_lnum;
19450 sourcing_lnum = 1;
19451 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019452 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 if (sourcing_name != NULL)
19454 {
19455 if (save_sourcing_name != NULL
19456 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19457 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19458 else
19459 STRCPY(sourcing_name, "function ");
19460 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19461
19462 if (p_verbose >= 12)
19463 {
19464 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019465 verbose_enter_scroll();
19466
Bram Moolenaar555b2802005-05-19 21:08:39 +000019467 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 if (p_verbose >= 14)
19469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019471 char_u numbuf[NUMBUFLEN];
19472 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473
19474 msg_puts((char_u *)"(");
19475 for (i = 0; i < argcount; ++i)
19476 {
19477 if (i > 0)
19478 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019479 if (argvars[i].v_type == VAR_NUMBER)
19480 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 else
19482 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019483 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019484 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019486 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 }
19488 }
19489 msg_puts((char_u *)")");
19490 }
19491 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019492
19493 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494 --no_wait_return;
19495 }
19496 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019497#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019498 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019499 {
19500 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19501 func_do_profile(fp);
19502 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019503 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019504 {
19505 ++fp->uf_tm_count;
19506 profile_start(&fp->uf_tm_start);
19507 profile_zero(&fp->uf_tm_children);
19508 }
19509 script_prof_save(&wait_start);
19510 }
19511#endif
19512
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019514 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 save_did_emsg = did_emsg;
19516 did_emsg = FALSE;
19517
19518 /* call do_cmdline() to execute the lines */
19519 do_cmdline(NULL, get_func_line, (void *)&fc,
19520 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19521
19522 --RedrawingDisabled;
19523
19524 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019525 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019527 clear_tv(rettv);
19528 rettv->v_type = VAR_NUMBER;
19529 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 }
19531
Bram Moolenaar05159a02005-02-26 23:04:13 +000019532#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019533 if (do_profiling == PROF_YES && (fp->uf_profiling
19534 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019535 {
19536 profile_end(&fp->uf_tm_start);
19537 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19538 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019539 profile_self(&fp->uf_tm_self, &fp->uf_tm_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019540 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019541 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019542 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19543 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019544 }
19545 }
19546#endif
19547
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 /* when being verbose, mention the return value */
19549 if (p_verbose >= 12)
19550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019552 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019555 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019557 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19558 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019561 char_u buf[MSG_BUF_LEN];
19562 char_u numbuf[NUMBUFLEN];
19563 char_u *tofree;
19564
Bram Moolenaar555b2802005-05-19 21:08:39 +000019565 /* The value may be very long. Skip the middle part, so that we
19566 * have some idea how it starts and ends. smsg() would always
19567 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019568 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019569 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019570 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 }
19573 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019574
19575 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 --no_wait_return;
19577 }
19578
19579 vim_free(sourcing_name);
19580 sourcing_name = save_sourcing_name;
19581 sourcing_lnum = save_sourcing_lnum;
19582 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019583#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019584 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019585 script_prof_restore(&wait_start);
19586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587
19588 if (p_verbose >= 12 && sourcing_name != NULL)
19589 {
19590 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019591 verbose_enter_scroll();
19592
Bram Moolenaar555b2802005-05-19 21:08:39 +000019593 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019595
19596 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 --no_wait_return;
19598 }
19599
19600 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019601 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 /* The a: variables typevals were not alloced, only free the allocated
19604 * variables. */
19605 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19606
19607 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 --depth;
19609}
19610
19611/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 * Add a number variable "name" to dict "dp" with value "nr".
19613 */
19614 static void
19615add_nr_var(dp, v, name, nr)
19616 dict_T *dp;
19617 dictitem_T *v;
19618 char *name;
19619 varnumber_T nr;
19620{
19621 STRCPY(v->di_key, name);
19622 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19623 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19624 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019625 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 v->di_tv.vval.v_number = nr;
19627}
19628
19629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 * ":return [expr]"
19631 */
19632 void
19633ex_return(eap)
19634 exarg_T *eap;
19635{
19636 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 int returning = FALSE;
19639
19640 if (current_funccal == NULL)
19641 {
19642 EMSG(_("E133: :return not inside a function"));
19643 return;
19644 }
19645
19646 if (eap->skip)
19647 ++emsg_skip;
19648
19649 eap->nextcmd = NULL;
19650 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019651 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 {
19653 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019654 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019656 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 }
19658 /* It's safer to return also on error. */
19659 else if (!eap->skip)
19660 {
19661 /*
19662 * Return unless the expression evaluation has been cancelled due to an
19663 * aborting error, an interrupt, or an exception.
19664 */
19665 if (!aborting())
19666 returning = do_return(eap, FALSE, TRUE, NULL);
19667 }
19668
19669 /* When skipping or the return gets pending, advance to the next command
19670 * in this line (!returning). Otherwise, ignore the rest of the line.
19671 * Following lines will be ignored by get_func_line(). */
19672 if (returning)
19673 eap->nextcmd = NULL;
19674 else if (eap->nextcmd == NULL) /* no argument */
19675 eap->nextcmd = check_nextcmd(arg);
19676
19677 if (eap->skip)
19678 --emsg_skip;
19679}
19680
19681/*
19682 * Return from a function. Possibly makes the return pending. Also called
19683 * for a pending return at the ":endtry" or after returning from an extra
19684 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 * FALSE when the return gets pending.
19688 */
19689 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019690do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 exarg_T *eap;
19692 int reanimate;
19693 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019694 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695{
19696 int idx;
19697 struct condstack *cstack = eap->cstack;
19698
19699 if (reanimate)
19700 /* Undo the return. */
19701 current_funccal->returned = FALSE;
19702
19703 /*
19704 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19705 * not in its finally clause (which then is to be executed next) is found.
19706 * In this case, make the ":return" pending for execution at the ":endtry".
19707 * Otherwise, return normally.
19708 */
19709 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19710 if (idx >= 0)
19711 {
19712 cstack->cs_pending[idx] = CSTP_RETURN;
19713
19714 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715 /* A pending return again gets pending. "rettv" points to an
19716 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019718 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 else
19720 {
19721 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019724 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 {
19728 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 else
19732 EMSG(_(e_outofmem));
19733 }
19734 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736
19737 if (reanimate)
19738 {
19739 /* The pending return value could be overwritten by a ":return"
19740 * without argument in a finally clause; reset the default
19741 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742 current_funccal->rettv->v_type = VAR_NUMBER;
19743 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 }
19745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 }
19748 else
19749 {
19750 current_funccal->returned = TRUE;
19751
19752 /* If the return is carried out now, store the return value. For
19753 * a return immediately after reanimation, the value is already
19754 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019758 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 }
19762 }
19763
19764 return idx < 0;
19765}
19766
19767/*
19768 * Free the variable with a pending return value.
19769 */
19770 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019771discard_pending_return(rettv)
19772 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773{
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775}
19776
19777/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019778 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 * is an allocated string. Used by report_pending() for verbose messages.
19780 */
19781 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782get_return_cmd(rettv)
19783 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019785 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019786 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019787 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019789 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019790 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019791 if (s == NULL)
19792 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793
19794 STRCPY(IObuff, ":return ");
19795 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19796 if (STRLEN(s) + 8 >= IOSIZE)
19797 STRCPY(IObuff + IOSIZE - 4, "...");
19798 vim_free(tofree);
19799 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800}
19801
19802/*
19803 * Get next function line.
19804 * Called by do_cmdline() to get the next line.
19805 * Returns allocated string, or NULL for end of function.
19806 */
19807/* ARGSUSED */
19808 char_u *
19809get_func_line(c, cookie, indent)
19810 int c; /* not used */
19811 void *cookie;
19812 int indent; /* not used */
19813{
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019815 ufunc_T *fp = fcp->func;
19816 char_u *retval;
19817 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818
19819 /* If breakpoints have been added/deleted need to check for it. */
19820 if (fcp->dbg_tick != debug_tick)
19821 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019822 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 sourcing_lnum);
19824 fcp->dbg_tick = debug_tick;
19825 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019826#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019827 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019828 func_line_end(cookie);
19829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830
Bram Moolenaar05159a02005-02-26 23:04:13 +000019831 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019832 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19833 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 retval = NULL;
19835 else
19836 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019837 /* Skip NULL lines (continuation lines). */
19838 while (fcp->linenr < gap->ga_len
19839 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19840 ++fcp->linenr;
19841 if (fcp->linenr >= gap->ga_len)
19842 retval = NULL;
19843 else
19844 {
19845 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19846 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019847#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000019848 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019849 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019850#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 }
19853
19854 /* Did we encounter a breakpoint? */
19855 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19856 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019857 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019859 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 sourcing_lnum);
19861 fcp->dbg_tick = debug_tick;
19862 }
19863
19864 return retval;
19865}
19866
Bram Moolenaar05159a02005-02-26 23:04:13 +000019867#if defined(FEAT_PROFILE) || defined(PROTO)
19868/*
19869 * Called when starting to read a function line.
19870 * "sourcing_lnum" must be correct!
19871 * When skipping lines it may not actually be executed, but we won't find out
19872 * until later and we need to store the time now.
19873 */
19874 void
19875func_line_start(cookie)
19876 void *cookie;
19877{
19878 funccall_T *fcp = (funccall_T *)cookie;
19879 ufunc_T *fp = fcp->func;
19880
19881 if (fp->uf_profiling && sourcing_lnum >= 1
19882 && sourcing_lnum <= fp->uf_lines.ga_len)
19883 {
19884 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019885 /* Skip continuation lines. */
19886 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19887 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019888 fp->uf_tml_execed = FALSE;
19889 profile_start(&fp->uf_tml_start);
19890 profile_zero(&fp->uf_tml_children);
19891 profile_get_wait(&fp->uf_tml_wait);
19892 }
19893}
19894
19895/*
19896 * Called when actually executing a function line.
19897 */
19898 void
19899func_line_exec(cookie)
19900 void *cookie;
19901{
19902 funccall_T *fcp = (funccall_T *)cookie;
19903 ufunc_T *fp = fcp->func;
19904
19905 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19906 fp->uf_tml_execed = TRUE;
19907}
19908
19909/*
19910 * Called when done with a function line.
19911 */
19912 void
19913func_line_end(cookie)
19914 void *cookie;
19915{
19916 funccall_T *fcp = (funccall_T *)cookie;
19917 ufunc_T *fp = fcp->func;
19918
19919 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19920 {
19921 if (fp->uf_tml_execed)
19922 {
19923 ++fp->uf_tml_count[fp->uf_tml_idx];
19924 profile_end(&fp->uf_tml_start);
19925 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019926 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000019927 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
19928 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019929 }
19930 fp->uf_tml_idx = -1;
19931 }
19932}
19933#endif
19934
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935/*
19936 * Return TRUE if the currently active function should be ended, because a
19937 * return was encountered or an error occured. Used inside a ":while".
19938 */
19939 int
19940func_has_ended(cookie)
19941 void *cookie;
19942{
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944
19945 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19946 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019947 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 || fcp->returned);
19949}
19950
19951/*
19952 * return TRUE if cookie indicates a function which "abort"s on errors.
19953 */
19954 int
19955func_has_abort(cookie)
19956 void *cookie;
19957{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019958 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959}
19960
19961#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19962typedef enum
19963{
19964 VAR_FLAVOUR_DEFAULT,
19965 VAR_FLAVOUR_SESSION,
19966 VAR_FLAVOUR_VIMINFO
19967} var_flavour_T;
19968
19969static var_flavour_T var_flavour __ARGS((char_u *varname));
19970
19971 static var_flavour_T
19972var_flavour(varname)
19973 char_u *varname;
19974{
19975 char_u *p = varname;
19976
19977 if (ASCII_ISUPPER(*p))
19978 {
19979 while (*(++p))
19980 if (ASCII_ISLOWER(*p))
19981 return VAR_FLAVOUR_SESSION;
19982 return VAR_FLAVOUR_VIMINFO;
19983 }
19984 else
19985 return VAR_FLAVOUR_DEFAULT;
19986}
19987#endif
19988
19989#if defined(FEAT_VIMINFO) || defined(PROTO)
19990/*
19991 * Restore global vars that start with a capital from the viminfo file
19992 */
19993 int
19994read_viminfo_varlist(virp, writing)
19995 vir_T *virp;
19996 int writing;
19997{
19998 char_u *tab;
19999 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020000 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001
20002 if (!writing && (find_viminfo_parameter('!') != NULL))
20003 {
20004 tab = vim_strchr(virp->vir_line + 1, '\t');
20005 if (tab != NULL)
20006 {
20007 *tab++ = '\0'; /* isolate the variable name */
20008 if (*tab == 'S') /* string var */
20009 is_string = TRUE;
20010
20011 tab = vim_strchr(tab, '\t');
20012 if (tab != NULL)
20013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 if (is_string)
20015 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020016 tv.v_type = VAR_STRING;
20017 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 }
20020 else
20021 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020022 tv.v_type = VAR_NUMBER;
20023 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020025 set_var(virp->vir_line + 1, &tv, FALSE);
20026 if (is_string)
20027 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 }
20029 }
20030 }
20031
20032 return viminfo_readline(virp);
20033}
20034
20035/*
20036 * Write global vars that start with a capital to the viminfo file
20037 */
20038 void
20039write_viminfo_varlist(fp)
20040 FILE *fp;
20041{
Bram Moolenaar33570922005-01-25 22:26:29 +000020042 hashitem_T *hi;
20043 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020044 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020045 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020046 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020048 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049
20050 if (find_viminfo_parameter('!') == NULL)
20051 return;
20052
20053 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020054
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 todo = globvarht.ht_used;
20056 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020058 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020060 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020061 this_var = HI2DI(hi);
20062 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020065 {
20066 case VAR_STRING: s = "STR"; break;
20067 case VAR_NUMBER: s = "NUM"; break;
20068 default: continue;
20069 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020071 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020072 if (p != NULL)
20073 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020074 vim_free(tofree);
20075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 }
20077 }
20078}
20079#endif
20080
20081#if defined(FEAT_SESSION) || defined(PROTO)
20082 int
20083store_session_globals(fd)
20084 FILE *fd;
20085{
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 hashitem_T *hi;
20087 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020088 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 char_u *p, *t;
20090
Bram Moolenaar33570922005-01-25 22:26:29 +000020091 todo = globvarht.ht_used;
20092 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020094 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020096 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 this_var = HI2DI(hi);
20098 if ((this_var->di_tv.v_type == VAR_NUMBER
20099 || this_var->di_tv.v_type == VAR_STRING)
20100 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020101 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020102 /* Escape special characters with a backslash. Turn a LF and
20103 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020104 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020105 (char_u *)"\\\"\n\r");
20106 if (p == NULL) /* out of memory */
20107 break;
20108 for (t = p; *t != NUL; ++t)
20109 if (*t == '\n')
20110 *t = 'n';
20111 else if (*t == '\r')
20112 *t = 'r';
20113 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020114 this_var->di_key,
20115 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20116 : ' ',
20117 p,
20118 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20119 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020120 || put_eol(fd) == FAIL)
20121 {
20122 vim_free(p);
20123 return FAIL;
20124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 vim_free(p);
20126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 }
20128 }
20129 return OK;
20130}
20131#endif
20132
Bram Moolenaar661b1822005-07-28 22:36:45 +000020133/*
20134 * Display script name where an item was last set.
20135 * Should only be invoked when 'verbose' is non-zero.
20136 */
20137 void
20138last_set_msg(scriptID)
20139 scid_T scriptID;
20140{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020141 char_u *p;
20142
Bram Moolenaar661b1822005-07-28 22:36:45 +000020143 if (scriptID != 0)
20144 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020145 p = home_replace_save(NULL, get_scriptname(scriptID));
20146 if (p != NULL)
20147 {
20148 verbose_enter();
20149 MSG_PUTS(_("\n\tLast set from "));
20150 MSG_PUTS(p);
20151 vim_free(p);
20152 verbose_leave();
20153 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020154 }
20155}
20156
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157#endif /* FEAT_EVAL */
20158
20159#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20160
20161
20162#ifdef WIN3264
20163/*
20164 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20165 */
20166static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20167static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20168static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20169
20170/*
20171 * Get the short pathname of a file.
20172 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
20173 */
20174 static int
20175get_short_pathname(fnamep, bufp, fnamelen)
20176 char_u **fnamep;
20177 char_u **bufp;
20178 int *fnamelen;
20179{
20180 int l,len;
20181 char_u *newbuf;
20182
20183 len = *fnamelen;
20184
20185 l = GetShortPathName(*fnamep, *fnamep, len);
20186 if (l > len - 1)
20187 {
20188 /* If that doesn't work (not enough space), then save the string
20189 * and try again with a new buffer big enough
20190 */
20191 newbuf = vim_strnsave(*fnamep, l);
20192 if (newbuf == NULL)
20193 return 0;
20194
20195 vim_free(*bufp);
20196 *fnamep = *bufp = newbuf;
20197
20198 l = GetShortPathName(*fnamep,*fnamep,l+1);
20199
20200 /* Really should always succeed, as the buffer is big enough */
20201 }
20202
20203 *fnamelen = l;
20204 return 1;
20205}
20206
20207/*
20208 * Create a short path name. Returns the length of the buffer it needs.
20209 * Doesn't copy over the end of the buffer passed in.
20210 */
20211 static int
20212shortpath_for_invalid_fname(fname, bufp, fnamelen)
20213 char_u **fname;
20214 char_u **bufp;
20215 int *fnamelen;
20216{
20217 char_u *s, *p, *pbuf2, *pbuf3;
20218 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020219 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220
20221 /* Make a copy */
20222 len2 = *fnamelen;
20223 pbuf2 = vim_strnsave(*fname, len2);
20224 pbuf3 = NULL;
20225
20226 s = pbuf2 + len2 - 1; /* Find the end */
20227 slen = 1;
20228 plen = len2;
20229
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020230 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 {
20232 --s;
20233 ++slen;
20234 --plen;
20235 }
20236
20237 do
20238 {
20239 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020240 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 {
20242 --s;
20243 ++slen;
20244 --plen;
20245 }
20246 if (s <= pbuf2)
20247 break;
20248
20249 /* Remeber the character that is about to be blatted */
20250 ch = *s;
20251 *s = 0; /* get_short_pathname requires a null-terminated string */
20252
20253 /* Try it in situ */
20254 p = pbuf2;
20255 if (!get_short_pathname(&p, &pbuf3, &plen))
20256 {
20257 vim_free(pbuf2);
20258 return -1;
20259 }
20260 *s = ch; /* Preserve the string */
20261 } while (plen == 0);
20262
20263 if (plen > 0)
20264 {
20265 /* Remeber the length of the new string. */
20266 *fnamelen = len = plen + slen;
20267 vim_free(*bufp);
20268 if (len > len2)
20269 {
20270 /* If there's not enough space in the currently allocated string,
20271 * then copy it to a buffer big enough.
20272 */
20273 *fname= *bufp = vim_strnsave(p, len);
20274 if (*fname == NULL)
20275 return -1;
20276 }
20277 else
20278 {
20279 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20280 *fname = *bufp = pbuf2;
20281 if (p != pbuf2)
20282 strncpy(*fname, p, plen);
20283 pbuf2 = NULL;
20284 }
20285 /* Concat the next bit */
20286 strncpy(*fname + plen, s, slen);
20287 (*fname)[len] = '\0';
20288 }
20289 vim_free(pbuf3);
20290 vim_free(pbuf2);
20291 return 0;
20292}
20293
20294/*
20295 * Get a pathname for a partial path.
20296 */
20297 static int
20298shortpath_for_partial(fnamep, bufp, fnamelen)
20299 char_u **fnamep;
20300 char_u **bufp;
20301 int *fnamelen;
20302{
20303 int sepcount, len, tflen;
20304 char_u *p;
20305 char_u *pbuf, *tfname;
20306 int hasTilde;
20307
20308 /* Count up the path seperators from the RHS.. so we know which part
20309 * of the path to return.
20310 */
20311 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020312 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 if (vim_ispathsep(*p))
20314 ++sepcount;
20315
20316 /* Need full path first (use expand_env() to remove a "~/") */
20317 hasTilde = (**fnamep == '~');
20318 if (hasTilde)
20319 pbuf = tfname = expand_env_save(*fnamep);
20320 else
20321 pbuf = tfname = FullName_save(*fnamep, FALSE);
20322
20323 len = tflen = STRLEN(tfname);
20324
20325 if (!get_short_pathname(&tfname, &pbuf, &len))
20326 return -1;
20327
20328 if (len == 0)
20329 {
20330 /* Don't have a valid filename, so shorten the rest of the
20331 * path if we can. This CAN give us invalid 8.3 filenames, but
20332 * there's not a lot of point in guessing what it might be.
20333 */
20334 len = tflen;
20335 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20336 return -1;
20337 }
20338
20339 /* Count the paths backward to find the beginning of the desired string. */
20340 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020341 {
20342#ifdef FEAT_MBYTE
20343 if (has_mbyte)
20344 p -= mb_head_off(tfname, p);
20345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 if (vim_ispathsep(*p))
20347 {
20348 if (sepcount == 0 || (hasTilde && sepcount == 1))
20349 break;
20350 else
20351 sepcount --;
20352 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 if (hasTilde)
20355 {
20356 --p;
20357 if (p >= tfname)
20358 *p = '~';
20359 else
20360 return -1;
20361 }
20362 else
20363 ++p;
20364
20365 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20366 vim_free(*bufp);
20367 *fnamelen = (int)STRLEN(p);
20368 *bufp = pbuf;
20369 *fnamep = p;
20370
20371 return 0;
20372}
20373#endif /* WIN3264 */
20374
20375/*
20376 * Adjust a filename, according to a string of modifiers.
20377 * *fnamep must be NUL terminated when called. When returning, the length is
20378 * determined by *fnamelen.
20379 * Returns valid flags.
20380 * When there is an error, *fnamep is set to NULL.
20381 */
20382 int
20383modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20384 char_u *src; /* string with modifiers */
20385 int *usedlen; /* characters after src that are used */
20386 char_u **fnamep; /* file name so far */
20387 char_u **bufp; /* buffer for allocated file name or NULL */
20388 int *fnamelen; /* length of fnamep */
20389{
20390 int valid = 0;
20391 char_u *tail;
20392 char_u *s, *p, *pbuf;
20393 char_u dirname[MAXPATHL];
20394 int c;
20395 int has_fullname = 0;
20396#ifdef WIN3264
20397 int has_shortname = 0;
20398#endif
20399
20400repeat:
20401 /* ":p" - full path/file_name */
20402 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20403 {
20404 has_fullname = 1;
20405
20406 valid |= VALID_PATH;
20407 *usedlen += 2;
20408
20409 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20410 if ((*fnamep)[0] == '~'
20411#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20412 && ((*fnamep)[1] == '/'
20413# ifdef BACKSLASH_IN_FILENAME
20414 || (*fnamep)[1] == '\\'
20415# endif
20416 || (*fnamep)[1] == NUL)
20417
20418#endif
20419 )
20420 {
20421 *fnamep = expand_env_save(*fnamep);
20422 vim_free(*bufp); /* free any allocated file name */
20423 *bufp = *fnamep;
20424 if (*fnamep == NULL)
20425 return -1;
20426 }
20427
20428 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020429 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 {
20431 if (vim_ispathsep(*p)
20432 && p[1] == '.'
20433 && (p[2] == NUL
20434 || vim_ispathsep(p[2])
20435 || (p[2] == '.'
20436 && (p[3] == NUL || vim_ispathsep(p[3])))))
20437 break;
20438 }
20439
20440 /* FullName_save() is slow, don't use it when not needed. */
20441 if (*p != NUL || !vim_isAbsName(*fnamep))
20442 {
20443 *fnamep = FullName_save(*fnamep, *p != NUL);
20444 vim_free(*bufp); /* free any allocated file name */
20445 *bufp = *fnamep;
20446 if (*fnamep == NULL)
20447 return -1;
20448 }
20449
20450 /* Append a path separator to a directory. */
20451 if (mch_isdir(*fnamep))
20452 {
20453 /* Make room for one or two extra characters. */
20454 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20455 vim_free(*bufp); /* free any allocated file name */
20456 *bufp = *fnamep;
20457 if (*fnamep == NULL)
20458 return -1;
20459 add_pathsep(*fnamep);
20460 }
20461 }
20462
20463 /* ":." - path relative to the current directory */
20464 /* ":~" - path relative to the home directory */
20465 /* ":8" - shortname path - postponed till after */
20466 while (src[*usedlen] == ':'
20467 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20468 {
20469 *usedlen += 2;
20470 if (c == '8')
20471 {
20472#ifdef WIN3264
20473 has_shortname = 1; /* Postpone this. */
20474#endif
20475 continue;
20476 }
20477 pbuf = NULL;
20478 /* Need full path first (use expand_env() to remove a "~/") */
20479 if (!has_fullname)
20480 {
20481 if (c == '.' && **fnamep == '~')
20482 p = pbuf = expand_env_save(*fnamep);
20483 else
20484 p = pbuf = FullName_save(*fnamep, FALSE);
20485 }
20486 else
20487 p = *fnamep;
20488
20489 has_fullname = 0;
20490
20491 if (p != NULL)
20492 {
20493 if (c == '.')
20494 {
20495 mch_dirname(dirname, MAXPATHL);
20496 s = shorten_fname(p, dirname);
20497 if (s != NULL)
20498 {
20499 *fnamep = s;
20500 if (pbuf != NULL)
20501 {
20502 vim_free(*bufp); /* free any allocated file name */
20503 *bufp = pbuf;
20504 pbuf = NULL;
20505 }
20506 }
20507 }
20508 else
20509 {
20510 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20511 /* Only replace it when it starts with '~' */
20512 if (*dirname == '~')
20513 {
20514 s = vim_strsave(dirname);
20515 if (s != NULL)
20516 {
20517 *fnamep = s;
20518 vim_free(*bufp);
20519 *bufp = s;
20520 }
20521 }
20522 }
20523 vim_free(pbuf);
20524 }
20525 }
20526
20527 tail = gettail(*fnamep);
20528 *fnamelen = (int)STRLEN(*fnamep);
20529
20530 /* ":h" - head, remove "/file_name", can be repeated */
20531 /* Don't remove the first "/" or "c:\" */
20532 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20533 {
20534 valid |= VALID_HEAD;
20535 *usedlen += 2;
20536 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020537 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 --tail;
20539 *fnamelen = (int)(tail - *fnamep);
20540#ifdef VMS
20541 if (*fnamelen > 0)
20542 *fnamelen += 1; /* the path separator is part of the path */
20543#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020544 while (tail > s && !after_pathsep(s, tail))
20545 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 }
20547
20548 /* ":8" - shortname */
20549 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20550 {
20551 *usedlen += 2;
20552#ifdef WIN3264
20553 has_shortname = 1;
20554#endif
20555 }
20556
20557#ifdef WIN3264
20558 /* Check shortname after we have done 'heads' and before we do 'tails'
20559 */
20560 if (has_shortname)
20561 {
20562 pbuf = NULL;
20563 /* Copy the string if it is shortened by :h */
20564 if (*fnamelen < (int)STRLEN(*fnamep))
20565 {
20566 p = vim_strnsave(*fnamep, *fnamelen);
20567 if (p == 0)
20568 return -1;
20569 vim_free(*bufp);
20570 *bufp = *fnamep = p;
20571 }
20572
20573 /* Split into two implementations - makes it easier. First is where
20574 * there isn't a full name already, second is where there is.
20575 */
20576 if (!has_fullname && !vim_isAbsName(*fnamep))
20577 {
20578 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20579 return -1;
20580 }
20581 else
20582 {
20583 int l;
20584
20585 /* Simple case, already have the full-name
20586 * Nearly always shorter, so try first time. */
20587 l = *fnamelen;
20588 if (!get_short_pathname(fnamep, bufp, &l))
20589 return -1;
20590
20591 if (l == 0)
20592 {
20593 /* Couldn't find the filename.. search the paths.
20594 */
20595 l = *fnamelen;
20596 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20597 return -1;
20598 }
20599 *fnamelen = l;
20600 }
20601 }
20602#endif /* WIN3264 */
20603
20604 /* ":t" - tail, just the basename */
20605 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20606 {
20607 *usedlen += 2;
20608 *fnamelen -= (int)(tail - *fnamep);
20609 *fnamep = tail;
20610 }
20611
20612 /* ":e" - extension, can be repeated */
20613 /* ":r" - root, without extension, can be repeated */
20614 while (src[*usedlen] == ':'
20615 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20616 {
20617 /* find a '.' in the tail:
20618 * - for second :e: before the current fname
20619 * - otherwise: The last '.'
20620 */
20621 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20622 s = *fnamep - 2;
20623 else
20624 s = *fnamep + *fnamelen - 1;
20625 for ( ; s > tail; --s)
20626 if (s[0] == '.')
20627 break;
20628 if (src[*usedlen + 1] == 'e') /* :e */
20629 {
20630 if (s > tail)
20631 {
20632 *fnamelen += (int)(*fnamep - (s + 1));
20633 *fnamep = s + 1;
20634#ifdef VMS
20635 /* cut version from the extension */
20636 s = *fnamep + *fnamelen - 1;
20637 for ( ; s > *fnamep; --s)
20638 if (s[0] == ';')
20639 break;
20640 if (s > *fnamep)
20641 *fnamelen = s - *fnamep;
20642#endif
20643 }
20644 else if (*fnamep <= tail)
20645 *fnamelen = 0;
20646 }
20647 else /* :r */
20648 {
20649 if (s > tail) /* remove one extension */
20650 *fnamelen = (int)(s - *fnamep);
20651 }
20652 *usedlen += 2;
20653 }
20654
20655 /* ":s?pat?foo?" - substitute */
20656 /* ":gs?pat?foo?" - global substitute */
20657 if (src[*usedlen] == ':'
20658 && (src[*usedlen + 1] == 's'
20659 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20660 {
20661 char_u *str;
20662 char_u *pat;
20663 char_u *sub;
20664 int sep;
20665 char_u *flags;
20666 int didit = FALSE;
20667
20668 flags = (char_u *)"";
20669 s = src + *usedlen + 2;
20670 if (src[*usedlen + 1] == 'g')
20671 {
20672 flags = (char_u *)"g";
20673 ++s;
20674 }
20675
20676 sep = *s++;
20677 if (sep)
20678 {
20679 /* find end of pattern */
20680 p = vim_strchr(s, sep);
20681 if (p != NULL)
20682 {
20683 pat = vim_strnsave(s, (int)(p - s));
20684 if (pat != NULL)
20685 {
20686 s = p + 1;
20687 /* find end of substitution */
20688 p = vim_strchr(s, sep);
20689 if (p != NULL)
20690 {
20691 sub = vim_strnsave(s, (int)(p - s));
20692 str = vim_strnsave(*fnamep, *fnamelen);
20693 if (sub != NULL && str != NULL)
20694 {
20695 *usedlen = (int)(p + 1 - src);
20696 s = do_string_sub(str, pat, sub, flags);
20697 if (s != NULL)
20698 {
20699 *fnamep = s;
20700 *fnamelen = (int)STRLEN(s);
20701 vim_free(*bufp);
20702 *bufp = s;
20703 didit = TRUE;
20704 }
20705 }
20706 vim_free(sub);
20707 vim_free(str);
20708 }
20709 vim_free(pat);
20710 }
20711 }
20712 /* after using ":s", repeat all the modifiers */
20713 if (didit)
20714 goto repeat;
20715 }
20716 }
20717
20718 return valid;
20719}
20720
20721/*
20722 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20723 * "flags" can be "g" to do a global substitute.
20724 * Returns an allocated string, NULL for error.
20725 */
20726 char_u *
20727do_string_sub(str, pat, sub, flags)
20728 char_u *str;
20729 char_u *pat;
20730 char_u *sub;
20731 char_u *flags;
20732{
20733 int sublen;
20734 regmatch_T regmatch;
20735 int i;
20736 int do_all;
20737 char_u *tail;
20738 garray_T ga;
20739 char_u *ret;
20740 char_u *save_cpo;
20741
20742 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20743 save_cpo = p_cpo;
20744 p_cpo = (char_u *)"";
20745
20746 ga_init2(&ga, 1, 200);
20747
20748 do_all = (flags[0] == 'g');
20749
20750 regmatch.rm_ic = p_ic;
20751 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20752 if (regmatch.regprog != NULL)
20753 {
20754 tail = str;
20755 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20756 {
20757 /*
20758 * Get some space for a temporary buffer to do the substitution
20759 * into. It will contain:
20760 * - The text up to where the match is.
20761 * - The substituted text.
20762 * - The text after the match.
20763 */
20764 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20765 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20766 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20767 {
20768 ga_clear(&ga);
20769 break;
20770 }
20771
20772 /* copy the text up to where the match is */
20773 i = (int)(regmatch.startp[0] - tail);
20774 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20775 /* add the substituted text */
20776 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20777 + ga.ga_len + i, TRUE, TRUE, FALSE);
20778 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 /* avoid getting stuck on a match with an empty string */
20780 if (tail == regmatch.endp[0])
20781 {
20782 if (*tail == NUL)
20783 break;
20784 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20785 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 }
20787 else
20788 {
20789 tail = regmatch.endp[0];
20790 if (*tail == NUL)
20791 break;
20792 }
20793 if (!do_all)
20794 break;
20795 }
20796
20797 if (ga.ga_data != NULL)
20798 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20799
20800 vim_free(regmatch.regprog);
20801 }
20802
20803 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20804 ga_clear(&ga);
20805 p_cpo = save_cpo;
20806
20807 return ret;
20808}
20809
20810#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */