blob: 4336a26334112d75dfcaf31431f627a86cfa616b [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));
373static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
374static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
375static int check_changedtick __ARGS((char_u *arg));
376static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
377static void clear_lval __ARGS((lval_T *lp));
378static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
379static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
380static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
381static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
382static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
383static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
384static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
385static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
386static void item_lock __ARGS((typval_T *tv, int deep, int lock));
387static int tv_islocked __ARGS((typval_T *tv));
388
Bram Moolenaar33570922005-01-25 22:26:29 +0000389static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
390static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
393static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
394static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
395static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000398static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
400static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000403static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static listitem_T *listitem_alloc __ARGS((void));
405static void listitem_free __ARGS((listitem_T *item));
406static void listitem_remove __ARGS((list_T *l, listitem_T *item));
407static long list_len __ARGS((list_T *l));
408static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
409static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
410static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000411static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000412static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static void list_append __ARGS((list_T *l, listitem_T *item));
415static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000416static int list_append_string __ARGS((list_T *l, char_u *str, int len));
417static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
419static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
420static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000421static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000423static char_u *list2string __ARGS((typval_T *tv, int copyID));
424static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000425static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
426static void set_ref_in_list __ARGS((list_T *l, int copyID));
427static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static void dict_unref __ARGS((dict_T *d));
429static void dict_free __ARGS((dict_T *d));
430static dictitem_T *dictitem_alloc __ARGS((char_u *key));
431static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
432static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
433static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int dict_add __ARGS((dict_T *d, dictitem_T *item));
436static long dict_len __ARGS((dict_T *d));
437static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
443static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
444static int find_internal_func __ARGS((char_u *name));
445static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
446static 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));
447static 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 +0000448static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
450static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000469#if defined(FEAT_INS_EXPAND)
470static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
478static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000504static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000506static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000512static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000513static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000520static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000521static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000544static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000550static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000567static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571#ifdef vim_mkdir
572static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
573#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000578static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000579static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000581static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000593static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000595static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000602static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000603static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000604static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000609static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
613#ifdef HAVE_STRFTIME
614static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
616static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000628static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000629static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000630static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000631static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000632static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000634static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000648static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000653static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
654static pos_T *var2fpos __ARGS((typval_T *varp, int lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static int get_env_len __ARGS((char_u **arg));
656static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000657static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000658static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
659#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
660#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
661 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000662static 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 +0000663static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000664static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000665static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
666static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static typval_T *alloc_tv __ARGS((void));
668static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void init_tv __ARGS((typval_T *varp));
670static long get_tv_number __ARGS((typval_T *varp));
671static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000672static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static char_u *get_tv_string __ARGS((typval_T *varp));
674static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000675static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
679static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
680static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
681static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
682static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
683static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
684static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000685static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000687static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
689static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
690static int eval_fname_script __ARGS((char_u *p));
691static int eval_fname_sid __ARGS((char_u *p));
692static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static ufunc_T *find_func __ARGS((char_u *name));
694static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000695static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696#ifdef FEAT_PROFILE
697static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000698static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
699static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
700static int
701# ifdef __BORLANDC__
702 _RTLENTRYF
703# endif
704 prof_total_cmp __ARGS((const void *s1, const void *s2));
705static int
706# ifdef __BORLANDC__
707 _RTLENTRYF
708# endif
709 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000710#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000711static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000712static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000713static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void func_free __ARGS((ufunc_T *fp));
715static void func_unref __ARGS((char_u *name));
716static void func_ref __ARGS((char_u *name));
717static 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));
718static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000719static win_T *find_win_by_nr __ARGS((typval_T *vp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000720static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000721static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000723/* Character used as separated in autoload function/variable names. */
724#define AUTOLOAD_CHAR '#'
725
Bram Moolenaar33570922005-01-25 22:26:29 +0000726/*
727 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000728 */
729 void
730eval_init()
731{
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 int i;
733 struct vimvar *p;
734
735 init_var_dict(&globvardict, &globvars_var);
736 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000737 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000738 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000739
740 for (i = 0; i < VV_LEN; ++i)
741 {
742 p = &vimvars[i];
743 STRCPY(p->vv_di.di_key, p->vv_name);
744 if (p->vv_flags & VV_RO)
745 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
746 else if (p->vv_flags & VV_RO_SBX)
747 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
748 else
749 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000750
751 /* add to v: scope dict, unless the value is not always available */
752 if (p->vv_type != VAR_UNKNOWN)
753 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000755 /* add to compat scope dict */
756 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000757 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000758}
759
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000760#if defined(EXITFREE) || defined(PROTO)
761 void
762eval_clear()
763{
764 int i;
765 struct vimvar *p;
766
767 for (i = 0; i < VV_LEN; ++i)
768 {
769 p = &vimvars[i];
770 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000771 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000772 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000773 p->vv_di.di_tv.vval.v_string = NULL;
774 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000775 }
776 hash_clear(&vimvarht);
777 hash_clear(&compat_hashtab);
778
779 /* script-local variables */
780 for (i = 1; i <= ga_scripts.ga_len; ++i)
781 vars_clear(&SCRIPT_VARS(i));
782 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000783 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000784
785 /* global variables */
786 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000787
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000788 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000789 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000790 hash_clear(&func_hashtab);
791
792 /* unreferenced lists and dicts */
793 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000794}
795#endif
796
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 * Return the name of the executed function.
799 */
800 char_u *
801func_name(cookie)
802 void *cookie;
803{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000804 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806
807/*
808 * Return the address holding the next breakpoint line for a funccall cookie.
809 */
810 linenr_T *
811func_breakpoint(cookie)
812 void *cookie;
813{
Bram Moolenaar33570922005-01-25 22:26:29 +0000814 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
817/*
818 * Return the address holding the debug tick for a funccall cookie.
819 */
820 int *
821func_dbg_tick(cookie)
822 void *cookie;
823{
Bram Moolenaar33570922005-01-25 22:26:29 +0000824 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
827/*
828 * Return the nesting level for a funccall cookie.
829 */
830 int
831func_level(cookie)
832 void *cookie;
833{
Bram Moolenaar33570922005-01-25 22:26:29 +0000834 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835}
836
837/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000838funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
840/*
841 * Return TRUE when a function was ended by a ":return" command.
842 */
843 int
844current_func_returned()
845{
846 return current_funccal->returned;
847}
848
849
850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 * Set an internal variable to a string value. Creates the variable if it does
852 * not already exist.
853 */
854 void
855set_internal_string_var(name, value)
856 char_u *name;
857 char_u *value;
858{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000859 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862 val = vim_strsave(value);
863 if (val != NULL)
864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000865 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000866 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000868 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 }
872}
873
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000874static lval_T *redir_lval = NULL;
875static char_u *redir_endp = NULL;
876static char_u *redir_varname = NULL;
877
878/*
879 * Start recording command output to a variable
880 * Returns OK if successfully completed the setup. FAIL otherwise.
881 */
882 int
883var_redir_start(name, append)
884 char_u *name;
885 int append; /* append to an existing variable */
886{
887 int save_emsg;
888 int err;
889 typval_T tv;
890
891 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000892 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000893 {
894 EMSG(_(e_invarg));
895 return FAIL;
896 }
897
898 redir_varname = vim_strsave(name);
899 if (redir_varname == NULL)
900 return FAIL;
901
902 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
903 if (redir_lval == NULL)
904 {
905 var_redir_stop();
906 return FAIL;
907 }
908
909 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000910 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
911 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000912 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
913 {
914 if (redir_endp != NULL && *redir_endp != NUL)
915 /* Trailing characters are present after the variable name */
916 EMSG(_(e_trailing));
917 else
918 EMSG(_(e_invarg));
919 var_redir_stop();
920 return FAIL;
921 }
922
923 /* check if we can write to the variable: set it to or append an empty
924 * string */
925 save_emsg = did_emsg;
926 did_emsg = FALSE;
927 tv.v_type = VAR_STRING;
928 tv.vval.v_string = (char_u *)"";
929 if (append)
930 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
931 else
932 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
933 err = did_emsg;
934 did_emsg += save_emsg;
935 if (err)
936 {
937 var_redir_stop();
938 return FAIL;
939 }
940 if (redir_lval->ll_newkey != NULL)
941 {
942 /* Dictionary item was created, don't do it again. */
943 vim_free(redir_lval->ll_newkey);
944 redir_lval->ll_newkey = NULL;
945 }
946
947 return OK;
948}
949
950/*
951 * Append "value[len]" to the variable set by var_redir_start().
952 */
953 void
954var_redir_str(value, len)
955 char_u *value;
956 int len;
957{
958 char_u *val;
959 typval_T tv;
960 int save_emsg;
961 int err;
962
963 if (redir_lval == NULL)
964 return;
965
966 if (len == -1)
967 /* Append the entire string */
968 val = vim_strsave(value);
969 else
970 /* Append only the specified number of characters */
971 val = vim_strnsave(value, len);
972 if (val == NULL)
973 return;
974
975 tv.v_type = VAR_STRING;
976 tv.vval.v_string = val;
977
978 save_emsg = did_emsg;
979 did_emsg = FALSE;
980 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
981 err = did_emsg;
982 did_emsg += save_emsg;
983 if (err)
984 var_redir_stop();
985
986 vim_free(tv.vval.v_string);
987}
988
989/*
990 * Stop redirecting command output to a variable.
991 */
992 void
993var_redir_stop()
994{
995 if (redir_lval != NULL)
996 {
997 clear_lval(redir_lval);
998 vim_free(redir_lval);
999 redir_lval = NULL;
1000 }
1001 vim_free(redir_varname);
1002 redir_varname = NULL;
1003}
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005# if defined(FEAT_MBYTE) || defined(PROTO)
1006 int
1007eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1008 char_u *enc_from;
1009 char_u *enc_to;
1010 char_u *fname_from;
1011 char_u *fname_to;
1012{
1013 int err = FALSE;
1014
1015 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1016 set_vim_var_string(VV_CC_TO, enc_to, -1);
1017 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1018 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1019 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1020 err = TRUE;
1021 set_vim_var_string(VV_CC_FROM, NULL, -1);
1022 set_vim_var_string(VV_CC_TO, NULL, -1);
1023 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1024 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1025
1026 if (err)
1027 return FAIL;
1028 return OK;
1029}
1030# endif
1031
1032# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1033 int
1034eval_printexpr(fname, args)
1035 char_u *fname;
1036 char_u *args;
1037{
1038 int err = FALSE;
1039
1040 set_vim_var_string(VV_FNAME_IN, fname, -1);
1041 set_vim_var_string(VV_CMDARG, args, -1);
1042 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1043 err = TRUE;
1044 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1045 set_vim_var_string(VV_CMDARG, NULL, -1);
1046
1047 if (err)
1048 {
1049 mch_remove(fname);
1050 return FAIL;
1051 }
1052 return OK;
1053}
1054# endif
1055
1056# if defined(FEAT_DIFF) || defined(PROTO)
1057 void
1058eval_diff(origfile, newfile, outfile)
1059 char_u *origfile;
1060 char_u *newfile;
1061 char_u *outfile;
1062{
1063 int err = FALSE;
1064
1065 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1066 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1067 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1068 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1069 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1070 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1071 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1072}
1073
1074 void
1075eval_patch(origfile, difffile, outfile)
1076 char_u *origfile;
1077 char_u *difffile;
1078 char_u *outfile;
1079{
1080 int err;
1081
1082 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1083 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1084 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1085 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1086 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1087 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1088 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1089}
1090# endif
1091
1092/*
1093 * Top level evaluation function, returning a boolean.
1094 * Sets "error" to TRUE if there was an error.
1095 * Return TRUE or FALSE.
1096 */
1097 int
1098eval_to_bool(arg, error, nextcmd, skip)
1099 char_u *arg;
1100 int *error;
1101 char_u **nextcmd;
1102 int skip; /* only parse, don't execute */
1103{
Bram Moolenaar33570922005-01-25 22:26:29 +00001104 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 int retval = FALSE;
1106
1107 if (skip)
1108 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001109 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 else
1112 {
1113 *error = FALSE;
1114 if (!skip)
1115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001116 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 }
1119 }
1120 if (skip)
1121 --emsg_skip;
1122
1123 return retval;
1124}
1125
1126/*
1127 * Top level evaluation function, returning a string. If "skip" is TRUE,
1128 * only parsing to "nextcmd" is done, without reporting errors. Return
1129 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1130 */
1131 char_u *
1132eval_to_string_skip(arg, nextcmd, skip)
1133 char_u *arg;
1134 char_u **nextcmd;
1135 int skip; /* only parse, don't execute */
1136{
Bram Moolenaar33570922005-01-25 22:26:29 +00001137 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 char_u *retval;
1139
1140 if (skip)
1141 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 retval = NULL;
1144 else
1145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 retval = vim_strsave(get_tv_string(&tv));
1147 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 if (skip)
1150 --emsg_skip;
1151
1152 return retval;
1153}
1154
1155/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001156 * Skip over an expression at "*pp".
1157 * Return FAIL for an error, OK otherwise.
1158 */
1159 int
1160skip_expr(pp)
1161 char_u **pp;
1162{
Bram Moolenaar33570922005-01-25 22:26:29 +00001163 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001164
1165 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001167}
1168
1169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 * Top level evaluation function, returning a string.
1171 * Return pointer to allocated memory, or NULL for failure.
1172 */
1173 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001174eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 char_u *arg;
1176 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001177 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178{
Bram Moolenaar33570922005-01-25 22:26:29 +00001179 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001181 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 retval = NULL;
1185 else
1186 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001187 if (dolist && tv.v_type == VAR_LIST)
1188 {
1189 ga_init2(&ga, (int)sizeof(char), 80);
1190 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1191 ga_append(&ga, NUL);
1192 retval = (char_u *)ga.ga_data;
1193 }
1194 else
1195 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001196 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198
1199 return retval;
1200}
1201
1202/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001203 * Call eval_to_string() without using current local variables and using
1204 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 */
1206 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001207eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 char_u *arg;
1209 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001210 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
1212 char_u *retval;
1213 void *save_funccalp;
1214
1215 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001216 if (use_sandbox)
1217 ++sandbox;
1218 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001219 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001220 if (use_sandbox)
1221 --sandbox;
1222 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 restore_funccal(save_funccalp);
1224 return retval;
1225}
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227/*
1228 * Top level evaluation function, returning a number.
1229 * Evaluates "expr" silently.
1230 * Returns -1 for an error.
1231 */
1232 int
1233eval_to_number(expr)
1234 char_u *expr;
1235{
Bram Moolenaar33570922005-01-25 22:26:29 +00001236 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001238 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239
1240 ++emsg_off;
1241
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001242 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 retval = -1;
1244 else
1245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001246 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001247 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249 --emsg_off;
1250
1251 return retval;
1252}
1253
Bram Moolenaara40058a2005-07-11 22:42:07 +00001254/*
1255 * Prepare v: variable "idx" to be used.
1256 * Save the current typeval in "save_tv".
1257 * When not used yet add the variable to the v: hashtable.
1258 */
1259 static void
1260prepare_vimvar(idx, save_tv)
1261 int idx;
1262 typval_T *save_tv;
1263{
1264 *save_tv = vimvars[idx].vv_tv;
1265 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1266 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1267}
1268
1269/*
1270 * Restore v: variable "idx" to typeval "save_tv".
1271 * When no longer defined, remove the variable from the v: hashtable.
1272 */
1273 static void
1274restore_vimvar(idx, save_tv)
1275 int idx;
1276 typval_T *save_tv;
1277{
1278 hashitem_T *hi;
1279
1280 clear_tv(&vimvars[idx].vv_tv);
1281 vimvars[idx].vv_tv = *save_tv;
1282 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1283 {
1284 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1285 if (HASHITEM_EMPTY(hi))
1286 EMSG2(_(e_intern2), "restore_vimvar()");
1287 else
1288 hash_remove(&vimvarht, hi);
1289 }
1290}
1291
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001292#if defined(FEAT_SYN_HL) || defined(PROTO)
1293/*
1294 * Evaluate an expression to a list with suggestions.
1295 * For the "expr:" part of 'spellsuggest'.
1296 */
1297 list_T *
1298eval_spell_expr(badword, expr)
1299 char_u *badword;
1300 char_u *expr;
1301{
1302 typval_T save_val;
1303 typval_T rettv;
1304 list_T *list = NULL;
1305 char_u *p = skipwhite(expr);
1306
1307 /* Set "v:val" to the bad word. */
1308 prepare_vimvar(VV_VAL, &save_val);
1309 vimvars[VV_VAL].vv_type = VAR_STRING;
1310 vimvars[VV_VAL].vv_str = badword;
1311 if (p_verbose == 0)
1312 ++emsg_off;
1313
1314 if (eval1(&p, &rettv, TRUE) == OK)
1315 {
1316 if (rettv.v_type != VAR_LIST)
1317 clear_tv(&rettv);
1318 else
1319 list = rettv.vval.v_list;
1320 }
1321
1322 if (p_verbose == 0)
1323 --emsg_off;
1324 vimvars[VV_VAL].vv_str = NULL;
1325 restore_vimvar(VV_VAL, &save_val);
1326
1327 return list;
1328}
1329
1330/*
1331 * "list" is supposed to contain two items: a word and a number. Return the
1332 * word in "pp" and the number as the return value.
1333 * Return -1 if anything isn't right.
1334 * Used to get the good word and score from the eval_spell_expr() result.
1335 */
1336 int
1337get_spellword(list, pp)
1338 list_T *list;
1339 char_u **pp;
1340{
1341 listitem_T *li;
1342
1343 li = list->lv_first;
1344 if (li == NULL)
1345 return -1;
1346 *pp = get_tv_string(&li->li_tv);
1347
1348 li = li->li_next;
1349 if (li == NULL)
1350 return -1;
1351 return get_tv_number(&li->li_tv);
1352}
1353#endif
1354
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001355/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001356 * Top level evaluation function.
1357 * Returns an allocated typval_T with the result.
1358 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001359 */
1360 typval_T *
1361eval_expr(arg, nextcmd)
1362 char_u *arg;
1363 char_u **nextcmd;
1364{
1365 typval_T *tv;
1366
1367 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001368 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001369 {
1370 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001371 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001372 }
1373
1374 return tv;
1375}
1376
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1379/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001380 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001382 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001384 static int
1385call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 char_u *func;
1387 int argc;
1388 char_u **argv;
1389 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
Bram Moolenaar33570922005-01-25 22:26:29 +00001392 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 long n;
1394 int len;
1395 int i;
1396 int doesrange;
1397 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001398 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar33570922005-01-25 22:26:29 +00001400 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404 for (i = 0; i < argc; i++)
1405 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001406 /* Pass a NULL or empty argument as an empty string */
1407 if (argv[i] == NULL || *argv[i] == NUL)
1408 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001409 argvars[i].v_type = VAR_STRING;
1410 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001411 continue;
1412 }
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /* Recognize a number argument, the others must be strings. */
1415 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1416 if (len != 0 && len == (int)STRLEN(argv[i]))
1417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001418 argvars[i].v_type = VAR_NUMBER;
1419 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421 else
1422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001423 argvars[i].v_type = VAR_STRING;
1424 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 }
1427
1428 if (safe)
1429 {
1430 save_funccalp = save_funccal();
1431 ++sandbox;
1432 }
1433
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1435 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001437 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (safe)
1439 {
1440 --sandbox;
1441 restore_funccal(save_funccalp);
1442 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001443 vim_free(argvars);
1444
1445 if (ret == FAIL)
1446 clear_tv(rettv);
1447
1448 return ret;
1449}
1450
1451/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001452 * Call vimL function "func" and return the result as a string.
1453 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001454 * Uses argv[argc] for the function arguments.
1455 */
1456 void *
1457call_func_retstr(func, argc, argv, safe)
1458 char_u *func;
1459 int argc;
1460 char_u **argv;
1461 int safe; /* use the sandbox */
1462{
1463 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001464 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001465
1466 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1467 return NULL;
1468
1469 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001470 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return retval;
1472}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001473
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001474#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001475/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001476 * Call vimL function "func" and return the result as a number.
1477 * Returns -1 when calling the function fails.
1478 * Uses argv[argc] for the function arguments.
1479 */
1480 long
1481call_func_retnr(func, argc, argv, safe)
1482 char_u *func;
1483 int argc;
1484 char_u **argv;
1485 int safe; /* use the sandbox */
1486{
1487 typval_T rettv;
1488 long retval;
1489
1490 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1491 return -1;
1492
1493 retval = get_tv_number_chk(&rettv, NULL);
1494 clear_tv(&rettv);
1495 return retval;
1496}
1497#endif
1498
1499/*
1500 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001501 * Uses argv[argc] for the function arguments.
1502 */
1503 void *
1504call_func_retlist(func, argc, argv, safe)
1505 char_u *func;
1506 int argc;
1507 char_u **argv;
1508 int safe; /* use the sandbox */
1509{
1510 typval_T rettv;
1511
1512 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1513 return NULL;
1514
1515 if (rettv.v_type != VAR_LIST)
1516 {
1517 clear_tv(&rettv);
1518 return NULL;
1519 }
1520
1521 return rettv.vval.v_list;
1522}
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#endif
1525
1526/*
1527 * Save the current function call pointer, and set it to NULL.
1528 * Used when executing autocommands and for ":source".
1529 */
1530 void *
1531save_funccal()
1532{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001533 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 current_funccal = NULL;
1536 return (void *)fc;
1537}
1538
1539 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001540restore_funccal(vfc)
1541 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001543 funccall_T *fc = (funccall_T *)vfc;
1544
1545 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546}
1547
Bram Moolenaar05159a02005-02-26 23:04:13 +00001548#if defined(FEAT_PROFILE) || defined(PROTO)
1549/*
1550 * Prepare profiling for entering a child or something else that is not
1551 * counted for the script/function itself.
1552 * Should always be called in pair with prof_child_exit().
1553 */
1554 void
1555prof_child_enter(tm)
1556 proftime_T *tm; /* place to store waittime */
1557{
1558 funccall_T *fc = current_funccal;
1559
1560 if (fc != NULL && fc->func->uf_profiling)
1561 profile_start(&fc->prof_child);
1562 script_prof_save(tm);
1563}
1564
1565/*
1566 * Take care of time spent in a child.
1567 * Should always be called after prof_child_enter().
1568 */
1569 void
1570prof_child_exit(tm)
1571 proftime_T *tm; /* where waittime was stored */
1572{
1573 funccall_T *fc = current_funccal;
1574
1575 if (fc != NULL && fc->func->uf_profiling)
1576 {
1577 profile_end(&fc->prof_child);
1578 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1579 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1580 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1581 }
1582 script_prof_restore(tm);
1583}
1584#endif
1585
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#ifdef FEAT_FOLDING
1588/*
1589 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1590 * it in "*cp". Doesn't give error messages.
1591 */
1592 int
1593eval_foldexpr(arg, cp)
1594 char_u *arg;
1595 int *cp;
1596{
Bram Moolenaar33570922005-01-25 22:26:29 +00001597 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 int retval;
1599 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001600 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1601 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001604 if (use_sandbox)
1605 ++sandbox;
1606 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 retval = 0;
1610 else
1611 {
1612 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 if (tv.v_type == VAR_NUMBER)
1614 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001615 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 retval = 0;
1617 else
1618 {
1619 /* If the result is a string, check if there is a non-digit before
1620 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (!VIM_ISDIGIT(*s) && *s != '-')
1623 *cp = *s++;
1624 retval = atol((char *)s);
1625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001626 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 }
1628 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001629 if (use_sandbox)
1630 --sandbox;
1631 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 return retval;
1634}
1635#endif
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001638 * ":let" list all variable values
1639 * ":let var1 var2" list variable values
1640 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001641 * ":let var += expr" assignment command.
1642 * ":let var -= expr" assignment command.
1643 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001644 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 */
1646 void
1647ex_let(eap)
1648 exarg_T *eap;
1649{
1650 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001652 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 int var_count = 0;
1655 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001656 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 expr = skip_var_list(arg, &var_count, &semicolon);
1659 if (expr == NULL)
1660 return;
1661 expr = vim_strchr(expr, '=');
1662 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001664 /*
1665 * ":let" without "=": list variables
1666 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 if (*arg == '[')
1668 EMSG(_(e_invarg));
1669 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001670 /* ":let var1 var2" */
1671 arg = list_arg_vars(eap, arg);
1672 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001674 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001675 list_glob_vars();
1676 list_buf_vars();
1677 list_win_vars();
1678 list_vim_vars();
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 eap->nextcmd = check_nextcmd(arg);
1681 }
1682 else
1683 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 op[0] = '=';
1685 op[1] = NUL;
1686 if (expr > arg)
1687 {
1688 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1689 op[0] = expr[-1]; /* +=, -= or .= */
1690 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (eap->skip)
1694 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (eap->skip)
1697 {
1698 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 --emsg_skip;
1701 }
1702 else if (i != FAIL)
1703 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001705 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001706 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708 }
1709}
1710
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711/*
1712 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1713 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001714 * When "nextchars" is not NULL it points to a string with characters that
1715 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1716 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 * Returns OK or FAIL;
1718 */
1719 static int
1720ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1721 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001722 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 int copy; /* copy values from "tv", don't move */
1724 int semicolon; /* from skip_var_list() */
1725 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727{
1728 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001729 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001731 listitem_T *item;
1732 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001733
1734 if (*arg != '[')
1735 {
1736 /*
1737 * ":let var = expr" or ":for var in list"
1738 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 return FAIL;
1741 return OK;
1742 }
1743
1744 /*
1745 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1746 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001747 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 {
1749 EMSG(_(e_listreq));
1750 return FAIL;
1751 }
1752
1753 i = list_len(l);
1754 if (semicolon == 0 && var_count < i)
1755 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001756 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 return FAIL;
1758 }
1759 if (var_count - semicolon > i)
1760 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001761 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762 return FAIL;
1763 }
1764
1765 item = l->lv_first;
1766 while (*arg != ']')
1767 {
1768 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001769 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 item = item->li_next;
1771 if (arg == NULL)
1772 return FAIL;
1773
1774 arg = skipwhite(arg);
1775 if (*arg == ';')
1776 {
1777 /* Put the rest of the list (may be empty) in the var after ';'.
1778 * Create a new list for this. */
1779 l = list_alloc();
1780 if (l == NULL)
1781 return FAIL;
1782 while (item != NULL)
1783 {
1784 list_append_tv(l, &item->li_tv);
1785 item = item->li_next;
1786 }
1787
1788 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001789 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 ltv.vval.v_list = l;
1791 l->lv_refcount = 1;
1792
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1794 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 clear_tv(&ltv);
1796 if (arg == NULL)
1797 return FAIL;
1798 break;
1799 }
1800 else if (*arg != ',' && *arg != ']')
1801 {
1802 EMSG2(_(e_intern2), "ex_let_vars()");
1803 return FAIL;
1804 }
1805 }
1806
1807 return OK;
1808}
1809
1810/*
1811 * Skip over assignable variable "var" or list of variables "[var, var]".
1812 * Used for ":let varvar = expr" and ":for varvar in expr".
1813 * For "[var, var]" increment "*var_count" for each variable.
1814 * for "[var, var; var]" set "semicolon".
1815 * Return NULL for an error.
1816 */
1817 static char_u *
1818skip_var_list(arg, var_count, semicolon)
1819 char_u *arg;
1820 int *var_count;
1821 int *semicolon;
1822{
1823 char_u *p, *s;
1824
1825 if (*arg == '[')
1826 {
1827 /* "[var, var]": find the matching ']'. */
1828 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001829 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 {
1831 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1832 s = skip_var_one(p);
1833 if (s == p)
1834 {
1835 EMSG2(_(e_invarg2), p);
1836 return NULL;
1837 }
1838 ++*var_count;
1839
1840 p = skipwhite(s);
1841 if (*p == ']')
1842 break;
1843 else if (*p == ';')
1844 {
1845 if (*semicolon == 1)
1846 {
1847 EMSG(_("Double ; in list of variables"));
1848 return NULL;
1849 }
1850 *semicolon = 1;
1851 }
1852 else if (*p != ',')
1853 {
1854 EMSG2(_(e_invarg2), p);
1855 return NULL;
1856 }
1857 }
1858 return p + 1;
1859 }
1860 else
1861 return skip_var_one(arg);
1862}
1863
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001864/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001865 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1866 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001867 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 static char_u *
1869skip_var_one(arg)
1870 char_u *arg;
1871{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001872 if (*arg == '@' && arg[1] != NUL)
1873 return arg + 2;
1874 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1875 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876}
1877
Bram Moolenaara7043832005-01-21 11:56:39 +00001878/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001879 * List variables for hashtab "ht" with prefix "prefix".
1880 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001883list_hashtable_vars(ht, prefix, empty)
1884 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001885 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001886 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001887{
Bram Moolenaar33570922005-01-25 22:26:29 +00001888 hashitem_T *hi;
1889 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001890 int todo;
1891
1892 todo = ht->ht_used;
1893 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1894 {
1895 if (!HASHITEM_EMPTY(hi))
1896 {
1897 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 di = HI2DI(hi);
1899 if (empty || di->di_tv.v_type != VAR_STRING
1900 || di->di_tv.vval.v_string != NULL)
1901 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001902 }
1903 }
1904}
1905
1906/*
1907 * List global variables.
1908 */
1909 static void
1910list_glob_vars()
1911{
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001913}
1914
1915/*
1916 * List buffer variables.
1917 */
1918 static void
1919list_buf_vars()
1920{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 char_u numbuf[NUMBUFLEN];
1922
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001924
1925 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1926 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001927}
1928
1929/*
1930 * List window variables.
1931 */
1932 static void
1933list_win_vars()
1934{
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001936}
1937
1938/*
1939 * List Vim variables.
1940 */
1941 static void
1942list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943{
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945}
1946
1947/*
1948 * List variables in "arg".
1949 */
1950 static char_u *
1951list_arg_vars(eap, arg)
1952 exarg_T *eap;
1953 char_u *arg;
1954{
1955 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001956 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958 char_u *name_start;
1959 char_u *arg_subsc;
1960 char_u *tofree;
1961 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962
1963 while (!ends_excmd(*arg) && !got_int)
1964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001967 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1969 {
1970 emsg_severe = TRUE;
1971 EMSG(_(e_trailing));
1972 break;
1973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001977 /* get_name_len() takes care of expanding curly braces */
1978 name_start = name = arg;
1979 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1980 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001982 /* This is mainly to keep test 49 working: when expanding
1983 * curly braces fails overrule the exception error message. */
1984 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001986 emsg_severe = TRUE;
1987 EMSG2(_(e_invarg2), arg);
1988 break;
1989 }
1990 error = TRUE;
1991 }
1992 else
1993 {
1994 if (tofree != NULL)
1995 name = tofree;
1996 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 else
1999 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002000 /* handle d.key, l[idx], f(expr) */
2001 arg_subsc = arg;
2002 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002003 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002004 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002005 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002006 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002007 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002008 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002009 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002010 case 'g': list_glob_vars(); break;
2011 case 'b': list_buf_vars(); break;
2012 case 'w': list_win_vars(); break;
2013 case 'v': list_vim_vars(); break;
2014 default:
2015 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002016 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002017 }
2018 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002019 {
2020 char_u numbuf[NUMBUFLEN];
2021 char_u *tf;
2022 int c;
2023 char_u *s;
2024
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002025 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002026 c = *arg;
2027 *arg = NUL;
2028 list_one_var_a((char_u *)"",
2029 arg == arg_subsc ? name : name_start,
2030 tv.v_type, s == NULL ? (char_u *)"" : s);
2031 *arg = c;
2032 vim_free(tf);
2033 }
2034 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 }
2037 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002038
2039 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002041
2042 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 }
2044
2045 return arg;
2046}
2047
2048/*
2049 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2050 * Returns a pointer to the char just after the var name.
2051 * Returns NULL if there is an error.
2052 */
2053 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002054ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002055 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002056 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002057 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002059 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060{
2061 int c1;
2062 char_u *name;
2063 char_u *p;
2064 char_u *arg_end = NULL;
2065 int len;
2066 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002067 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068
2069 /*
2070 * ":let $VAR = expr": Set environment variable.
2071 */
2072 if (*arg == '$')
2073 {
2074 /* Find the end of the name. */
2075 ++arg;
2076 name = arg;
2077 len = get_env_len(&arg);
2078 if (len == 0)
2079 EMSG2(_(e_invarg2), name - 1);
2080 else
2081 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002082 if (op != NULL && (*op == '+' || *op == '-'))
2083 EMSG2(_(e_letwrong), op);
2084 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002086 EMSG(_(e_letunexp));
2087 else
2088 {
2089 c1 = name[len];
2090 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002091 p = get_tv_string_chk(tv);
2092 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002093 {
2094 int mustfree = FALSE;
2095 char_u *s = vim_getenv(name, &mustfree);
2096
2097 if (s != NULL)
2098 {
2099 p = tofree = concat_str(s, p);
2100 if (mustfree)
2101 vim_free(s);
2102 }
2103 }
2104 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002105 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002106 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002107 if (STRICMP(name, "HOME") == 0)
2108 init_homedir();
2109 else if (didset_vim && STRICMP(name, "VIM") == 0)
2110 didset_vim = FALSE;
2111 else if (didset_vimruntime
2112 && STRICMP(name, "VIMRUNTIME") == 0)
2113 didset_vimruntime = FALSE;
2114 arg_end = arg;
2115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002116 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002117 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 }
2119 }
2120 }
2121
2122 /*
2123 * ":let &option = expr": Set option value.
2124 * ":let &l:option = expr": Set local option value.
2125 * ":let &g:option = expr": Set global option value.
2126 */
2127 else if (*arg == '&')
2128 {
2129 /* Find the end of the name. */
2130 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131 if (p == NULL || (endchars != NULL
2132 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 EMSG(_(e_letunexp));
2134 else
2135 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 long n;
2137 int opt_type;
2138 long numval;
2139 char_u *stringval = NULL;
2140 char_u *s;
2141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142 c1 = *p;
2143 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002144
2145 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002146 s = get_tv_string_chk(tv); /* != NULL if number or string */
2147 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002148 {
2149 opt_type = get_option_value(arg, &numval,
2150 &stringval, opt_flags);
2151 if ((opt_type == 1 && *op == '.')
2152 || (opt_type == 0 && *op != '.'))
2153 EMSG2(_(e_letwrong), op);
2154 else
2155 {
2156 if (opt_type == 1) /* number */
2157 {
2158 if (*op == '+')
2159 n = numval + n;
2160 else
2161 n = numval - n;
2162 }
2163 else if (opt_type == 0 && stringval != NULL) /* string */
2164 {
2165 s = concat_str(stringval, s);
2166 vim_free(stringval);
2167 stringval = s;
2168 }
2169 }
2170 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002171 if (s != NULL)
2172 {
2173 set_option_value(arg, n, s, opt_flags);
2174 arg_end = p;
2175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002177 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 }
2179 }
2180
2181 /*
2182 * ":let @r = expr": Set register contents.
2183 */
2184 else if (*arg == '@')
2185 {
2186 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002187 if (op != NULL && (*op == '+' || *op == '-'))
2188 EMSG2(_(e_letwrong), op);
2189 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002190 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 EMSG(_(e_letunexp));
2192 else
2193 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002194 char_u *tofree = NULL;
2195 char_u *s;
2196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002197 p = get_tv_string_chk(tv);
2198 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002199 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002200 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002201 if (s != NULL)
2202 {
2203 p = tofree = concat_str(s, p);
2204 vim_free(s);
2205 }
2206 }
2207 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002208 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002209 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002210 arg_end = arg + 1;
2211 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002212 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 }
2214 }
2215
2216 /*
2217 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002220 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002222 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002224 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002225 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002227 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2228 EMSG(_(e_letunexp));
2229 else
2230 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002231 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002232 arg_end = p;
2233 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 }
2237
2238 else
2239 EMSG2(_(e_invarg2), arg);
2240
2241 return arg_end;
2242}
2243
2244/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002245 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2246 */
2247 static int
2248check_changedtick(arg)
2249 char_u *arg;
2250{
2251 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2252 {
2253 EMSG2(_(e_readonlyvar), arg);
2254 return TRUE;
2255 }
2256 return FALSE;
2257}
2258
2259/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 * Get an lval: variable, Dict item or List item that can be assigned a value
2261 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2262 * "name.key", "name.key[expr]" etc.
2263 * Indexing only works if "name" is an existing List or Dictionary.
2264 * "name" points to the start of the name.
2265 * If "rettv" is not NULL it points to the value to be assigned.
2266 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2267 * wrong; must end in space or cmd separator.
2268 *
2269 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 */
2273 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002274get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 typval_T *rettv;
2277 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 int unlet;
2279 int skip;
2280 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002281 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 char_u *expr_start, *expr_end;
2285 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002286 dictitem_T *v;
2287 typval_T var1;
2288 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002290 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002291 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002292 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002293 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002296 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002297
2298 if (skip)
2299 {
2300 /* When skipping just find the end of the name. */
2301 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002302 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002303 }
2304
2305 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002306 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 if (expr_start != NULL)
2308 {
2309 /* Don't expand the name when we already know there is an error. */
2310 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2311 && *p != '[' && *p != '.')
2312 {
2313 EMSG(_(e_trailing));
2314 return NULL;
2315 }
2316
2317 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2318 if (lp->ll_exp_name == NULL)
2319 {
2320 /* Report an invalid expression in braces, unless the
2321 * expression evaluation has been cancelled due to an
2322 * aborting error, an interrupt, or an exception. */
2323 if (!aborting() && !quiet)
2324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002325 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 EMSG2(_(e_invarg2), name);
2327 return NULL;
2328 }
2329 }
2330 lp->ll_name = lp->ll_exp_name;
2331 }
2332 else
2333 lp->ll_name = name;
2334
2335 /* Without [idx] or .key we are done. */
2336 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2337 return p;
2338
2339 cc = *p;
2340 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002341 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002342 if (v == NULL && !quiet)
2343 EMSG2(_(e_undefvar), lp->ll_name);
2344 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 if (v == NULL)
2346 return NULL;
2347
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 /*
2349 * Loop until no more [idx] or .key is following.
2350 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002351 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2355 && !(lp->ll_tv->v_type == VAR_DICT
2356 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 if (!quiet)
2359 EMSG(_("E689: Can only index a List or Dictionary"));
2360 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002362 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 if (!quiet)
2365 EMSG(_("E708: [:] must come last"));
2366 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368
Bram Moolenaar8c711452005-01-14 21:53:12 +00002369 len = -1;
2370 if (*p == '.')
2371 {
2372 key = p + 1;
2373 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2374 ;
2375 if (len == 0)
2376 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 if (!quiet)
2378 EMSG(_(e_emptykey));
2379 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 }
2381 p = key + len;
2382 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002383 else
2384 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002385 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002386 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002387 if (*p == ':')
2388 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002389 else
2390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002391 empty1 = FALSE;
2392 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002393 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 if (get_tv_string_chk(&var1) == NULL)
2395 {
2396 /* not a number or string */
2397 clear_tv(&var1);
2398 return NULL;
2399 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 }
2401
2402 /* Optionally get the second index [ :expr]. */
2403 if (*p == ':')
2404 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002409 if (!empty1)
2410 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002411 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002412 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (rettv != NULL && (rettv->v_type != VAR_LIST
2414 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002416 if (!quiet)
2417 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002418 if (!empty1)
2419 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002421 }
2422 p = skipwhite(p + 1);
2423 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002425 else
2426 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2429 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002430 if (!empty1)
2431 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002434 if (get_tv_string_chk(&var2) == NULL)
2435 {
2436 /* not a number or string */
2437 if (!empty1)
2438 clear_tv(&var1);
2439 clear_tv(&var2);
2440 return NULL;
2441 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002444 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002447
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002449 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 if (!quiet)
2451 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 if (!empty1)
2453 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002455 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 }
2458
2459 /* Skip to past ']'. */
2460 ++p;
2461 }
2462
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002464 {
2465 if (len == -1)
2466 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002469 if (*key == NUL)
2470 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 if (!quiet)
2472 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002475 }
2476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 lp->ll_list = NULL;
2478 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002479 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002482 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 if (len == -1)
2488 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 }
2491 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 if (len == -1)
2496 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 p = NULL;
2499 break;
2500 }
2501 if (len == -1)
2502 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002504 }
2505 else
2506 {
2507 /*
2508 * Get the number and item for the only or first index of the List.
2509 */
2510 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002512 else
2513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 clear_tv(&var1);
2516 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 lp->ll_list = lp->ll_tv->vval.v_list;
2519 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2520 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (!quiet)
2523 EMSGN(_(e_listidx), lp->ll_n1);
2524 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002525 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 }
2528
2529 /*
2530 * May need to find the item or absolute index for the second
2531 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 * When no index given: "lp->ll_empty2" is TRUE.
2533 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 if (ni == NULL)
2543 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 if (!quiet)
2545 EMSGN(_(e_listidx), lp->ll_n2);
2546 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 }
2550
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2552 if (lp->ll_n1 < 0)
2553 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2554 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (!quiet)
2557 EMSGN(_(e_listidx), lp->ll_n2);
2558 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002559 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002560 }
2561
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564 }
2565
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 return p;
2567}
2568
2569/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002570 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 */
2572 static void
2573clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002574 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575{
2576 vim_free(lp->ll_exp_name);
2577 vim_free(lp->ll_newkey);
2578}
2579
2580/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002581 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 */
2585 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002586set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592{
2593 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 listitem_T *ri;
2595 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596
2597 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 cc = *endp;
2602 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002603 if (op != NULL && *op != '=')
2604 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002605 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002606
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002607 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002608 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2609 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002610 {
2611 if (tv_op(&tv, rettv, op) == OK)
2612 set_var(lp->ll_name, &tv, FALSE);
2613 clear_tv(&tv);
2614 }
2615 }
2616 else
2617 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002621 else if (tv_check_lock(lp->ll_newkey == NULL
2622 ? lp->ll_tv->v_lock
2623 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2624 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 else if (lp->ll_range)
2626 {
2627 /*
2628 * Assign the List values to the list items.
2629 */
2630 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002632 if (op != NULL && *op != '=')
2633 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2634 else
2635 {
2636 clear_tv(&lp->ll_li->li_tv);
2637 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2638 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 ri = ri->li_next;
2640 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2641 break;
2642 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002645 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 ri = NULL;
2648 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 lp->ll_li = lp->ll_li->li_next;
2652 ++lp->ll_n1;
2653 }
2654 if (ri != NULL)
2655 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 else if (lp->ll_empty2
2657 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 : lp->ll_n1 != lp->ll_n2)
2659 EMSG(_("E711: List value has not enough items"));
2660 }
2661 else
2662 {
2663 /*
2664 * Assign to a List or Dictionary item.
2665 */
2666 if (lp->ll_newkey != NULL)
2667 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 if (op != NULL && *op != '=')
2669 {
2670 EMSG2(_(e_letwrong), op);
2671 return;
2672 }
2673
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002675 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (di == NULL)
2677 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002678 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2679 {
2680 vim_free(di);
2681 return;
2682 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002684 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002685 else if (op != NULL && *op != '=')
2686 {
2687 tv_op(lp->ll_tv, rettv, op);
2688 return;
2689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002690 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 /*
2694 * Assign the value to the variable or list item.
2695 */
2696 if (copy)
2697 copy_tv(rettv, lp->ll_tv);
2698 else
2699 {
2700 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002701 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 }
2704 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705}
2706
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002707/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2709 * Returns OK or FAIL.
2710 */
2711 static int
2712tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002713 typval_T *tv1;
2714 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 char_u *op;
2716{
2717 long n;
2718 char_u numbuf[NUMBUFLEN];
2719 char_u *s;
2720
2721 /* Can't do anything with a Funcref or a Dict on the right. */
2722 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2723 {
2724 switch (tv1->v_type)
2725 {
2726 case VAR_DICT:
2727 case VAR_FUNC:
2728 break;
2729
2730 case VAR_LIST:
2731 if (*op != '+' || tv2->v_type != VAR_LIST)
2732 break;
2733 /* List += List */
2734 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2735 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2736 return OK;
2737
2738 case VAR_NUMBER:
2739 case VAR_STRING:
2740 if (tv2->v_type == VAR_LIST)
2741 break;
2742 if (*op == '+' || *op == '-')
2743 {
2744 /* nr += nr or nr -= nr*/
2745 n = get_tv_number(tv1);
2746 if (*op == '+')
2747 n += get_tv_number(tv2);
2748 else
2749 n -= get_tv_number(tv2);
2750 clear_tv(tv1);
2751 tv1->v_type = VAR_NUMBER;
2752 tv1->vval.v_number = n;
2753 }
2754 else
2755 {
2756 /* str .= str */
2757 s = get_tv_string(tv1);
2758 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2759 clear_tv(tv1);
2760 tv1->v_type = VAR_STRING;
2761 tv1->vval.v_string = s;
2762 }
2763 return OK;
2764 }
2765 }
2766
2767 EMSG2(_(e_letwrong), op);
2768 return FAIL;
2769}
2770
2771/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002772 * Add a watcher to a list.
2773 */
2774 static void
2775list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002776 list_T *l;
2777 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002778{
2779 lw->lw_next = l->lv_watch;
2780 l->lv_watch = lw;
2781}
2782
2783/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002784 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002785 * No warning when it isn't found...
2786 */
2787 static void
2788list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002789 list_T *l;
2790 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002791{
Bram Moolenaar33570922005-01-25 22:26:29 +00002792 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002793
2794 lwp = &l->lv_watch;
2795 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2796 {
2797 if (lw == lwrem)
2798 {
2799 *lwp = lw->lw_next;
2800 break;
2801 }
2802 lwp = &lw->lw_next;
2803 }
2804}
2805
2806/*
2807 * Just before removing an item from a list: advance watchers to the next
2808 * item.
2809 */
2810 static void
2811list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002812 list_T *l;
2813 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002814{
Bram Moolenaar33570922005-01-25 22:26:29 +00002815 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002816
2817 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2818 if (lw->lw_item == item)
2819 lw->lw_item = item->li_next;
2820}
2821
2822/*
2823 * Evaluate the expression used in a ":for var in expr" command.
2824 * "arg" points to "var".
2825 * Set "*errp" to TRUE for an error, FALSE otherwise;
2826 * Return a pointer that holds the info. Null when there is an error.
2827 */
2828 void *
2829eval_for_line(arg, errp, nextcmdp, skip)
2830 char_u *arg;
2831 int *errp;
2832 char_u **nextcmdp;
2833 int skip;
2834{
Bram Moolenaar33570922005-01-25 22:26:29 +00002835 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002836 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002837 typval_T tv;
2838 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839
2840 *errp = TRUE; /* default: there is an error */
2841
Bram Moolenaar33570922005-01-25 22:26:29 +00002842 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002843 if (fi == NULL)
2844 return NULL;
2845
2846 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2847 if (expr == NULL)
2848 return fi;
2849
2850 expr = skipwhite(expr);
2851 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2852 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002853 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002854 return fi;
2855 }
2856
2857 if (skip)
2858 ++emsg_skip;
2859 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2860 {
2861 *errp = FALSE;
2862 if (!skip)
2863 {
2864 l = tv.vval.v_list;
2865 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002866 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002867 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002868 clear_tv(&tv);
2869 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002870 else
2871 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002872 /* No need to increment the refcount, it's already set for the
2873 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002874 fi->fi_list = l;
2875 list_add_watch(l, &fi->fi_lw);
2876 fi->fi_lw.lw_item = l->lv_first;
2877 }
2878 }
2879 }
2880 if (skip)
2881 --emsg_skip;
2882
2883 return fi;
2884}
2885
2886/*
2887 * Use the first item in a ":for" list. Advance to the next.
2888 * Assign the values to the variable (list). "arg" points to the first one.
2889 * Return TRUE when a valid item was found, FALSE when at end of list or
2890 * something wrong.
2891 */
2892 int
2893next_for_item(fi_void, arg)
2894 void *fi_void;
2895 char_u *arg;
2896{
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002898 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900
2901 item = fi->fi_lw.lw_item;
2902 if (item == NULL)
2903 result = FALSE;
2904 else
2905 {
2906 fi->fi_lw.lw_item = item->li_next;
2907 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2908 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2909 }
2910 return result;
2911}
2912
2913/*
2914 * Free the structure used to store info used by ":for".
2915 */
2916 void
2917free_for_info(fi_void)
2918 void *fi_void;
2919{
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002921
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002922 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002923 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002924 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002925 list_unref(fi->fi_list);
2926 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002927 vim_free(fi);
2928}
2929
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2931
2932 void
2933set_context_for_expression(xp, arg, cmdidx)
2934 expand_T *xp;
2935 char_u *arg;
2936 cmdidx_T cmdidx;
2937{
2938 int got_eq = FALSE;
2939 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002940 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942 if (cmdidx == CMD_let)
2943 {
2944 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002945 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946 {
2947 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002948 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002949 {
2950 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002952 if (vim_iswhite(*p))
2953 break;
2954 }
2955 return;
2956 }
2957 }
2958 else
2959 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2960 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 while ((xp->xp_pattern = vim_strpbrk(arg,
2962 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2963 {
2964 c = *xp->xp_pattern;
2965 if (c == '&')
2966 {
2967 c = xp->xp_pattern[1];
2968 if (c == '&')
2969 {
2970 ++xp->xp_pattern;
2971 xp->xp_context = cmdidx != CMD_let || got_eq
2972 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2973 }
2974 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002977 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2978 xp->xp_pattern += 2;
2979
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 else if (c == '$')
2983 {
2984 /* environment variable */
2985 xp->xp_context = EXPAND_ENV_VARS;
2986 }
2987 else if (c == '=')
2988 {
2989 got_eq = TRUE;
2990 xp->xp_context = EXPAND_EXPRESSION;
2991 }
2992 else if (c == '<'
2993 && xp->xp_context == EXPAND_FUNCTIONS
2994 && vim_strchr(xp->xp_pattern, '(') == NULL)
2995 {
2996 /* Function name can start with "<SNR>" */
2997 break;
2998 }
2999 else if (cmdidx != CMD_let || got_eq)
3000 {
3001 if (c == '"') /* string */
3002 {
3003 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3004 if (c == '\\' && xp->xp_pattern[1] != NUL)
3005 ++xp->xp_pattern;
3006 xp->xp_context = EXPAND_NOTHING;
3007 }
3008 else if (c == '\'') /* literal string */
3009 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003010 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3012 /* skip */ ;
3013 xp->xp_context = EXPAND_NOTHING;
3014 }
3015 else if (c == '|')
3016 {
3017 if (xp->xp_pattern[1] == '|')
3018 {
3019 ++xp->xp_pattern;
3020 xp->xp_context = EXPAND_EXPRESSION;
3021 }
3022 else
3023 xp->xp_context = EXPAND_COMMANDS;
3024 }
3025 else
3026 xp->xp_context = EXPAND_EXPRESSION;
3027 }
3028 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003029 /* Doesn't look like something valid, expand as an expression
3030 * anyway. */
3031 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 arg = xp->xp_pattern;
3033 if (*arg != NUL)
3034 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3035 /* skip */ ;
3036 }
3037 xp->xp_pattern = arg;
3038}
3039
3040#endif /* FEAT_CMDL_COMPL */
3041
3042/*
3043 * ":1,25call func(arg1, arg2)" function call.
3044 */
3045 void
3046ex_call(eap)
3047 exarg_T *eap;
3048{
3049 char_u *arg = eap->arg;
3050 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003054 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 linenr_T lnum;
3056 int doesrange;
3057 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003058 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003060 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3061 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 if (tofree == NULL)
3063 return;
3064
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003065 /* Increase refcount on dictionary, it could get deleted when evaluating
3066 * the arguments. */
3067 if (fudi.fd_dict != NULL)
3068 ++fudi.fd_dict->dv_refcount;
3069
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3071 len = STRLEN(tofree);
3072 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073
Bram Moolenaar532c7802005-01-27 14:44:31 +00003074 /* Skip white space to allow ":call func ()". Not good, but required for
3075 * backward compatibility. */
3076 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 if (*startarg != '(')
3080 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003081 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 goto end;
3083 }
3084
3085 /*
3086 * When skipping, evaluate the function once, to find the end of the
3087 * arguments.
3088 * When the function takes a range, this is discovered after the first
3089 * call, and the loop is broken.
3090 */
3091 if (eap->skip)
3092 {
3093 ++emsg_skip;
3094 lnum = eap->line2; /* do it once, also with an invalid range */
3095 }
3096 else
3097 lnum = eap->line1;
3098 for ( ; lnum <= eap->line2; ++lnum)
3099 {
3100 if (!eap->skip && eap->addr_count > 0)
3101 {
3102 curwin->w_cursor.lnum = lnum;
3103 curwin->w_cursor.col = 0;
3104 }
3105 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003107 eap->line1, eap->line2, &doesrange,
3108 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 failed = TRUE;
3111 break;
3112 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (doesrange || eap->skip)
3115 break;
3116 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003117 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003118 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003119 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if (aborting())
3121 break;
3122 }
3123 if (eap->skip)
3124 --emsg_skip;
3125
3126 if (!failed)
3127 {
3128 /* Check for trailing illegal characters and a following command. */
3129 if (!ends_excmd(*arg))
3130 {
3131 emsg_severe = TRUE;
3132 EMSG(_(e_trailing));
3133 }
3134 else
3135 eap->nextcmd = check_nextcmd(arg);
3136 }
3137
3138end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003139 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141}
3142
3143/*
3144 * ":unlet[!] var1 ... " command.
3145 */
3146 void
3147ex_unlet(eap)
3148 exarg_T *eap;
3149{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003150 ex_unletlock(eap, eap->arg, 0);
3151}
3152
3153/*
3154 * ":lockvar" and ":unlockvar" commands
3155 */
3156 void
3157ex_lockvar(eap)
3158 exarg_T *eap;
3159{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003161 int deep = 2;
3162
3163 if (eap->forceit)
3164 deep = -1;
3165 else if (vim_isdigit(*arg))
3166 {
3167 deep = getdigits(&arg);
3168 arg = skipwhite(arg);
3169 }
3170
3171 ex_unletlock(eap, arg, deep);
3172}
3173
3174/*
3175 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3176 */
3177 static void
3178ex_unletlock(eap, argstart, deep)
3179 exarg_T *eap;
3180 char_u *argstart;
3181 int deep;
3182{
3183 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003186 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 do
3189 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003190 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003191 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3192 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003193 if (lv.ll_name == NULL)
3194 error = TRUE; /* error but continue parsing */
3195 if (name_end == NULL || (!vim_iswhite(*name_end)
3196 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003198 if (name_end != NULL)
3199 {
3200 emsg_severe = TRUE;
3201 EMSG(_(e_trailing));
3202 }
3203 if (!(eap->skip || error))
3204 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 break;
3206 }
3207
3208 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003209 {
3210 if (eap->cmdidx == CMD_unlet)
3211 {
3212 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3213 error = TRUE;
3214 }
3215 else
3216 {
3217 if (do_lock_var(&lv, name_end, deep,
3218 eap->cmdidx == CMD_lockvar) == FAIL)
3219 error = TRUE;
3220 }
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223 if (!eap->skip)
3224 clear_lval(&lv);
3225
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 arg = skipwhite(name_end);
3227 } while (!ends_excmd(*arg));
3228
3229 eap->nextcmd = check_nextcmd(arg);
3230}
3231
Bram Moolenaar8c711452005-01-14 21:53:12 +00003232 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003233do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003236 int forceit;
3237{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 int ret = OK;
3239 int cc;
3240
3241 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003242 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003243 cc = *name_end;
3244 *name_end = NUL;
3245
3246 /* Normal name or expanded name. */
3247 if (check_changedtick(lp->ll_name))
3248 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003249 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003250 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003251 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003252 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3254 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003255 else if (lp->ll_range)
3256 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003257 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003258
3259 /* Delete a range of List items. */
3260 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3261 {
3262 li = lp->ll_li->li_next;
3263 listitem_remove(lp->ll_list, lp->ll_li);
3264 lp->ll_li = li;
3265 ++lp->ll_n1;
3266 }
3267 }
3268 else
3269 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003270 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003271 /* unlet a List item. */
3272 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003273 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003274 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003275 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003276 }
3277
3278 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003279}
3280
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281/*
3282 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003283 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 */
3285 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003286do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003288 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
Bram Moolenaar33570922005-01-25 22:26:29 +00003290 hashtab_T *ht;
3291 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003292 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaar33570922005-01-25 22:26:29 +00003294 ht = find_var_ht(name, &varname);
3295 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 hi = hash_find(ht, varname);
3298 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003299 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003300 if (var_check_ro(HI2DI(hi)->di_flags, name))
3301 return FAIL;
3302 delete_var(ht, hi);
3303 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003306 if (forceit)
3307 return OK;
3308 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 return FAIL;
3310}
3311
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003312/*
3313 * Lock or unlock variable indicated by "lp".
3314 * "deep" is the levels to go (-1 for unlimited);
3315 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3316 */
3317 static int
3318do_lock_var(lp, name_end, deep, lock)
3319 lval_T *lp;
3320 char_u *name_end;
3321 int deep;
3322 int lock;
3323{
3324 int ret = OK;
3325 int cc;
3326 dictitem_T *di;
3327
3328 if (deep == 0) /* nothing to do */
3329 return OK;
3330
3331 if (lp->ll_tv == NULL)
3332 {
3333 cc = *name_end;
3334 *name_end = NUL;
3335
3336 /* Normal name or expanded name. */
3337 if (check_changedtick(lp->ll_name))
3338 ret = FAIL;
3339 else
3340 {
3341 di = find_var(lp->ll_name, NULL);
3342 if (di == NULL)
3343 ret = FAIL;
3344 else
3345 {
3346 if (lock)
3347 di->di_flags |= DI_FLAGS_LOCK;
3348 else
3349 di->di_flags &= ~DI_FLAGS_LOCK;
3350 item_lock(&di->di_tv, deep, lock);
3351 }
3352 }
3353 *name_end = cc;
3354 }
3355 else if (lp->ll_range)
3356 {
3357 listitem_T *li = lp->ll_li;
3358
3359 /* (un)lock a range of List items. */
3360 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3361 {
3362 item_lock(&li->li_tv, deep, lock);
3363 li = li->li_next;
3364 ++lp->ll_n1;
3365 }
3366 }
3367 else if (lp->ll_list != NULL)
3368 /* (un)lock a List item. */
3369 item_lock(&lp->ll_li->li_tv, deep, lock);
3370 else
3371 /* un(lock) a Dictionary item. */
3372 item_lock(&lp->ll_di->di_tv, deep, lock);
3373
3374 return ret;
3375}
3376
3377/*
3378 * Lock or unlock an item. "deep" is nr of levels to go.
3379 */
3380 static void
3381item_lock(tv, deep, lock)
3382 typval_T *tv;
3383 int deep;
3384 int lock;
3385{
3386 static int recurse = 0;
3387 list_T *l;
3388 listitem_T *li;
3389 dict_T *d;
3390 hashitem_T *hi;
3391 int todo;
3392
3393 if (recurse >= DICT_MAXNEST)
3394 {
3395 EMSG(_("E743: variable nested too deep for (un)lock"));
3396 return;
3397 }
3398 if (deep == 0)
3399 return;
3400 ++recurse;
3401
3402 /* lock/unlock the item itself */
3403 if (lock)
3404 tv->v_lock |= VAR_LOCKED;
3405 else
3406 tv->v_lock &= ~VAR_LOCKED;
3407
3408 switch (tv->v_type)
3409 {
3410 case VAR_LIST:
3411 if ((l = tv->vval.v_list) != NULL)
3412 {
3413 if (lock)
3414 l->lv_lock |= VAR_LOCKED;
3415 else
3416 l->lv_lock &= ~VAR_LOCKED;
3417 if (deep < 0 || deep > 1)
3418 /* recursive: lock/unlock the items the List contains */
3419 for (li = l->lv_first; li != NULL; li = li->li_next)
3420 item_lock(&li->li_tv, deep - 1, lock);
3421 }
3422 break;
3423 case VAR_DICT:
3424 if ((d = tv->vval.v_dict) != NULL)
3425 {
3426 if (lock)
3427 d->dv_lock |= VAR_LOCKED;
3428 else
3429 d->dv_lock &= ~VAR_LOCKED;
3430 if (deep < 0 || deep > 1)
3431 {
3432 /* recursive: lock/unlock the items the List contains */
3433 todo = d->dv_hashtab.ht_used;
3434 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3435 {
3436 if (!HASHITEM_EMPTY(hi))
3437 {
3438 --todo;
3439 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3440 }
3441 }
3442 }
3443 }
3444 }
3445 --recurse;
3446}
3447
Bram Moolenaara40058a2005-07-11 22:42:07 +00003448/*
3449 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3450 * it refers to a List or Dictionary that is locked.
3451 */
3452 static int
3453tv_islocked(tv)
3454 typval_T *tv;
3455{
3456 return (tv->v_lock & VAR_LOCKED)
3457 || (tv->v_type == VAR_LIST
3458 && tv->vval.v_list != NULL
3459 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3460 || (tv->v_type == VAR_DICT
3461 && tv->vval.v_dict != NULL
3462 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3463}
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3466/*
3467 * Delete all "menutrans_" variables.
3468 */
3469 void
3470del_menutrans_vars()
3471{
Bram Moolenaar33570922005-01-25 22:26:29 +00003472 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003473 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaar33570922005-01-25 22:26:29 +00003475 hash_lock(&globvarht);
3476 todo = globvarht.ht_used;
3477 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003478 {
3479 if (!HASHITEM_EMPTY(hi))
3480 {
3481 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003482 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3483 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003484 }
3485 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487}
3488#endif
3489
3490#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3491
3492/*
3493 * Local string buffer for the next two functions to store a variable name
3494 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3495 * get_user_var_name().
3496 */
3497
3498static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3499
3500static char_u *varnamebuf = NULL;
3501static int varnamebuflen = 0;
3502
3503/*
3504 * Function to concatenate a prefix and a variable name.
3505 */
3506 static char_u *
3507cat_prefix_varname(prefix, name)
3508 int prefix;
3509 char_u *name;
3510{
3511 int len;
3512
3513 len = (int)STRLEN(name) + 3;
3514 if (len > varnamebuflen)
3515 {
3516 vim_free(varnamebuf);
3517 len += 10; /* some additional space */
3518 varnamebuf = alloc(len);
3519 if (varnamebuf == NULL)
3520 {
3521 varnamebuflen = 0;
3522 return NULL;
3523 }
3524 varnamebuflen = len;
3525 }
3526 *varnamebuf = prefix;
3527 varnamebuf[1] = ':';
3528 STRCPY(varnamebuf + 2, name);
3529 return varnamebuf;
3530}
3531
3532/*
3533 * Function given to ExpandGeneric() to obtain the list of user defined
3534 * (global/buffer/window/built-in) variable names.
3535 */
3536/*ARGSUSED*/
3537 char_u *
3538get_user_var_name(xp, idx)
3539 expand_T *xp;
3540 int idx;
3541{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003542 static long_u gdone;
3543 static long_u bdone;
3544 static long_u wdone;
3545 static int vidx;
3546 static hashitem_T *hi;
3547 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003550 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003551
3552 /* Global variables */
3553 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003555 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003556 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003557 else
3558 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003559 while (HASHITEM_EMPTY(hi))
3560 ++hi;
3561 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3562 return cat_prefix_varname('g', hi->hi_key);
3563 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003565
3566 /* b: variables */
3567 ht = &curbuf->b_vars.dv_hashtab;
3568 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003570 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003572 else
3573 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003574 while (HASHITEM_EMPTY(hi))
3575 ++hi;
3576 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003578 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003580 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return (char_u *)"b:changedtick";
3582 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003583
3584 /* w: variables */
3585 ht = &curwin->w_vars.dv_hashtab;
3586 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003588 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003589 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003590 else
3591 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003592 while (HASHITEM_EMPTY(hi))
3593 ++hi;
3594 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003596
3597 /* v: variables */
3598 if (vidx < VV_LEN)
3599 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 vim_free(varnamebuf);
3602 varnamebuf = NULL;
3603 varnamebuflen = 0;
3604 return NULL;
3605}
3606
3607#endif /* FEAT_CMDL_COMPL */
3608
3609/*
3610 * types for expressions.
3611 */
3612typedef enum
3613{
3614 TYPE_UNKNOWN = 0
3615 , TYPE_EQUAL /* == */
3616 , TYPE_NEQUAL /* != */
3617 , TYPE_GREATER /* > */
3618 , TYPE_GEQUAL /* >= */
3619 , TYPE_SMALLER /* < */
3620 , TYPE_SEQUAL /* <= */
3621 , TYPE_MATCH /* =~ */
3622 , TYPE_NOMATCH /* !~ */
3623} exptype_T;
3624
3625/*
3626 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003627 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3629 */
3630
3631/*
3632 * Handle zero level expression.
3633 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003634 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003635 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 * Return OK or FAIL.
3637 */
3638 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003639eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 char_u **nextcmd;
3643 int evaluate;
3644{
3645 int ret;
3646 char_u *p;
3647
3648 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003649 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (ret == FAIL || !ends_excmd(*p))
3651 {
3652 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003653 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 /*
3655 * Report the invalid expression unless the expression evaluation has
3656 * been cancelled due to an aborting error, an interrupt, or an
3657 * exception.
3658 */
3659 if (!aborting())
3660 EMSG2(_(e_invexpr2), arg);
3661 ret = FAIL;
3662 }
3663 if (nextcmd != NULL)
3664 *nextcmd = check_nextcmd(p);
3665
3666 return ret;
3667}
3668
3669/*
3670 * Handle top level expression:
3671 * expr1 ? expr0 : expr0
3672 *
3673 * "arg" must point to the first non-white of the expression.
3674 * "arg" is advanced to the next non-white after the recognized expression.
3675 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003676 * Note: "rettv.v_lock" is not set.
3677 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 * Return OK or FAIL.
3679 */
3680 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003681eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 int evaluate;
3685{
3686 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003687 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
3689 /*
3690 * Get the first variable.
3691 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003692 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return FAIL;
3694
3695 if ((*arg)[0] == '?')
3696 {
3697 result = FALSE;
3698 if (evaluate)
3699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003700 int error = FALSE;
3701
3702 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003704 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003705 if (error)
3706 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708
3709 /*
3710 * Get the second variable.
3711 */
3712 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return FAIL;
3715
3716 /*
3717 * Check for the ":".
3718 */
3719 if ((*arg)[0] != ':')
3720 {
3721 EMSG(_("E109: Missing ':' after '?'"));
3722 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003723 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 return FAIL;
3725 }
3726
3727 /*
3728 * Get the third variable.
3729 */
3730 *arg = skipwhite(*arg + 1);
3731 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3732 {
3733 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 return FAIL;
3736 }
3737 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003738 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
3740
3741 return OK;
3742}
3743
3744/*
3745 * Handle first level expression:
3746 * expr2 || expr2 || expr2 logical OR
3747 *
3748 * "arg" must point to the first non-white of the expression.
3749 * "arg" is advanced to the next non-white after the recognized expression.
3750 *
3751 * Return OK or FAIL.
3752 */
3753 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003754eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 int evaluate;
3758{
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 long result;
3761 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 /*
3765 * Get the first variable.
3766 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003767 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 return FAIL;
3769
3770 /*
3771 * Repeat until there is no following "||".
3772 */
3773 first = TRUE;
3774 result = FALSE;
3775 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3776 {
3777 if (evaluate && first)
3778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 if (error)
3783 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 first = FALSE;
3785 }
3786
3787 /*
3788 * Get the second variable.
3789 */
3790 *arg = skipwhite(*arg + 2);
3791 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3792 return FAIL;
3793
3794 /*
3795 * Compute the result.
3796 */
3797 if (evaluate && !result)
3798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003799 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003801 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003802 if (error)
3803 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 if (evaluate)
3806 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003807 rettv->v_type = VAR_NUMBER;
3808 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 }
3811
3812 return OK;
3813}
3814
3815/*
3816 * Handle second level expression:
3817 * expr3 && expr3 && expr3 logical AND
3818 *
3819 * "arg" must point to the first non-white of the expression.
3820 * "arg" is advanced to the next non-white after the recognized expression.
3821 *
3822 * Return OK or FAIL.
3823 */
3824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003825eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 int evaluate;
3829{
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 long result;
3832 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003833 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 /*
3836 * Get the first variable.
3837 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003838 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return FAIL;
3840
3841 /*
3842 * Repeat until there is no following "&&".
3843 */
3844 first = TRUE;
3845 result = TRUE;
3846 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3847 {
3848 if (evaluate && first)
3849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003852 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003853 if (error)
3854 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 first = FALSE;
3856 }
3857
3858 /*
3859 * Get the second variable.
3860 */
3861 *arg = skipwhite(*arg + 2);
3862 if (eval4(arg, &var2, evaluate && result) == FAIL)
3863 return FAIL;
3864
3865 /*
3866 * Compute the result.
3867 */
3868 if (evaluate && result)
3869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003870 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003872 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003873 if (error)
3874 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 if (evaluate)
3877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003878 rettv->v_type = VAR_NUMBER;
3879 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 }
3882
3883 return OK;
3884}
3885
3886/*
3887 * Handle third level expression:
3888 * var1 == var2
3889 * var1 =~ var2
3890 * var1 != var2
3891 * var1 !~ var2
3892 * var1 > var2
3893 * var1 >= var2
3894 * var1 < var2
3895 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003896 * var1 is var2
3897 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 *
3899 * "arg" must point to the first non-white of the expression.
3900 * "arg" is advanced to the next non-white after the recognized expression.
3901 *
3902 * Return OK or FAIL.
3903 */
3904 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003905eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 int evaluate;
3909{
Bram Moolenaar33570922005-01-25 22:26:29 +00003910 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 char_u *p;
3912 int i;
3913 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003914 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 int len = 2;
3916 long n1, n2;
3917 char_u *s1, *s2;
3918 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3919 regmatch_T regmatch;
3920 int ic;
3921 char_u *save_cpo;
3922
3923 /*
3924 * Get the first variable.
3925 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 return FAIL;
3928
3929 p = *arg;
3930 switch (p[0])
3931 {
3932 case '=': if (p[1] == '=')
3933 type = TYPE_EQUAL;
3934 else if (p[1] == '~')
3935 type = TYPE_MATCH;
3936 break;
3937 case '!': if (p[1] == '=')
3938 type = TYPE_NEQUAL;
3939 else if (p[1] == '~')
3940 type = TYPE_NOMATCH;
3941 break;
3942 case '>': if (p[1] != '=')
3943 {
3944 type = TYPE_GREATER;
3945 len = 1;
3946 }
3947 else
3948 type = TYPE_GEQUAL;
3949 break;
3950 case '<': if (p[1] != '=')
3951 {
3952 type = TYPE_SMALLER;
3953 len = 1;
3954 }
3955 else
3956 type = TYPE_SEQUAL;
3957 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003958 case 'i': if (p[1] == 's')
3959 {
3960 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3961 len = 5;
3962 if (!vim_isIDc(p[len]))
3963 {
3964 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3965 type_is = TRUE;
3966 }
3967 }
3968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970
3971 /*
3972 * If there is a comparitive operator, use it.
3973 */
3974 if (type != TYPE_UNKNOWN)
3975 {
3976 /* extra question mark appended: ignore case */
3977 if (p[len] == '?')
3978 {
3979 ic = TRUE;
3980 ++len;
3981 }
3982 /* extra '#' appended: match case */
3983 else if (p[len] == '#')
3984 {
3985 ic = FALSE;
3986 ++len;
3987 }
3988 /* nothing appened: use 'ignorecase' */
3989 else
3990 ic = p_ic;
3991
3992 /*
3993 * Get the second variable.
3994 */
3995 *arg = skipwhite(p + len);
3996 if (eval5(arg, &var2, evaluate) == FAIL)
3997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003998 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return FAIL;
4000 }
4001
4002 if (evaluate)
4003 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 if (type_is && rettv->v_type != var2.v_type)
4005 {
4006 /* For "is" a different type always means FALSE, for "notis"
4007 * it means TRUE. */
4008 n1 = (type == TYPE_NEQUAL);
4009 }
4010 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4011 {
4012 if (type_is)
4013 {
4014 n1 = (rettv->v_type == var2.v_type
4015 && rettv->vval.v_list == var2.vval.v_list);
4016 if (type == TYPE_NEQUAL)
4017 n1 = !n1;
4018 }
4019 else if (rettv->v_type != var2.v_type
4020 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4021 {
4022 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004023 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004024 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004025 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004026 clear_tv(rettv);
4027 clear_tv(&var2);
4028 return FAIL;
4029 }
4030 else
4031 {
4032 /* Compare two Lists for being equal or unequal. */
4033 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4034 if (type == TYPE_NEQUAL)
4035 n1 = !n1;
4036 }
4037 }
4038
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004039 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4040 {
4041 if (type_is)
4042 {
4043 n1 = (rettv->v_type == var2.v_type
4044 && rettv->vval.v_dict == var2.vval.v_dict);
4045 if (type == TYPE_NEQUAL)
4046 n1 = !n1;
4047 }
4048 else if (rettv->v_type != var2.v_type
4049 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4050 {
4051 if (rettv->v_type != var2.v_type)
4052 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4053 else
4054 EMSG(_("E736: Invalid operation for Dictionary"));
4055 clear_tv(rettv);
4056 clear_tv(&var2);
4057 return FAIL;
4058 }
4059 else
4060 {
4061 /* Compare two Dictionaries for being equal or unequal. */
4062 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4063 if (type == TYPE_NEQUAL)
4064 n1 = !n1;
4065 }
4066 }
4067
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004068 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4069 {
4070 if (rettv->v_type != var2.v_type
4071 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4072 {
4073 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004074 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004075 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004076 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004077 clear_tv(rettv);
4078 clear_tv(&var2);
4079 return FAIL;
4080 }
4081 else
4082 {
4083 /* Compare two Funcrefs for being equal or unequal. */
4084 if (rettv->vval.v_string == NULL
4085 || var2.vval.v_string == NULL)
4086 n1 = FALSE;
4087 else
4088 n1 = STRCMP(rettv->vval.v_string,
4089 var2.vval.v_string) == 0;
4090 if (type == TYPE_NEQUAL)
4091 n1 = !n1;
4092 }
4093 }
4094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 /*
4096 * If one of the two variables is a number, compare as a number.
4097 * When using "=~" or "!~", always compare as string.
4098 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004099 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 n1 = get_tv_number(rettv);
4103 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 switch (type)
4105 {
4106 case TYPE_EQUAL: n1 = (n1 == n2); break;
4107 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4108 case TYPE_GREATER: n1 = (n1 > n2); break;
4109 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4110 case TYPE_SMALLER: n1 = (n1 < n2); break;
4111 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4112 case TYPE_UNKNOWN:
4113 case TYPE_MATCH:
4114 case TYPE_NOMATCH: break; /* avoid gcc warning */
4115 }
4116 }
4117 else
4118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 s1 = get_tv_string_buf(rettv, buf1);
4120 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4122 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4123 else
4124 i = 0;
4125 n1 = FALSE;
4126 switch (type)
4127 {
4128 case TYPE_EQUAL: n1 = (i == 0); break;
4129 case TYPE_NEQUAL: n1 = (i != 0); break;
4130 case TYPE_GREATER: n1 = (i > 0); break;
4131 case TYPE_GEQUAL: n1 = (i >= 0); break;
4132 case TYPE_SMALLER: n1 = (i < 0); break;
4133 case TYPE_SEQUAL: n1 = (i <= 0); break;
4134
4135 case TYPE_MATCH:
4136 case TYPE_NOMATCH:
4137 /* avoid 'l' flag in 'cpoptions' */
4138 save_cpo = p_cpo;
4139 p_cpo = (char_u *)"";
4140 regmatch.regprog = vim_regcomp(s2,
4141 RE_MAGIC + RE_STRING);
4142 regmatch.rm_ic = ic;
4143 if (regmatch.regprog != NULL)
4144 {
4145 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4146 vim_free(regmatch.regprog);
4147 if (type == TYPE_NOMATCH)
4148 n1 = !n1;
4149 }
4150 p_cpo = save_cpo;
4151 break;
4152
4153 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4154 }
4155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 clear_tv(rettv);
4157 clear_tv(&var2);
4158 rettv->v_type = VAR_NUMBER;
4159 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161 }
4162
4163 return OK;
4164}
4165
4166/*
4167 * Handle fourth level expression:
4168 * + number addition
4169 * - number subtraction
4170 * . string concatenation
4171 *
4172 * "arg" must point to the first non-white of the expression.
4173 * "arg" is advanced to the next non-white after the recognized expression.
4174 *
4175 * Return OK or FAIL.
4176 */
4177 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 int evaluate;
4182{
Bram Moolenaar33570922005-01-25 22:26:29 +00004183 typval_T var2;
4184 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 int op;
4186 long n1, n2;
4187 char_u *s1, *s2;
4188 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4189 char_u *p;
4190
4191 /*
4192 * Get the first variable.
4193 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 return FAIL;
4196
4197 /*
4198 * Repeat computing, until no '+', '-' or '.' is following.
4199 */
4200 for (;;)
4201 {
4202 op = **arg;
4203 if (op != '+' && op != '-' && op != '.')
4204 break;
4205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 if (op != '+' || rettv->v_type != VAR_LIST)
4207 {
4208 /* For "list + ...", an illegal use of the first operand as
4209 * a number cannot be determined before evaluating the 2nd
4210 * operand: if this is also a list, all is ok.
4211 * For "something . ...", "something - ..." or "non-list + ...",
4212 * we know that the first operand needs to be a string or number
4213 * without evaluating the 2nd operand. So check before to avoid
4214 * side effects after an error. */
4215 if (evaluate && get_tv_string_chk(rettv) == NULL)
4216 {
4217 clear_tv(rettv);
4218 return FAIL;
4219 }
4220 }
4221
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 /*
4223 * Get the second variable.
4224 */
4225 *arg = skipwhite(*arg + 1);
4226 if (eval6(arg, &var2, evaluate) == FAIL)
4227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return FAIL;
4230 }
4231
4232 if (evaluate)
4233 {
4234 /*
4235 * Compute the result.
4236 */
4237 if (op == '.')
4238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4240 s2 = get_tv_string_buf_chk(&var2, buf2);
4241 if (s2 == NULL) /* type error ? */
4242 {
4243 clear_tv(rettv);
4244 clear_tv(&var2);
4245 return FAIL;
4246 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004247 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 clear_tv(rettv);
4249 rettv->v_type = VAR_STRING;
4250 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004252 else if (op == '+' && rettv->v_type == VAR_LIST
4253 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004254 {
4255 /* concatenate Lists */
4256 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4257 &var3) == FAIL)
4258 {
4259 clear_tv(rettv);
4260 clear_tv(&var2);
4261 return FAIL;
4262 }
4263 clear_tv(rettv);
4264 *rettv = var3;
4265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 else
4267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 int error = FALSE;
4269
4270 n1 = get_tv_number_chk(rettv, &error);
4271 if (error)
4272 {
4273 /* This can only happen for "list + non-list".
4274 * For "non-list + ..." or "something - ...", we returned
4275 * before evaluating the 2nd operand. */
4276 clear_tv(rettv);
4277 return FAIL;
4278 }
4279 n2 = get_tv_number_chk(&var2, &error);
4280 if (error)
4281 {
4282 clear_tv(rettv);
4283 clear_tv(&var2);
4284 return FAIL;
4285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (op == '+')
4288 n1 = n1 + n2;
4289 else
4290 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 rettv->v_type = VAR_NUMBER;
4292 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 }
4297 return OK;
4298}
4299
4300/*
4301 * Handle fifth level expression:
4302 * * number multiplication
4303 * / number division
4304 * % number modulo
4305 *
4306 * "arg" must point to the first non-white of the expression.
4307 * "arg" is advanced to the next non-white after the recognized expression.
4308 *
4309 * Return OK or FAIL.
4310 */
4311 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int evaluate;
4316{
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 int op;
4319 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 /*
4329 * Repeat computing, until no '*', '/' or '%' is following.
4330 */
4331 for (;;)
4332 {
4333 op = **arg;
4334 if (op != '*' && op != '/' && op != '%')
4335 break;
4336
4337 if (evaluate)
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 else
4345 n1 = 0;
4346
4347 /*
4348 * Get the second variable.
4349 */
4350 *arg = skipwhite(*arg + 1);
4351 if (eval7(arg, &var2, evaluate) == FAIL)
4352 return FAIL;
4353
4354 if (evaluate)
4355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (error)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 /*
4362 * Compute the result.
4363 */
4364 if (op == '*')
4365 n1 = n1 * n2;
4366 else if (op == '/')
4367 {
4368 if (n2 == 0) /* give an error message? */
4369 n1 = 0x7fffffffL;
4370 else
4371 n1 = n1 / n2;
4372 }
4373 else
4374 {
4375 if (n2 == 0) /* give an error message? */
4376 n1 = 0;
4377 else
4378 n1 = n1 % n2;
4379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 rettv->v_type = VAR_NUMBER;
4381 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 }
4384
4385 return OK;
4386}
4387
4388/*
4389 * Handle sixth level expression:
4390 * number number constant
4391 * "string" string contstant
4392 * 'string' literal string contstant
4393 * &option-name option value
4394 * @r register contents
4395 * identifier variable value
4396 * function() function call
4397 * $VAR environment variable
4398 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004399 * [expr, expr] List
4400 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 *
4402 * Also handle:
4403 * ! in front logical NOT
4404 * - in front unary minus
4405 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004406 * trailing [] subscript in String or List
4407 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 *
4409 * "arg" must point to the first non-white of the expression.
4410 * "arg" is advanced to the next non-white after the recognized expression.
4411 *
4412 * Return OK or FAIL.
4413 */
4414 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004415eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 int evaluate;
4419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 long n;
4421 int len;
4422 char_u *s;
4423 int val;
4424 char_u *start_leader, *end_leader;
4425 int ret = OK;
4426 char_u *alias;
4427
4428 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 /*
4435 * Skip '!' and '-' characters. They are handled later.
4436 */
4437 start_leader = *arg;
4438 while (**arg == '!' || **arg == '-' || **arg == '+')
4439 *arg = skipwhite(*arg + 1);
4440 end_leader = *arg;
4441
4442 switch (**arg)
4443 {
4444 /*
4445 * Number constant.
4446 */
4447 case '0':
4448 case '1':
4449 case '2':
4450 case '3':
4451 case '4':
4452 case '5':
4453 case '6':
4454 case '7':
4455 case '8':
4456 case '9':
4457 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4458 *arg += len;
4459 if (evaluate)
4460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 rettv->v_type = VAR_NUMBER;
4462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 break;
4465
4466 /*
4467 * String constant: "string".
4468 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 break;
4471
4472 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004476 break;
4477
4478 /*
4479 * List: [expr, expr]
4480 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 break;
4483
4484 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004485 * Dictionary: {key: val, key: val}
4486 */
4487 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4488 break;
4489
4490 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004491 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004493 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
4495
4496 /*
4497 * Environment variable: $VAR.
4498 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004499 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 break;
4501
4502 /*
4503 * Register contents: @r.
4504 */
4505 case '@': ++*arg;
4506 if (evaluate)
4507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004508 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004509 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 if (**arg != NUL)
4512 ++*arg;
4513 break;
4514
4515 /*
4516 * nested expression: (expression).
4517 */
4518 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (**arg == ')')
4521 ++*arg;
4522 else if (ret == OK)
4523 {
4524 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 ret = FAIL;
4527 }
4528 break;
4529
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 break;
4532 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004533
4534 if (ret == NOTDONE)
4535 {
4536 /*
4537 * Must be a variable or function name.
4538 * Can also be a curly-braces kind of name: {expr}.
4539 */
4540 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004541 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004542 if (alias != NULL)
4543 s = alias;
4544
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004545 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004546 ret = FAIL;
4547 else
4548 {
4549 if (**arg == '(') /* recursive! */
4550 {
4551 /* If "s" is the name of a variable of type VAR_FUNC
4552 * use its contents. */
4553 s = deref_func_name(s, &len);
4554
4555 /* Invoke the function. */
4556 ret = get_func_tv(s, len, rettv, arg,
4557 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004558 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 /* Stop the expression evaluation when immediately
4560 * aborting on error, or when an interrupt occurred or
4561 * an exception was thrown but not caught. */
4562 if (aborting())
4563 {
4564 if (ret == OK)
4565 clear_tv(rettv);
4566 ret = FAIL;
4567 }
4568 }
4569 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004570 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004571 else
4572 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004573 }
4574
4575 if (alias != NULL)
4576 vim_free(alias);
4577 }
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 *arg = skipwhite(*arg);
4580
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004581 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4582 * expr(expr). */
4583 if (ret == OK)
4584 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 /*
4587 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4588 */
4589 if (ret == OK && evaluate && end_leader > start_leader)
4590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004591 int error = FALSE;
4592
4593 val = get_tv_number_chk(rettv, &error);
4594 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004596 clear_tv(rettv);
4597 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004599 else
4600 {
4601 while (end_leader > start_leader)
4602 {
4603 --end_leader;
4604 if (*end_leader == '!')
4605 val = !val;
4606 else if (*end_leader == '-')
4607 val = -val;
4608 }
4609 clear_tv(rettv);
4610 rettv->v_type = VAR_NUMBER;
4611 rettv->vval.v_number = val;
4612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614
4615 return ret;
4616}
4617
4618/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 * Evaluate an "[expr]" or "[expr:expr]" index.
4620 * "*arg" points to the '['.
4621 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4622 */
4623 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004624eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004626 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004627 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004628 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629{
4630 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004631 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004632 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633 long len = -1;
4634 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004635 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004638 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004640 if (verbose)
4641 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004642 return FAIL;
4643 }
4644
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004647 /*
4648 * dict.name
4649 */
4650 key = *arg + 1;
4651 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4652 ;
4653 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004655 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004656 }
4657 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 /*
4660 * something[idx]
4661 *
4662 * Get the (first) variable from inside the [].
4663 */
4664 *arg = skipwhite(*arg + 1);
4665 if (**arg == ':')
4666 empty1 = TRUE;
4667 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4668 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4670 {
4671 /* not a number or string */
4672 clear_tv(&var1);
4673 return FAIL;
4674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004675
4676 /*
4677 * Get the second variable from inside the [:].
4678 */
4679 if (**arg == ':')
4680 {
4681 range = TRUE;
4682 *arg = skipwhite(*arg + 1);
4683 if (**arg == ']')
4684 empty2 = TRUE;
4685 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687 if (!empty1)
4688 clear_tv(&var1);
4689 return FAIL;
4690 }
4691 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4692 {
4693 /* not a number or string */
4694 if (!empty1)
4695 clear_tv(&var1);
4696 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697 return FAIL;
4698 }
4699 }
4700
4701 /* Check for the ']'. */
4702 if (**arg != ']')
4703 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004704 if (verbose)
4705 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004706 clear_tv(&var1);
4707 if (range)
4708 clear_tv(&var2);
4709 return FAIL;
4710 }
4711 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 }
4713
4714 if (evaluate)
4715 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 n1 = 0;
4717 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 n1 = get_tv_number(&var1);
4720 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 }
4722 if (range)
4723 {
4724 if (empty2)
4725 n2 = -1;
4726 else
4727 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 n2 = get_tv_number(&var2);
4729 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004730 }
4731 }
4732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 {
4735 case VAR_NUMBER:
4736 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004737 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 len = (long)STRLEN(s);
4739 if (range)
4740 {
4741 /* The resulting variable is a substring. If the indexes
4742 * are out of range the result is empty. */
4743 if (n1 < 0)
4744 {
4745 n1 = len + n1;
4746 if (n1 < 0)
4747 n1 = 0;
4748 }
4749 if (n2 < 0)
4750 n2 = len + n2;
4751 else if (n2 >= len)
4752 n2 = len;
4753 if (n1 >= len || n2 < 0 || n1 > n2)
4754 s = NULL;
4755 else
4756 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4757 }
4758 else
4759 {
4760 /* The resulting variable is a string of a single
4761 * character. If the index is too big or negative the
4762 * result is empty. */
4763 if (n1 >= len || n1 < 0)
4764 s = NULL;
4765 else
4766 s = vim_strnsave(s + n1, 1);
4767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 clear_tv(rettv);
4769 rettv->v_type = VAR_STRING;
4770 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 break;
4772
4773 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 if (n1 < 0)
4776 n1 = len + n1;
4777 if (!empty1 && (n1 < 0 || n1 >= len))
4778 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004779 if (verbose)
4780 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 return FAIL;
4782 }
4783 if (range)
4784 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004785 list_T *l;
4786 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787
4788 if (n2 < 0)
4789 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004792 if (verbose)
4793 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004794 return FAIL;
4795 }
4796 l = list_alloc();
4797 if (l == NULL)
4798 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004799 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 n1 <= n2; ++n1)
4801 {
4802 if (list_append_tv(l, &item->li_tv) == FAIL)
4803 {
4804 list_free(l);
4805 return FAIL;
4806 }
4807 item = item->li_next;
4808 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004809 clear_tv(rettv);
4810 rettv->v_type = VAR_LIST;
4811 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004812 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 }
4814 else
4815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004818 clear_tv(rettv);
4819 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820 }
4821 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004822
4823 case VAR_DICT:
4824 if (range)
4825 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004826 if (verbose)
4827 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828 if (len == -1)
4829 clear_tv(&var1);
4830 return FAIL;
4831 }
4832 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004833 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004834
4835 if (len == -1)
4836 {
4837 key = get_tv_string(&var1);
4838 if (*key == NUL)
4839 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004840 if (verbose)
4841 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004842 clear_tv(&var1);
4843 return FAIL;
4844 }
4845 }
4846
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004847 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004848
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004849 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004850 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851 if (len == -1)
4852 clear_tv(&var1);
4853 if (item == NULL)
4854 return FAIL;
4855
4856 copy_tv(&item->di_tv, &var1);
4857 clear_tv(rettv);
4858 *rettv = var1;
4859 }
4860 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 }
4862 }
4863
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 return OK;
4865}
4866
4867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 * Get an option value.
4869 * "arg" points to the '&' or '+' before the option name.
4870 * "arg" is advanced to character after the option name.
4871 * Return OK or FAIL.
4872 */
4873 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004876 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int evaluate;
4878{
4879 char_u *option_end;
4880 long numval;
4881 char_u *stringval;
4882 int opt_type;
4883 int c;
4884 int working = (**arg == '+'); /* has("+option") */
4885 int ret = OK;
4886 int opt_flags;
4887
4888 /*
4889 * Isolate the option name and find its value.
4890 */
4891 option_end = find_option_end(arg, &opt_flags);
4892 if (option_end == NULL)
4893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004894 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 EMSG2(_("E112: Option name missing: %s"), *arg);
4896 return FAIL;
4897 }
4898
4899 if (!evaluate)
4900 {
4901 *arg = option_end;
4902 return OK;
4903 }
4904
4905 c = *option_end;
4906 *option_end = NUL;
4907 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 if (opt_type == -3) /* invalid name */
4911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 EMSG2(_("E113: Unknown option: %s"), *arg);
4914 ret = FAIL;
4915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 if (opt_type == -2) /* hidden string option */
4919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 rettv->v_type = VAR_STRING;
4921 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 else if (opt_type == -1) /* hidden number option */
4924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 rettv->v_type = VAR_NUMBER;
4926 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else if (opt_type == 1) /* number option */
4929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 rettv->v_type = VAR_NUMBER;
4931 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 else /* string option */
4934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004935 rettv->v_type = VAR_STRING;
4936 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 }
4939 else if (working && (opt_type == -2 || opt_type == -1))
4940 ret = FAIL;
4941
4942 *option_end = c; /* put back for error messages */
4943 *arg = option_end;
4944
4945 return ret;
4946}
4947
4948/*
4949 * Allocate a variable for a string constant.
4950 * Return OK or FAIL.
4951 */
4952 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004953get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 int evaluate;
4957{
4958 char_u *p;
4959 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 int extra = 0;
4961
4962 /*
4963 * Find the end of the string, skipping backslashed characters.
4964 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004965 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 if (*p == '\\' && p[1] != NUL)
4968 {
4969 ++p;
4970 /* A "\<x>" form occupies at least 4 characters, and produces up
4971 * to 6 characters: reserve space for 2 extra */
4972 if (*p == '<')
4973 extra += 2;
4974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976
4977 if (*p != '"')
4978 {
4979 EMSG2(_("E114: Missing quote: %s"), *arg);
4980 return FAIL;
4981 }
4982
4983 /* If only parsing, set *arg and return here */
4984 if (!evaluate)
4985 {
4986 *arg = p + 1;
4987 return OK;
4988 }
4989
4990 /*
4991 * Copy the string into allocated memory, handling backslashed
4992 * characters.
4993 */
4994 name = alloc((unsigned)(p - *arg + extra));
4995 if (name == NULL)
4996 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 rettv->v_type = VAR_STRING;
4998 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 if (*p == '\\')
5003 {
5004 switch (*++p)
5005 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 case 'b': *name++ = BS; ++p; break;
5007 case 'e': *name++ = ESC; ++p; break;
5008 case 'f': *name++ = FF; ++p; break;
5009 case 'n': *name++ = NL; ++p; break;
5010 case 'r': *name++ = CAR; ++p; break;
5011 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 case 'X': /* hex: "\x1", "\x12" */
5014 case 'x':
5015 case 'u': /* Unicode: "\u0023" */
5016 case 'U':
5017 if (vim_isxdigit(p[1]))
5018 {
5019 int n, nr;
5020 int c = toupper(*p);
5021
5022 if (c == 'X')
5023 n = 2;
5024 else
5025 n = 4;
5026 nr = 0;
5027 while (--n >= 0 && vim_isxdigit(p[1]))
5028 {
5029 ++p;
5030 nr = (nr << 4) + hex2nr(*p);
5031 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033#ifdef FEAT_MBYTE
5034 /* For "\u" store the number according to
5035 * 'encoding'. */
5036 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 else
5039#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 break;
5043
5044 /* octal: "\1", "\12", "\123" */
5045 case '0':
5046 case '1':
5047 case '2':
5048 case '3':
5049 case '4':
5050 case '5':
5051 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 case '7': *name = *p++ - '0';
5053 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005055 *name = (*name << 3) + *p++ - '0';
5056 if (*p >= '0' && *p <= '7')
5057 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061
5062 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (extra != 0)
5065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068 }
5069 /* FALLTHROUGH */
5070
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073 }
5074 }
5075 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 *arg = p + 1;
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 return OK;
5083}
5084
5085/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 * Return OK or FAIL.
5088 */
5089 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 int evaluate;
5094{
5095 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 int reduce = 0;
5098
5099 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 */
5102 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5103 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 break;
5108 ++reduce;
5109 ++p;
5110 }
5111 }
5112
Bram Moolenaar8c711452005-01-14 21:53:12 +00005113 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116 return FAIL;
5117 }
5118
Bram Moolenaar8c711452005-01-14 21:53:12 +00005119 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005120 if (!evaluate)
5121 {
5122 *arg = p + 1;
5123 return OK;
5124 }
5125
5126 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128 */
5129 str = alloc((unsigned)((p - *arg) - reduce));
5130 if (str == NULL)
5131 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 rettv->v_type = VAR_STRING;
5133 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005134
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005138 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005140 break;
5141 ++p;
5142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005144 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005146 *arg = p + 1;
5147
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005148 return OK;
5149}
5150
5151/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005152 * Allocate a variable for a List and fill it from "*arg".
5153 * Return OK or FAIL.
5154 */
5155 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005158 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159 int evaluate;
5160{
Bram Moolenaar33570922005-01-25 22:26:29 +00005161 list_T *l = NULL;
5162 typval_T tv;
5163 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164
5165 if (evaluate)
5166 {
5167 l = list_alloc();
5168 if (l == NULL)
5169 return FAIL;
5170 }
5171
5172 *arg = skipwhite(*arg + 1);
5173 while (**arg != ']' && **arg != NUL)
5174 {
5175 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5176 goto failret;
5177 if (evaluate)
5178 {
5179 item = listitem_alloc();
5180 if (item != NULL)
5181 {
5182 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005183 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 list_append(l, item);
5185 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005186 else
5187 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 }
5189
5190 if (**arg == ']')
5191 break;
5192 if (**arg != ',')
5193 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 goto failret;
5196 }
5197 *arg = skipwhite(*arg + 1);
5198 }
5199
5200 if (**arg != ']')
5201 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203failret:
5204 if (evaluate)
5205 list_free(l);
5206 return FAIL;
5207 }
5208
5209 *arg = skipwhite(*arg + 1);
5210 if (evaluate)
5211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 rettv->v_type = VAR_LIST;
5213 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214 ++l->lv_refcount;
5215 }
5216
5217 return OK;
5218}
5219
5220/*
5221 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005222 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005224 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225list_alloc()
5226{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005227 list_T *l;
5228
5229 l = (list_T *)alloc_clear(sizeof(list_T));
5230 if (l != NULL)
5231 {
5232 /* Prepend the list to the list of lists for garbage collection. */
5233 if (first_list != NULL)
5234 first_list->lv_used_prev = l;
5235 l->lv_used_prev = NULL;
5236 l->lv_used_next = first_list;
5237 first_list = l;
5238 }
5239 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240}
5241
5242/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005243 * Allocate an empty list for a return value.
5244 * Returns OK or FAIL.
5245 */
5246 static int
5247rettv_list_alloc(rettv)
5248 typval_T *rettv;
5249{
5250 list_T *l = list_alloc();
5251
5252 if (l == NULL)
5253 return FAIL;
5254
5255 rettv->vval.v_list = l;
5256 rettv->v_type = VAR_LIST;
5257 ++l->lv_refcount;
5258 return OK;
5259}
5260
5261/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 * Unreference a list: decrement the reference count and free it when it
5263 * becomes zero.
5264 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005265 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005269 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5270 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271}
5272
5273/*
5274 * Free a list, including all items it points to.
5275 * Ignores the reference count.
5276 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005277 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280{
Bram Moolenaar33570922005-01-25 22:26:29 +00005281 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282
Bram Moolenaard9fba312005-06-26 22:34:35 +00005283 /* Avoid that recursive reference to the list frees us again. */
5284 l->lv_refcount = DEL_REFCOUNT;
5285
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005286 /* Remove the list from the list of lists for garbage collection. */
5287 if (l->lv_used_prev == NULL)
5288 first_list = l->lv_used_next;
5289 else
5290 l->lv_used_prev->lv_used_next = l->lv_used_next;
5291 if (l->lv_used_next != NULL)
5292 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5293
Bram Moolenaard9fba312005-06-26 22:34:35 +00005294 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005296 /* Remove the item before deleting it. */
5297 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 listitem_free(item);
5299 }
5300 vim_free(l);
5301}
5302
5303/*
5304 * Allocate a list item.
5305 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005306 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307listitem_alloc()
5308{
Bram Moolenaar33570922005-01-25 22:26:29 +00005309 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310}
5311
5312/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005313 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 */
5315 static void
5316listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005319 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 vim_free(item);
5321}
5322
5323/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005324 * Remove a list item from a List and free it. Also clears the value.
5325 */
5326 static void
5327listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 list_T *l;
5329 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005330{
5331 list_remove(l, item, item);
5332 listitem_free(item);
5333}
5334
5335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 * Get the number of items in a list.
5337 */
5338 static long
5339list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005342 if (l == NULL)
5343 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005344 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345}
5346
5347/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348 * Return TRUE when two lists have exactly the same values.
5349 */
5350 static int
5351list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 list_T *l1;
5353 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005354 int ic; /* ignore case for strings */
5355{
Bram Moolenaar33570922005-01-25 22:26:29 +00005356 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005357
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005358 if (list_len(l1) != list_len(l2))
5359 return FALSE;
5360
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005361 for (item1 = l1->lv_first, item2 = l2->lv_first;
5362 item1 != NULL && item2 != NULL;
5363 item1 = item1->li_next, item2 = item2->li_next)
5364 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5365 return FALSE;
5366 return item1 == NULL && item2 == NULL;
5367}
5368
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005369#if defined(FEAT_PYTHON) || defined(PROTO)
5370/*
5371 * Return the dictitem that an entry in a hashtable points to.
5372 */
5373 dictitem_T *
5374dict_lookup(hi)
5375 hashitem_T *hi;
5376{
5377 return HI2DI(hi);
5378}
5379#endif
5380
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005381/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005382 * Return TRUE when two dictionaries have exactly the same key/values.
5383 */
5384 static int
5385dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005386 dict_T *d1;
5387 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005388 int ic; /* ignore case for strings */
5389{
Bram Moolenaar33570922005-01-25 22:26:29 +00005390 hashitem_T *hi;
5391 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005392 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005393
5394 if (dict_len(d1) != dict_len(d2))
5395 return FALSE;
5396
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 todo = d1->dv_hashtab.ht_used;
5398 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005400 if (!HASHITEM_EMPTY(hi))
5401 {
5402 item2 = dict_find(d2, hi->hi_key, -1);
5403 if (item2 == NULL)
5404 return FALSE;
5405 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5406 return FALSE;
5407 --todo;
5408 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005409 }
5410 return TRUE;
5411}
5412
5413/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005414 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005415 * Compares the items just like "==" would compare them, but strings and
5416 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005417 */
5418 static int
5419tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 typval_T *tv1;
5421 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005422 int ic; /* ignore case */
5423{
5424 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005425 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005426
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005427 if (tv1->v_type != tv2->v_type)
5428 return FALSE;
5429
5430 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005431 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005432 case VAR_LIST:
5433 /* recursive! */
5434 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5435
5436 case VAR_DICT:
5437 /* recursive! */
5438 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5439
5440 case VAR_FUNC:
5441 return (tv1->vval.v_string != NULL
5442 && tv2->vval.v_string != NULL
5443 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5444
5445 case VAR_NUMBER:
5446 return tv1->vval.v_number == tv2->vval.v_number;
5447
5448 case VAR_STRING:
5449 s1 = get_tv_string_buf(tv1, buf1);
5450 s2 = get_tv_string_buf(tv2, buf2);
5451 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005452 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005453
5454 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005455 return TRUE;
5456}
5457
5458/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 * Locate item with index "n" in list "l" and return it.
5460 * A negative index is counted from the end; -1 is the last item.
5461 * Returns NULL when "n" is out of range.
5462 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005463 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005465 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 long n;
5467{
Bram Moolenaar33570922005-01-25 22:26:29 +00005468 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 long idx;
5470
5471 if (l == NULL)
5472 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005473
5474 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005476 n = l->lv_len + n;
5477
5478 /* Check for index out of range. */
5479 if (n < 0 || n >= l->lv_len)
5480 return NULL;
5481
5482 /* When there is a cached index may start search from there. */
5483 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005485 if (n < l->lv_idx / 2)
5486 {
5487 /* closest to the start of the list */
5488 item = l->lv_first;
5489 idx = 0;
5490 }
5491 else if (n > (l->lv_idx + l->lv_len) / 2)
5492 {
5493 /* closest to the end of the list */
5494 item = l->lv_last;
5495 idx = l->lv_len - 1;
5496 }
5497 else
5498 {
5499 /* closest to the cached index */
5500 item = l->lv_idx_item;
5501 idx = l->lv_idx;
5502 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504 else
5505 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005506 if (n < l->lv_len / 2)
5507 {
5508 /* closest to the start of the list */
5509 item = l->lv_first;
5510 idx = 0;
5511 }
5512 else
5513 {
5514 /* closest to the end of the list */
5515 item = l->lv_last;
5516 idx = l->lv_len - 1;
5517 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005519
5520 while (n > idx)
5521 {
5522 /* search forward */
5523 item = item->li_next;
5524 ++idx;
5525 }
5526 while (n < idx)
5527 {
5528 /* search backward */
5529 item = item->li_prev;
5530 --idx;
5531 }
5532
5533 /* cache the used index */
5534 l->lv_idx = idx;
5535 l->lv_idx_item = item;
5536
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005537 return item;
5538}
5539
5540/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005541 * Get list item "l[idx]" as a number.
5542 */
5543 static long
5544list_find_nr(l, idx, errorp)
5545 list_T *l;
5546 long idx;
5547 int *errorp; /* set to TRUE when something wrong */
5548{
5549 listitem_T *li;
5550
5551 li = list_find(l, idx);
5552 if (li == NULL)
5553 {
5554 if (errorp != NULL)
5555 *errorp = TRUE;
5556 return -1L;
5557 }
5558 return get_tv_number_chk(&li->li_tv, errorp);
5559}
5560
5561/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005562 * Locate "item" list "l" and return its index.
5563 * Returns -1 when "item" is not in the list.
5564 */
5565 static long
5566list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005567 list_T *l;
5568 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005569{
5570 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005571 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005572
5573 if (l == NULL)
5574 return -1;
5575 idx = 0;
5576 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5577 ++idx;
5578 if (li == NULL)
5579 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005580 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005581}
5582
5583/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 * Append item "item" to the end of list "l".
5585 */
5586 static void
5587list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005588 list_T *l;
5589 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590{
5591 if (l->lv_last == NULL)
5592 {
5593 /* empty list */
5594 l->lv_first = item;
5595 l->lv_last = item;
5596 item->li_prev = NULL;
5597 }
5598 else
5599 {
5600 l->lv_last->li_next = item;
5601 item->li_prev = l->lv_last;
5602 l->lv_last = item;
5603 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005604 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 item->li_next = NULL;
5606}
5607
5608/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005609 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005610 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005611 */
5612 static int
5613list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 list_T *l;
5615 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005617 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618
Bram Moolenaar05159a02005-02-26 23:04:13 +00005619 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005621 copy_tv(tv, &li->li_tv);
5622 list_append(l, li);
5623 return OK;
5624}
5625
5626/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005627 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005628 * Return FAIL when out of memory.
5629 */
5630 int
5631list_append_dict(list, dict)
5632 list_T *list;
5633 dict_T *dict;
5634{
5635 listitem_T *li = listitem_alloc();
5636
5637 if (li == NULL)
5638 return FAIL;
5639 li->li_tv.v_type = VAR_DICT;
5640 li->li_tv.v_lock = 0;
5641 li->li_tv.vval.v_dict = dict;
5642 list_append(list, li);
5643 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 return OK;
5645}
5646
5647/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005648 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005649 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005650 * Returns FAIL when out of memory.
5651 */
5652 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005653list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005654 list_T *l;
5655 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005656 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005657{
5658 listitem_T *li = listitem_alloc();
5659
5660 if (li == NULL)
5661 return FAIL;
5662 list_append(l, li);
5663 li->li_tv.v_type = VAR_STRING;
5664 li->li_tv.v_lock = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005665 if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
5666 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005667 return FAIL;
5668 return OK;
5669}
5670
5671/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005672 * Append "n" to list "l".
5673 * Returns FAIL when out of memory.
5674 */
5675 static int
5676list_append_number(l, n)
5677 list_T *l;
5678 varnumber_T n;
5679{
5680 listitem_T *li;
5681
5682 li = listitem_alloc();
5683 if (li == NULL)
5684 return FAIL;
5685 li->li_tv.v_type = VAR_NUMBER;
5686 li->li_tv.v_lock = 0;
5687 li->li_tv.vval.v_number = n;
5688 list_append(l, li);
5689 return OK;
5690}
5691
5692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005693 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005694 * If "item" is NULL append at the end.
5695 * Return FAIL when out of memory.
5696 */
5697 static int
5698list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 list_T *l;
5700 typval_T *tv;
5701 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005702{
Bram Moolenaar33570922005-01-25 22:26:29 +00005703 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005704
5705 if (ni == NULL)
5706 return FAIL;
5707 copy_tv(tv, &ni->li_tv);
5708 if (item == NULL)
5709 /* Append new item at end of list. */
5710 list_append(l, ni);
5711 else
5712 {
5713 /* Insert new item before existing item. */
5714 ni->li_prev = item->li_prev;
5715 ni->li_next = item;
5716 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005717 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005718 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005719 ++l->lv_idx;
5720 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005721 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005722 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005723 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005724 l->lv_idx_item = NULL;
5725 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005726 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005727 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005728 }
5729 return OK;
5730}
5731
5732/*
5733 * Extend "l1" with "l2".
5734 * If "bef" is NULL append at the end, otherwise insert before this item.
5735 * Returns FAIL when out of memory.
5736 */
5737 static int
5738list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 list_T *l1;
5740 list_T *l2;
5741 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005742{
Bram Moolenaar33570922005-01-25 22:26:29 +00005743 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005744
5745 for (item = l2->lv_first; item != NULL; item = item->li_next)
5746 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5747 return FAIL;
5748 return OK;
5749}
5750
5751/*
5752 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5753 * Return FAIL when out of memory.
5754 */
5755 static int
5756list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 list_T *l1;
5758 list_T *l2;
5759 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005760{
Bram Moolenaar33570922005-01-25 22:26:29 +00005761 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005762
5763 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005764 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005765 if (l == NULL)
5766 return FAIL;
5767 tv->v_type = VAR_LIST;
5768 tv->vval.v_list = l;
5769
5770 /* append all items from the second list */
5771 return list_extend(l, l2, NULL);
5772}
5773
5774/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005775 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005777 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 * Returns NULL when out of memory.
5779 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005780 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005781list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005782 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005783 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005784 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005785{
Bram Moolenaar33570922005-01-25 22:26:29 +00005786 list_T *copy;
5787 listitem_T *item;
5788 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789
5790 if (orig == NULL)
5791 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792
5793 copy = list_alloc();
5794 if (copy != NULL)
5795 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005796 if (copyID != 0)
5797 {
5798 /* Do this before adding the items, because one of the items may
5799 * refer back to this list. */
5800 orig->lv_copyID = copyID;
5801 orig->lv_copylist = copy;
5802 }
5803 for (item = orig->lv_first; item != NULL && !got_int;
5804 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 {
5806 ni = listitem_alloc();
5807 if (ni == NULL)
5808 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005809 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005810 {
5811 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5812 {
5813 vim_free(ni);
5814 break;
5815 }
5816 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005818 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819 list_append(copy, ni);
5820 }
5821 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005822 if (item != NULL)
5823 {
5824 list_unref(copy);
5825 copy = NULL;
5826 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 }
5828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 return copy;
5830}
5831
5832/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005833 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005834 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005836 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005837list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005838 list_T *l;
5839 listitem_T *item;
5840 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005841{
Bram Moolenaar33570922005-01-25 22:26:29 +00005842 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005844 /* notify watchers */
5845 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005847 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005848 list_fix_watch(l, ip);
5849 if (ip == item2)
5850 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005852
5853 if (item2->li_next == NULL)
5854 l->lv_last = item->li_prev;
5855 else
5856 item2->li_next->li_prev = item->li_prev;
5857 if (item->li_prev == NULL)
5858 l->lv_first = item2->li_next;
5859 else
5860 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005861 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862}
5863
5864/*
5865 * Return an allocated string with the string representation of a list.
5866 * May return NULL.
5867 */
5868 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005869list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005870 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005871 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872{
5873 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874
5875 if (tv->vval.v_list == NULL)
5876 return NULL;
5877 ga_init2(&ga, (int)sizeof(char), 80);
5878 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005879 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880 {
5881 vim_free(ga.ga_data);
5882 return NULL;
5883 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 ga_append(&ga, ']');
5885 ga_append(&ga, NUL);
5886 return (char_u *)ga.ga_data;
5887}
5888
5889/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 * Join list "l" into a string in "*gap", using separator "sep".
5891 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005892 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005894 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005895list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 char_u *sep;
5899 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005900 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901{
5902 int first = TRUE;
5903 char_u *tofree;
5904 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 char_u *s;
5907
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005908 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 {
5910 if (first)
5911 first = FALSE;
5912 else
5913 ga_concat(gap, sep);
5914
5915 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005916 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005918 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 if (s != NULL)
5920 ga_concat(gap, s);
5921 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 if (s == NULL)
5923 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005925 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926}
5927
5928/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005929 * Garbage collection for lists and dictionaries.
5930 *
5931 * We use reference counts to be able to free most items right away when they
5932 * are no longer used. But for composite items it's possible that it becomes
5933 * unused while the reference count is > 0: When there is a recursive
5934 * reference. Example:
5935 * :let l = [1, 2, 3]
5936 * :let d = {9: l}
5937 * :let l[1] = d
5938 *
5939 * Since this is quite unusual we handle this with garbage collection: every
5940 * once in a while find out which lists and dicts are not referenced from any
5941 * variable.
5942 *
5943 * Here is a good reference text about garbage collection (refers to Python
5944 * but it applies to all reference-counting mechanisms):
5945 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005946 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005947
5948/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005949 * Do garbage collection for lists and dicts.
5950 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005951 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005952 int
5953garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005954{
5955 dict_T *dd;
5956 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005957 int copyID = ++current_copyID;
5958 buf_T *buf;
5959 win_T *wp;
5960 int i;
5961 funccall_T *fc;
5962 int did_free = FALSE;
5963
5964 /*
5965 * 1. Go through all accessible variables and mark all lists and dicts
5966 * with copyID.
5967 */
5968 /* script-local variables */
5969 for (i = 1; i <= ga_scripts.ga_len; ++i)
5970 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5971
5972 /* buffer-local variables */
5973 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5974 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5975
5976 /* window-local variables */
5977 FOR_ALL_WINDOWS(wp)
5978 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5979
5980 /* global variables */
5981 set_ref_in_ht(&globvarht, copyID);
5982
5983 /* function-local variables */
5984 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5985 {
5986 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5987 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5988 }
5989
5990 /*
5991 * 2. Go through the list of dicts and free items without the copyID.
5992 */
5993 for (dd = first_dict; dd != NULL; )
5994 if (dd->dv_copyID != copyID)
5995 {
5996 dict_free(dd);
5997 did_free = TRUE;
5998
5999 /* restart, next dict may also have been freed */
6000 dd = first_dict;
6001 }
6002 else
6003 dd = dd->dv_used_next;
6004
6005 /*
6006 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006007 * But don't free a list that has a watcher (used in a for loop), these
6008 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006009 */
6010 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006011 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006012 {
6013 list_free(ll);
6014 did_free = TRUE;
6015
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006016 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006017 ll = first_list;
6018 }
6019 else
6020 ll = ll->lv_used_next;
6021
6022 return did_free;
6023}
6024
6025/*
6026 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6027 */
6028 static void
6029set_ref_in_ht(ht, copyID)
6030 hashtab_T *ht;
6031 int copyID;
6032{
6033 int todo;
6034 hashitem_T *hi;
6035
6036 todo = ht->ht_used;
6037 for (hi = ht->ht_array; todo > 0; ++hi)
6038 if (!HASHITEM_EMPTY(hi))
6039 {
6040 --todo;
6041 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6042 }
6043}
6044
6045/*
6046 * Mark all lists and dicts referenced through list "l" with "copyID".
6047 */
6048 static void
6049set_ref_in_list(l, copyID)
6050 list_T *l;
6051 int copyID;
6052{
6053 listitem_T *li;
6054
6055 for (li = l->lv_first; li != NULL; li = li->li_next)
6056 set_ref_in_item(&li->li_tv, copyID);
6057}
6058
6059/*
6060 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6061 */
6062 static void
6063set_ref_in_item(tv, copyID)
6064 typval_T *tv;
6065 int copyID;
6066{
6067 dict_T *dd;
6068 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006069
6070 switch (tv->v_type)
6071 {
6072 case VAR_DICT:
6073 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006074 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006075 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006076 /* Didn't see this dict yet. */
6077 dd->dv_copyID = copyID;
6078 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006079 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006080 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006081
6082 case VAR_LIST:
6083 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006084 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006085 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006086 /* Didn't see this list yet. */
6087 ll->lv_copyID = copyID;
6088 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006089 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006090 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006091 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006093}
6094
6095/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006096 * Allocate an empty header for a dictionary.
6097 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006098 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006099dict_alloc()
6100{
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006102
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006105 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006106 /* Add the list to the hashtable for garbage collection. */
6107 if (first_dict != NULL)
6108 first_dict->dv_used_prev = d;
6109 d->dv_used_next = first_dict;
6110 d->dv_used_prev = NULL;
6111
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006113 d->dv_lock = 0;
6114 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006115 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006116 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006117 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006118}
6119
6120/*
6121 * Unreference a Dictionary: decrement the reference count and free it when it
6122 * becomes zero.
6123 */
6124 static void
6125dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006127{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006128 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6129 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006130}
6131
6132/*
6133 * Free a Dictionary, including all items it contains.
6134 * Ignores the reference count.
6135 */
6136 static void
6137dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006139{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006142 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006143
Bram Moolenaard9fba312005-06-26 22:34:35 +00006144 /* Avoid that recursive reference to the dict frees us again. */
6145 d->dv_refcount = DEL_REFCOUNT;
6146
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006147 /* Remove the dict from the list of dicts for garbage collection. */
6148 if (d->dv_used_prev == NULL)
6149 first_dict = d->dv_used_next;
6150 else
6151 d->dv_used_prev->dv_used_next = d->dv_used_next;
6152 if (d->dv_used_next != NULL)
6153 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6154
6155 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006156 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 todo = d->dv_hashtab.ht_used;
6158 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 if (!HASHITEM_EMPTY(hi))
6161 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006162 /* Remove the item before deleting it, just in case there is
6163 * something recursive causing trouble. */
6164 di = HI2DI(hi);
6165 hash_remove(&d->dv_hashtab, hi);
6166 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006167 --todo;
6168 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006169 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006171 vim_free(d);
6172}
6173
6174/*
6175 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006176 * The "key" is copied to the new item.
6177 * Note that the value of the item "di_tv" still needs to be initialized!
6178 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006179 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006181dictitem_alloc(key)
6182 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006183{
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006185
Bram Moolenaar33570922005-01-25 22:26:29 +00006186 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006187 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006188 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006189 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006190 di->di_flags = 0;
6191 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006192 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006193}
6194
6195/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006196 * Make a copy of a Dictionary item.
6197 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006199dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006201{
Bram Moolenaar33570922005-01-25 22:26:29 +00006202 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006203
Bram Moolenaar33570922005-01-25 22:26:29 +00006204 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006205 if (di != NULL)
6206 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006207 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006208 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006209 copy_tv(&org->di_tv, &di->di_tv);
6210 }
6211 return di;
6212}
6213
6214/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006215 * Remove item "item" from Dictionary "dict" and free it.
6216 */
6217 static void
6218dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006219 dict_T *dict;
6220 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006221{
Bram Moolenaar33570922005-01-25 22:26:29 +00006222 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006223
Bram Moolenaar33570922005-01-25 22:26:29 +00006224 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006225 if (HASHITEM_EMPTY(hi))
6226 EMSG2(_(e_intern2), "dictitem_remove()");
6227 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006228 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 dictitem_free(item);
6230}
6231
6232/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006233 * Free a dict item. Also clears the value.
6234 */
6235 static void
6236dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006238{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006239 clear_tv(&item->di_tv);
6240 vim_free(item);
6241}
6242
6243/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006244 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6245 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006246 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006247 * Returns NULL when out of memory.
6248 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006250dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006252 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006253 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006254{
Bram Moolenaar33570922005-01-25 22:26:29 +00006255 dict_T *copy;
6256 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006257 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006258 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006259
6260 if (orig == NULL)
6261 return NULL;
6262
6263 copy = dict_alloc();
6264 if (copy != NULL)
6265 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006266 if (copyID != 0)
6267 {
6268 orig->dv_copyID = copyID;
6269 orig->dv_copydict = copy;
6270 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006271 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006272 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006273 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006276 --todo;
6277
6278 di = dictitem_alloc(hi->hi_key);
6279 if (di == NULL)
6280 break;
6281 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006282 {
6283 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6284 copyID) == FAIL)
6285 {
6286 vim_free(di);
6287 break;
6288 }
6289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006290 else
6291 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6292 if (dict_add(copy, di) == FAIL)
6293 {
6294 dictitem_free(di);
6295 break;
6296 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006298 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006299
Bram Moolenaare9a41262005-01-15 22:18:47 +00006300 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006301 if (todo > 0)
6302 {
6303 dict_unref(copy);
6304 copy = NULL;
6305 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006306 }
6307
6308 return copy;
6309}
6310
6311/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006313 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006315 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 dict_T *d;
6318 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006319{
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006321}
6322
Bram Moolenaar8c711452005-01-14 21:53:12 +00006323/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006324 * Add a number or string entry to dictionary "d".
6325 * When "str" is NULL use number "nr", otherwise use "str".
6326 * Returns FAIL when out of memory and when key already exists.
6327 */
6328 int
6329dict_add_nr_str(d, key, nr, str)
6330 dict_T *d;
6331 char *key;
6332 long nr;
6333 char_u *str;
6334{
6335 dictitem_T *item;
6336
6337 item = dictitem_alloc((char_u *)key);
6338 if (item == NULL)
6339 return FAIL;
6340 item->di_tv.v_lock = 0;
6341 if (str == NULL)
6342 {
6343 item->di_tv.v_type = VAR_NUMBER;
6344 item->di_tv.vval.v_number = nr;
6345 }
6346 else
6347 {
6348 item->di_tv.v_type = VAR_STRING;
6349 item->di_tv.vval.v_string = vim_strsave(str);
6350 }
6351 if (dict_add(d, item) == FAIL)
6352 {
6353 dictitem_free(item);
6354 return FAIL;
6355 }
6356 return OK;
6357}
6358
6359/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360 * Get the number of items in a Dictionary.
6361 */
6362 static long
6363dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006365{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366 if (d == NULL)
6367 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006368 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006369}
6370
6371/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006372 * Find item "key[len]" in Dictionary "d".
6373 * If "len" is negative use strlen(key).
6374 * Returns NULL when not found.
6375 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006377dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006379 char_u *key;
6380 int len;
6381{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006382#define AKEYLEN 200
6383 char_u buf[AKEYLEN];
6384 char_u *akey;
6385 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006387
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006388 if (len < 0)
6389 akey = key;
6390 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006392 tofree = akey = vim_strnsave(key, len);
6393 if (akey == NULL)
6394 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006395 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006396 else
6397 {
6398 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006399 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006400 akey = buf;
6401 }
6402
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404 vim_free(tofree);
6405 if (HASHITEM_EMPTY(hi))
6406 return NULL;
6407 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006408}
6409
6410/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006411 * Get a string item from a dictionary.
6412 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006413 * Returns NULL if the entry doesn't exist or out of memory.
6414 */
6415 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006416get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006417 dict_T *d;
6418 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006419 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006420{
6421 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006422 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006423
6424 di = dict_find(d, key, -1);
6425 if (di == NULL)
6426 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006427 s = get_tv_string(&di->di_tv);
6428 if (save && s != NULL)
6429 s = vim_strsave(s);
6430 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006431}
6432
6433/*
6434 * Get a number item from a dictionary.
6435 * Returns 0 if the entry doesn't exist or out of memory.
6436 */
6437 long
6438get_dict_number(d, key)
6439 dict_T *d;
6440 char_u *key;
6441{
6442 dictitem_T *di;
6443
6444 di = dict_find(d, key, -1);
6445 if (di == NULL)
6446 return 0;
6447 return get_tv_number(&di->di_tv);
6448}
6449
6450/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006451 * Return an allocated string with the string representation of a Dictionary.
6452 * May return NULL.
6453 */
6454 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006455dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006457 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006458{
6459 garray_T ga;
6460 int first = TRUE;
6461 char_u *tofree;
6462 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006464 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006466 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006468 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006469 return NULL;
6470 ga_init2(&ga, (int)sizeof(char), 80);
6471 ga_append(&ga, '{');
6472
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006474 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006475 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006476 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006478 --todo;
6479
6480 if (first)
6481 first = FALSE;
6482 else
6483 ga_concat(&ga, (char_u *)", ");
6484
6485 tofree = string_quote(hi->hi_key, FALSE);
6486 if (tofree != NULL)
6487 {
6488 ga_concat(&ga, tofree);
6489 vim_free(tofree);
6490 }
6491 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006492 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006493 if (s != NULL)
6494 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006495 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 if (s == NULL)
6497 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006499 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 if (todo > 0)
6501 {
6502 vim_free(ga.ga_data);
6503 return NULL;
6504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505
6506 ga_append(&ga, '}');
6507 ga_append(&ga, NUL);
6508 return (char_u *)ga.ga_data;
6509}
6510
6511/*
6512 * Allocate a variable for a Dictionary and fill it from "*arg".
6513 * Return OK or FAIL. Returns NOTDONE for {expr}.
6514 */
6515 static int
6516get_dict_tv(arg, rettv, evaluate)
6517 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006519 int evaluate;
6520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 dict_T *d = NULL;
6522 typval_T tvkey;
6523 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006524 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006526 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006527 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006528
6529 /*
6530 * First check if it's not a curly-braces thing: {expr}.
6531 * Must do this without evaluating, otherwise a function may be called
6532 * twice. Unfortunately this means we need to call eval1() twice for the
6533 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006534 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006535 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006536 if (*start != '}')
6537 {
6538 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6539 return FAIL;
6540 if (*start == '}')
6541 return NOTDONE;
6542 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006543
6544 if (evaluate)
6545 {
6546 d = dict_alloc();
6547 if (d == NULL)
6548 return FAIL;
6549 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006550 tvkey.v_type = VAR_UNKNOWN;
6551 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006552
6553 *arg = skipwhite(*arg + 1);
6554 while (**arg != '}' && **arg != NUL)
6555 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006556 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006557 goto failret;
6558 if (**arg != ':')
6559 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006560 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006561 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006562 goto failret;
6563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006564 key = get_tv_string_buf_chk(&tvkey, buf);
6565 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006567 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6568 if (key != NULL)
6569 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006570 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006571 goto failret;
6572 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573
6574 *arg = skipwhite(*arg + 1);
6575 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006577 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006578 goto failret;
6579 }
6580 if (evaluate)
6581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006582 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006583 if (item != NULL)
6584 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006585 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006586 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006587 clear_tv(&tv);
6588 goto failret;
6589 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006590 item = dictitem_alloc(key);
6591 clear_tv(&tvkey);
6592 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006595 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006596 if (dict_add(d, item) == FAIL)
6597 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006598 }
6599 }
6600
6601 if (**arg == '}')
6602 break;
6603 if (**arg != ',')
6604 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006605 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006606 goto failret;
6607 }
6608 *arg = skipwhite(*arg + 1);
6609 }
6610
6611 if (**arg != '}')
6612 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006613 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006614failret:
6615 if (evaluate)
6616 dict_free(d);
6617 return FAIL;
6618 }
6619
6620 *arg = skipwhite(*arg + 1);
6621 if (evaluate)
6622 {
6623 rettv->v_type = VAR_DICT;
6624 rettv->vval.v_dict = d;
6625 ++d->dv_refcount;
6626 }
6627
6628 return OK;
6629}
6630
Bram Moolenaar8c711452005-01-14 21:53:12 +00006631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 * Return a string with the string representation of a variable.
6633 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006635 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006636 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 * May return NULL;
6638 */
6639 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006640echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006644 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006646 static int recurse = 0;
6647 char_u *r = NULL;
6648
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006650 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006651 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006652 *tofree = NULL;
6653 return NULL;
6654 }
6655 ++recurse;
6656
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 switch (tv->v_type)
6658 {
6659 case VAR_FUNC:
6660 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006661 r = tv->vval.v_string;
6662 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006663
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006665 if (tv->vval.v_list == NULL)
6666 {
6667 *tofree = NULL;
6668 r = NULL;
6669 }
6670 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6671 {
6672 *tofree = NULL;
6673 r = (char_u *)"[...]";
6674 }
6675 else
6676 {
6677 tv->vval.v_list->lv_copyID = copyID;
6678 *tofree = list2string(tv, copyID);
6679 r = *tofree;
6680 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006681 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682
Bram Moolenaar8c711452005-01-14 21:53:12 +00006683 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006684 if (tv->vval.v_dict == NULL)
6685 {
6686 *tofree = NULL;
6687 r = NULL;
6688 }
6689 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6690 {
6691 *tofree = NULL;
6692 r = (char_u *)"{...}";
6693 }
6694 else
6695 {
6696 tv->vval.v_dict->dv_copyID = copyID;
6697 *tofree = dict2string(tv, copyID);
6698 r = *tofree;
6699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006700 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006701
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006702 case VAR_STRING:
6703 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006704 *tofree = NULL;
6705 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006708 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006712
6713 --recurse;
6714 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715}
6716
6717/*
6718 * Return a string with the string representation of a variable.
6719 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6720 * "numbuf" is used for a number.
6721 * Puts quotes around strings, so that they can be parsed back by eval().
6722 * May return NULL;
6723 */
6724 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006725tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006726 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006727 char_u **tofree;
6728 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006729 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730{
6731 switch (tv->v_type)
6732 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 case VAR_FUNC:
6734 *tofree = string_quote(tv->vval.v_string, TRUE);
6735 return *tofree;
6736 case VAR_STRING:
6737 *tofree = string_quote(tv->vval.v_string, FALSE);
6738 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006739 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006741 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006742 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006744 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006745 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006746 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006747}
6748
6749/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006750 * Return string "str" in ' quotes, doubling ' characters.
6751 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006753 */
6754 static char_u *
6755string_quote(str, function)
6756 char_u *str;
6757 int function;
6758{
Bram Moolenaar33570922005-01-25 22:26:29 +00006759 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760 char_u *p, *r, *s;
6761
Bram Moolenaar33570922005-01-25 22:26:29 +00006762 len = (function ? 13 : 3);
6763 if (str != NULL)
6764 {
6765 len += STRLEN(str);
6766 for (p = str; *p != NUL; mb_ptr_adv(p))
6767 if (*p == '\'')
6768 ++len;
6769 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006770 s = r = alloc(len);
6771 if (r != NULL)
6772 {
6773 if (function)
6774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006775 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 r += 10;
6777 }
6778 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006779 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 if (str != NULL)
6781 for (p = str; *p != NUL; )
6782 {
6783 if (*p == '\'')
6784 *r++ = '\'';
6785 MB_COPY_CHAR(p, r);
6786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788 if (function)
6789 *r++ = ')';
6790 *r++ = NUL;
6791 }
6792 return s;
6793}
6794
6795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 * Get the value of an environment variable.
6797 * "arg" is pointing to the '$'. It is advanced to after the name.
6798 * If the environment variable was not set, silently assume it is empty.
6799 * Always return OK.
6800 */
6801 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006802get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 int evaluate;
6806{
6807 char_u *string = NULL;
6808 int len;
6809 int cc;
6810 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006811 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
6813 ++*arg;
6814 name = *arg;
6815 len = get_env_len(arg);
6816 if (evaluate)
6817 {
6818 if (len != 0)
6819 {
6820 cc = name[len];
6821 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006822 /* first try vim_getenv(), fast for normal environment vars */
6823 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006825 {
6826 if (!mustfree)
6827 string = vim_strsave(string);
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 else
6830 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006831 if (mustfree)
6832 vim_free(string);
6833
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 /* next try expanding things like $VIM and ${HOME} */
6835 string = expand_env_save(name - 1);
6836 if (string != NULL && *string == '$')
6837 {
6838 vim_free(string);
6839 string = NULL;
6840 }
6841 }
6842 name[len] = cc;
6843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006844 rettv->v_type = VAR_STRING;
6845 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 }
6847
6848 return OK;
6849}
6850
6851/*
6852 * Array with names and number of arguments of all internal functions
6853 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6854 */
6855static struct fst
6856{
6857 char *f_name; /* function name */
6858 char f_min_argc; /* minimal number of arguments */
6859 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862} functions[] =
6863{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006864 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {"append", 2, 2, f_append},
6866 {"argc", 0, 0, f_argc},
6867 {"argidx", 0, 0, f_argidx},
6868 {"argv", 1, 1, f_argv},
6869 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006870 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"bufexists", 1, 1, f_bufexists},
6872 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6873 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6874 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6875 {"buflisted", 1, 1, f_buflisted},
6876 {"bufloaded", 1, 1, f_bufloaded},
6877 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006878 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {"bufwinnr", 1, 1, f_bufwinnr},
6880 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006881 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006882 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {"char2nr", 1, 1, f_char2nr},
6884 {"cindent", 1, 1, f_cindent},
6885 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006886#if defined(FEAT_INS_EXPAND)
6887 {"complete_add", 1, 1, f_complete_add},
6888 {"complete_check", 0, 0, f_complete_check},
6889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006891 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006892 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00006894 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006895 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 {"delete", 1, 1, f_delete},
6897 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006898 {"diff_filler", 1, 1, f_diff_filler},
6899 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006900 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006902 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {"eventhandler", 0, 0, f_eventhandler},
6904 {"executable", 1, 1, f_executable},
6905 {"exists", 1, 1, f_exists},
6906 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006907 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6909 {"filereadable", 1, 1, f_filereadable},
6910 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006911 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006912 {"finddir", 1, 3, f_finddir},
6913 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {"fnamemodify", 2, 2, f_fnamemodify},
6915 {"foldclosed", 1, 1, f_foldclosed},
6916 {"foldclosedend", 1, 1, f_foldclosedend},
6917 {"foldlevel", 1, 1, f_foldlevel},
6918 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006919 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006921 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006923 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006924 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {"getbufvar", 2, 2, f_getbufvar},
6926 {"getchar", 0, 1, f_getchar},
6927 {"getcharmod", 0, 0, f_getcharmod},
6928 {"getcmdline", 0, 0, f_getcmdline},
6929 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006930 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006932 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006933 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {"getfsize", 1, 1, f_getfsize},
6935 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006936 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006937 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00006938 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaara5525202006-03-02 22:52:09 +00006939 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006940 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006941 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 {"getregtype", 0, 1, f_getregtype},
6943 {"getwinposx", 0, 0, f_getwinposx},
6944 {"getwinposy", 0, 0, f_getwinposy},
6945 {"getwinvar", 2, 2, f_getwinvar},
6946 {"glob", 1, 1, f_glob},
6947 {"globpath", 2, 2, f_globpath},
6948 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006949 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {"hasmapto", 1, 2, f_hasmapto},
6951 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6952 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6953 {"histadd", 2, 2, f_histadd},
6954 {"histdel", 1, 2, f_histdel},
6955 {"histget", 1, 2, f_histget},
6956 {"histnr", 1, 1, f_histnr},
6957 {"hlID", 1, 1, f_hlID},
6958 {"hlexists", 1, 1, f_hlexists},
6959 {"hostname", 0, 0, f_hostname},
6960 {"iconv", 3, 3, f_iconv},
6961 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006962 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006963 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006965 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {"inputrestore", 0, 0, f_inputrestore},
6967 {"inputsave", 0, 0, f_inputsave},
6968 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006969 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006971 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006973 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006976 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {"libcall", 3, 3, f_libcall},
6978 {"libcallnr", 3, 3, f_libcallnr},
6979 {"line", 1, 1, f_line},
6980 {"line2byte", 1, 1, f_line2byte},
6981 {"lispindent", 1, 1, f_lispindent},
6982 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006983 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {"maparg", 1, 2, f_maparg},
6985 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006986 {"match", 2, 4, f_match},
6987 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006988 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006989 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006990 {"max", 1, 1, f_max},
6991 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006992#ifdef vim_mkdir
6993 {"mkdir", 1, 3, f_mkdir},
6994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {"mode", 0, 0, f_mode},
6996 {"nextnonblank", 1, 1, f_nextnonblank},
6997 {"nr2char", 1, 1, f_nr2char},
6998 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006999 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007000 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007002 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {"remote_expr", 2, 3, f_remote_expr},
7004 {"remote_foreground", 1, 1, f_remote_foreground},
7005 {"remote_peek", 1, 2, f_remote_peek},
7006 {"remote_read", 1, 1, f_remote_read},
7007 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007008 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007010 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007012 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007013 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007014 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007015 {"searchpair", 3, 6, f_searchpair},
7016 {"searchpairpos", 3, 6, f_searchpairpos},
7017 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {"server2client", 2, 2, f_server2client},
7019 {"serverlist", 0, 0, f_serverlist},
7020 {"setbufvar", 3, 3, f_setbufvar},
7021 {"setcmdpos", 1, 1, f_setcmdpos},
7022 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007023 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007024 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007025 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {"setreg", 2, 3, f_setreg},
7027 {"setwinvar", 3, 3, f_setwinvar},
7028 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007029 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007030 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007031 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007032 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007033 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034#ifdef HAVE_STRFTIME
7035 {"strftime", 1, 2, f_strftime},
7036#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007038 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {"strlen", 1, 1, f_strlen},
7040 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007041 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {"strtrans", 1, 1, f_strtrans},
7043 {"submatch", 1, 1, f_submatch},
7044 {"substitute", 4, 4, f_substitute},
7045 {"synID", 3, 3, f_synID},
7046 {"synIDattr", 2, 3, f_synIDattr},
7047 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007048 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007049 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007050 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007051 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007052 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007053 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007055 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {"tolower", 1, 1, f_tolower},
7057 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007058 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"virtcol", 1, 1, f_virtcol},
7062 {"visualmode", 0, 1, f_visualmode},
7063 {"winbufnr", 1, 1, f_winbufnr},
7064 {"wincol", 0, 0, f_wincol},
7065 {"winheight", 1, 1, f_winheight},
7066 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007067 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007069 {"winrestview", 1, 1, f_winrestview},
7070 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007072 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073};
7074
7075#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7076
7077/*
7078 * Function given to ExpandGeneric() to obtain the list of internal
7079 * or user defined function names.
7080 */
7081 char_u *
7082get_function_name(xp, idx)
7083 expand_T *xp;
7084 int idx;
7085{
7086 static int intidx = -1;
7087 char_u *name;
7088
7089 if (idx == 0)
7090 intidx = -1;
7091 if (intidx < 0)
7092 {
7093 name = get_user_func_name(xp, idx);
7094 if (name != NULL)
7095 return name;
7096 }
7097 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7098 {
7099 STRCPY(IObuff, functions[intidx].f_name);
7100 STRCAT(IObuff, "(");
7101 if (functions[intidx].f_max_argc == 0)
7102 STRCAT(IObuff, ")");
7103 return IObuff;
7104 }
7105
7106 return NULL;
7107}
7108
7109/*
7110 * Function given to ExpandGeneric() to obtain the list of internal or
7111 * user defined variable or function names.
7112 */
7113/*ARGSUSED*/
7114 char_u *
7115get_expr_name(xp, idx)
7116 expand_T *xp;
7117 int idx;
7118{
7119 static int intidx = -1;
7120 char_u *name;
7121
7122 if (idx == 0)
7123 intidx = -1;
7124 if (intidx < 0)
7125 {
7126 name = get_function_name(xp, idx);
7127 if (name != NULL)
7128 return name;
7129 }
7130 return get_user_var_name(xp, ++intidx);
7131}
7132
7133#endif /* FEAT_CMDL_COMPL */
7134
7135/*
7136 * Find internal function in table above.
7137 * Return index, or -1 if not found
7138 */
7139 static int
7140find_internal_func(name)
7141 char_u *name; /* name of the function */
7142{
7143 int first = 0;
7144 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7145 int cmp;
7146 int x;
7147
7148 /*
7149 * Find the function name in the table. Binary search.
7150 */
7151 while (first <= last)
7152 {
7153 x = first + ((unsigned)(last - first) >> 1);
7154 cmp = STRCMP(name, functions[x].f_name);
7155 if (cmp < 0)
7156 last = x - 1;
7157 else if (cmp > 0)
7158 first = x + 1;
7159 else
7160 return x;
7161 }
7162 return -1;
7163}
7164
7165/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007166 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7167 * name it contains, otherwise return "name".
7168 */
7169 static char_u *
7170deref_func_name(name, lenp)
7171 char_u *name;
7172 int *lenp;
7173{
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007175 int cc;
7176
7177 cc = name[*lenp];
7178 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007179 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007180 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007182 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007184 {
7185 *lenp = 0;
7186 return (char_u *)""; /* just in case */
7187 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 *lenp = STRLEN(v->di_tv.vval.v_string);
7189 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007190 }
7191
7192 return name;
7193}
7194
7195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 * Allocate a variable for the result of a function.
7197 * Return OK or FAIL.
7198 */
7199 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7201 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 char_u *name; /* name of the function */
7203 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 char_u **arg; /* argument, pointing to the '(' */
7206 linenr_T firstline; /* first line of range */
7207 linenr_T lastline; /* last line of range */
7208 int *doesrange; /* return: function handled range */
7209 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 char_u *argp;
7213 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 int argcount = 0; /* number of arguments found */
7216
7217 /*
7218 * Get the arguments.
7219 */
7220 argp = *arg;
7221 while (argcount < MAX_FUNC_ARGS)
7222 {
7223 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7224 if (*argp == ')' || *argp == ',' || *argp == NUL)
7225 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7227 {
7228 ret = FAIL;
7229 break;
7230 }
7231 ++argcount;
7232 if (*argp != ',')
7233 break;
7234 }
7235 if (*argp == ')')
7236 ++argp;
7237 else
7238 ret = FAIL;
7239
7240 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007241 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007242 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 {
7245 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007246 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007252 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
7254 *arg = skipwhite(argp);
7255 return ret;
7256}
7257
7258
7259/*
7260 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007261 * Return OK when the function can't be called, FAIL otherwise.
7262 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 */
7264 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007265call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007266 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 char_u *name; /* name of the function */
7268 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 linenr_T firstline; /* first line of range */
7273 linenr_T lastline; /* last line of range */
7274 int *doesrange; /* return: function handled range */
7275 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007276 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
7278 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279#define ERROR_UNKNOWN 0
7280#define ERROR_TOOMANY 1
7281#define ERROR_TOOFEW 2
7282#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007283#define ERROR_DICT 4
7284#define ERROR_NONE 5
7285#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 int error = ERROR_NONE;
7287 int i;
7288 int llen;
7289 ufunc_T *fp;
7290 int cc;
7291#define FLEN_FIXED 40
7292 char_u fname_buf[FLEN_FIXED + 1];
7293 char_u *fname;
7294
7295 /*
7296 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7297 * Change <SNR>123_name() to K_SNR 123_name().
7298 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7299 */
7300 cc = name[len];
7301 name[len] = NUL;
7302 llen = eval_fname_script(name);
7303 if (llen > 0)
7304 {
7305 fname_buf[0] = K_SPECIAL;
7306 fname_buf[1] = KS_EXTRA;
7307 fname_buf[2] = (int)KE_SNR;
7308 i = 3;
7309 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7310 {
7311 if (current_SID <= 0)
7312 error = ERROR_SCRIPT;
7313 else
7314 {
7315 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7316 i = (int)STRLEN(fname_buf);
7317 }
7318 }
7319 if (i + STRLEN(name + llen) < FLEN_FIXED)
7320 {
7321 STRCPY(fname_buf + i, name + llen);
7322 fname = fname_buf;
7323 }
7324 else
7325 {
7326 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7327 if (fname == NULL)
7328 error = ERROR_OTHER;
7329 else
7330 {
7331 mch_memmove(fname, fname_buf, (size_t)i);
7332 STRCPY(fname + i, name + llen);
7333 }
7334 }
7335 }
7336 else
7337 fname = name;
7338
7339 *doesrange = FALSE;
7340
7341
7342 /* execute the function if no errors detected and executing */
7343 if (evaluate && error == ERROR_NONE)
7344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007345 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 error = ERROR_UNKNOWN;
7347
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007348 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 {
7350 /*
7351 * User defined function.
7352 */
7353 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007354
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007356 /* Trigger FuncUndefined event, may load the function. */
7357 if (fp == NULL
7358 && apply_autocmds(EVENT_FUNCUNDEFINED,
7359 fname, fname, TRUE, NULL)
7360 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007362 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 fp = find_func(fname);
7364 }
7365#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007366 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007367 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007368 {
7369 /* loaded a package, search for the function again */
7370 fp = find_func(fname);
7371 }
7372
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 if (fp != NULL)
7374 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007375 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007377 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007379 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007381 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 else
7384 {
7385 /*
7386 * Call the user function.
7387 * Save and restore search patterns, script variables and
7388 * redo buffer.
7389 */
7390 save_search_patterns();
7391 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007392 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007393 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007395 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7396 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7397 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007398 /* Function was unreferenced while being used, free it
7399 * now. */
7400 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 restoreRedobuff();
7402 restore_search_patterns();
7403 error = ERROR_NONE;
7404 }
7405 }
7406 }
7407 else
7408 {
7409 /*
7410 * Find the function name in the table, call its implementation.
7411 */
7412 i = find_internal_func(fname);
7413 if (i >= 0)
7414 {
7415 if (argcount < functions[i].f_min_argc)
7416 error = ERROR_TOOFEW;
7417 else if (argcount > functions[i].f_max_argc)
7418 error = ERROR_TOOMANY;
7419 else
7420 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007421 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007422 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 error = ERROR_NONE;
7424 }
7425 }
7426 }
7427 /*
7428 * The function call (or "FuncUndefined" autocommand sequence) might
7429 * have been aborted by an error, an interrupt, or an explicitly thrown
7430 * exception that has not been caught so far. This situation can be
7431 * tested for by calling aborting(). For an error in an internal
7432 * function or for the "E132" error in call_user_func(), however, the
7433 * throw point at which the "force_abort" flag (temporarily reset by
7434 * emsg()) is normally updated has not been reached yet. We need to
7435 * update that flag first to make aborting() reliable.
7436 */
7437 update_force_abort();
7438 }
7439 if (error == ERROR_NONE)
7440 ret = OK;
7441
7442 /*
7443 * Report an error unless the argument evaluation or function call has been
7444 * cancelled due to an aborting error, an interrupt, or an exception.
7445 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446 if (!aborting())
7447 {
7448 switch (error)
7449 {
7450 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 break;
7453 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007454 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455 break;
7456 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007457 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 name);
7459 break;
7460 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007461 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 name);
7463 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007464 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007465 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 name);
7467 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 }
7469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
7471 name[len] = cc;
7472 if (fname != name && fname != fname_buf)
7473 vim_free(fname);
7474
7475 return ret;
7476}
7477
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007478/*
7479 * Give an error message with a function name. Handle <SNR> things.
7480 */
7481 static void
7482emsg_funcname(msg, name)
7483 char *msg;
7484 char_u *name;
7485{
7486 char_u *p;
7487
7488 if (*name == K_SPECIAL)
7489 p = concat_str((char_u *)"<SNR>", name + 3);
7490 else
7491 p = name;
7492 EMSG2(_(msg), p);
7493 if (p != name)
7494 vim_free(p);
7495}
7496
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497/*********************************************
7498 * Implementation of the built-in functions
7499 */
7500
7501/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007502 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 */
7504 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007505f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007506 typval_T *argvars;
7507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508{
Bram Moolenaar33570922005-01-25 22:26:29 +00007509 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007511 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007512 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007514 if ((l = argvars[0].vval.v_list) != NULL
7515 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7516 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007518 }
7519 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007520 EMSG(_(e_listreq));
7521}
7522
7523/*
7524 * "append(lnum, string/list)" function
7525 */
7526 static void
7527f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 typval_T *argvars;
7529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007530{
7531 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007532 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007533 list_T *l = NULL;
7534 listitem_T *li = NULL;
7535 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007536 long added = 0;
7537
Bram Moolenaar0d660222005-01-07 21:51:51 +00007538 lnum = get_tv_lnum(argvars);
7539 if (lnum >= 0
7540 && lnum <= curbuf->b_ml.ml_line_count
7541 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007542 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007543 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007544 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007545 l = argvars[1].vval.v_list;
7546 if (l == NULL)
7547 return;
7548 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007550 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007551 for (;;)
7552 {
7553 if (l == NULL)
7554 tv = &argvars[1]; /* append a string */
7555 else if (li == NULL)
7556 break; /* end of list */
7557 else
7558 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007559 line = get_tv_string_chk(tv);
7560 if (line == NULL) /* type error */
7561 {
7562 rettv->vval.v_number = 1; /* Failed */
7563 break;
7564 }
7565 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007566 ++added;
7567 if (l == NULL)
7568 break;
7569 li = li->li_next;
7570 }
7571
7572 appended_lines_mark(lnum, added);
7573 if (curwin->w_cursor.lnum > lnum)
7574 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007576 else
7577 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578}
7579
7580/*
7581 * "argc()" function
7582 */
7583/* ARGSUSED */
7584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007585f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 typval_T *argvars;
7587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007589 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590}
7591
7592/*
7593 * "argidx()" function
7594 */
7595/* ARGSUSED */
7596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007597f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 typval_T *argvars;
7599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602}
7603
7604/*
7605 * "argv(nr)" function
7606 */
7607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007608f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 typval_T *argvars;
7610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 int idx;
7613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007614 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007616 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007618 rettv->vval.v_string = NULL;
7619 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620}
7621
7622/*
7623 * "browse(save, title, initdir, default)" function
7624 */
7625/* ARGSUSED */
7626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 typval_T *argvars;
7629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631#ifdef FEAT_BROWSE
7632 int save;
7633 char_u *title;
7634 char_u *initdir;
7635 char_u *defname;
7636 char_u buf[NUMBUFLEN];
7637 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007638 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007640 save = get_tv_number_chk(&argvars[0], &error);
7641 title = get_tv_string_chk(&argvars[1]);
7642 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7643 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007645 if (error || title == NULL || initdir == NULL || defname == NULL)
7646 rettv->vval.v_string = NULL;
7647 else
7648 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007649 do_browse(save ? BROWSE_SAVE : 0,
7650 title, defname, NULL, initdir, NULL, curbuf);
7651#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007653#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007654 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007655}
7656
7657/*
7658 * "browsedir(title, initdir)" function
7659 */
7660/* ARGSUSED */
7661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 typval_T *argvars;
7664 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007665{
7666#ifdef FEAT_BROWSE
7667 char_u *title;
7668 char_u *initdir;
7669 char_u buf[NUMBUFLEN];
7670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007671 title = get_tv_string_chk(&argvars[0]);
7672 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007674 if (title == NULL || initdir == NULL)
7675 rettv->vval.v_string = NULL;
7676 else
7677 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007678 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683}
7684
Bram Moolenaar33570922005-01-25 22:26:29 +00007685static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007686
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687/*
7688 * Find a buffer by number or exact name.
7689 */
7690 static buf_T *
7691find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007696 if (avar->v_type == VAR_NUMBER)
7697 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007698 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007701 if (buf == NULL)
7702 {
7703 /* No full path name match, try a match with a URL or a "nofile"
7704 * buffer, these don't use the full path. */
7705 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7706 if (buf->b_fname != NULL
7707 && (path_with_url(buf->b_fname)
7708#ifdef FEAT_QUICKFIX
7709 || bt_nofile(buf)
7710#endif
7711 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007712 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007713 break;
7714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
7716 return buf;
7717}
7718
7719/*
7720 * "bufexists(expr)" function
7721 */
7722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 typval_T *argvars;
7725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728}
7729
7730/*
7731 * "buflisted(expr)" function
7732 */
7733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 typval_T *argvars;
7736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
7738 buf_T *buf;
7739
7740 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007741 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742}
7743
7744/*
7745 * "bufloaded(expr)" function
7746 */
7747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007748f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 typval_T *argvars;
7750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 buf_T *buf;
7753
7754 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756}
7757
Bram Moolenaar33570922005-01-25 22:26:29 +00007758static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007759
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760/*
7761 * Get buffer by number or pattern.
7762 */
7763 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007765 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 int save_magic;
7769 char_u *save_cpo;
7770 buf_T *buf;
7771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007772 if (tv->v_type == VAR_NUMBER)
7773 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007774 if (tv->v_type != VAR_STRING)
7775 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (name == NULL || *name == NUL)
7777 return curbuf;
7778 if (name[0] == '$' && name[1] == NUL)
7779 return lastbuf;
7780
7781 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7782 save_magic = p_magic;
7783 p_magic = TRUE;
7784 save_cpo = p_cpo;
7785 p_cpo = (char_u *)"";
7786
7787 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7788 TRUE, FALSE));
7789
7790 p_magic = save_magic;
7791 p_cpo = save_cpo;
7792
7793 /* If not found, try expanding the name, like done for bufexists(). */
7794 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007795 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796
7797 return buf;
7798}
7799
7800/*
7801 * "bufname(expr)" function
7802 */
7803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 typval_T *argvars;
7806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 buf_T *buf;
7809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007810 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 buf = get_buf_tv(&argvars[0]);
7813 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 --emsg_off;
7819}
7820
7821/*
7822 * "bufnr(expr)" function
7823 */
7824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007825f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007826 typval_T *argvars;
7827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007830 int error = FALSE;
7831 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007833 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007836 --emsg_off;
7837
7838 /* If the buffer isn't found and the second argument is not zero create a
7839 * new buffer. */
7840 if (buf == NULL
7841 && argvars[1].v_type != VAR_UNKNOWN
7842 && get_tv_number_chk(&argvars[1], &error) != 0
7843 && !error
7844 && (name = get_tv_string_chk(&argvars[0])) != NULL
7845 && !error)
7846 buf = buflist_new(name, NULL, (linenr_T)1, 0);
7847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
7854/*
7855 * "bufwinnr(nr)" function
7856 */
7857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 typval_T *argvars;
7860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
7862#ifdef FEAT_WINDOWS
7863 win_T *wp;
7864 int winnr = 0;
7865#endif
7866 buf_T *buf;
7867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007868 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871#ifdef FEAT_WINDOWS
7872 for (wp = firstwin; wp; wp = wp->w_next)
7873 {
7874 ++winnr;
7875 if (wp->w_buffer == buf)
7876 break;
7877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007878 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007880 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881#endif
7882 --emsg_off;
7883}
7884
7885/*
7886 * "byte2line(byte)" function
7887 */
7888/*ARGSUSED*/
7889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 typval_T *argvars;
7892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
7894#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896#else
7897 long boff = 0;
7898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007899 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 (linenr_T)0, &boff);
7905#endif
7906}
7907
7908/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007909 * "byteidx()" function
7910 */
7911/*ARGSUSED*/
7912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007913f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007914 typval_T *argvars;
7915 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007916{
7917#ifdef FEAT_MBYTE
7918 char_u *t;
7919#endif
7920 char_u *str;
7921 long idx;
7922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007923 str = get_tv_string_chk(&argvars[0]);
7924 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007925 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007926 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007927 return;
7928
7929#ifdef FEAT_MBYTE
7930 t = str;
7931 for ( ; idx > 0; idx--)
7932 {
7933 if (*t == NUL) /* EOL reached */
7934 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007935 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007936 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007938#else
7939 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007940 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007941#endif
7942}
7943
7944/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007945 * "call(func, arglist)" function
7946 */
7947 static void
7948f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007951{
7952 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007953 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007954 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007955 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007956 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007957 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007958
7959 rettv->vval.v_number = 0;
7960 if (argvars[1].v_type != VAR_LIST)
7961 {
7962 EMSG(_(e_listreq));
7963 return;
7964 }
7965 if (argvars[1].vval.v_list == NULL)
7966 return;
7967
7968 if (argvars[0].v_type == VAR_FUNC)
7969 func = argvars[0].vval.v_string;
7970 else
7971 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007972 if (*func == NUL)
7973 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007974
Bram Moolenaare9a41262005-01-15 22:18:47 +00007975 if (argvars[2].v_type != VAR_UNKNOWN)
7976 {
7977 if (argvars[2].v_type != VAR_DICT)
7978 {
7979 EMSG(_(e_dictreq));
7980 return;
7981 }
7982 selfdict = argvars[2].vval.v_dict;
7983 }
7984
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007985 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7986 item = item->li_next)
7987 {
7988 if (argc == MAX_FUNC_ARGS)
7989 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007990 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991 break;
7992 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007993 /* Make a copy of each argument. This is needed to be able to set
7994 * v_lock to VAR_FIXED in the copy without changing the original list.
7995 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007996 copy_tv(&item->li_tv, &argv[argc++]);
7997 }
7998
7999 if (item == NULL)
8000 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008001 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8002 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008003
8004 /* Free the arguments. */
8005 while (argc > 0)
8006 clear_tv(&argv[--argc]);
8007}
8008
8009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 * "char2nr(string)" function
8011 */
8012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008014 typval_T *argvars;
8015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017#ifdef FEAT_MBYTE
8018 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008019 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 else
8021#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008022 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
8024
8025/*
8026 * "cindent(lnum)" function
8027 */
8028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008029f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 typval_T *argvars;
8031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032{
8033#ifdef FEAT_CINDENT
8034 pos_T pos;
8035 linenr_T lnum;
8036
8037 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8040 {
8041 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008042 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 curwin->w_cursor = pos;
8044 }
8045 else
8046#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048}
8049
8050/*
8051 * "col(string)" function
8052 */
8053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008054f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 typval_T *argvars;
8056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 colnr_T col = 0;
8059 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008060 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008062 fp = var2fpos(&argvars[0], FALSE, &fnum);
8063 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {
8065 if (fp->col == MAXCOL)
8066 {
8067 /* '> can be MAXCOL, get the length of the line then */
8068 if (fp->lnum <= curbuf->b_ml.ml_line_count)
8069 col = STRLEN(ml_get(fp->lnum)) + 1;
8070 else
8071 col = MAXCOL;
8072 }
8073 else
8074 {
8075 col = fp->col + 1;
8076#ifdef FEAT_VIRTUALEDIT
8077 /* col(".") when the cursor is on the NUL at the end of the line
8078 * because of "coladd" can be seen as an extra column. */
8079 if (virtual_active() && fp == &curwin->w_cursor)
8080 {
8081 char_u *p = ml_get_cursor();
8082
8083 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8084 curwin->w_virtcol - curwin->w_cursor.coladd))
8085 {
8086# ifdef FEAT_MBYTE
8087 int l;
8088
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008089 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 col += l;
8091# else
8092 if (*p != NUL && p[1] == NUL)
8093 ++col;
8094# endif
8095 }
8096 }
8097#endif
8098 }
8099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101}
8102
Bram Moolenaar572cb562005-08-05 21:35:02 +00008103#if defined(FEAT_INS_EXPAND)
8104/*
8105 * "complete_add()" function
8106 */
8107/*ARGSUSED*/
8108 static void
8109f_complete_add(argvars, rettv)
8110 typval_T *argvars;
8111 typval_T *rettv;
8112{
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008113 char_u *word;
8114 char_u *extra = NULL;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008115 int icase = FALSE;
Bram Moolenaar572cb562005-08-05 21:35:02 +00008116
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008117 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
8118 {
8119 word = get_dict_string(argvars[0].vval.v_dict,
8120 (char_u *)"word", FALSE);
8121 extra = get_dict_string(argvars[0].vval.v_dict,
8122 (char_u *)"menu", FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008123 icase = get_dict_number(argvars[0].vval.v_dict, (char_u *)"icase");
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00008124 }
8125 else
8126 word = get_tv_string_chk(&argvars[0]);
8127 if (word != NULL)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008128 rettv->vval.v_number = ins_compl_add(word, -1, icase,
8129 NULL, extra, 0, 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008130}
8131
8132/*
8133 * "complete_check()" function
8134 */
8135/*ARGSUSED*/
8136 static void
8137f_complete_check(argvars, rettv)
8138 typval_T *argvars;
8139 typval_T *rettv;
8140{
8141 int saved = RedrawingDisabled;
8142
8143 RedrawingDisabled = 0;
8144 ins_compl_check_keys(0);
8145 rettv->vval.v_number = compl_interrupted;
8146 RedrawingDisabled = saved;
8147}
8148#endif
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150/*
8151 * "confirm(message, buttons[, default [, type]])" function
8152 */
8153/*ARGSUSED*/
8154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008156 typval_T *argvars;
8157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8160 char_u *message;
8161 char_u *buttons = NULL;
8162 char_u buf[NUMBUFLEN];
8163 char_u buf2[NUMBUFLEN];
8164 int def = 1;
8165 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008166 char_u *typestr;
8167 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008169 message = get_tv_string_chk(&argvars[0]);
8170 if (message == NULL)
8171 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008172 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008174 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8175 if (buttons == NULL)
8176 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008177 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008179 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008180 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008182 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8183 if (typestr == NULL)
8184 error = TRUE;
8185 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008187 switch (TOUPPER_ASC(*typestr))
8188 {
8189 case 'E': type = VIM_ERROR; break;
8190 case 'Q': type = VIM_QUESTION; break;
8191 case 'I': type = VIM_INFO; break;
8192 case 'W': type = VIM_WARNING; break;
8193 case 'G': type = VIM_GENERIC; break;
8194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 }
8196 }
8197 }
8198 }
8199
8200 if (buttons == NULL || *buttons == NUL)
8201 buttons = (char_u *)_("&Ok");
8202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008203 if (error)
8204 rettv->vval.v_number = 0;
8205 else
8206 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 def, NULL);
8208#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008209 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210#endif
8211}
8212
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008213/*
8214 * "copy()" function
8215 */
8216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008217f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 typval_T *argvars;
8219 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008220{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008221 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223
8224/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008225 * "count()" function
8226 */
8227 static void
8228f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 typval_T *argvars;
8230 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008231{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008232 long n = 0;
8233 int ic = FALSE;
8234
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008236 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008237 listitem_T *li;
8238 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008239 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240
Bram Moolenaare9a41262005-01-15 22:18:47 +00008241 if ((l = argvars[0].vval.v_list) != NULL)
8242 {
8243 li = l->lv_first;
8244 if (argvars[2].v_type != VAR_UNKNOWN)
8245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008246 int error = FALSE;
8247
8248 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008249 if (argvars[3].v_type != VAR_UNKNOWN)
8250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008251 idx = get_tv_number_chk(&argvars[3], &error);
8252 if (!error)
8253 {
8254 li = list_find(l, idx);
8255 if (li == NULL)
8256 EMSGN(_(e_listidx), idx);
8257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008259 if (error)
8260 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008261 }
8262
8263 for ( ; li != NULL; li = li->li_next)
8264 if (tv_equal(&li->li_tv, &argvars[1], ic))
8265 ++n;
8266 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008267 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008268 else if (argvars[0].v_type == VAR_DICT)
8269 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 int todo;
8271 dict_T *d;
8272 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008273
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008274 if ((d = argvars[0].vval.v_dict) != NULL)
8275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008276 int error = FALSE;
8277
Bram Moolenaare9a41262005-01-15 22:18:47 +00008278 if (argvars[2].v_type != VAR_UNKNOWN)
8279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008280 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281 if (argvars[3].v_type != VAR_UNKNOWN)
8282 EMSG(_(e_invarg));
8283 }
8284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008285 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008287 {
8288 if (!HASHITEM_EMPTY(hi))
8289 {
8290 --todo;
8291 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8292 ++n;
8293 }
8294 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008295 }
8296 }
8297 else
8298 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008299 rettv->vval.v_number = n;
8300}
8301
8302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8304 *
8305 * Checks the existence of a cscope connection.
8306 */
8307/*ARGSUSED*/
8308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 typval_T *argvars;
8311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313#ifdef FEAT_CSCOPE
8314 int num = 0;
8315 char_u *dbpath = NULL;
8316 char_u *prepend = NULL;
8317 char_u buf[NUMBUFLEN];
8318
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 if (argvars[0].v_type != VAR_UNKNOWN
8320 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 num = (int)get_tv_number(&argvars[0]);
8323 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008324 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008325 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
8327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008328 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008330 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331#endif
8332}
8333
8334/*
8335 * "cursor(lnum, col)" function
8336 *
8337 * Moves the cursor to the specified line and column
8338 */
8339/*ARGSUSED*/
8340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 typval_T *argvars;
8343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
8345 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008346#ifdef FEAT_VIRTUALEDIT
8347 long coladd = 0;
8348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
Bram Moolenaara5525202006-03-02 22:52:09 +00008350 if (argvars[1].v_type == VAR_UNKNOWN)
8351 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008352 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008353
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008354 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008355 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008356 line = pos.lnum;
8357 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008358#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008359 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008360#endif
8361 }
8362 else
8363 {
8364 line = get_tv_lnum(argvars);
8365 col = get_tv_number_chk(&argvars[1], NULL);
8366#ifdef FEAT_VIRTUALEDIT
8367 if (argvars[2].v_type != VAR_UNKNOWN)
8368 coladd = get_tv_number_chk(&argvars[2], NULL);
8369#endif
8370 }
8371 if (line < 0 || col < 0
8372#ifdef FEAT_VIRTUALEDIT
8373 || coladd < 0
8374#endif
8375 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008376 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 if (line > 0)
8378 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 if (col > 0)
8380 curwin->w_cursor.col = col - 1;
8381#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008382 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#endif
8384
8385 /* Make sure the cursor is in a valid position. */
8386 check_cursor();
8387#ifdef FEAT_MBYTE
8388 /* Correct cursor for multi-byte character. */
8389 if (has_mbyte)
8390 mb_adjust_cursor();
8391#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008392
8393 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394}
8395
8396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008397 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 */
8399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008401 typval_T *argvars;
8402 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008404 int noref = 0;
8405
8406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008408 if (noref < 0 || noref > 1)
8409 EMSG(_(e_invarg));
8410 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008411 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412}
8413
8414/*
8415 * "delete()" function
8416 */
8417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008419 typval_T *argvars;
8420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426}
8427
8428/*
8429 * "did_filetype()" function
8430 */
8431/*ARGSUSED*/
8432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008433f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008434 typval_T *argvars;
8435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
8437#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441#endif
8442}
8443
8444/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008445 * "diff_filler()" function
8446 */
8447/*ARGSUSED*/
8448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008450 typval_T *argvars;
8451 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008452{
8453#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008454 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008455#endif
8456}
8457
8458/*
8459 * "diff_hlID()" function
8460 */
8461/*ARGSUSED*/
8462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008463f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008464 typval_T *argvars;
8465 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008466{
8467#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008469 static linenr_T prev_lnum = 0;
8470 static int changedtick = 0;
8471 static int fnum = 0;
8472 static int change_start = 0;
8473 static int change_end = 0;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008474 static hlf_T hlID = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008475 int filler_lines;
8476 int col;
8477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008478 if (lnum < 0) /* ignore type error in {lnum} arg */
8479 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008480 if (lnum != prev_lnum
8481 || changedtick != curbuf->b_changedtick
8482 || fnum != curbuf->b_fnum)
8483 {
8484 /* New line, buffer, change: need to get the values. */
8485 filler_lines = diff_check(curwin, lnum);
8486 if (filler_lines < 0)
8487 {
8488 if (filler_lines == -1)
8489 {
8490 change_start = MAXCOL;
8491 change_end = -1;
8492 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8493 hlID = HLF_ADD; /* added line */
8494 else
8495 hlID = HLF_CHD; /* changed line */
8496 }
8497 else
8498 hlID = HLF_ADD; /* added line */
8499 }
8500 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008501 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008502 prev_lnum = lnum;
8503 changedtick = curbuf->b_changedtick;
8504 fnum = curbuf->b_fnum;
8505 }
8506
8507 if (hlID == HLF_CHD || hlID == HLF_TXD)
8508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008509 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008510 if (col >= change_start && col <= change_end)
8511 hlID = HLF_TXD; /* changed text */
8512 else
8513 hlID = HLF_CHD; /* changed line */
8514 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008515 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008516#endif
8517}
8518
8519/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008520 * "empty({expr})" function
8521 */
8522 static void
8523f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008524 typval_T *argvars;
8525 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008526{
8527 int n;
8528
8529 switch (argvars[0].v_type)
8530 {
8531 case VAR_STRING:
8532 case VAR_FUNC:
8533 n = argvars[0].vval.v_string == NULL
8534 || *argvars[0].vval.v_string == NUL;
8535 break;
8536 case VAR_NUMBER:
8537 n = argvars[0].vval.v_number == 0;
8538 break;
8539 case VAR_LIST:
8540 n = argvars[0].vval.v_list == NULL
8541 || argvars[0].vval.v_list->lv_first == NULL;
8542 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 case VAR_DICT:
8544 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008545 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008546 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008547 default:
8548 EMSG2(_(e_intern2), "f_empty()");
8549 n = 0;
8550 }
8551
8552 rettv->vval.v_number = n;
8553}
8554
8555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 * "escape({string}, {chars})" function
8557 */
8558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 typval_T *argvars;
8561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 char_u buf[NUMBUFLEN];
8564
Bram Moolenaar758711c2005-02-02 23:11:38 +00008565 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8566 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008567 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568}
8569
8570/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008571 * "eval()" function
8572 */
8573/*ARGSUSED*/
8574 static void
8575f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008576 typval_T *argvars;
8577 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008578{
8579 char_u *s;
8580
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008581 s = get_tv_string_chk(&argvars[0]);
8582 if (s != NULL)
8583 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8586 {
8587 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008588 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008589 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008590 else if (*s != NUL)
8591 EMSG(_(e_trailing));
8592}
8593
8594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 * "eventhandler()" function
8596 */
8597/*ARGSUSED*/
8598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008599f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *argvars;
8601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008603 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604}
8605
8606/*
8607 * "executable()" function
8608 */
8609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008610f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 typval_T *argvars;
8612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008614 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615}
8616
8617/*
8618 * "exists()" function
8619 */
8620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008621f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008622 typval_T *argvars;
8623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624{
8625 char_u *p;
8626 char_u *name;
8627 int n = FALSE;
8628 int len = 0;
8629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 if (*p == '$') /* environment variable */
8632 {
8633 /* first try "normal" environment variables (fast) */
8634 if (mch_getenv(p + 1) != NULL)
8635 n = TRUE;
8636 else
8637 {
8638 /* try expanding things like $VIM and ${HOME} */
8639 p = expand_env_save(p);
8640 if (p != NULL && *p != '$')
8641 n = TRUE;
8642 vim_free(p);
8643 }
8644 }
8645 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008646 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 else if (*p == '*') /* internal or user defined function */
8648 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008649 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 }
8651 else if (*p == ':')
8652 {
8653 n = cmd_exists(p + 1);
8654 }
8655 else if (*p == '#')
8656 {
8657#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008658 if (p[1] == '#')
8659 n = autocmd_supported(p + 2);
8660 else
8661 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662#endif
8663 }
8664 else /* internal variable */
8665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008666 char_u *tofree;
8667 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008669 /* get_name_len() takes care of expanding curly braces */
8670 name = p;
8671 len = get_name_len(&p, &tofree, TRUE, FALSE);
8672 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008674 if (tofree != NULL)
8675 name = tofree;
8676 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8677 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008679 /* handle d.key, l[idx], f(expr) */
8680 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8681 if (n)
8682 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 }
8684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008686 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 }
8688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008689 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
8693 * "expand()" function
8694 */
8695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 typval_T *argvars;
8698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699{
8700 char_u *s;
8701 int len;
8702 char_u *errormsg;
8703 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8704 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->v_type = VAR_STRING;
8708 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 if (*s == '%' || *s == '#' || *s == '<')
8710 {
8711 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 --emsg_off;
8714 }
8715 else
8716 {
8717 /* When the optional second argument is non-zero, don't remove matches
8718 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 if (argvars[1].v_type != VAR_UNKNOWN
8720 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 if (!error)
8723 {
8724 ExpandInit(&xpc);
8725 xpc.xp_context = EXPAND_FILES;
8726 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8727 ExpandCleanup(&xpc);
8728 }
8729 else
8730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
8732}
8733
8734/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008735 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008736 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008737 */
8738 static void
8739f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *argvars;
8741 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008742{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008743 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008744 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008745 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 list_T *l1, *l2;
8747 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008749 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008750
Bram Moolenaare9a41262005-01-15 22:18:47 +00008751 l1 = argvars[0].vval.v_list;
8752 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008753 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8754 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 {
8756 if (argvars[2].v_type != VAR_UNKNOWN)
8757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008758 before = get_tv_number_chk(&argvars[2], &error);
8759 if (error)
8760 return; /* type error; errmsg already given */
8761
Bram Moolenaar758711c2005-02-02 23:11:38 +00008762 if (before == l1->lv_len)
8763 item = NULL;
8764 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008765 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008766 item = list_find(l1, before);
8767 if (item == NULL)
8768 {
8769 EMSGN(_(e_listidx), before);
8770 return;
8771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008772 }
8773 }
8774 else
8775 item = NULL;
8776 list_extend(l1, l2, item);
8777
Bram Moolenaare9a41262005-01-15 22:18:47 +00008778 copy_tv(&argvars[0], rettv);
8779 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008780 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008781 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8782 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008783 dict_T *d1, *d2;
8784 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008785 char_u *action;
8786 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008788 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008789
8790 d1 = argvars[0].vval.v_dict;
8791 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008792 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8793 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008794 {
8795 /* Check the third argument. */
8796 if (argvars[2].v_type != VAR_UNKNOWN)
8797 {
8798 static char *(av[]) = {"keep", "force", "error"};
8799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008800 action = get_tv_string_chk(&argvars[2]);
8801 if (action == NULL)
8802 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008803 for (i = 0; i < 3; ++i)
8804 if (STRCMP(action, av[i]) == 0)
8805 break;
8806 if (i == 3)
8807 {
8808 EMSGN(_(e_invarg2), action);
8809 return;
8810 }
8811 }
8812 else
8813 action = (char_u *)"force";
8814
8815 /* Go over all entries in the second dict and add them to the
8816 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008817 todo = d2->dv_hashtab.ht_used;
8818 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008819 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008820 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008821 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008822 --todo;
8823 di1 = dict_find(d1, hi2->hi_key, -1);
8824 if (di1 == NULL)
8825 {
8826 di1 = dictitem_copy(HI2DI(hi2));
8827 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8828 dictitem_free(di1);
8829 }
8830 else if (*action == 'e')
8831 {
8832 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8833 break;
8834 }
8835 else if (*action == 'f')
8836 {
8837 clear_tv(&di1->di_tv);
8838 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008840 }
8841 }
8842
Bram Moolenaare9a41262005-01-15 22:18:47 +00008843 copy_tv(&argvars[0], rettv);
8844 }
8845 }
8846 else
8847 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008848}
8849
8850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 * "filereadable()" function
8852 */
8853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 typval_T *argvars;
8856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
8858 FILE *fd;
8859 char_u *p;
8860 int n;
8861
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8864 {
8865 n = TRUE;
8866 fclose(fd);
8867 }
8868 else
8869 n = FALSE;
8870
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872}
8873
8874/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008875 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 * rights to write into.
8877 */
8878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T *argvars;
8881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008883 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885
Bram Moolenaar33570922005-01-25 22:26:29 +00008886static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008887
8888 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008889findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008890 typval_T *argvars;
8891 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008892 int dir;
8893{
8894#ifdef FEAT_SEARCHPATH
8895 char_u *fname;
8896 char_u *fresult = NULL;
8897 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8898 char_u *p;
8899 char_u pathbuf[NUMBUFLEN];
8900 int count = 1;
8901 int first = TRUE;
8902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008904
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008905 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8908 if (p == NULL)
8909 count = -1; /* error */
8910 else
8911 {
8912 if (*p != NUL)
8913 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008915 if (argvars[2].v_type != VAR_UNKNOWN)
8916 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8917 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008918 }
8919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 do
8923 {
8924 vim_free(fresult);
8925 fresult = find_file_in_path_option(first ? fname : NULL,
8926 first ? (int)STRLEN(fname) : 0,
8927 0, first, path, dir, NULL);
8928 first = FALSE;
8929 } while (--count > 0 && fresult != NULL);
8930 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008931
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008933#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008935#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008937}
8938
Bram Moolenaar33570922005-01-25 22:26:29 +00008939static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8940static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008941
8942/*
8943 * Implementation of map() and filter().
8944 */
8945 static void
8946filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008947 typval_T *argvars;
8948 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008949 int map;
8950{
8951 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 listitem_T *li, *nli;
8954 list_T *l = NULL;
8955 dictitem_T *di;
8956 hashtab_T *ht;
8957 hashitem_T *hi;
8958 dict_T *d = NULL;
8959 typval_T save_val;
8960 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008961 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008962 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008963 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar280f1262006-01-30 00:14:18 +00008964 int save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008965
8966 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008968 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008969 if ((l = argvars[0].vval.v_list) == NULL
8970 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 return;
8972 }
8973 else if (argvars[0].v_type == VAR_DICT)
8974 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008975 if ((d = argvars[0].vval.v_dict) == NULL
8976 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008977 return;
8978 }
8979 else
8980 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008981 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 return;
8983 }
8984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 expr = get_tv_string_buf_chk(&argvars[1], buf);
8986 /* On type errors, the preceding call has already displayed an error
8987 * message. Avoid a misleading error message for an empty string that
8988 * was not passed as argument. */
8989 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008991 prepare_vimvar(VV_VAL, &save_val);
8992 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008993
Bram Moolenaar280f1262006-01-30 00:14:18 +00008994 /* We reset "called_emsg" to be able to detect whether an error
8995 * occurred during evaluation of the expression. "did_emsg" can't be
8996 * used, because it is reset when calling a function. */
8997 save_called_emsg = called_emsg;
8998 called_emsg = FALSE;
8999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 prepare_vimvar(VV_KEY, &save_key);
9003 vimvars[VV_KEY].vv_type = VAR_STRING;
9004
9005 ht = &d->dv_hashtab;
9006 hash_lock(ht);
9007 todo = ht->ht_used;
9008 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 if (!HASHITEM_EMPTY(hi))
9011 {
9012 --todo;
9013 di = HI2DI(hi);
9014 if (tv_check_lock(di->di_tv.v_lock, msg))
9015 break;
9016 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009017 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
9018 || called_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 break;
9020 if (!map && rem)
9021 dictitem_remove(d, di);
9022 clear_tv(&vimvars[VV_KEY].vv_tv);
9023 }
9024 }
9025 hash_unlock(ht);
9026
9027 restore_vimvar(VV_KEY, &save_key);
9028 }
9029 else
9030 {
9031 for (li = l->lv_first; li != NULL; li = nli)
9032 {
9033 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009034 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009036 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
9037 || called_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009038 break;
9039 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009040 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009041 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009042 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009045
9046 called_emsg |= save_called_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009048
9049 copy_tv(&argvars[0], rettv);
9050}
9051
9052 static int
9053filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009055 char_u *expr;
9056 int map;
9057 int *remp;
9058{
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009060 char_u *s;
9061
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009063 s = expr;
9064 if (eval1(&s, &rettv, TRUE) == FAIL)
9065 return FAIL;
9066 if (*s != NUL) /* check for trailing chars after expr */
9067 {
9068 EMSG2(_(e_invexpr2), s);
9069 return FAIL;
9070 }
9071 if (map)
9072 {
9073 /* map(): replace the list item value */
9074 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009075 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009076 *tv = rettv;
9077 }
9078 else
9079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009080 int error = FALSE;
9081
Bram Moolenaare9a41262005-01-15 22:18:47 +00009082 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009084 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 /* On type error, nothing has been removed; return FAIL to stop the
9086 * loop. The error message was given by get_tv_number_chk(). */
9087 if (error)
9088 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009089 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009091 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009092}
9093
9094/*
9095 * "filter()" function
9096 */
9097 static void
9098f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009101{
9102 filter_map(argvars, rettv, FALSE);
9103}
9104
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009105/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009106 * "finddir({fname}[, {path}[, {count}]])" function
9107 */
9108 static void
9109f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 typval_T *argvars;
9111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009112{
9113 findfilendir(argvars, rettv, TRUE);
9114}
9115
9116/*
9117 * "findfile({fname}[, {path}[, {count}]])" function
9118 */
9119 static void
9120f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009123{
9124 findfilendir(argvars, rettv, FALSE);
9125}
9126
9127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 * "fnamemodify({fname}, {mods})" function
9129 */
9130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 typval_T *argvars;
9133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 char_u *fname;
9136 char_u *mods;
9137 int usedlen = 0;
9138 int len;
9139 char_u *fbuf = NULL;
9140 char_u buf[NUMBUFLEN];
9141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009142 fname = get_tv_string_chk(&argvars[0]);
9143 mods = get_tv_string_buf_chk(&argvars[1], buf);
9144 if (fname == NULL || mods == NULL)
9145 fname = NULL;
9146 else
9147 {
9148 len = (int)STRLEN(fname);
9149 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 vim_free(fbuf);
9158}
9159
Bram Moolenaar33570922005-01-25 22:26:29 +00009160static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
9162/*
9163 * "foldclosed()" function
9164 */
9165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *argvars;
9168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 int end;
9170{
9171#ifdef FEAT_FOLDING
9172 linenr_T lnum;
9173 linenr_T first, last;
9174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9177 {
9178 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9179 {
9180 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 return;
9185 }
9186 }
9187#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189}
9190
9191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009192 * "foldclosed()" function
9193 */
9194 static void
9195f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *argvars;
9197 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009198{
9199 foldclosed_both(argvars, rettv, FALSE);
9200}
9201
9202/*
9203 * "foldclosedend()" function
9204 */
9205 static void
9206f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009207 typval_T *argvars;
9208 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009209{
9210 foldclosed_both(argvars, rettv, TRUE);
9211}
9212
9213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 * "foldlevel()" function
9215 */
9216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009218 typval_T *argvars;
9219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220{
9221#ifdef FEAT_FOLDING
9222 linenr_T lnum;
9223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 else
9228#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230}
9231
9232/*
9233 * "foldtext()" function
9234 */
9235/*ARGSUSED*/
9236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009237f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009238 typval_T *argvars;
9239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241#ifdef FEAT_FOLDING
9242 linenr_T lnum;
9243 char_u *s;
9244 char_u *r;
9245 int len;
9246 char *txt;
9247#endif
9248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009249 rettv->v_type = VAR_STRING;
9250 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009252 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9253 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9254 <= curbuf->b_ml.ml_line_count
9255 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 {
9257 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009258 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9259 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 {
9261 if (!linewhite(lnum))
9262 break;
9263 ++lnum;
9264 }
9265
9266 /* Find interesting text in this line. */
9267 s = skipwhite(ml_get(lnum));
9268 /* skip C comment-start */
9269 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009272 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009273 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009274 {
9275 s = skipwhite(ml_get(lnum + 1));
9276 if (*s == '*')
9277 s = skipwhite(s + 1);
9278 }
9279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 txt = _("+-%s%3ld lines: ");
9281 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009282 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 + 20 /* for %3ld */
9284 + STRLEN(s))); /* concatenated */
9285 if (r != NULL)
9286 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009287 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9288 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9289 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 len = (int)STRLEN(r);
9291 STRCAT(r, s);
9292 /* remove 'foldmarker' and 'commentstring' */
9293 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 }
9296 }
9297#endif
9298}
9299
9300/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009301 * "foldtextresult(lnum)" function
9302 */
9303/*ARGSUSED*/
9304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009306 typval_T *argvars;
9307 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009308{
9309#ifdef FEAT_FOLDING
9310 linenr_T lnum;
9311 char_u *text;
9312 char_u buf[51];
9313 foldinfo_T foldinfo;
9314 int fold_count;
9315#endif
9316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 rettv->v_type = VAR_STRING;
9318 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009319#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 /* treat illegal types and illegal string values for {lnum} the same */
9322 if (lnum < 0)
9323 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009324 fold_count = foldedCount(curwin, lnum, &foldinfo);
9325 if (fold_count > 0)
9326 {
9327 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9328 &foldinfo, buf);
9329 if (text == buf)
9330 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009332 }
9333#endif
9334}
9335
9336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 * "foreground()" function
9338 */
9339/*ARGSUSED*/
9340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009342 typval_T *argvars;
9343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346#ifdef FEAT_GUI
9347 if (gui.in_use)
9348 gui_mch_set_foreground();
9349#else
9350# ifdef WIN32
9351 win32_set_foreground();
9352# endif
9353#endif
9354}
9355
9356/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009357 * "function()" function
9358 */
9359/*ARGSUSED*/
9360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009362 typval_T *argvars;
9363 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009364{
9365 char_u *s;
9366
Bram Moolenaara7043832005-01-21 11:56:39 +00009367 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009369 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009370 EMSG2(_(e_invarg2), s);
9371 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009372 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009373 else
9374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375 rettv->vval.v_string = vim_strsave(s);
9376 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009377 }
9378}
9379
9380/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009381 * "garbagecollect()" function
9382 */
9383/*ARGSUSED*/
9384 static void
9385f_garbagecollect(argvars, rettv)
9386 typval_T *argvars;
9387 typval_T *rettv;
9388{
9389 garbage_collect();
9390}
9391
9392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009393 * "get()" function
9394 */
9395 static void
9396f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009397 typval_T *argvars;
9398 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009399{
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 listitem_T *li;
9401 list_T *l;
9402 dictitem_T *di;
9403 dict_T *d;
9404 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405
Bram Moolenaare9a41262005-01-15 22:18:47 +00009406 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009407 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009408 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009410 int error = FALSE;
9411
9412 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9413 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009414 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009415 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009417 else if (argvars[0].v_type == VAR_DICT)
9418 {
9419 if ((d = argvars[0].vval.v_dict) != NULL)
9420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009421 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009422 if (di != NULL)
9423 tv = &di->di_tv;
9424 }
9425 }
9426 else
9427 EMSG2(_(e_listdictarg), "get()");
9428
9429 if (tv == NULL)
9430 {
9431 if (argvars[2].v_type == VAR_UNKNOWN)
9432 rettv->vval.v_number = 0;
9433 else
9434 copy_tv(&argvars[2], rettv);
9435 }
9436 else
9437 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009438}
9439
Bram Moolenaar342337a2005-07-21 21:11:17 +00009440static 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 +00009441
9442/*
9443 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009444 * Return a range (from start to end) of lines in rettv from the specified
9445 * buffer.
9446 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009447 */
9448 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009449get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009450 buf_T *buf;
9451 linenr_T start;
9452 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009453 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009454 typval_T *rettv;
9455{
9456 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009457
Bram Moolenaar342337a2005-07-21 21:11:17 +00009458 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009459 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009460 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009461 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009462 }
9463 else
9464 rettv->vval.v_number = 0;
9465
9466 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9467 return;
9468
9469 if (!retlist)
9470 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009471 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9472 p = ml_get_buf(buf, start, FALSE);
9473 else
9474 p = (char_u *)"";
9475
9476 rettv->v_type = VAR_STRING;
9477 rettv->vval.v_string = vim_strsave(p);
9478 }
9479 else
9480 {
9481 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009482 return;
9483
9484 if (start < 1)
9485 start = 1;
9486 if (end > buf->b_ml.ml_line_count)
9487 end = buf->b_ml.ml_line_count;
9488 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009489 if (list_append_string(rettv->vval.v_list,
9490 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009491 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009492 }
9493}
9494
9495/*
9496 * "getbufline()" function
9497 */
9498 static void
9499f_getbufline(argvars, rettv)
9500 typval_T *argvars;
9501 typval_T *rettv;
9502{
9503 linenr_T lnum;
9504 linenr_T end;
9505 buf_T *buf;
9506
9507 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9508 ++emsg_off;
9509 buf = get_buf_tv(&argvars[0]);
9510 --emsg_off;
9511
Bram Moolenaar661b1822005-07-28 22:36:45 +00009512 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009513 if (argvars[2].v_type == VAR_UNKNOWN)
9514 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009515 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009516 end = get_tv_lnum_buf(&argvars[2], buf);
9517
Bram Moolenaar342337a2005-07-21 21:11:17 +00009518 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009519}
9520
Bram Moolenaar0d660222005-01-07 21:51:51 +00009521/*
9522 * "getbufvar()" function
9523 */
9524 static void
9525f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009526 typval_T *argvars;
9527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009528{
9529 buf_T *buf;
9530 buf_T *save_curbuf;
9531 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009532 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9535 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009536 ++emsg_off;
9537 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538
9539 rettv->v_type = VAR_STRING;
9540 rettv->vval.v_string = NULL;
9541
9542 if (buf != NULL && varname != NULL)
9543 {
9544 if (*varname == '&') /* buffer-local-option */
9545 {
9546 /* set curbuf to be our buf, temporarily */
9547 save_curbuf = curbuf;
9548 curbuf = buf;
9549
9550 get_option_tv(&varname, rettv, TRUE);
9551
9552 /* restore previous notion of curbuf */
9553 curbuf = save_curbuf;
9554 }
9555 else
9556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 if (*varname == NUL)
9558 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9559 * scope prefix before the NUL byte is required by
9560 * find_var_in_ht(). */
9561 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009562 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009563 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009564 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009565 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009566 }
9567 }
9568
9569 --emsg_off;
9570}
9571
9572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 * "getchar()" function
9574 */
9575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009577 typval_T *argvars;
9578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582
9583 ++no_mapping;
9584 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009585 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 /* getchar(): blocking wait. */
9587 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 /* getchar(1): only check if char avail */
9590 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009591 else if (error || vpeekc() == NUL)
9592 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 n = 0;
9594 else
9595 /* getchar(0) and char avail: return char */
9596 n = safe_vgetc();
9597 --no_mapping;
9598 --allow_keys;
9599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 if (IS_SPECIAL(n) || mod_mask != 0)
9602 {
9603 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9604 int i = 0;
9605
9606 /* Turn a special key into three bytes, plus modifier. */
9607 if (mod_mask != 0)
9608 {
9609 temp[i++] = K_SPECIAL;
9610 temp[i++] = KS_MODIFIER;
9611 temp[i++] = mod_mask;
9612 }
9613 if (IS_SPECIAL(n))
9614 {
9615 temp[i++] = K_SPECIAL;
9616 temp[i++] = K_SECOND(n);
9617 temp[i++] = K_THIRD(n);
9618 }
9619#ifdef FEAT_MBYTE
9620 else if (has_mbyte)
9621 i += (*mb_char2bytes)(n, temp + i);
9622#endif
9623 else
9624 temp[i++] = n;
9625 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->v_type = VAR_STRING;
9627 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 }
9629}
9630
9631/*
9632 * "getcharmod()" function
9633 */
9634/*ARGSUSED*/
9635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 typval_T *argvars;
9638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641}
9642
9643/*
9644 * "getcmdline()" function
9645 */
9646/*ARGSUSED*/
9647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009649 typval_T *argvars;
9650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 rettv->v_type = VAR_STRING;
9653 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654}
9655
9656/*
9657 * "getcmdpos()" function
9658 */
9659/*ARGSUSED*/
9660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009662 typval_T *argvars;
9663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009665 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666}
9667
9668/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00009669 * "getcmdtype()" function
9670 */
9671/*ARGSUSED*/
9672 static void
9673f_getcmdtype(argvars, rettv)
9674 typval_T *argvars;
9675 typval_T *rettv;
9676{
9677 rettv->v_type = VAR_STRING;
9678 rettv->vval.v_string = alloc(2);
9679 if (rettv->vval.v_string != NULL)
9680 {
9681 rettv->vval.v_string[0] = get_cmdline_type();
9682 rettv->vval.v_string[1] = NUL;
9683 }
9684}
9685
9686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 * "getcwd()" function
9688 */
9689/*ARGSUSED*/
9690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009692 typval_T *argvars;
9693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695 char_u cwd[MAXPATHL];
9696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 else
9701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009704 if (rettv->vval.v_string != NULL)
9705 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706#endif
9707 }
9708}
9709
9710/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009711 * "getfontname()" function
9712 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009713/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009716 typval_T *argvars;
9717 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009718{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->v_type = VAR_STRING;
9720 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009721#ifdef FEAT_GUI
9722 if (gui.in_use)
9723 {
9724 GuiFont font;
9725 char_u *name = NULL;
9726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009727 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009728 {
9729 /* Get the "Normal" font. Either the name saved by
9730 * hl_set_font_name() or from the font ID. */
9731 font = gui.norm_font;
9732 name = hl_get_font_name();
9733 }
9734 else
9735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009737 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9738 return;
9739 font = gui_mch_get_font(name, FALSE);
9740 if (font == NOFONT)
9741 return; /* Invalid font name, return empty string. */
9742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009744 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009745 gui_mch_free_font(font);
9746 }
9747#endif
9748}
9749
9750/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009751 * "getfperm({fname})" function
9752 */
9753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009755 typval_T *argvars;
9756 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009757{
9758 char_u *fname;
9759 struct stat st;
9760 char_u *perm = NULL;
9761 char_u flags[] = "rwx";
9762 int i;
9763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009767 if (mch_stat((char *)fname, &st) >= 0)
9768 {
9769 perm = vim_strsave((char_u *)"---------");
9770 if (perm != NULL)
9771 {
9772 for (i = 0; i < 9; i++)
9773 {
9774 if (st.st_mode & (1 << (8 - i)))
9775 perm[i] = flags[i % 3];
9776 }
9777 }
9778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009780}
9781
9782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 * "getfsize({fname})" function
9784 */
9785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009787 typval_T *argvars;
9788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
9790 char_u *fname;
9791 struct stat st;
9792
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796
9797 if (mch_stat((char *)fname, &st) >= 0)
9798 {
9799 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 }
9804 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806}
9807
9808/*
9809 * "getftime({fname})" function
9810 */
9811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009813 typval_T *argvars;
9814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816 char_u *fname;
9817 struct stat st;
9818
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820
9821 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009824 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825}
9826
9827/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009828 * "getftype({fname})" function
9829 */
9830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009832 typval_T *argvars;
9833 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009834{
9835 char_u *fname;
9836 struct stat st;
9837 char_u *type = NULL;
9838 char *t;
9839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009841
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009842 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009843 if (mch_lstat((char *)fname, &st) >= 0)
9844 {
9845#ifdef S_ISREG
9846 if (S_ISREG(st.st_mode))
9847 t = "file";
9848 else if (S_ISDIR(st.st_mode))
9849 t = "dir";
9850# ifdef S_ISLNK
9851 else if (S_ISLNK(st.st_mode))
9852 t = "link";
9853# endif
9854# ifdef S_ISBLK
9855 else if (S_ISBLK(st.st_mode))
9856 t = "bdev";
9857# endif
9858# ifdef S_ISCHR
9859 else if (S_ISCHR(st.st_mode))
9860 t = "cdev";
9861# endif
9862# ifdef S_ISFIFO
9863 else if (S_ISFIFO(st.st_mode))
9864 t = "fifo";
9865# endif
9866# ifdef S_ISSOCK
9867 else if (S_ISSOCK(st.st_mode))
9868 t = "fifo";
9869# endif
9870 else
9871 t = "other";
9872#else
9873# ifdef S_IFMT
9874 switch (st.st_mode & S_IFMT)
9875 {
9876 case S_IFREG: t = "file"; break;
9877 case S_IFDIR: t = "dir"; break;
9878# ifdef S_IFLNK
9879 case S_IFLNK: t = "link"; break;
9880# endif
9881# ifdef S_IFBLK
9882 case S_IFBLK: t = "bdev"; break;
9883# endif
9884# ifdef S_IFCHR
9885 case S_IFCHR: t = "cdev"; break;
9886# endif
9887# ifdef S_IFIFO
9888 case S_IFIFO: t = "fifo"; break;
9889# endif
9890# ifdef S_IFSOCK
9891 case S_IFSOCK: t = "socket"; break;
9892# endif
9893 default: t = "other";
9894 }
9895# else
9896 if (mch_isdir(fname))
9897 t = "dir";
9898 else
9899 t = "file";
9900# endif
9901#endif
9902 type = vim_strsave((char_u *)t);
9903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009905}
9906
9907/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009908 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009909 */
9910 static void
9911f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009912 typval_T *argvars;
9913 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009914{
9915 linenr_T lnum;
9916 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009917 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009918
9919 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009920 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009921 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009922 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009923 retlist = FALSE;
9924 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009925 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009926 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009927 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009928 retlist = TRUE;
9929 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009930
Bram Moolenaar342337a2005-07-21 21:11:17 +00009931 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009932}
9933
9934/*
Bram Moolenaara5525202006-03-02 22:52:09 +00009935 * "getpos(string)" function
9936 */
9937 static void
9938f_getpos(argvars, rettv)
9939 typval_T *argvars;
9940 typval_T *rettv;
9941{
9942 pos_T *fp;
9943 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009944 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009945
9946 if (rettv_list_alloc(rettv) == OK)
9947 {
9948 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009949 fp = var2fpos(&argvars[0], TRUE, &fnum);
9950 if (fnum != -1)
9951 list_append_number(l, (varnumber_T)fnum);
9952 else
9953 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +00009954 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
9955 : (varnumber_T)0);
9956 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->col + 1
9957 : (varnumber_T)0);
9958 list_append_number(l,
9959#ifdef FEAT_VIRTUALEDIT
9960 (fp != NULL) ? (varnumber_T)fp->coladd :
9961#endif
9962 (varnumber_T)0);
9963 }
9964 else
9965 rettv->vval.v_number = FALSE;
9966}
9967
9968/*
Bram Moolenaar280f1262006-01-30 00:14:18 +00009969 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +00009970 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00009971/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +00009972 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +00009973f_getqflist(argvars, rettv)
9974 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009975 typval_T *rettv;
9976{
9977#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +00009978 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +00009979#endif
9980
9981 rettv->vval.v_number = FALSE;
9982#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009983 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +00009984 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00009985 wp = NULL;
9986 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
9987 {
9988 wp = find_win_by_nr(&argvars[0]);
9989 if (wp == NULL)
9990 return;
9991 }
9992
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009993 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009994 }
9995#endif
9996}
9997
9998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 * "getreg()" function
10000 */
10001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010003 typval_T *argvars;
10004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 char_u *strregname;
10007 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010008 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010011 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013 strregname = get_tv_string_chk(&argvars[0]);
10014 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010015 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010016 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010019 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 regname = (strregname == NULL ? '"' : *strregname);
10021 if (regname == 0)
10022 regname = '"';
10023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 rettv->vval.v_string = error ? NULL :
10026 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027}
10028
10029/*
10030 * "getregtype()" function
10031 */
10032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010034 typval_T *argvars;
10035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 char_u *strregname;
10038 int regname;
10039 char_u buf[NUMBUFLEN + 2];
10040 long reglen = 0;
10041
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010042 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 {
10044 strregname = get_tv_string_chk(&argvars[0]);
10045 if (strregname == NULL) /* type error; errmsg already given */
10046 {
10047 rettv->v_type = VAR_STRING;
10048 rettv->vval.v_string = NULL;
10049 return;
10050 }
10051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 else
10053 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010054 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055
10056 regname = (strregname == NULL ? '"' : *strregname);
10057 if (regname == 0)
10058 regname = '"';
10059
10060 buf[0] = NUL;
10061 buf[1] = NUL;
10062 switch (get_reg_type(regname, &reglen))
10063 {
10064 case MLINE: buf[0] = 'V'; break;
10065 case MCHAR: buf[0] = 'v'; break;
10066#ifdef FEAT_VISUAL
10067 case MBLOCK:
10068 buf[0] = Ctrl_V;
10069 sprintf((char *)buf + 1, "%ld", reglen + 1);
10070 break;
10071#endif
10072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->v_type = VAR_STRING;
10074 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
10077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 * "getwinposx()" function
10079 */
10080/*ARGSUSED*/
10081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010083 typval_T *argvars;
10084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087#ifdef FEAT_GUI
10088 if (gui.in_use)
10089 {
10090 int x, y;
10091
10092 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 }
10095#endif
10096}
10097
10098/*
10099 * "getwinposy()" function
10100 */
10101/*ARGSUSED*/
10102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010104 typval_T *argvars;
10105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108#ifdef FEAT_GUI
10109 if (gui.in_use)
10110 {
10111 int x, y;
10112
10113 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 }
10116#endif
10117}
10118
Bram Moolenaara40058a2005-07-11 22:42:07 +000010119 static win_T *
10120find_win_by_nr(vp)
10121 typval_T *vp;
10122{
10123#ifdef FEAT_WINDOWS
10124 win_T *wp;
10125#endif
10126 int nr;
10127
10128 nr = get_tv_number_chk(vp, NULL);
10129
10130#ifdef FEAT_WINDOWS
10131 if (nr < 0)
10132 return NULL;
10133 if (nr == 0)
10134 return curwin;
10135
10136 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10137 if (--nr <= 0)
10138 break;
10139 return wp;
10140#else
10141 if (nr == 0 || nr == 1)
10142 return curwin;
10143 return NULL;
10144#endif
10145}
10146
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147/*
10148 * "getwinvar()" function
10149 */
10150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010152 typval_T *argvars;
10153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154{
10155 win_T *win, *oldcurwin;
10156 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 varname = get_tv_string_chk(&argvars[1]);
10161 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 rettv->v_type = VAR_STRING;
10164 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165
10166 if (win != NULL && varname != NULL)
10167 {
10168 if (*varname == '&') /* window-local-option */
10169 {
Bram Moolenaarc0761132005-03-18 20:30:32 +000010170 /* Set curwin to be our win, temporarily. Also set curbuf, so
10171 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 oldcurwin = curwin;
10173 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010174 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177
10178 /* restore previous notion of curwin */
10179 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +000010180 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 }
10182 else
10183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 if (*varname == NUL)
10185 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10186 * scope prefix before the NUL byte is required by
10187 * find_var_in_ht(). */
10188 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010190 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010192 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 }
10194 }
10195
10196 --emsg_off;
10197}
10198
10199/*
10200 * "glob()" function
10201 */
10202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010203f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010204 typval_T *argvars;
10205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206{
10207 expand_T xpc;
10208
10209 ExpandInit(&xpc);
10210 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211 rettv->v_type = VAR_STRING;
10212 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
10214 ExpandCleanup(&xpc);
10215}
10216
10217/*
10218 * "globpath()" function
10219 */
10220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010221f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 typval_T *argvars;
10223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224{
10225 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 if (file == NULL)
10230 rettv->vval.v_string = NULL;
10231 else
10232 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233}
10234
10235/*
10236 * "has()" function
10237 */
10238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010240 typval_T *argvars;
10241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242{
10243 int i;
10244 char_u *name;
10245 int n = FALSE;
10246 static char *(has_list[]) =
10247 {
10248#ifdef AMIGA
10249 "amiga",
10250# ifdef FEAT_ARP
10251 "arp",
10252# endif
10253#endif
10254#ifdef __BEOS__
10255 "beos",
10256#endif
10257#ifdef MSDOS
10258# ifdef DJGPP
10259 "dos32",
10260# else
10261 "dos16",
10262# endif
10263#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010264#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 "mac",
10266#endif
10267#if defined(MACOS_X_UNIX)
10268 "macunix",
10269#endif
10270#ifdef OS2
10271 "os2",
10272#endif
10273#ifdef __QNX__
10274 "qnx",
10275#endif
10276#ifdef RISCOS
10277 "riscos",
10278#endif
10279#ifdef UNIX
10280 "unix",
10281#endif
10282#ifdef VMS
10283 "vms",
10284#endif
10285#ifdef WIN16
10286 "win16",
10287#endif
10288#ifdef WIN32
10289 "win32",
10290#endif
10291#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10292 "win32unix",
10293#endif
10294#ifdef WIN64
10295 "win64",
10296#endif
10297#ifdef EBCDIC
10298 "ebcdic",
10299#endif
10300#ifndef CASE_INSENSITIVE_FILENAME
10301 "fname_case",
10302#endif
10303#ifdef FEAT_ARABIC
10304 "arabic",
10305#endif
10306#ifdef FEAT_AUTOCMD
10307 "autocmd",
10308#endif
10309#ifdef FEAT_BEVAL
10310 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010311# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10312 "balloon_multiline",
10313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314#endif
10315#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10316 "builtin_terms",
10317# ifdef ALL_BUILTIN_TCAPS
10318 "all_builtin_terms",
10319# endif
10320#endif
10321#ifdef FEAT_BYTEOFF
10322 "byte_offset",
10323#endif
10324#ifdef FEAT_CINDENT
10325 "cindent",
10326#endif
10327#ifdef FEAT_CLIENTSERVER
10328 "clientserver",
10329#endif
10330#ifdef FEAT_CLIPBOARD
10331 "clipboard",
10332#endif
10333#ifdef FEAT_CMDL_COMPL
10334 "cmdline_compl",
10335#endif
10336#ifdef FEAT_CMDHIST
10337 "cmdline_hist",
10338#endif
10339#ifdef FEAT_COMMENTS
10340 "comments",
10341#endif
10342#ifdef FEAT_CRYPT
10343 "cryptv",
10344#endif
10345#ifdef FEAT_CSCOPE
10346 "cscope",
10347#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010348#ifdef CURSOR_SHAPE
10349 "cursorshape",
10350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351#ifdef DEBUG
10352 "debug",
10353#endif
10354#ifdef FEAT_CON_DIALOG
10355 "dialog_con",
10356#endif
10357#ifdef FEAT_GUI_DIALOG
10358 "dialog_gui",
10359#endif
10360#ifdef FEAT_DIFF
10361 "diff",
10362#endif
10363#ifdef FEAT_DIGRAPHS
10364 "digraphs",
10365#endif
10366#ifdef FEAT_DND
10367 "dnd",
10368#endif
10369#ifdef FEAT_EMACS_TAGS
10370 "emacs_tags",
10371#endif
10372 "eval", /* always present, of course! */
10373#ifdef FEAT_EX_EXTRA
10374 "ex_extra",
10375#endif
10376#ifdef FEAT_SEARCH_EXTRA
10377 "extra_search",
10378#endif
10379#ifdef FEAT_FKMAP
10380 "farsi",
10381#endif
10382#ifdef FEAT_SEARCHPATH
10383 "file_in_path",
10384#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010385#if defined(UNIX) && !defined(USE_SYSTEM)
10386 "filterpipe",
10387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388#ifdef FEAT_FIND_ID
10389 "find_in_path",
10390#endif
10391#ifdef FEAT_FOLDING
10392 "folding",
10393#endif
10394#ifdef FEAT_FOOTER
10395 "footer",
10396#endif
10397#if !defined(USE_SYSTEM) && defined(UNIX)
10398 "fork",
10399#endif
10400#ifdef FEAT_GETTEXT
10401 "gettext",
10402#endif
10403#ifdef FEAT_GUI
10404 "gui",
10405#endif
10406#ifdef FEAT_GUI_ATHENA
10407# ifdef FEAT_GUI_NEXTAW
10408 "gui_neXtaw",
10409# else
10410 "gui_athena",
10411# endif
10412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413#ifdef FEAT_GUI_GTK
10414 "gui_gtk",
10415# ifdef HAVE_GTK2
10416 "gui_gtk2",
10417# endif
10418#endif
10419#ifdef FEAT_GUI_MAC
10420 "gui_mac",
10421#endif
10422#ifdef FEAT_GUI_MOTIF
10423 "gui_motif",
10424#endif
10425#ifdef FEAT_GUI_PHOTON
10426 "gui_photon",
10427#endif
10428#ifdef FEAT_GUI_W16
10429 "gui_win16",
10430#endif
10431#ifdef FEAT_GUI_W32
10432 "gui_win32",
10433#endif
10434#ifdef FEAT_HANGULIN
10435 "hangul_input",
10436#endif
10437#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10438 "iconv",
10439#endif
10440#ifdef FEAT_INS_EXPAND
10441 "insert_expand",
10442#endif
10443#ifdef FEAT_JUMPLIST
10444 "jumplist",
10445#endif
10446#ifdef FEAT_KEYMAP
10447 "keymap",
10448#endif
10449#ifdef FEAT_LANGMAP
10450 "langmap",
10451#endif
10452#ifdef FEAT_LIBCALL
10453 "libcall",
10454#endif
10455#ifdef FEAT_LINEBREAK
10456 "linebreak",
10457#endif
10458#ifdef FEAT_LISP
10459 "lispindent",
10460#endif
10461#ifdef FEAT_LISTCMDS
10462 "listcmds",
10463#endif
10464#ifdef FEAT_LOCALMAP
10465 "localmap",
10466#endif
10467#ifdef FEAT_MENU
10468 "menu",
10469#endif
10470#ifdef FEAT_SESSION
10471 "mksession",
10472#endif
10473#ifdef FEAT_MODIFY_FNAME
10474 "modify_fname",
10475#endif
10476#ifdef FEAT_MOUSE
10477 "mouse",
10478#endif
10479#ifdef FEAT_MOUSESHAPE
10480 "mouseshape",
10481#endif
10482#if defined(UNIX) || defined(VMS)
10483# ifdef FEAT_MOUSE_DEC
10484 "mouse_dec",
10485# endif
10486# ifdef FEAT_MOUSE_GPM
10487 "mouse_gpm",
10488# endif
10489# ifdef FEAT_MOUSE_JSB
10490 "mouse_jsbterm",
10491# endif
10492# ifdef FEAT_MOUSE_NET
10493 "mouse_netterm",
10494# endif
10495# ifdef FEAT_MOUSE_PTERM
10496 "mouse_pterm",
10497# endif
10498# ifdef FEAT_MOUSE_XTERM
10499 "mouse_xterm",
10500# endif
10501#endif
10502#ifdef FEAT_MBYTE
10503 "multi_byte",
10504#endif
10505#ifdef FEAT_MBYTE_IME
10506 "multi_byte_ime",
10507#endif
10508#ifdef FEAT_MULTI_LANG
10509 "multi_lang",
10510#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010511#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010512#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010513 "mzscheme",
10514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516#ifdef FEAT_OLE
10517 "ole",
10518#endif
10519#ifdef FEAT_OSFILETYPE
10520 "osfiletype",
10521#endif
10522#ifdef FEAT_PATH_EXTRA
10523 "path_extra",
10524#endif
10525#ifdef FEAT_PERL
10526#ifndef DYNAMIC_PERL
10527 "perl",
10528#endif
10529#endif
10530#ifdef FEAT_PYTHON
10531#ifndef DYNAMIC_PYTHON
10532 "python",
10533#endif
10534#endif
10535#ifdef FEAT_POSTSCRIPT
10536 "postscript",
10537#endif
10538#ifdef FEAT_PRINTER
10539 "printer",
10540#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010541#ifdef FEAT_PROFILE
10542 "profile",
10543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544#ifdef FEAT_QUICKFIX
10545 "quickfix",
10546#endif
10547#ifdef FEAT_RIGHTLEFT
10548 "rightleft",
10549#endif
10550#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10551 "ruby",
10552#endif
10553#ifdef FEAT_SCROLLBIND
10554 "scrollbind",
10555#endif
10556#ifdef FEAT_CMDL_INFO
10557 "showcmd",
10558 "cmdline_info",
10559#endif
10560#ifdef FEAT_SIGNS
10561 "signs",
10562#endif
10563#ifdef FEAT_SMARTINDENT
10564 "smartindent",
10565#endif
10566#ifdef FEAT_SNIFF
10567 "sniff",
10568#endif
10569#ifdef FEAT_STL_OPT
10570 "statusline",
10571#endif
10572#ifdef FEAT_SUN_WORKSHOP
10573 "sun_workshop",
10574#endif
10575#ifdef FEAT_NETBEANS_INTG
10576 "netbeans_intg",
10577#endif
10578#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010579 "spell",
10580#endif
10581#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 "syntax",
10583#endif
10584#if defined(USE_SYSTEM) || !defined(UNIX)
10585 "system",
10586#endif
10587#ifdef FEAT_TAG_BINS
10588 "tag_binary",
10589#endif
10590#ifdef FEAT_TAG_OLDSTATIC
10591 "tag_old_static",
10592#endif
10593#ifdef FEAT_TAG_ANYWHITE
10594 "tag_any_white",
10595#endif
10596#ifdef FEAT_TCL
10597# ifndef DYNAMIC_TCL
10598 "tcl",
10599# endif
10600#endif
10601#ifdef TERMINFO
10602 "terminfo",
10603#endif
10604#ifdef FEAT_TERMRESPONSE
10605 "termresponse",
10606#endif
10607#ifdef FEAT_TEXTOBJ
10608 "textobjects",
10609#endif
10610#ifdef HAVE_TGETENT
10611 "tgetent",
10612#endif
10613#ifdef FEAT_TITLE
10614 "title",
10615#endif
10616#ifdef FEAT_TOOLBAR
10617 "toolbar",
10618#endif
10619#ifdef FEAT_USR_CMDS
10620 "user-commands", /* was accidentally included in 5.4 */
10621 "user_commands",
10622#endif
10623#ifdef FEAT_VIMINFO
10624 "viminfo",
10625#endif
10626#ifdef FEAT_VERTSPLIT
10627 "vertsplit",
10628#endif
10629#ifdef FEAT_VIRTUALEDIT
10630 "virtualedit",
10631#endif
10632#ifdef FEAT_VISUAL
10633 "visual",
10634#endif
10635#ifdef FEAT_VISUALEXTRA
10636 "visualextra",
10637#endif
10638#ifdef FEAT_VREPLACE
10639 "vreplace",
10640#endif
10641#ifdef FEAT_WILDIGN
10642 "wildignore",
10643#endif
10644#ifdef FEAT_WILDMENU
10645 "wildmenu",
10646#endif
10647#ifdef FEAT_WINDOWS
10648 "windows",
10649#endif
10650#ifdef FEAT_WAK
10651 "winaltkeys",
10652#endif
10653#ifdef FEAT_WRITEBACKUP
10654 "writebackup",
10655#endif
10656#ifdef FEAT_XIM
10657 "xim",
10658#endif
10659#ifdef FEAT_XFONTSET
10660 "xfontset",
10661#endif
10662#ifdef USE_XSMP
10663 "xsmp",
10664#endif
10665#ifdef USE_XSMP_INTERACT
10666 "xsmp_interact",
10667#endif
10668#ifdef FEAT_XCLIPBOARD
10669 "xterm_clipboard",
10670#endif
10671#ifdef FEAT_XTERM_SAVE
10672 "xterm_save",
10673#endif
10674#if defined(UNIX) && defined(FEAT_X11)
10675 "X11",
10676#endif
10677 NULL
10678 };
10679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010680 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 for (i = 0; has_list[i] != NULL; ++i)
10682 if (STRICMP(name, has_list[i]) == 0)
10683 {
10684 n = TRUE;
10685 break;
10686 }
10687
10688 if (n == FALSE)
10689 {
10690 if (STRNICMP(name, "patch", 5) == 0)
10691 n = has_patch(atoi((char *)name + 5));
10692 else if (STRICMP(name, "vim_starting") == 0)
10693 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010694#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10695 else if (STRICMP(name, "balloon_multiline") == 0)
10696 n = multiline_balloon_available();
10697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#ifdef DYNAMIC_TCL
10699 else if (STRICMP(name, "tcl") == 0)
10700 n = tcl_enabled(FALSE);
10701#endif
10702#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10703 else if (STRICMP(name, "iconv") == 0)
10704 n = iconv_enabled(FALSE);
10705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010706#ifdef DYNAMIC_MZSCHEME
10707 else if (STRICMP(name, "mzscheme") == 0)
10708 n = mzscheme_enabled(FALSE);
10709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710#ifdef DYNAMIC_RUBY
10711 else if (STRICMP(name, "ruby") == 0)
10712 n = ruby_enabled(FALSE);
10713#endif
10714#ifdef DYNAMIC_PYTHON
10715 else if (STRICMP(name, "python") == 0)
10716 n = python_enabled(FALSE);
10717#endif
10718#ifdef DYNAMIC_PERL
10719 else if (STRICMP(name, "perl") == 0)
10720 n = perl_enabled(FALSE);
10721#endif
10722#ifdef FEAT_GUI
10723 else if (STRICMP(name, "gui_running") == 0)
10724 n = (gui.in_use || gui.starting);
10725# ifdef FEAT_GUI_W32
10726 else if (STRICMP(name, "gui_win32s") == 0)
10727 n = gui_is_win32s();
10728# endif
10729# ifdef FEAT_BROWSE
10730 else if (STRICMP(name, "browse") == 0)
10731 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10732# endif
10733#endif
10734#ifdef FEAT_SYN_HL
10735 else if (STRICMP(name, "syntax_items") == 0)
10736 n = syntax_present(curbuf);
10737#endif
10738#if defined(WIN3264)
10739 else if (STRICMP(name, "win95") == 0)
10740 n = mch_windows95();
10741#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010742#ifdef FEAT_NETBEANS_INTG
10743 else if (STRICMP(name, "netbeans_enabled") == 0)
10744 n = usingNetbeans;
10745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 }
10747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749}
10750
10751/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010752 * "has_key()" function
10753 */
10754 static void
10755f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010756 typval_T *argvars;
10757 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010758{
10759 rettv->vval.v_number = 0;
10760 if (argvars[0].v_type != VAR_DICT)
10761 {
10762 EMSG(_(e_dictreq));
10763 return;
10764 }
10765 if (argvars[0].vval.v_dict == NULL)
10766 return;
10767
10768 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010769 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010770}
10771
10772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 * "hasmapto()" function
10774 */
10775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010777 typval_T *argvars;
10778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779{
10780 char_u *name;
10781 char_u *mode;
10782 char_u buf[NUMBUFLEN];
10783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010785 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 mode = (char_u *)"nvo";
10787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
10790 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794}
10795
10796/*
10797 * "histadd()" function
10798 */
10799/*ARGSUSED*/
10800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010802 typval_T *argvars;
10803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804{
10805#ifdef FEAT_CMDHIST
10806 int histype;
10807 char_u *str;
10808 char_u buf[NUMBUFLEN];
10809#endif
10810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 if (check_restricted() || check_secure())
10813 return;
10814#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010815 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10816 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 if (histype >= 0)
10818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 if (*str != NUL)
10821 {
10822 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 return;
10825 }
10826 }
10827#endif
10828}
10829
10830/*
10831 * "histdel()" function
10832 */
10833/*ARGSUSED*/
10834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 typval_T *argvars;
10837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
10839#ifdef FEAT_CMDHIST
10840 int n;
10841 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10845 if (str == NULL)
10846 n = 0;
10847 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010850 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010852 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 else
10855 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010857 get_tv_string_buf(&argvars[1], buf));
10858 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861#endif
10862}
10863
10864/*
10865 * "histget()" function
10866 */
10867/*ARGSUSED*/
10868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *argvars;
10871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872{
10873#ifdef FEAT_CMDHIST
10874 int type;
10875 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10879 if (str == NULL)
10880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 {
10883 type = get_histtype(str);
10884 if (argvars[1].v_type == VAR_UNKNOWN)
10885 idx = get_history_idx(type);
10886 else
10887 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10888 /* -1 on type error */
10889 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895}
10896
10897/*
10898 * "histnr()" function
10899 */
10900/*ARGSUSED*/
10901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *argvars;
10904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905{
10906 int i;
10907
10908#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 char_u *history = get_tv_string_chk(&argvars[0]);
10910
10911 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (i >= HIST_CMD && i < HIST_COUNT)
10913 i = get_history_idx(i);
10914 else
10915#endif
10916 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918}
10919
10920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 * "highlightID(name)" function
10922 */
10923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010925 typval_T *argvars;
10926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929}
10930
10931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010932 * "highlight_exists()" function
10933 */
10934 static void
10935f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *argvars;
10937 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010938{
10939 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10940}
10941
10942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 * "hostname()" function
10944 */
10945/*ARGSUSED*/
10946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010947f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 typval_T *argvars;
10949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950{
10951 char_u hostname[256];
10952
10953 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->v_type = VAR_STRING;
10955 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956}
10957
10958/*
10959 * iconv() function
10960 */
10961/*ARGSUSED*/
10962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010964 typval_T *argvars;
10965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
10967#ifdef FEAT_MBYTE
10968 char_u buf1[NUMBUFLEN];
10969 char_u buf2[NUMBUFLEN];
10970 char_u *from, *to, *str;
10971 vimconv_T vimconv;
10972#endif
10973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 rettv->v_type = VAR_STRING;
10975 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976
10977#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 str = get_tv_string(&argvars[0]);
10979 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10980 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 vimconv.vc_type = CONV_NONE;
10982 convert_setup(&vimconv, from, to);
10983
10984 /* If the encodings are equal, no conversion needed. */
10985 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989
10990 convert_setup(&vimconv, NULL, NULL);
10991 vim_free(from);
10992 vim_free(to);
10993#endif
10994}
10995
10996/*
10997 * "indent()" function
10998 */
10999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011001 typval_T *argvars;
11002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003{
11004 linenr_T lnum;
11005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011}
11012
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011013/*
11014 * "index()" function
11015 */
11016 static void
11017f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *argvars;
11019 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011020{
Bram Moolenaar33570922005-01-25 22:26:29 +000011021 list_T *l;
11022 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011023 long idx = 0;
11024 int ic = FALSE;
11025
11026 rettv->vval.v_number = -1;
11027 if (argvars[0].v_type != VAR_LIST)
11028 {
11029 EMSG(_(e_listreq));
11030 return;
11031 }
11032 l = argvars[0].vval.v_list;
11033 if (l != NULL)
11034 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011035 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011036 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011038 int error = FALSE;
11039
Bram Moolenaar758711c2005-02-02 23:11:38 +000011040 /* Start at specified item. Use the cached index that list_find()
11041 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011043 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011044 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 ic = get_tv_number_chk(&argvars[3], &error);
11046 if (error)
11047 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011048 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011049
Bram Moolenaar758711c2005-02-02 23:11:38 +000011050 for ( ; item != NULL; item = item->li_next, ++idx)
11051 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011052 {
11053 rettv->vval.v_number = idx;
11054 break;
11055 }
11056 }
11057}
11058
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059static int inputsecret_flag = 0;
11060
11061/*
11062 * "input()" function
11063 * Also handles inputsecret() when inputsecret is set.
11064 */
11065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011067 typval_T *argvars;
11068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 char_u *p = NULL;
11072 int c;
11073 char_u buf[NUMBUFLEN];
11074 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011076 int xp_type = EXPAND_NOTHING;
11077 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080
11081#ifdef NO_CONSOLE_INPUT
11082 /* While starting up, there is no place to enter text. */
11083 if (no_console_input())
11084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 return;
11087 }
11088#endif
11089
11090 cmd_silent = FALSE; /* Want to see the prompt. */
11091 if (prompt != NULL)
11092 {
11093 /* Only the part of the message after the last NL is considered as
11094 * prompt for the command line */
11095 p = vim_strrchr(prompt, '\n');
11096 if (p == NULL)
11097 p = prompt;
11098 else
11099 {
11100 ++p;
11101 c = *p;
11102 *p = NUL;
11103 msg_start();
11104 msg_clr_eos();
11105 msg_puts_attr(prompt, echo_attr);
11106 msg_didout = FALSE;
11107 msg_starthere();
11108 *p = c;
11109 }
11110 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 if (argvars[1].v_type != VAR_UNKNOWN)
11113 {
11114 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11115 if (defstr != NULL)
11116 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117
Bram Moolenaar4463f292005-09-25 22:20:24 +000011118 if (argvars[2].v_type != VAR_UNKNOWN)
11119 {
11120 char_u *xp_name;
11121 int xp_namelen;
11122 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011123
Bram Moolenaar4463f292005-09-25 22:20:24 +000011124 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011125
Bram Moolenaar4463f292005-09-25 22:20:24 +000011126 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11127 if (xp_name == NULL)
11128 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011129
Bram Moolenaar4463f292005-09-25 22:20:24 +000011130 xp_namelen = STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011131
Bram Moolenaar4463f292005-09-25 22:20:24 +000011132 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11133 &xp_arg) == FAIL)
11134 return;
11135 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011136 }
11137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011138 if (defstr != NULL)
11139 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011140 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11141 xp_type, xp_arg);
11142
11143 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 /* since the user typed this, no need to wait for return */
11146 need_wait_return = FALSE;
11147 msg_didout = FALSE;
11148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 cmd_silent = cmd_silent_save;
11150}
11151
11152/*
11153 * "inputdialog()" function
11154 */
11155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159{
11160#if defined(FEAT_GUI_TEXTDIALOG)
11161 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11162 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11163 {
11164 char_u *message;
11165 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011168 message = get_tv_string_chk(&argvars[0]);
11169 if (argvars[1].v_type != VAR_UNKNOWN
11170 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011171 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 else
11173 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174 if (message != NULL && defstr != NULL
11175 && do_dialog(VIM_QUESTION, NULL, message,
11176 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 else
11179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180 if (message != NULL && defstr != NULL
11181 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011182 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183 rettv->vval.v_string = vim_strsave(
11184 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011188 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 }
11190 else
11191#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193}
11194
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011195/*
11196 * "inputlist()" function
11197 */
11198 static void
11199f_inputlist(argvars, rettv)
11200 typval_T *argvars;
11201 typval_T *rettv;
11202{
11203 listitem_T *li;
11204 int selected;
11205 int mouse_used;
11206
11207 rettv->vval.v_number = 0;
11208#ifdef NO_CONSOLE_INPUT
11209 /* While starting up, there is no place to enter text. */
11210 if (no_console_input())
11211 return;
11212#endif
11213 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11214 {
11215 EMSG2(_(e_listarg), "inputlist()");
11216 return;
11217 }
11218
11219 msg_start();
11220 lines_left = Rows; /* avoid more prompt */
11221 msg_scroll = TRUE;
11222 msg_clr_eos();
11223
11224 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11225 {
11226 msg_puts(get_tv_string(&li->li_tv));
11227 msg_putchar('\n');
11228 }
11229
11230 /* Ask for choice. */
11231 selected = prompt_for_number(&mouse_used);
11232 if (mouse_used)
11233 selected -= lines_left;
11234
11235 rettv->vval.v_number = selected;
11236}
11237
11238
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11240
11241/*
11242 * "inputrestore()" function
11243 */
11244/*ARGSUSED*/
11245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011246f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011247 typval_T *argvars;
11248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249{
11250 if (ga_userinput.ga_len > 0)
11251 {
11252 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11254 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 }
11257 else if (p_verbose > 1)
11258 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011259 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 }
11262}
11263
11264/*
11265 * "inputsave()" function
11266 */
11267/*ARGSUSED*/
11268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011270 typval_T *argvars;
11271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272{
11273 /* Add an entry to the stack of typehead storage. */
11274 if (ga_grow(&ga_userinput, 1) == OK)
11275 {
11276 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11277 + ga_userinput.ga_len);
11278 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 }
11281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283}
11284
11285/*
11286 * "inputsecret()" function
11287 */
11288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011290 typval_T *argvars;
11291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292{
11293 ++cmdline_star;
11294 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 --cmdline_star;
11297 --inputsecret_flag;
11298}
11299
11300/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011301 * "insert()" function
11302 */
11303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011305 typval_T *argvars;
11306 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011307{
11308 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 listitem_T *item;
11310 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011312
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011313 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011314 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011315 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011316 else if ((l = argvars[0].vval.v_list) != NULL
11317 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011318 {
11319 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 before = get_tv_number_chk(&argvars[2], &error);
11321 if (error)
11322 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011323
Bram Moolenaar758711c2005-02-02 23:11:38 +000011324 if (before == l->lv_len)
11325 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011326 else
11327 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011328 item = list_find(l, before);
11329 if (item == NULL)
11330 {
11331 EMSGN(_(e_listidx), before);
11332 l = NULL;
11333 }
11334 }
11335 if (l != NULL)
11336 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011337 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011338 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011339 }
11340 }
11341}
11342
11343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 * "isdirectory()" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352}
11353
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011354/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011355 * "islocked()" function
11356 */
11357 static void
11358f_islocked(argvars, rettv)
11359 typval_T *argvars;
11360 typval_T *rettv;
11361{
11362 lval_T lv;
11363 char_u *end;
11364 dictitem_T *di;
11365
11366 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011367 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11368 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011369 if (end != NULL && lv.ll_name != NULL)
11370 {
11371 if (*end != NUL)
11372 EMSG(_(e_trailing));
11373 else
11374 {
11375 if (lv.ll_tv == NULL)
11376 {
11377 if (check_changedtick(lv.ll_name))
11378 rettv->vval.v_number = 1; /* always locked */
11379 else
11380 {
11381 di = find_var(lv.ll_name, NULL);
11382 if (di != NULL)
11383 {
11384 /* Consider a variable locked when:
11385 * 1. the variable itself is locked
11386 * 2. the value of the variable is locked.
11387 * 3. the List or Dict value is locked.
11388 */
11389 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11390 || tv_islocked(&di->di_tv));
11391 }
11392 }
11393 }
11394 else if (lv.ll_range)
11395 EMSG(_("E745: Range not allowed"));
11396 else if (lv.ll_newkey != NULL)
11397 EMSG2(_(e_dictkey), lv.ll_newkey);
11398 else if (lv.ll_list != NULL)
11399 /* List item. */
11400 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11401 else
11402 /* Dictionary item. */
11403 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11404 }
11405 }
11406
11407 clear_lval(&lv);
11408}
11409
Bram Moolenaar33570922005-01-25 22:26:29 +000011410static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011411
11412/*
11413 * Turn a dict into a list:
11414 * "what" == 0: list of keys
11415 * "what" == 1: list of values
11416 * "what" == 2: list of items
11417 */
11418 static void
11419dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011420 typval_T *argvars;
11421 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011422 int what;
11423{
Bram Moolenaar33570922005-01-25 22:26:29 +000011424 list_T *l2;
11425 dictitem_T *di;
11426 hashitem_T *hi;
11427 listitem_T *li;
11428 listitem_T *li2;
11429 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011430 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011431
11432 rettv->vval.v_number = 0;
11433 if (argvars[0].v_type != VAR_DICT)
11434 {
11435 EMSG(_(e_dictreq));
11436 return;
11437 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011438 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011439 return;
11440
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011441 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011442 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011443
Bram Moolenaar33570922005-01-25 22:26:29 +000011444 todo = d->dv_hashtab.ht_used;
11445 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011446 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011447 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011448 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011449 --todo;
11450 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011451
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011452 li = listitem_alloc();
11453 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011454 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011455 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011456
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011457 if (what == 0)
11458 {
11459 /* keys() */
11460 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011461 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011462 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11463 }
11464 else if (what == 1)
11465 {
11466 /* values() */
11467 copy_tv(&di->di_tv, &li->li_tv);
11468 }
11469 else
11470 {
11471 /* items() */
11472 l2 = list_alloc();
11473 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011474 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011475 li->li_tv.vval.v_list = l2;
11476 if (l2 == NULL)
11477 break;
11478 ++l2->lv_refcount;
11479
11480 li2 = listitem_alloc();
11481 if (li2 == NULL)
11482 break;
11483 list_append(l2, li2);
11484 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011485 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011486 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11487
11488 li2 = listitem_alloc();
11489 if (li2 == NULL)
11490 break;
11491 list_append(l2, li2);
11492 copy_tv(&di->di_tv, &li2->li_tv);
11493 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011494 }
11495 }
11496}
11497
11498/*
11499 * "items(dict)" function
11500 */
11501 static void
11502f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 typval_T *argvars;
11504 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011505{
11506 dict_list(argvars, rettv, 2);
11507}
11508
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011510 * "join()" function
11511 */
11512 static void
11513f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011514 typval_T *argvars;
11515 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011516{
11517 garray_T ga;
11518 char_u *sep;
11519
11520 rettv->vval.v_number = 0;
11521 if (argvars[0].v_type != VAR_LIST)
11522 {
11523 EMSG(_(e_listreq));
11524 return;
11525 }
11526 if (argvars[0].vval.v_list == NULL)
11527 return;
11528 if (argvars[1].v_type == VAR_UNKNOWN)
11529 sep = (char_u *)" ";
11530 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011532
11533 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534
11535 if (sep != NULL)
11536 {
11537 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000011538 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 ga_append(&ga, NUL);
11540 rettv->vval.v_string = (char_u *)ga.ga_data;
11541 }
11542 else
11543 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011544}
11545
11546/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011547 * "keys()" function
11548 */
11549 static void
11550f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011551 typval_T *argvars;
11552 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011553{
11554 dict_list(argvars, rettv, 0);
11555}
11556
11557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 * "last_buffer_nr()" function.
11559 */
11560/*ARGSUSED*/
11561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011563 typval_T *argvars;
11564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565{
11566 int n = 0;
11567 buf_T *buf;
11568
11569 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11570 if (n < buf->b_fnum)
11571 n = buf->b_fnum;
11572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011574}
11575
11576/*
11577 * "len()" function
11578 */
11579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T *argvars;
11582 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011583{
11584 switch (argvars[0].v_type)
11585 {
11586 case VAR_STRING:
11587 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 rettv->vval.v_number = (varnumber_T)STRLEN(
11589 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011590 break;
11591 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011594 case VAR_DICT:
11595 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11596 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011597 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011598 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011599 break;
11600 }
11601}
11602
Bram Moolenaar33570922005-01-25 22:26:29 +000011603static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011604
11605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 typval_T *argvars;
11608 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011609 int type;
11610{
11611#ifdef FEAT_LIBCALL
11612 char_u *string_in;
11613 char_u **string_result;
11614 int nr_result;
11615#endif
11616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011618 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011622
11623 if (check_restricted() || check_secure())
11624 return;
11625
11626#ifdef FEAT_LIBCALL
11627 /* The first two args must be strings, otherwise its meaningless */
11628 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11629 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011630 string_in = NULL;
11631 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011632 string_in = argvars[2].vval.v_string;
11633 if (type == VAR_NUMBER)
11634 string_result = NULL;
11635 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011637 if (mch_libcall(argvars[0].vval.v_string,
11638 argvars[1].vval.v_string,
11639 string_in,
11640 argvars[2].vval.v_number,
11641 string_result,
11642 &nr_result) == OK
11643 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011645 }
11646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647}
11648
11649/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011650 * "libcall()" function
11651 */
11652 static void
11653f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011654 typval_T *argvars;
11655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656{
11657 libcall_common(argvars, rettv, VAR_STRING);
11658}
11659
11660/*
11661 * "libcallnr()" function
11662 */
11663 static void
11664f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667{
11668 libcall_common(argvars, rettv, VAR_NUMBER);
11669}
11670
11671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672 * "line(string)" function
11673 */
11674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011676 typval_T *argvars;
11677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678{
11679 linenr_T lnum = 0;
11680 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011681 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011683 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684 if (fp != NULL)
11685 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687}
11688
11689/*
11690 * "line2byte(lnum)" function
11691 */
11692/*ARGSUSED*/
11693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011695 typval_T *argvars;
11696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697{
11698#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700#else
11701 linenr_T lnum;
11702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11708 if (rettv->vval.v_number >= 0)
11709 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710#endif
11711}
11712
11713/*
11714 * "lispindent(lnum)" function
11715 */
11716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011717f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 typval_T *argvars;
11719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720{
11721#ifdef FEAT_LISP
11722 pos_T pos;
11723 linenr_T lnum;
11724
11725 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11728 {
11729 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 curwin->w_cursor = pos;
11732 }
11733 else
11734#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736}
11737
11738/*
11739 * "localtime()" function
11740 */
11741/*ARGSUSED*/
11742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011744 typval_T *argvars;
11745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748}
11749
Bram Moolenaar33570922005-01-25 22:26:29 +000011750static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751
11752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011754 typval_T *argvars;
11755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 int exact;
11757{
11758 char_u *keys;
11759 char_u *which;
11760 char_u buf[NUMBUFLEN];
11761 char_u *keys_buf = NULL;
11762 char_u *rhs;
11763 int mode;
11764 garray_T ga;
11765
11766 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->v_type = VAR_STRING;
11768 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771 if (*keys == NUL)
11772 return;
11773
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011774 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011775 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 else
11777 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011778 if (which == NULL)
11779 return;
11780
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 mode = get_map_mode(&which, 0);
11782
11783 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011784 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 vim_free(keys_buf);
11786 if (rhs != NULL)
11787 {
11788 ga_init(&ga);
11789 ga.ga_itemsize = 1;
11790 ga.ga_growsize = 40;
11791
11792 while (*rhs != NUL)
11793 ga_concat(&ga, str2special(&rhs, FALSE));
11794
11795 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797 }
11798}
11799
11800/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011801 * "map()" function
11802 */
11803 static void
11804f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011805 typval_T *argvars;
11806 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011807{
11808 filter_map(argvars, rettv, TRUE);
11809}
11810
11811/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 */
11814 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011815f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011816 typval_T *argvars;
11817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820}
11821
11822/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011823 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 */
11825 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011826f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011827 typval_T *argvars;
11828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011830 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831}
11832
Bram Moolenaar33570922005-01-25 22:26:29 +000011833static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834
11835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011837 typval_T *argvars;
11838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 int type;
11840{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011841 char_u *str = NULL;
11842 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 char_u *pat;
11844 regmatch_T regmatch;
11845 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011846 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 char_u *save_cpo;
11848 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011849 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011850 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011851 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011852 list_T *l = NULL;
11853 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011854 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011855 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856
11857 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11858 save_cpo = p_cpo;
11859 p_cpo = (char_u *)"";
11860
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011861 rettv->vval.v_number = -1;
11862 if (type == 3)
11863 {
11864 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011865 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011866 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011867 }
11868 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870 rettv->v_type = VAR_STRING;
11871 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011874 if (argvars[0].v_type == VAR_LIST)
11875 {
11876 if ((l = argvars[0].vval.v_list) == NULL)
11877 goto theend;
11878 li = l->lv_first;
11879 }
11880 else
11881 expr = str = get_tv_string(&argvars[0]);
11882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11884 if (pat == NULL)
11885 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011886
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 int error = FALSE;
11890
11891 start = get_tv_number_chk(&argvars[2], &error);
11892 if (error)
11893 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011894 if (l != NULL)
11895 {
11896 li = list_find(l, start);
11897 if (li == NULL)
11898 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011899 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011900 }
11901 else
11902 {
11903 if (start < 0)
11904 start = 0;
11905 if (start > (long)STRLEN(str))
11906 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011907 /* When "count" argument is there ignore matches before "start",
11908 * otherwise skip part of the string. Differs when pattern is "^"
11909 * or "\<". */
11910 if (argvars[3].v_type != VAR_UNKNOWN)
11911 startcol = start;
11912 else
11913 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011914 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011915
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011916 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011917 nth = get_tv_number_chk(&argvars[3], &error);
11918 if (error)
11919 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 }
11921
11922 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11923 if (regmatch.regprog != NULL)
11924 {
11925 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011926
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011927 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011928 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929 if (l != NULL)
11930 {
11931 if (li == NULL)
11932 {
11933 match = FALSE;
11934 break;
11935 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011936 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011937 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011938 if (str == NULL)
11939 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011940 }
11941
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011942 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011943
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011944 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011945 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011946 if (l == NULL && !match)
11947 break;
11948
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011949 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011950 if (l != NULL)
11951 {
11952 li = li->li_next;
11953 ++idx;
11954 }
11955 else
11956 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011957#ifdef FEAT_MBYTE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011958 startcol = regmatch.startp[0]
11959 + (*mb_ptr2len)(regmatch.startp[0]) - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011960#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011961 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011962#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011963 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011964 }
11965
11966 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011968 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011970 int i;
11971
11972 /* return list with matched string and submatches */
11973 for (i = 0; i < NSUBEXP; ++i)
11974 {
11975 if (regmatch.endp[i] == NULL)
11976 break;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011977 if (list_append_string(rettv->vval.v_list,
11978 regmatch.startp[i],
11979 (int)(regmatch.endp[i] - regmatch.startp[i]))
11980 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011981 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011982 }
11983 }
11984 else if (type == 2)
11985 {
11986 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011987 if (l != NULL)
11988 copy_tv(&li->li_tv, rettv);
11989 else
11990 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011992 }
11993 else if (l != NULL)
11994 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 else
11996 {
11997 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011998 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999 (varnumber_T)(regmatch.startp[0] - str);
12000 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 }
12005 }
12006 vim_free(regmatch.regprog);
12007 }
12008
12009theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012010 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 p_cpo = save_cpo;
12012}
12013
12014/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012015 * "match()" function
12016 */
12017 static void
12018f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012019 typval_T *argvars;
12020 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012021{
12022 find_some_match(argvars, rettv, 1);
12023}
12024
12025/*
12026 * "matchend()" function
12027 */
12028 static void
12029f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012030 typval_T *argvars;
12031 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012032{
12033 find_some_match(argvars, rettv, 0);
12034}
12035
12036/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012037 * "matchlist()" function
12038 */
12039 static void
12040f_matchlist(argvars, rettv)
12041 typval_T *argvars;
12042 typval_T *rettv;
12043{
12044 find_some_match(argvars, rettv, 3);
12045}
12046
12047/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012048 * "matchstr()" function
12049 */
12050 static void
12051f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012054{
12055 find_some_match(argvars, rettv, 2);
12056}
12057
Bram Moolenaar33570922005-01-25 22:26:29 +000012058static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012059
12060 static void
12061max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 typval_T *argvars;
12063 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012064 int domax;
12065{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012066 long n = 0;
12067 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012069
12070 if (argvars[0].v_type == VAR_LIST)
12071 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012072 list_T *l;
12073 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012074
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012075 l = argvars[0].vval.v_list;
12076 if (l != NULL)
12077 {
12078 li = l->lv_first;
12079 if (li != NULL)
12080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012082 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012083 {
12084 li = li->li_next;
12085 if (li == NULL)
12086 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012087 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012088 if (domax ? i > n : i < n)
12089 n = i;
12090 }
12091 }
12092 }
12093 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012094 else if (argvars[0].v_type == VAR_DICT)
12095 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012097 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012098 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012099 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012100
12101 d = argvars[0].vval.v_dict;
12102 if (d != NULL)
12103 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012104 todo = d->dv_hashtab.ht_used;
12105 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012106 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012107 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012109 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012110 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012111 if (first)
12112 {
12113 n = i;
12114 first = FALSE;
12115 }
12116 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012117 n = i;
12118 }
12119 }
12120 }
12121 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012122 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012123 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012124 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012125}
12126
12127/*
12128 * "max()" function
12129 */
12130 static void
12131f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 typval_T *argvars;
12133 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012134{
12135 max_min(argvars, rettv, TRUE);
12136}
12137
12138/*
12139 * "min()" function
12140 */
12141 static void
12142f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012143 typval_T *argvars;
12144 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012145{
12146 max_min(argvars, rettv, FALSE);
12147}
12148
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012149static int mkdir_recurse __ARGS((char_u *dir, int prot));
12150
12151/*
12152 * Create the directory in which "dir" is located, and higher levels when
12153 * needed.
12154 */
12155 static int
12156mkdir_recurse(dir, prot)
12157 char_u *dir;
12158 int prot;
12159{
12160 char_u *p;
12161 char_u *updir;
12162 int r = FAIL;
12163
12164 /* Get end of directory name in "dir".
12165 * We're done when it's "/" or "c:/". */
12166 p = gettail_sep(dir);
12167 if (p <= get_past_head(dir))
12168 return OK;
12169
12170 /* If the directory exists we're done. Otherwise: create it.*/
12171 updir = vim_strnsave(dir, (int)(p - dir));
12172 if (updir == NULL)
12173 return FAIL;
12174 if (mch_isdir(updir))
12175 r = OK;
12176 else if (mkdir_recurse(updir, prot) == OK)
12177 r = vim_mkdir_emsg(updir, prot);
12178 vim_free(updir);
12179 return r;
12180}
12181
12182#ifdef vim_mkdir
12183/*
12184 * "mkdir()" function
12185 */
12186 static void
12187f_mkdir(argvars, rettv)
12188 typval_T *argvars;
12189 typval_T *rettv;
12190{
12191 char_u *dir;
12192 char_u buf[NUMBUFLEN];
12193 int prot = 0755;
12194
12195 rettv->vval.v_number = FAIL;
12196 if (check_restricted() || check_secure())
12197 return;
12198
12199 dir = get_tv_string_buf(&argvars[0], buf);
12200 if (argvars[1].v_type != VAR_UNKNOWN)
12201 {
12202 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012203 prot = get_tv_number_chk(&argvars[2], NULL);
12204 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012205 mkdir_recurse(dir, prot);
12206 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012207 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012208}
12209#endif
12210
Bram Moolenaar0d660222005-01-07 21:51:51 +000012211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 * "mode()" function
12213 */
12214/*ARGSUSED*/
12215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012217 typval_T *argvars;
12218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219{
12220 char_u buf[2];
12221
12222#ifdef FEAT_VISUAL
12223 if (VIsual_active)
12224 {
12225 if (VIsual_select)
12226 buf[0] = VIsual_mode + 's' - 'v';
12227 else
12228 buf[0] = VIsual_mode;
12229 }
12230 else
12231#endif
12232 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12233 buf[0] = 'r';
12234 else if (State & INSERT)
12235 {
12236 if (State & REPLACE_FLAG)
12237 buf[0] = 'R';
12238 else
12239 buf[0] = 'i';
12240 }
12241 else if (State & CMDLINE)
12242 buf[0] = 'c';
12243 else
12244 buf[0] = 'n';
12245
12246 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012247 rettv->vval.v_string = vim_strsave(buf);
12248 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249}
12250
12251/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012252 * "nextnonblank()" function
12253 */
12254 static void
12255f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012256 typval_T *argvars;
12257 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012258{
12259 linenr_T lnum;
12260
12261 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012263 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012264 {
12265 lnum = 0;
12266 break;
12267 }
12268 if (*skipwhite(ml_get(lnum)) != NUL)
12269 break;
12270 }
12271 rettv->vval.v_number = lnum;
12272}
12273
12274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 * "nr2char()" function
12276 */
12277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012279 typval_T *argvars;
12280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281{
12282 char_u buf[NUMBUFLEN];
12283
12284#ifdef FEAT_MBYTE
12285 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 else
12288#endif
12289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 buf[1] = NUL;
12292 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293 rettv->v_type = VAR_STRING;
12294 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012295}
12296
12297/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012298 * "prevnonblank()" function
12299 */
12300 static void
12301f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012302 typval_T *argvars;
12303 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012304{
12305 linenr_T lnum;
12306
12307 lnum = get_tv_lnum(argvars);
12308 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12309 lnum = 0;
12310 else
12311 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12312 --lnum;
12313 rettv->vval.v_number = lnum;
12314}
12315
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012316#ifdef HAVE_STDARG_H
12317/* This dummy va_list is here because:
12318 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12319 * - locally in the function results in a "used before set" warning
12320 * - using va_start() to initialize it gives "function with fixed args" error */
12321static va_list ap;
12322#endif
12323
Bram Moolenaar8c711452005-01-14 21:53:12 +000012324/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012325 * "printf()" function
12326 */
12327 static void
12328f_printf(argvars, rettv)
12329 typval_T *argvars;
12330 typval_T *rettv;
12331{
12332 rettv->v_type = VAR_STRING;
12333 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012334#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012335 {
12336 char_u buf[NUMBUFLEN];
12337 int len;
12338 char_u *s;
12339 int saved_did_emsg = did_emsg;
12340 char *fmt;
12341
12342 /* Get the required length, allocate the buffer and do it for real. */
12343 did_emsg = FALSE;
12344 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012345 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012346 if (!did_emsg)
12347 {
12348 s = alloc(len + 1);
12349 if (s != NULL)
12350 {
12351 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012352 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012353 }
12354 }
12355 did_emsg |= saved_did_emsg;
12356 }
12357#endif
12358}
12359
12360/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012361 * "pumvisible()" function
12362 */
12363/*ARGSUSED*/
12364 static void
12365f_pumvisible(argvars, rettv)
12366 typval_T *argvars;
12367 typval_T *rettv;
12368{
12369 rettv->vval.v_number = 0;
12370#ifdef FEAT_INS_EXPAND
12371 if (pum_visible())
12372 rettv->vval.v_number = 1;
12373#endif
12374}
12375
12376/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012377 * "range()" function
12378 */
12379 static void
12380f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *argvars;
12382 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012383{
12384 long start;
12385 long end;
12386 long stride = 1;
12387 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012391 if (argvars[1].v_type == VAR_UNKNOWN)
12392 {
12393 end = start - 1;
12394 start = 0;
12395 }
12396 else
12397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012398 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012399 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012400 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012401 }
12402
12403 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 if (error)
12405 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012406 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012407 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012408 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012409 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012410 else
12411 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012412 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012413 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012414 if (list_append_number(rettv->vval.v_list,
12415 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012416 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012417 }
12418}
12419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012420/*
12421 * "readfile()" function
12422 */
12423 static void
12424f_readfile(argvars, rettv)
12425 typval_T *argvars;
12426 typval_T *rettv;
12427{
12428 int binary = FALSE;
12429 char_u *fname;
12430 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012431 listitem_T *li;
12432#define FREAD_SIZE 200 /* optimized for text lines */
12433 char_u buf[FREAD_SIZE];
12434 int readlen; /* size of last fread() */
12435 int buflen; /* nr of valid chars in buf[] */
12436 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12437 int tolist; /* first byte in buf[] still to be put in list */
12438 int chop; /* how many CR to chop off */
12439 char_u *prev = NULL; /* previously read bytes, if any */
12440 int prevlen = 0; /* length of "prev" if not NULL */
12441 char_u *s;
12442 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012443 long maxline = MAXLNUM;
12444 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012445
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012446 if (argvars[1].v_type != VAR_UNKNOWN)
12447 {
12448 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12449 binary = TRUE;
12450 if (argvars[2].v_type != VAR_UNKNOWN)
12451 maxline = get_tv_number(&argvars[2]);
12452 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012454 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012455 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012456
12457 /* Always open the file in binary mode, library functions have a mind of
12458 * their own about CR-LF conversion. */
12459 fname = get_tv_string(&argvars[0]);
12460 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12461 {
12462 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12463 return;
12464 }
12465
12466 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012467 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012468 {
12469 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12470 buflen = filtd + readlen;
12471 tolist = 0;
12472 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12473 {
12474 if (buf[filtd] == '\n' || readlen <= 0)
12475 {
12476 /* Only when in binary mode add an empty list item when the
12477 * last line ends in a '\n'. */
12478 if (!binary && readlen == 0 && filtd == 0)
12479 break;
12480
12481 /* Found end-of-line or end-of-file: add a text line to the
12482 * list. */
12483 chop = 0;
12484 if (!binary)
12485 while (filtd - chop - 1 >= tolist
12486 && buf[filtd - chop - 1] == '\r')
12487 ++chop;
12488 len = filtd - tolist - chop;
12489 if (prev == NULL)
12490 s = vim_strnsave(buf + tolist, len);
12491 else
12492 {
12493 s = alloc((unsigned)(prevlen + len + 1));
12494 if (s != NULL)
12495 {
12496 mch_memmove(s, prev, prevlen);
12497 vim_free(prev);
12498 prev = NULL;
12499 mch_memmove(s + prevlen, buf + tolist, len);
12500 s[prevlen + len] = NUL;
12501 }
12502 }
12503 tolist = filtd + 1;
12504
12505 li = listitem_alloc();
12506 if (li == NULL)
12507 {
12508 vim_free(s);
12509 break;
12510 }
12511 li->li_tv.v_type = VAR_STRING;
12512 li->li_tv.v_lock = 0;
12513 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012514 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012515
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012516 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012517 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012518 if (readlen <= 0)
12519 break;
12520 }
12521 else if (buf[filtd] == NUL)
12522 buf[filtd] = '\n';
12523 }
12524 if (readlen <= 0)
12525 break;
12526
12527 if (tolist == 0)
12528 {
12529 /* "buf" is full, need to move text to an allocated buffer */
12530 if (prev == NULL)
12531 {
12532 prev = vim_strnsave(buf, buflen);
12533 prevlen = buflen;
12534 }
12535 else
12536 {
12537 s = alloc((unsigned)(prevlen + buflen));
12538 if (s != NULL)
12539 {
12540 mch_memmove(s, prev, prevlen);
12541 mch_memmove(s + prevlen, buf, buflen);
12542 vim_free(prev);
12543 prev = s;
12544 prevlen += buflen;
12545 }
12546 }
12547 filtd = 0;
12548 }
12549 else
12550 {
12551 mch_memmove(buf, buf + tolist, buflen - tolist);
12552 filtd -= tolist;
12553 }
12554 }
12555
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012556 /*
12557 * For a negative line count use only the lines at the end of the file,
12558 * free the rest.
12559 */
12560 if (maxline < 0)
12561 while (cnt > -maxline)
12562 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012563 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012564 --cnt;
12565 }
12566
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012567 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012568 fclose(fd);
12569}
12570
12571
Bram Moolenaar0d660222005-01-07 21:51:51 +000012572#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12573static void make_connection __ARGS((void));
12574static int check_connection __ARGS((void));
12575
12576 static void
12577make_connection()
12578{
12579 if (X_DISPLAY == NULL
12580# ifdef FEAT_GUI
12581 && !gui.in_use
12582# endif
12583 )
12584 {
12585 x_force_connect = TRUE;
12586 setup_term_clip();
12587 x_force_connect = FALSE;
12588 }
12589}
12590
12591 static int
12592check_connection()
12593{
12594 make_connection();
12595 if (X_DISPLAY == NULL)
12596 {
12597 EMSG(_("E240: No connection to Vim server"));
12598 return FAIL;
12599 }
12600 return OK;
12601}
12602#endif
12603
12604#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012605static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012606
12607 static void
12608remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012609 typval_T *argvars;
12610 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012611 int expr;
12612{
12613 char_u *server_name;
12614 char_u *keys;
12615 char_u *r = NULL;
12616 char_u buf[NUMBUFLEN];
12617# ifdef WIN32
12618 HWND w;
12619# else
12620 Window w;
12621# endif
12622
12623 if (check_restricted() || check_secure())
12624 return;
12625
12626# ifdef FEAT_X11
12627 if (check_connection() == FAIL)
12628 return;
12629# endif
12630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 server_name = get_tv_string_chk(&argvars[0]);
12632 if (server_name == NULL)
12633 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012634 keys = get_tv_string_buf(&argvars[1], buf);
12635# ifdef WIN32
12636 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12637# else
12638 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12639 < 0)
12640# endif
12641 {
12642 if (r != NULL)
12643 EMSG(r); /* sending worked but evaluation failed */
12644 else
12645 EMSG2(_("E241: Unable to send to %s"), server_name);
12646 return;
12647 }
12648
12649 rettv->vval.v_string = r;
12650
12651 if (argvars[2].v_type != VAR_UNKNOWN)
12652 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012653 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012654 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012655 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012656
12657 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 v.di_tv.v_type = VAR_STRING;
12659 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012660 idvar = get_tv_string_chk(&argvars[2]);
12661 if (idvar != NULL)
12662 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012663 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012664 }
12665}
12666#endif
12667
12668/*
12669 * "remote_expr()" function
12670 */
12671/*ARGSUSED*/
12672 static void
12673f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012674 typval_T *argvars;
12675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012676{
12677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = NULL;
12679#ifdef FEAT_CLIENTSERVER
12680 remote_common(argvars, rettv, TRUE);
12681#endif
12682}
12683
12684/*
12685 * "remote_foreground()" function
12686 */
12687/*ARGSUSED*/
12688 static void
12689f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *argvars;
12691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012692{
12693 rettv->vval.v_number = 0;
12694#ifdef FEAT_CLIENTSERVER
12695# ifdef WIN32
12696 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012697 {
12698 char_u *server_name = get_tv_string_chk(&argvars[0]);
12699
12700 if (server_name != NULL)
12701 serverForeground(server_name);
12702 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012703# else
12704 /* Send a foreground() expression to the server. */
12705 argvars[1].v_type = VAR_STRING;
12706 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12707 argvars[2].v_type = VAR_UNKNOWN;
12708 remote_common(argvars, rettv, TRUE);
12709 vim_free(argvars[1].vval.v_string);
12710# endif
12711#endif
12712}
12713
12714/*ARGSUSED*/
12715 static void
12716f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012717 typval_T *argvars;
12718 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012719{
12720#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012721 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012722 char_u *s = NULL;
12723# ifdef WIN32
12724 int n = 0;
12725# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012727
12728 if (check_restricted() || check_secure())
12729 {
12730 rettv->vval.v_number = -1;
12731 return;
12732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012733 serverid = get_tv_string_chk(&argvars[0]);
12734 if (serverid == NULL)
12735 {
12736 rettv->vval.v_number = -1;
12737 return; /* type error; errmsg already given */
12738 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012739# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012741 if (n == 0)
12742 rettv->vval.v_number = -1;
12743 else
12744 {
12745 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12746 rettv->vval.v_number = (s != NULL);
12747 }
12748# else
12749 rettv->vval.v_number = 0;
12750 if (check_connection() == FAIL)
12751 return;
12752
12753 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012755# endif
12756
12757 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012759 char_u *retvar;
12760
Bram Moolenaar33570922005-01-25 22:26:29 +000012761 v.di_tv.v_type = VAR_STRING;
12762 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 retvar = get_tv_string_chk(&argvars[1]);
12764 if (retvar != NULL)
12765 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012767 }
12768#else
12769 rettv->vval.v_number = -1;
12770#endif
12771}
12772
12773/*ARGSUSED*/
12774 static void
12775f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012776 typval_T *argvars;
12777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012778{
12779 char_u *r = NULL;
12780
12781#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 char_u *serverid = get_tv_string_chk(&argvars[0]);
12783
12784 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012785 {
12786# ifdef WIN32
12787 /* The server's HWND is encoded in the 'id' parameter */
12788 int n = 0;
12789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012790 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012791 if (n != 0)
12792 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12793 if (r == NULL)
12794# else
12795 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012796 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012797# endif
12798 EMSG(_("E277: Unable to read a server reply"));
12799 }
12800#endif
12801 rettv->v_type = VAR_STRING;
12802 rettv->vval.v_string = r;
12803}
12804
12805/*
12806 * "remote_send()" function
12807 */
12808/*ARGSUSED*/
12809 static void
12810f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 typval_T *argvars;
12812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012813{
12814 rettv->v_type = VAR_STRING;
12815 rettv->vval.v_string = NULL;
12816#ifdef FEAT_CLIENTSERVER
12817 remote_common(argvars, rettv, FALSE);
12818#endif
12819}
12820
12821/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012822 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012823 */
12824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012826 typval_T *argvars;
12827 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012828{
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 list_T *l;
12830 listitem_T *item, *item2;
12831 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012832 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012833 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012834 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 dict_T *d;
12836 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012837
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012838 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012839 if (argvars[0].v_type == VAR_DICT)
12840 {
12841 if (argvars[2].v_type != VAR_UNKNOWN)
12842 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012843 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012844 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012846 key = get_tv_string_chk(&argvars[1]);
12847 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012849 di = dict_find(d, key, -1);
12850 if (di == NULL)
12851 EMSG2(_(e_dictkey), key);
12852 else
12853 {
12854 *rettv = di->di_tv;
12855 init_tv(&di->di_tv);
12856 dictitem_remove(d, di);
12857 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012858 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012859 }
12860 }
12861 else if (argvars[0].v_type != VAR_LIST)
12862 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012863 else if ((l = argvars[0].vval.v_list) != NULL
12864 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 int error = FALSE;
12867
12868 idx = get_tv_number_chk(&argvars[1], &error);
12869 if (error)
12870 ; /* type error: do nothing, errmsg already given */
12871 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012872 EMSGN(_(e_listidx), idx);
12873 else
12874 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012875 if (argvars[2].v_type == VAR_UNKNOWN)
12876 {
12877 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012878 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012879 *rettv = item->li_tv;
12880 vim_free(item);
12881 }
12882 else
12883 {
12884 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012885 end = get_tv_number_chk(&argvars[2], &error);
12886 if (error)
12887 ; /* type error: do nothing */
12888 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012889 EMSGN(_(e_listidx), end);
12890 else
12891 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012892 int cnt = 0;
12893
12894 for (li = item; li != NULL; li = li->li_next)
12895 {
12896 ++cnt;
12897 if (li == item2)
12898 break;
12899 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012900 if (li == NULL) /* didn't find "item2" after "item" */
12901 EMSG(_(e_invrange));
12902 else
12903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012904 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012905 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012906 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012907 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012908 l->lv_first = item;
12909 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012910 item->li_prev = NULL;
12911 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012912 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012913 }
12914 }
12915 }
12916 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012917 }
12918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919}
12920
12921/*
12922 * "rename({from}, {to})" function
12923 */
12924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012925f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012926 typval_T *argvars;
12927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928{
12929 char_u buf[NUMBUFLEN];
12930
12931 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12935 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936}
12937
12938/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012939 * "repeat()" function
12940 */
12941/*ARGSUSED*/
12942 static void
12943f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012944 typval_T *argvars;
12945 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012946{
12947 char_u *p;
12948 int n;
12949 int slen;
12950 int len;
12951 char_u *r;
12952 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012953
12954 n = get_tv_number(&argvars[1]);
12955 if (argvars[0].v_type == VAR_LIST)
12956 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012957 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012958 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012959 if (list_extend(rettv->vval.v_list,
12960 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012961 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012962 }
12963 else
12964 {
12965 p = get_tv_string(&argvars[0]);
12966 rettv->v_type = VAR_STRING;
12967 rettv->vval.v_string = NULL;
12968
12969 slen = (int)STRLEN(p);
12970 len = slen * n;
12971 if (len <= 0)
12972 return;
12973
12974 r = alloc(len + 1);
12975 if (r != NULL)
12976 {
12977 for (i = 0; i < n; i++)
12978 mch_memmove(r + i * slen, p, (size_t)slen);
12979 r[len] = NUL;
12980 }
12981
12982 rettv->vval.v_string = r;
12983 }
12984}
12985
12986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 * "resolve()" function
12988 */
12989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012990f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012991 typval_T *argvars;
12992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993{
12994 char_u *p;
12995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997#ifdef FEAT_SHORTCUT
12998 {
12999 char_u *v = NULL;
13000
13001 v = mch_resolve_shortcut(p);
13002 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013005 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006 }
13007#else
13008# ifdef HAVE_READLINK
13009 {
13010 char_u buf[MAXPATHL + 1];
13011 char_u *cpy;
13012 int len;
13013 char_u *remain = NULL;
13014 char_u *q;
13015 int is_relative_to_current = FALSE;
13016 int has_trailing_pathsep = FALSE;
13017 int limit = 100;
13018
13019 p = vim_strsave(p);
13020
13021 if (p[0] == '.' && (vim_ispathsep(p[1])
13022 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13023 is_relative_to_current = TRUE;
13024
13025 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013026 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027 has_trailing_pathsep = TRUE;
13028
13029 q = getnextcomp(p);
13030 if (*q != NUL)
13031 {
13032 /* Separate the first path component in "p", and keep the
13033 * remainder (beginning with the path separator). */
13034 remain = vim_strsave(q - 1);
13035 q[-1] = NUL;
13036 }
13037
13038 for (;;)
13039 {
13040 for (;;)
13041 {
13042 len = readlink((char *)p, (char *)buf, MAXPATHL);
13043 if (len <= 0)
13044 break;
13045 buf[len] = NUL;
13046
13047 if (limit-- == 0)
13048 {
13049 vim_free(p);
13050 vim_free(remain);
13051 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053 goto fail;
13054 }
13055
13056 /* Ensure that the result will have a trailing path separator
13057 * if the argument has one. */
13058 if (remain == NULL && has_trailing_pathsep)
13059 add_pathsep(buf);
13060
13061 /* Separate the first path component in the link value and
13062 * concatenate the remainders. */
13063 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13064 if (*q != NUL)
13065 {
13066 if (remain == NULL)
13067 remain = vim_strsave(q - 1);
13068 else
13069 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013070 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 if (cpy != NULL)
13072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 vim_free(remain);
13074 remain = cpy;
13075 }
13076 }
13077 q[-1] = NUL;
13078 }
13079
13080 q = gettail(p);
13081 if (q > p && *q == NUL)
13082 {
13083 /* Ignore trailing path separator. */
13084 q[-1] = NUL;
13085 q = gettail(p);
13086 }
13087 if (q > p && !mch_isFullName(buf))
13088 {
13089 /* symlink is relative to directory of argument */
13090 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13091 if (cpy != NULL)
13092 {
13093 STRCPY(cpy, p);
13094 STRCPY(gettail(cpy), buf);
13095 vim_free(p);
13096 p = cpy;
13097 }
13098 }
13099 else
13100 {
13101 vim_free(p);
13102 p = vim_strsave(buf);
13103 }
13104 }
13105
13106 if (remain == NULL)
13107 break;
13108
13109 /* Append the first path component of "remain" to "p". */
13110 q = getnextcomp(remain + 1);
13111 len = q - remain - (*q != NUL);
13112 cpy = vim_strnsave(p, STRLEN(p) + len);
13113 if (cpy != NULL)
13114 {
13115 STRNCAT(cpy, remain, len);
13116 vim_free(p);
13117 p = cpy;
13118 }
13119 /* Shorten "remain". */
13120 if (*q != NUL)
13121 STRCPY(remain, q - 1);
13122 else
13123 {
13124 vim_free(remain);
13125 remain = NULL;
13126 }
13127 }
13128
13129 /* If the result is a relative path name, make it explicitly relative to
13130 * the current directory if and only if the argument had this form. */
13131 if (!vim_ispathsep(*p))
13132 {
13133 if (is_relative_to_current
13134 && *p != NUL
13135 && !(p[0] == '.'
13136 && (p[1] == NUL
13137 || vim_ispathsep(p[1])
13138 || (p[1] == '.'
13139 && (p[2] == NUL
13140 || vim_ispathsep(p[2]))))))
13141 {
13142 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013143 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 if (cpy != NULL)
13145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 vim_free(p);
13147 p = cpy;
13148 }
13149 }
13150 else if (!is_relative_to_current)
13151 {
13152 /* Strip leading "./". */
13153 q = p;
13154 while (q[0] == '.' && vim_ispathsep(q[1]))
13155 q += 2;
13156 if (q > p)
13157 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13158 }
13159 }
13160
13161 /* Ensure that the result will have no trailing path separator
13162 * if the argument had none. But keep "/" or "//". */
13163 if (!has_trailing_pathsep)
13164 {
13165 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013166 if (after_pathsep(p, q))
13167 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 }
13169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 }
13172# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174# endif
13175#endif
13176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178
13179#ifdef HAVE_READLINK
13180fail:
13181#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183}
13184
13185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013186 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 */
13188 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013189f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013190 typval_T *argvars;
13191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192{
Bram Moolenaar33570922005-01-25 22:26:29 +000013193 list_T *l;
13194 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195
Bram Moolenaar0d660222005-01-07 21:51:51 +000013196 rettv->vval.v_number = 0;
13197 if (argvars[0].v_type != VAR_LIST)
13198 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013199 else if ((l = argvars[0].vval.v_list) != NULL
13200 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013201 {
13202 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013203 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013204 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013205 while (li != NULL)
13206 {
13207 ni = li->li_prev;
13208 list_append(l, li);
13209 li = ni;
13210 }
13211 rettv->vval.v_list = l;
13212 rettv->v_type = VAR_LIST;
13213 ++l->lv_refcount;
13214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215}
13216
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013217#define SP_NOMOVE 0x01 /* don't move cursor */
13218#define SP_REPEAT 0x02 /* repeat to find outer pair */
13219#define SP_RETCOUNT 0x04 /* return matchcount */
13220#define SP_SETPCMARK 0x08 /* set previous context mark */
13221#define SP_START 0x10 /* accept match at start position */
13222#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13223#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013224
Bram Moolenaar33570922005-01-25 22:26:29 +000013225static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013226
13227/*
13228 * Get flags for a search function.
13229 * Possibly sets "p_ws".
13230 * Returns BACKWARD, FORWARD or zero (for an error).
13231 */
13232 static int
13233get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013234 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013235 int *flagsp;
13236{
13237 int dir = FORWARD;
13238 char_u *flags;
13239 char_u nbuf[NUMBUFLEN];
13240 int mask;
13241
13242 if (varp->v_type != VAR_UNKNOWN)
13243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 flags = get_tv_string_buf_chk(varp, nbuf);
13245 if (flags == NULL)
13246 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013247 while (*flags != NUL)
13248 {
13249 switch (*flags)
13250 {
13251 case 'b': dir = BACKWARD; break;
13252 case 'w': p_ws = TRUE; break;
13253 case 'W': p_ws = FALSE; break;
13254 default: mask = 0;
13255 if (flagsp != NULL)
13256 switch (*flags)
13257 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013258 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013259 case 'e': mask = SP_END; break;
13260 case 'm': mask = SP_RETCOUNT; break;
13261 case 'n': mask = SP_NOMOVE; break;
13262 case 'p': mask = SP_SUBPAT; break;
13263 case 'r': mask = SP_REPEAT; break;
13264 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013265 }
13266 if (mask == 0)
13267 {
13268 EMSG2(_(e_invarg2), flags);
13269 dir = 0;
13270 }
13271 else
13272 *flagsp |= mask;
13273 }
13274 if (dir == 0)
13275 break;
13276 ++flags;
13277 }
13278 }
13279 return dir;
13280}
13281
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013283 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013285 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013286search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013287 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013288 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013289 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013291 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 char_u *pat;
13293 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013294 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 int save_p_ws = p_ws;
13296 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013297 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013298 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013299 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013300 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013303 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013304 if (dir == 0)
13305 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013306 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013307 if (flags & SP_START)
13308 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013309 if (flags & SP_END)
13310 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013311
13312 /* Optional extra argument: line number to stop searching. */
13313 if (argvars[1].v_type != VAR_UNKNOWN
13314 && argvars[2].v_type != VAR_UNKNOWN)
13315 {
13316 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
13317 if (lnum_stop < 0)
13318 goto theend;
13319 }
13320
Bram Moolenaar231334e2005-07-25 20:46:57 +000013321 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013322 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013323 * Check to make sure only those flags are set.
13324 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13325 * flags cannot be set. Check for that condition also.
13326 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013327 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013328 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013331 goto theend;
13332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013334 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013335 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13336 options, RE_SEARCH, (linenr_T)lnum_stop);
13337 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013339 if (flags & SP_SUBPAT)
13340 retval = subpatnum;
13341 else
13342 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013343 if (flags & SP_SETPCMARK)
13344 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013346 if (match_pos != NULL)
13347 {
13348 /* Store the match cursor position */
13349 match_pos->lnum = pos.lnum;
13350 match_pos->col = pos.col + 1;
13351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 /* "/$" will put the cursor after the end of the line, may need to
13353 * correct that here */
13354 check_cursor();
13355 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013356
13357 /* If 'n' flag is used: restore cursor position. */
13358 if (flags & SP_NOMOVE)
13359 curwin->w_cursor = save_cursor;
13360theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013362
13363 return retval;
13364}
13365
13366/*
13367 * "search()" function
13368 */
13369 static void
13370f_search(argvars, rettv)
13371 typval_T *argvars;
13372 typval_T *rettv;
13373{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013374 int flags = 0;
13375
13376 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377}
13378
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013380 * "searchdecl()" function
13381 */
13382 static void
13383f_searchdecl(argvars, rettv)
13384 typval_T *argvars;
13385 typval_T *rettv;
13386{
13387 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013388 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013389 int error = FALSE;
13390 char_u *name;
13391
13392 rettv->vval.v_number = 1; /* default: FAIL */
13393
13394 name = get_tv_string_chk(&argvars[0]);
13395 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013396 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013397 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013398 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13399 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13400 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013401 if (!error && name != NULL)
13402 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013403 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013404}
13405
13406/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013407 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013409 static int
13410searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000013411 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013412 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413{
13414 char_u *spat, *mpat, *epat;
13415 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 int dir;
13418 int flags = 0;
13419 char_u nbuf1[NUMBUFLEN];
13420 char_u nbuf2[NUMBUFLEN];
13421 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013422 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013423 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013426 spat = get_tv_string_chk(&argvars[0]);
13427 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13428 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13429 if (spat == NULL || mpat == NULL || epat == NULL)
13430 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 /* Handle the optional fourth argument: flags */
13433 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013434 if (dir == 0)
13435 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013436
13437 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000013438 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13439 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013440 if ((flags & (SP_END | SP_SUBPAT)) != 0
13441 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000013442 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013443 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000013444 goto theend;
13445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013447 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013448 if (argvars[3].v_type == VAR_UNKNOWN
13449 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 skip = (char_u *)"";
13451 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013453 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013454 if (argvars[5].v_type != VAR_UNKNOWN)
13455 {
13456 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
13457 if (lnum_stop < 0)
13458 goto theend;
13459 }
13460 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013461 if (skip == NULL)
13462 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013464 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
13465 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013466
13467theend:
13468 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013469
13470 return retval;
13471}
13472
13473/*
13474 * "searchpair()" function
13475 */
13476 static void
13477f_searchpair(argvars, rettv)
13478 typval_T *argvars;
13479 typval_T *rettv;
13480{
13481 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
13482}
13483
13484/*
13485 * "searchpairpos()" function
13486 */
13487 static void
13488f_searchpairpos(argvars, rettv)
13489 typval_T *argvars;
13490 typval_T *rettv;
13491{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013492 pos_T match_pos;
13493 int lnum = 0;
13494 int col = 0;
13495
13496 rettv->vval.v_number = 0;
13497
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013498 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013499 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013500
13501 if (searchpair_cmn(argvars, &match_pos) > 0)
13502 {
13503 lnum = match_pos.lnum;
13504 col = match_pos.col;
13505 }
13506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013507 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13508 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013509}
13510
13511/*
13512 * Search for a start/middle/end thing.
13513 * Used by searchpair(), see its documentation for the details.
13514 * Returns 0 or -1 for no match,
13515 */
13516 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013517do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013518 char_u *spat; /* start pattern */
13519 char_u *mpat; /* middle pattern */
13520 char_u *epat; /* end pattern */
13521 int dir; /* BACKWARD or FORWARD */
13522 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013523 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013524 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013525 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013526{
13527 char_u *save_cpo;
13528 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13529 long retval = 0;
13530 pos_T pos;
13531 pos_T firstpos;
13532 pos_T foundpos;
13533 pos_T save_cursor;
13534 pos_T save_pos;
13535 int n;
13536 int r;
13537 int nest = 1;
13538 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013539 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013540
13541 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13542 save_cpo = p_cpo;
13543 p_cpo = (char_u *)"";
13544
13545 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13546 * start/middle/end (pat3, for the top pair). */
13547 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13548 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13549 if (pat2 == NULL || pat3 == NULL)
13550 goto theend;
13551 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13552 if (*mpat == NUL)
13553 STRCPY(pat3, pat2);
13554 else
13555 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13556 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013557 if (flags & SP_START)
13558 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013559
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 save_cursor = curwin->w_cursor;
13561 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000013562 clearpos(&firstpos);
13563 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 pat = pat3;
13565 for (;;)
13566 {
13567 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013568 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13570 /* didn't find it or found the first match again: FAIL */
13571 break;
13572
13573 if (firstpos.lnum == 0)
13574 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013575 if (equalpos(pos, foundpos))
13576 {
13577 /* Found the same position again. Can happen with a pattern that
13578 * has "\zs" at the end and searching backwards. Advance one
13579 * character and try again. */
13580 if (dir == BACKWARD)
13581 decl(&pos);
13582 else
13583 incl(&pos);
13584 }
13585 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586
13587 /* If the skip pattern matches, ignore this match. */
13588 if (*skip != NUL)
13589 {
13590 save_pos = curwin->w_cursor;
13591 curwin->w_cursor = pos;
13592 r = eval_to_bool(skip, &err, NULL, FALSE);
13593 curwin->w_cursor = save_pos;
13594 if (err)
13595 {
13596 /* Evaluating {skip} caused an error, break here. */
13597 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013598 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 break;
13600 }
13601 if (r)
13602 continue;
13603 }
13604
13605 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13606 {
13607 /* Found end when searching backwards or start when searching
13608 * forward: nested pair. */
13609 ++nest;
13610 pat = pat2; /* nested, don't search for middle */
13611 }
13612 else
13613 {
13614 /* Found end when searching forward or start when searching
13615 * backward: end of (nested) pair; or found middle in outer pair. */
13616 if (--nest == 1)
13617 pat = pat3; /* outer level, search for middle */
13618 }
13619
13620 if (nest == 0)
13621 {
13622 /* Found the match: return matchcount or line number. */
13623 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013624 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013626 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013627 if (flags & SP_SETPCMARK)
13628 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 curwin->w_cursor = pos;
13630 if (!(flags & SP_REPEAT))
13631 break;
13632 nest = 1; /* search for next unmatched */
13633 }
13634 }
13635
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013636 if (match_pos != NULL)
13637 {
13638 /* Store the match cursor position */
13639 match_pos->lnum = curwin->w_cursor.lnum;
13640 match_pos->col = curwin->w_cursor.col + 1;
13641 }
13642
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013644 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 curwin->w_cursor = save_cursor;
13646
13647theend:
13648 vim_free(pat2);
13649 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013651
13652 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653}
13654
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013655/*
13656 * "searchpos()" function
13657 */
13658 static void
13659f_searchpos(argvars, rettv)
13660 typval_T *argvars;
13661 typval_T *rettv;
13662{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013663 pos_T match_pos;
13664 int lnum = 0;
13665 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013666 int n;
13667 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013668
13669 rettv->vval.v_number = 0;
13670
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013671 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013672 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013673
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013674 n = search_cmn(argvars, &match_pos, &flags);
13675 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013676 {
13677 lnum = match_pos.lnum;
13678 col = match_pos.col;
13679 }
13680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013681 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
13682 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000013683 if (flags & SP_SUBPAT)
13684 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013685}
13686
13687
Bram Moolenaar0d660222005-01-07 21:51:51 +000013688/*ARGSUSED*/
13689 static void
13690f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013691 typval_T *argvars;
13692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013694#ifdef FEAT_CLIENTSERVER
13695 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 char_u *server = get_tv_string_chk(&argvars[0]);
13697 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698
Bram Moolenaar0d660222005-01-07 21:51:51 +000013699 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 if (server == NULL || reply == NULL)
13701 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702 if (check_restricted() || check_secure())
13703 return;
13704# ifdef FEAT_X11
13705 if (check_connection() == FAIL)
13706 return;
13707# endif
13708
13709 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013711 EMSG(_("E258: Unable to send to client"));
13712 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714 rettv->vval.v_number = 0;
13715#else
13716 rettv->vval.v_number = -1;
13717#endif
13718}
13719
13720/*ARGSUSED*/
13721 static void
13722f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 typval_T *argvars;
13724 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013725{
13726 char_u *r = NULL;
13727
13728#ifdef FEAT_CLIENTSERVER
13729# ifdef WIN32
13730 r = serverGetVimNames();
13731# else
13732 make_connection();
13733 if (X_DISPLAY != NULL)
13734 r = serverGetVimNames(X_DISPLAY);
13735# endif
13736#endif
13737 rettv->v_type = VAR_STRING;
13738 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739}
13740
13741/*
13742 * "setbufvar()" function
13743 */
13744/*ARGSUSED*/
13745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013747 typval_T *argvars;
13748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749{
13750 buf_T *buf;
13751#ifdef FEAT_AUTOCMD
13752 aco_save_T aco;
13753#else
13754 buf_T *save_curbuf;
13755#endif
13756 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013757 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 char_u nbuf[NUMBUFLEN];
13759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 rettv->vval.v_number = 0;
13761
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 if (check_restricted() || check_secure())
13763 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13765 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 varp = &argvars[2];
13768
13769 if (buf != NULL && varname != NULL && varp != NULL)
13770 {
13771 /* set curbuf to be our buf, temporarily */
13772#ifdef FEAT_AUTOCMD
13773 aucmd_prepbuf(&aco, buf);
13774#else
13775 save_curbuf = curbuf;
13776 curbuf = buf;
13777#endif
13778
13779 if (*varname == '&')
13780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013781 long numval;
13782 char_u *strval;
13783 int error = FALSE;
13784
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786 numval = get_tv_number_chk(varp, &error);
13787 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013788 if (!error && strval != NULL)
13789 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 }
13791 else
13792 {
13793 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13794 if (bufvarname != NULL)
13795 {
13796 STRCPY(bufvarname, "b:");
13797 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013798 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 vim_free(bufvarname);
13800 }
13801 }
13802
13803 /* reset notion of buffer */
13804#ifdef FEAT_AUTOCMD
13805 aucmd_restbuf(&aco);
13806#else
13807 curbuf = save_curbuf;
13808#endif
13809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810}
13811
13812/*
13813 * "setcmdpos()" function
13814 */
13815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013817 typval_T *argvars;
13818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013820 int pos = (int)get_tv_number(&argvars[0]) - 1;
13821
13822 if (pos >= 0)
13823 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824}
13825
13826/*
13827 * "setline()" function
13828 */
13829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013831 typval_T *argvars;
13832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833{
13834 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013835 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013836 list_T *l = NULL;
13837 listitem_T *li = NULL;
13838 long added = 0;
13839 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013841 lnum = get_tv_lnum(&argvars[0]);
13842 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013844 l = argvars[1].vval.v_list;
13845 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013847 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013849
13850 rettv->vval.v_number = 0; /* OK */
13851 for (;;)
13852 {
13853 if (l != NULL)
13854 {
13855 /* list argument, get next string */
13856 if (li == NULL)
13857 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013858 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013859 li = li->li_next;
13860 }
13861
13862 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013864 break;
13865 if (lnum <= curbuf->b_ml.ml_line_count)
13866 {
13867 /* existing line, replace it */
13868 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13869 {
13870 changed_bytes(lnum, 0);
13871 check_cursor_col();
13872 rettv->vval.v_number = 0; /* OK */
13873 }
13874 }
13875 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13876 {
13877 /* lnum is one past the last line, append the line */
13878 ++added;
13879 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13880 rettv->vval.v_number = 0; /* OK */
13881 }
13882
13883 if (l == NULL) /* only one string argument */
13884 break;
13885 ++lnum;
13886 }
13887
13888 if (added > 0)
13889 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890}
13891
13892/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013893 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013894 */
13895/*ARGSUSED*/
13896 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013897set_qf_ll_list(wp, list_arg, action_arg, rettv)
13898 win_T *wp;
13899 typval_T *list_arg;
13900 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013901 typval_T *rettv;
13902{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013903#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013904 char_u *act;
13905 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000013906#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013907
Bram Moolenaar2641f772005-03-25 21:58:17 +000013908 rettv->vval.v_number = -1;
13909
13910#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013911 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013912 EMSG(_(e_listreq));
13913 else
13914 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013915 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013916
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013917 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013918 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013919 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013920 if (act == NULL)
13921 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013922 if (*act == 'a' || *act == 'r')
13923 action = *act;
13924 }
13925
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013926 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013927 rettv->vval.v_number = 0;
13928 }
13929#endif
13930}
13931
13932/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013933 * "setloclist()" function
13934 */
13935/*ARGSUSED*/
13936 static void
13937f_setloclist(argvars, rettv)
13938 typval_T *argvars;
13939 typval_T *rettv;
13940{
13941 win_T *win;
13942
13943 rettv->vval.v_number = -1;
13944
13945 win = find_win_by_nr(&argvars[0]);
13946 if (win != NULL)
13947 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
13948}
13949
13950/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013951 * "setpos()" function
13952 */
13953/*ARGSUSED*/
13954 static void
13955f_setpos(argvars, rettv)
13956 typval_T *argvars;
13957 typval_T *rettv;
13958{
13959 pos_T pos;
13960 int fnum;
13961 char_u *name;
13962
13963 name = get_tv_string_chk(argvars);
13964 if (name != NULL)
13965 {
13966 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
13967 {
13968 --pos.col;
13969 if (name[0] == '.') /* cursor */
13970 {
13971 if (fnum == curbuf->b_fnum)
13972 {
13973 curwin->w_cursor = pos;
13974 check_cursor();
13975 }
13976 else
13977 EMSG(_(e_invarg));
13978 }
13979 else if (name[0] == '\'') /* mark */
13980 (void)setmark_pos(name[1], &pos, fnum);
13981 else
13982 EMSG(_(e_invarg));
13983 }
13984 }
13985}
13986
13987/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000013988 * "setqflist()" function
13989 */
13990/*ARGSUSED*/
13991 static void
13992f_setqflist(argvars, rettv)
13993 typval_T *argvars;
13994 typval_T *rettv;
13995{
13996 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
13997}
13998
13999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 * "setreg()" function
14001 */
14002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014004 typval_T *argvars;
14005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006{
14007 int regname;
14008 char_u *strregname;
14009 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 int append;
14012 char_u yank_type;
14013 long block_len;
14014
14015 block_len = -1;
14016 yank_type = MAUTO;
14017 append = FALSE;
14018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014019 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014020 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014022 if (strregname == NULL)
14023 return; /* type error; errmsg already given */
14024 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 if (regname == 0 || regname == '@')
14026 regname = '"';
14027 else if (regname == '=')
14028 return;
14029
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014030 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 stropt = get_tv_string_chk(&argvars[2]);
14033 if (stropt == NULL)
14034 return; /* type error */
14035 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 switch (*stropt)
14037 {
14038 case 'a': case 'A': /* append */
14039 append = TRUE;
14040 break;
14041 case 'v': case 'c': /* character-wise selection */
14042 yank_type = MCHAR;
14043 break;
14044 case 'V': case 'l': /* line-wise selection */
14045 yank_type = MLINE;
14046 break;
14047#ifdef FEAT_VISUAL
14048 case 'b': case Ctrl_V: /* block-wise selection */
14049 yank_type = MBLOCK;
14050 if (VIM_ISDIGIT(stropt[1]))
14051 {
14052 ++stropt;
14053 block_len = getdigits(&stropt) - 1;
14054 --stropt;
14055 }
14056 break;
14057#endif
14058 }
14059 }
14060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014061 strval = get_tv_string_chk(&argvars[1]);
14062 if (strval != NULL)
14063 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014065 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066}
14067
14068
14069/*
14070 * "setwinvar(expr)" function
14071 */
14072/*ARGSUSED*/
14073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014074f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 typval_T *argvars;
14076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077{
14078 win_T *win;
14079#ifdef FEAT_WINDOWS
14080 win_T *save_curwin;
14081#endif
14082 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014083 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 char_u nbuf[NUMBUFLEN];
14085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 rettv->vval.v_number = 0;
14087
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 if (check_restricted() || check_secure())
14089 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 varp = &argvars[2];
14093
14094 if (win != NULL && varname != NULL && varp != NULL)
14095 {
14096#ifdef FEAT_WINDOWS
14097 /* set curwin to be our win, temporarily */
14098 save_curwin = curwin;
14099 curwin = win;
14100 curbuf = curwin->w_buffer;
14101#endif
14102
14103 if (*varname == '&')
14104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 long numval;
14106 char_u *strval;
14107 int error = FALSE;
14108
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014110 numval = get_tv_number_chk(varp, &error);
14111 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 if (!error && strval != NULL)
14113 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 }
14115 else
14116 {
14117 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14118 if (winvarname != NULL)
14119 {
14120 STRCPY(winvarname, "w:");
14121 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014122 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 vim_free(winvarname);
14124 }
14125 }
14126
14127#ifdef FEAT_WINDOWS
14128 /* Restore current window, if it's still valid (autocomands can make
14129 * it invalid). */
14130 if (win_valid(save_curwin))
14131 {
14132 curwin = save_curwin;
14133 curbuf = curwin->w_buffer;
14134 }
14135#endif
14136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137}
14138
14139/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014140 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 */
14142 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014143f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 typval_T *argvars;
14145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014147 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149 p = get_tv_string(&argvars[0]);
14150 rettv->vval.v_string = vim_strsave(p);
14151 simplify_filename(rettv->vval.v_string); /* simplify in place */
14152 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153}
14154
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155static int
14156#ifdef __BORLANDC__
14157 _RTLENTRYF
14158#endif
14159 item_compare __ARGS((const void *s1, const void *s2));
14160static int
14161#ifdef __BORLANDC__
14162 _RTLENTRYF
14163#endif
14164 item_compare2 __ARGS((const void *s1, const void *s2));
14165
14166static int item_compare_ic;
14167static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014169#define ITEM_COMPARE_FAIL 999
14170
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014174 static int
14175#ifdef __BORLANDC__
14176_RTLENTRYF
14177#endif
14178item_compare(s1, s2)
14179 const void *s1;
14180 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014182 char_u *p1, *p2;
14183 char_u *tofree1, *tofree2;
14184 int res;
14185 char_u numbuf1[NUMBUFLEN];
14186 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014187
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014188 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
14189 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014190 if (item_compare_ic)
14191 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193 res = STRCMP(p1, p2);
14194 vim_free(tofree1);
14195 vim_free(tofree2);
14196 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197}
14198
14199 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000014200#ifdef __BORLANDC__
14201_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000014203item_compare2(s1, s2)
14204 const void *s1;
14205 const void *s2;
14206{
14207 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000014208 typval_T rettv;
14209 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000014210 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 /* shortcut after failure in previous call; compare all items equal */
14213 if (item_compare_func_err)
14214 return 0;
14215
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014216 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
14217 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
14219 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014220
14221 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
14222 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000014223 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224 clear_tv(&argv[0]);
14225 clear_tv(&argv[1]);
14226
14227 if (res == FAIL)
14228 res = ITEM_COMPARE_FAIL;
14229 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 /* return value has wrong type */
14231 res = get_tv_number_chk(&rettv, &item_compare_func_err);
14232 if (item_compare_func_err)
14233 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014234 clear_tv(&rettv);
14235 return res;
14236}
14237
14238/*
14239 * "sort({list})" function
14240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014242f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014243 typval_T *argvars;
14244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245{
Bram Moolenaar33570922005-01-25 22:26:29 +000014246 list_T *l;
14247 listitem_T *li;
14248 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014249 long len;
14250 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251
Bram Moolenaar0d660222005-01-07 21:51:51 +000014252 rettv->vval.v_number = 0;
14253 if (argvars[0].v_type != VAR_LIST)
14254 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 else
14256 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014257 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014258 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014259 return;
14260 rettv->vval.v_list = l;
14261 rettv->v_type = VAR_LIST;
14262 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263
Bram Moolenaar0d660222005-01-07 21:51:51 +000014264 len = list_len(l);
14265 if (len <= 1)
14266 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267
Bram Moolenaar0d660222005-01-07 21:51:51 +000014268 item_compare_ic = FALSE;
14269 item_compare_func = NULL;
14270 if (argvars[1].v_type != VAR_UNKNOWN)
14271 {
14272 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014274 else
14275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014276 int error = FALSE;
14277
14278 i = get_tv_number_chk(&argvars[1], &error);
14279 if (error)
14280 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281 if (i == 1)
14282 item_compare_ic = TRUE;
14283 else
14284 item_compare_func = get_tv_string(&argvars[1]);
14285 }
14286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287
Bram Moolenaar0d660222005-01-07 21:51:51 +000014288 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014289 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014290 if (ptrs == NULL)
14291 return;
14292 i = 0;
14293 for (li = l->lv_first; li != NULL; li = li->li_next)
14294 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014297 /* test the compare function */
14298 if (item_compare_func != NULL
14299 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
14300 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014301 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014303 {
14304 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014305 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000014306 item_compare_func == NULL ? item_compare : item_compare2);
14307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014308 if (!item_compare_func_err)
14309 {
14310 /* Clear the List and append the items in the sorted order. */
14311 l->lv_first = l->lv_last = NULL;
14312 l->lv_len = 0;
14313 for (i = 0; i < len; ++i)
14314 list_append(l, ptrs[i]);
14315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014316 }
14317
14318 vim_free(ptrs);
14319 }
14320}
14321
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014322/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000014323 * "soundfold({word})" function
14324 */
14325 static void
14326f_soundfold(argvars, rettv)
14327 typval_T *argvars;
14328 typval_T *rettv;
14329{
14330 char_u *s;
14331
14332 rettv->v_type = VAR_STRING;
14333 s = get_tv_string(&argvars[0]);
14334#ifdef FEAT_SYN_HL
14335 rettv->vval.v_string = eval_soundfold(s);
14336#else
14337 rettv->vval.v_string = vim_strsave(s);
14338#endif
14339}
14340
14341/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014342 * "spellbadword()" function
14343 */
14344/* ARGSUSED */
14345 static void
14346f_spellbadword(argvars, rettv)
14347 typval_T *argvars;
14348 typval_T *rettv;
14349{
Bram Moolenaar4463f292005-09-25 22:20:24 +000014350 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014351 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014352 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014353
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014354 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014355 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014356
14357#ifdef FEAT_SYN_HL
Bram Moolenaar4463f292005-09-25 22:20:24 +000014358 if (argvars[0].v_type == VAR_UNKNOWN)
14359 {
14360 /* Find the start and length of the badly spelled word. */
14361 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
14362 if (len != 0)
14363 word = ml_get_cursor();
14364 }
14365 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14366 {
14367 char_u *str = get_tv_string_chk(&argvars[0]);
14368 int capcol = -1;
14369
14370 if (str != NULL)
14371 {
14372 /* Check the argument for spelling. */
14373 while (*str != NUL)
14374 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014375 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014376 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014377 {
14378 word = str;
14379 break;
14380 }
14381 str += len;
14382 }
14383 }
14384 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014385#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000014386
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014387 list_append_string(rettv->vval.v_list, word, len);
14388 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000014389 attr == HLF_SPB ? "bad" :
14390 attr == HLF_SPR ? "rare" :
14391 attr == HLF_SPL ? "local" :
14392 attr == HLF_SPC ? "caps" :
14393 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014394}
14395
14396/*
14397 * "spellsuggest()" function
14398 */
14399 static void
14400f_spellsuggest(argvars, rettv)
14401 typval_T *argvars;
14402 typval_T *rettv;
14403{
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014404#ifdef FEAT_SYN_HL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014405 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014406 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014407 int maxcount;
14408 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014409 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014410 listitem_T *li;
14411 int need_capital = FALSE;
14412#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014413
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014414 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014415 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014416
14417#ifdef FEAT_SYN_HL
14418 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
14419 {
14420 str = get_tv_string(&argvars[0]);
14421 if (argvars[1].v_type != VAR_UNKNOWN)
14422 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014423 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014424 if (maxcount <= 0)
14425 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000014426 if (argvars[2].v_type != VAR_UNKNOWN)
14427 {
14428 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
14429 if (typeerr)
14430 return;
14431 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014432 }
14433 else
14434 maxcount = 25;
14435
Bram Moolenaar4770d092006-01-12 23:22:24 +000014436 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014437
14438 for (i = 0; i < ga.ga_len; ++i)
14439 {
14440 str = ((char_u **)ga.ga_data)[i];
14441
14442 li = listitem_alloc();
14443 if (li == NULL)
14444 vim_free(str);
14445 else
14446 {
14447 li->li_tv.v_type = VAR_STRING;
14448 li->li_tv.v_lock = 0;
14449 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014450 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014451 }
14452 }
14453 ga_clear(&ga);
14454 }
14455#endif
14456}
14457
Bram Moolenaar0d660222005-01-07 21:51:51 +000014458 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014459f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014460 typval_T *argvars;
14461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462{
14463 char_u *str;
14464 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014465 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014466 regmatch_T regmatch;
14467 char_u patbuf[NUMBUFLEN];
14468 char_u *save_cpo;
14469 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014471 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014472 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014473
14474 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14475 save_cpo = p_cpo;
14476 p_cpo = (char_u *)"";
14477
14478 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014479 if (argvars[1].v_type != VAR_UNKNOWN)
14480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014481 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14482 if (pat == NULL)
14483 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014484 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014486 }
14487 if (pat == NULL || *pat == NUL)
14488 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014490 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014492 if (typeerr)
14493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14496 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014498 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014499 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014501 if (*str == NUL)
14502 match = FALSE; /* empty item at the end */
14503 else
14504 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014505 if (match)
14506 end = regmatch.startp[0];
14507 else
14508 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014509 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
14510 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014511 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014512 if (list_append_string(rettv->vval.v_list, str,
14513 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014514 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014515 }
14516 if (!match)
14517 break;
14518 /* Advance to just after the match. */
14519 if (regmatch.endp[0] > str)
14520 col = 0;
14521 else
14522 {
14523 /* Don't get stuck at the same match. */
14524#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014525 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526#else
14527 col = 1;
14528#endif
14529 }
14530 str = regmatch.endp[0];
14531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532
Bram Moolenaar0d660222005-01-07 21:51:51 +000014533 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537}
14538
14539#ifdef HAVE_STRFTIME
14540/*
14541 * "strftime({format}[, {time}])" function
14542 */
14543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014545 typval_T *argvars;
14546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547{
14548 char_u result_buf[256];
14549 struct tm *curtime;
14550 time_t seconds;
14551 char_u *p;
14552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014553 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014556 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557 seconds = time(NULL);
14558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 curtime = localtime(&seconds);
14561 /* MSVC returns NULL for an invalid value of seconds. */
14562 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 else
14565 {
14566# ifdef FEAT_MBYTE
14567 vimconv_T conv;
14568 char_u *enc;
14569
14570 conv.vc_type = CONV_NONE;
14571 enc = enc_locale();
14572 convert_setup(&conv, p_enc, enc);
14573 if (conv.vc_type != CONV_NONE)
14574 p = string_convert(&conv, p, NULL);
14575# endif
14576 if (p != NULL)
14577 (void)strftime((char *)result_buf, sizeof(result_buf),
14578 (char *)p, curtime);
14579 else
14580 result_buf[0] = NUL;
14581
14582# ifdef FEAT_MBYTE
14583 if (conv.vc_type != CONV_NONE)
14584 vim_free(p);
14585 convert_setup(&conv, enc, p_enc);
14586 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 else
14589# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014590 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591
14592# ifdef FEAT_MBYTE
14593 /* Release conversion descriptors */
14594 convert_setup(&conv, NULL, NULL);
14595 vim_free(enc);
14596# endif
14597 }
14598}
14599#endif
14600
14601/*
14602 * "stridx()" function
14603 */
14604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 typval_T *argvars;
14607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608{
14609 char_u buf[NUMBUFLEN];
14610 char_u *needle;
14611 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014612 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 needle = get_tv_string_chk(&argvars[1]);
14617 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014618 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 if (needle == NULL || haystack == NULL)
14620 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 if (argvars[2].v_type != VAR_UNKNOWN)
14623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 int error = FALSE;
14625
14626 start_idx = get_tv_number_chk(&argvars[2], &error);
14627 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014629 if (start_idx >= 0)
14630 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 }
14632
14633 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14634 if (pos != NULL)
14635 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636}
14637
14638/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014639 * "string()" function
14640 */
14641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014642f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014643 typval_T *argvars;
14644 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014645{
14646 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014647 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014649 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014650 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653}
14654
14655/*
14656 * "strlen()" function
14657 */
14658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014659f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014660 typval_T *argvars;
14661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_number = (varnumber_T)(STRLEN(
14664 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665}
14666
14667/*
14668 * "strpart()" function
14669 */
14670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014671f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014672 typval_T *argvars;
14673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674{
14675 char_u *p;
14676 int n;
14677 int len;
14678 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014679 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 slen = (int)STRLEN(p);
14683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 n = get_tv_number_chk(&argvars[1], &error);
14685 if (error)
14686 len = 0;
14687 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014688 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 else
14690 len = slen - n; /* default len: all bytes that are available. */
14691
14692 /*
14693 * Only return the overlap between the specified part and the actual
14694 * string.
14695 */
14696 if (n < 0)
14697 {
14698 len += n;
14699 n = 0;
14700 }
14701 else if (n > slen)
14702 n = slen;
14703 if (len < 0)
14704 len = 0;
14705 else if (n + len > slen)
14706 len = slen - n;
14707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014708 rettv->v_type = VAR_STRING;
14709 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710}
14711
14712/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713 * "strridx()" function
14714 */
14715 static void
14716f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014717 typval_T *argvars;
14718 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719{
14720 char_u buf[NUMBUFLEN];
14721 char_u *needle;
14722 char_u *haystack;
14723 char_u *rest;
14724 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014725 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014727 needle = get_tv_string_chk(&argvars[1]);
14728 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014729 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014730
14731 rettv->vval.v_number = -1;
14732 if (needle == NULL || haystack == NULL)
14733 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014734 if (argvars[2].v_type != VAR_UNKNOWN)
14735 {
14736 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014737 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014738 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014739 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014740 }
14741 else
14742 end_idx = haystack_len;
14743
Bram Moolenaar0d660222005-01-07 21:51:51 +000014744 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014745 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014746 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014747 lastmatch = haystack + end_idx;
14748 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014749 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014750 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014751 for (rest = haystack; *rest != '\0'; ++rest)
14752 {
14753 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014754 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014755 break;
14756 lastmatch = rest;
14757 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014758 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014759
14760 if (lastmatch == NULL)
14761 rettv->vval.v_number = -1;
14762 else
14763 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14764}
14765
14766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 * "strtrans()" function
14768 */
14769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014770f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 typval_T *argvars;
14772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 rettv->v_type = VAR_STRING;
14775 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776}
14777
14778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014779 * "submatch()" function
14780 */
14781 static void
14782f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014783 typval_T *argvars;
14784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014785{
14786 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 rettv->vval.v_string =
14788 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014789}
14790
14791/*
14792 * "substitute()" function
14793 */
14794 static void
14795f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014796 typval_T *argvars;
14797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014798{
14799 char_u patbuf[NUMBUFLEN];
14800 char_u subbuf[NUMBUFLEN];
14801 char_u flagsbuf[NUMBUFLEN];
14802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014803 char_u *str = get_tv_string_chk(&argvars[0]);
14804 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14805 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14806 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14807
Bram Moolenaar0d660222005-01-07 21:51:51 +000014808 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014809 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14810 rettv->vval.v_string = NULL;
14811 else
14812 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014813}
14814
14815/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014816 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 */
14818/*ARGSUSED*/
14819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014820f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014821 typval_T *argvars;
14822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823{
14824 int id = 0;
14825#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014826 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 long col;
14828 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014829 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 lnum = get_tv_lnum(argvars); /* -1 on type error */
14832 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14833 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014836 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014837 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838#endif
14839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014840 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841}
14842
14843/*
14844 * "synIDattr(id, what [, mode])" function
14845 */
14846/*ARGSUSED*/
14847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014848f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014849 typval_T *argvars;
14850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851{
14852 char_u *p = NULL;
14853#ifdef FEAT_SYN_HL
14854 int id;
14855 char_u *what;
14856 char_u *mode;
14857 char_u modebuf[NUMBUFLEN];
14858 int modec;
14859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 id = get_tv_number(&argvars[0]);
14861 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014862 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 modec = TOLOWER_ASC(mode[0]);
14866 if (modec != 't' && modec != 'c'
14867#ifdef FEAT_GUI
14868 && modec != 'g'
14869#endif
14870 )
14871 modec = 0; /* replace invalid with current */
14872 }
14873 else
14874 {
14875#ifdef FEAT_GUI
14876 if (gui.in_use)
14877 modec = 'g';
14878 else
14879#endif
14880 if (t_colors > 1)
14881 modec = 'c';
14882 else
14883 modec = 't';
14884 }
14885
14886
14887 switch (TOLOWER_ASC(what[0]))
14888 {
14889 case 'b':
14890 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14891 p = highlight_color(id, what, modec);
14892 else /* bold */
14893 p = highlight_has_attr(id, HL_BOLD, modec);
14894 break;
14895
14896 case 'f': /* fg[#] */
14897 p = highlight_color(id, what, modec);
14898 break;
14899
14900 case 'i':
14901 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14902 p = highlight_has_attr(id, HL_INVERSE, modec);
14903 else /* italic */
14904 p = highlight_has_attr(id, HL_ITALIC, modec);
14905 break;
14906
14907 case 'n': /* name */
14908 p = get_highlight_name(NULL, id - 1);
14909 break;
14910
14911 case 'r': /* reverse */
14912 p = highlight_has_attr(id, HL_INVERSE, modec);
14913 break;
14914
14915 case 's': /* standout */
14916 p = highlight_has_attr(id, HL_STANDOUT, modec);
14917 break;
14918
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014919 case 'u':
14920 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14921 /* underline */
14922 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14923 else
14924 /* undercurl */
14925 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 break;
14927 }
14928
14929 if (p != NULL)
14930 p = vim_strsave(p);
14931#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 rettv->v_type = VAR_STRING;
14933 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934}
14935
14936/*
14937 * "synIDtrans(id)" function
14938 */
14939/*ARGSUSED*/
14940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014942 typval_T *argvars;
14943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944{
14945 int id;
14946
14947#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014948 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949
14950 if (id > 0)
14951 id = syn_get_final_id(id);
14952 else
14953#endif
14954 id = 0;
14955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014956 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957}
14958
14959/*
14960 * "system()" function
14961 */
14962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 typval_T *argvars;
14965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014967 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014969 char_u *infile = NULL;
14970 char_u buf[NUMBUFLEN];
14971 int err = FALSE;
14972 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014973
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014974 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014975 {
14976 /*
14977 * Write the string to a temp file, to be used for input of the shell
14978 * command.
14979 */
14980 if ((infile = vim_tempname('i')) == NULL)
14981 {
14982 EMSG(_(e_notmp));
14983 return;
14984 }
14985
14986 fd = mch_fopen((char *)infile, WRITEBIN);
14987 if (fd == NULL)
14988 {
14989 EMSG2(_(e_notopen), infile);
14990 goto done;
14991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014992 p = get_tv_string_buf_chk(&argvars[1], buf);
14993 if (p == NULL)
14994 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014995 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14996 err = TRUE;
14997 if (fclose(fd) != 0)
14998 err = TRUE;
14999 if (err)
15000 {
15001 EMSG(_("E677: Error writing temp file"));
15002 goto done;
15003 }
15004 }
15005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015006 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015007
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008#ifdef USE_CR
15009 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015010 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 {
15012 char_u *s;
15013
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015014 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 {
15016 if (*s == CAR)
15017 *s = NL;
15018 }
15019 }
15020#else
15021# ifdef USE_CRNL
15022 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015023 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 {
15025 char_u *s, *d;
15026
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015027 d = res;
15028 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 {
15030 if (s[0] == CAR && s[1] == NL)
15031 ++s;
15032 *d++ = *s;
15033 }
15034 *d = NUL;
15035 }
15036# endif
15037#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015038
15039done:
15040 if (infile != NULL)
15041 {
15042 mch_remove(infile);
15043 vim_free(infile);
15044 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 rettv->v_type = VAR_STRING;
15046 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047}
15048
15049/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015050 * "tabpagebuflist()" function
15051 */
15052/* ARGSUSED */
15053 static void
15054f_tabpagebuflist(argvars, rettv)
15055 typval_T *argvars;
15056 typval_T *rettv;
15057{
15058#ifndef FEAT_WINDOWS
15059 rettv->vval.v_number = 0;
15060#else
15061 tabpage_T *tp;
15062 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015063
15064 if (argvars[0].v_type == VAR_UNKNOWN)
15065 wp = firstwin;
15066 else
15067 {
15068 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15069 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015070 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015071 }
15072 if (wp == NULL)
15073 rettv->vval.v_number = 0;
15074 else
15075 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015076 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015077 rettv->vval.v_number = 0;
15078 else
15079 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015080 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015081 if (list_append_number(rettv->vval.v_list,
15082 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015083 break;
15084 }
15085 }
15086#endif
15087}
15088
15089
15090/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015091 * "tabpagenr()" function
15092 */
15093/* ARGSUSED */
15094 static void
15095f_tabpagenr(argvars, rettv)
15096 typval_T *argvars;
15097 typval_T *rettv;
15098{
15099 int nr = 1;
15100#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015101 char_u *arg;
15102
15103 if (argvars[0].v_type != VAR_UNKNOWN)
15104 {
15105 arg = get_tv_string_chk(&argvars[0]);
15106 nr = 0;
15107 if (arg != NULL)
15108 {
15109 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015110 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015111 else
15112 EMSG2(_(e_invexpr2), arg);
15113 }
15114 }
15115 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015116 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015117#endif
15118 rettv->vval.v_number = nr;
15119}
15120
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015121
15122#ifdef FEAT_WINDOWS
15123static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15124
15125/*
15126 * Common code for tabpagewinnr() and winnr().
15127 */
15128 static int
15129get_winnr(tp, argvar)
15130 tabpage_T *tp;
15131 typval_T *argvar;
15132{
15133 win_T *twin;
15134 int nr = 1;
15135 win_T *wp;
15136 char_u *arg;
15137
15138 twin = (tp == curtab) ? curwin : tp->tp_curwin;
15139 if (argvar->v_type != VAR_UNKNOWN)
15140 {
15141 arg = get_tv_string_chk(argvar);
15142 if (arg == NULL)
15143 nr = 0; /* type error; errmsg already given */
15144 else if (STRCMP(arg, "$") == 0)
15145 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
15146 else if (STRCMP(arg, "#") == 0)
15147 {
15148 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
15149 if (twin == NULL)
15150 nr = 0;
15151 }
15152 else
15153 {
15154 EMSG2(_(e_invexpr2), arg);
15155 nr = 0;
15156 }
15157 }
15158
15159 if (nr > 0)
15160 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
15161 wp != twin; wp = wp->w_next)
15162 ++nr;
15163 return nr;
15164}
15165#endif
15166
15167/*
15168 * "tabpagewinnr()" function
15169 */
15170/* ARGSUSED */
15171 static void
15172f_tabpagewinnr(argvars, rettv)
15173 typval_T *argvars;
15174 typval_T *rettv;
15175{
15176 int nr = 1;
15177#ifdef FEAT_WINDOWS
15178 tabpage_T *tp;
15179
15180 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15181 if (tp == NULL)
15182 nr = 0;
15183 else
15184 nr = get_winnr(tp, &argvars[1]);
15185#endif
15186 rettv->vval.v_number = nr;
15187}
15188
15189
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015190/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015191 * "tagfiles()" function
15192 */
15193/*ARGSUSED*/
15194 static void
15195f_tagfiles(argvars, rettv)
15196 typval_T *argvars;
15197 typval_T *rettv;
15198{
15199 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015200 tagname_T tn;
15201 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015202
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015203 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015204 {
15205 rettv->vval.v_number = 0;
15206 return;
15207 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015208
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015209 for (first = TRUE; ; first = FALSE)
15210 if (get_tagfname(&tn, first, fname) == FAIL
15211 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015212 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015213 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000015214}
15215
15216/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000015217 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015218 */
15219 static void
15220f_taglist(argvars, rettv)
15221 typval_T *argvars;
15222 typval_T *rettv;
15223{
15224 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015225
15226 tag_pattern = get_tv_string(&argvars[0]);
15227
15228 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 if (*tag_pattern == NUL)
15230 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015231
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015232 if (rettv_list_alloc(rettv) == OK)
15233 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015234}
15235
15236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 * "tempname()" function
15238 */
15239/*ARGSUSED*/
15240 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015241f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015242 typval_T *argvars;
15243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244{
15245 static int x = 'A';
15246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015247 rettv->v_type = VAR_STRING;
15248 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249
15250 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
15251 * names. Skip 'I' and 'O', they are used for shell redirection. */
15252 do
15253 {
15254 if (x == 'Z')
15255 x = '0';
15256 else if (x == '9')
15257 x = 'A';
15258 else
15259 {
15260#ifdef EBCDIC
15261 if (x == 'I')
15262 x = 'J';
15263 else if (x == 'R')
15264 x = 'S';
15265 else
15266#endif
15267 ++x;
15268 }
15269 } while (x == 'I' || x == 'O');
15270}
15271
15272/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000015273 * "test(list)" function: Just checking the walls...
15274 */
15275/*ARGSUSED*/
15276 static void
15277f_test(argvars, rettv)
15278 typval_T *argvars;
15279 typval_T *rettv;
15280{
15281 /* Used for unit testing. Change the code below to your liking. */
15282#if 0
15283 listitem_T *li;
15284 list_T *l;
15285 char_u *bad, *good;
15286
15287 if (argvars[0].v_type != VAR_LIST)
15288 return;
15289 l = argvars[0].vval.v_list;
15290 if (l == NULL)
15291 return;
15292 li = l->lv_first;
15293 if (li == NULL)
15294 return;
15295 bad = get_tv_string(&li->li_tv);
15296 li = li->li_next;
15297 if (li == NULL)
15298 return;
15299 good = get_tv_string(&li->li_tv);
15300 rettv->vval.v_number = test_edit_score(bad, good);
15301#endif
15302}
15303
15304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 * "tolower(string)" function
15306 */
15307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015309 typval_T *argvars;
15310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311{
15312 char_u *p;
15313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015314 p = vim_strsave(get_tv_string(&argvars[0]));
15315 rettv->v_type = VAR_STRING;
15316 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317
15318 if (p != NULL)
15319 while (*p != NUL)
15320 {
15321#ifdef FEAT_MBYTE
15322 int l;
15323
15324 if (enc_utf8)
15325 {
15326 int c, lc;
15327
15328 c = utf_ptr2char(p);
15329 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015330 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 /* TODO: reallocate string when byte count changes. */
15332 if (utf_char2len(lc) == l)
15333 utf_char2bytes(lc, p);
15334 p += l;
15335 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015336 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 p += l; /* skip multi-byte character */
15338 else
15339#endif
15340 {
15341 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
15342 ++p;
15343 }
15344 }
15345}
15346
15347/*
15348 * "toupper(string)" function
15349 */
15350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015352 typval_T *argvars;
15353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015356 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357}
15358
15359/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000015360 * "tr(string, fromstr, tostr)" function
15361 */
15362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015364 typval_T *argvars;
15365 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015366{
15367 char_u *instr;
15368 char_u *fromstr;
15369 char_u *tostr;
15370 char_u *p;
15371#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000015372 int inlen;
15373 int fromlen;
15374 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015375 int idx;
15376 char_u *cpstr;
15377 int cplen;
15378 int first = TRUE;
15379#endif
15380 char_u buf[NUMBUFLEN];
15381 char_u buf2[NUMBUFLEN];
15382 garray_T ga;
15383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015385 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
15386 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015387
15388 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389 rettv->v_type = VAR_STRING;
15390 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015391 if (fromstr == NULL || tostr == NULL)
15392 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000015393 ga_init2(&ga, (int)sizeof(char), 80);
15394
15395#ifdef FEAT_MBYTE
15396 if (!has_mbyte)
15397#endif
15398 /* not multi-byte: fromstr and tostr must be the same length */
15399 if (STRLEN(fromstr) != STRLEN(tostr))
15400 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015401#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000015402error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015403#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000015404 EMSG2(_(e_invarg2), fromstr);
15405 ga_clear(&ga);
15406 return;
15407 }
15408
15409 /* fromstr and tostr have to contain the same number of chars */
15410 while (*instr != NUL)
15411 {
15412#ifdef FEAT_MBYTE
15413 if (has_mbyte)
15414 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015415 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015416 cpstr = instr;
15417 cplen = inlen;
15418 idx = 0;
15419 for (p = fromstr; *p != NUL; p += fromlen)
15420 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015421 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015422 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
15423 {
15424 for (p = tostr; *p != NUL; p += tolen)
15425 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015426 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015427 if (idx-- == 0)
15428 {
15429 cplen = tolen;
15430 cpstr = p;
15431 break;
15432 }
15433 }
15434 if (*p == NUL) /* tostr is shorter than fromstr */
15435 goto error;
15436 break;
15437 }
15438 ++idx;
15439 }
15440
15441 if (first && cpstr == instr)
15442 {
15443 /* Check that fromstr and tostr have the same number of
15444 * (multi-byte) characters. Done only once when a character
15445 * of instr doesn't appear in fromstr. */
15446 first = FALSE;
15447 for (p = tostr; *p != NUL; p += tolen)
15448 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015449 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015450 --idx;
15451 }
15452 if (idx != 0)
15453 goto error;
15454 }
15455
15456 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000015457 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000015458 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015459
15460 instr += inlen;
15461 }
15462 else
15463#endif
15464 {
15465 /* When not using multi-byte chars we can do it faster. */
15466 p = vim_strchr(fromstr, *instr);
15467 if (p != NULL)
15468 ga_append(&ga, tostr[p - fromstr]);
15469 else
15470 ga_append(&ga, *instr);
15471 ++instr;
15472 }
15473 }
15474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015475 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000015476}
15477
15478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 * "type(expr)" function
15480 */
15481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015482f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015483 typval_T *argvars;
15484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015486 int n;
15487
15488 switch (argvars[0].v_type)
15489 {
15490 case VAR_NUMBER: n = 0; break;
15491 case VAR_STRING: n = 1; break;
15492 case VAR_FUNC: n = 2; break;
15493 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015494 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015495 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
15496 }
15497 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498}
15499
15500/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015501 * "values(dict)" function
15502 */
15503 static void
15504f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015505 typval_T *argvars;
15506 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015507{
15508 dict_list(argvars, rettv, 1);
15509}
15510
15511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 * "virtcol(string)" function
15513 */
15514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015515f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015516 typval_T *argvars;
15517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518{
15519 colnr_T vcol = 0;
15520 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015521 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015523 fp = var2fpos(&argvars[0], FALSE, &fnum);
15524 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15525 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 {
15527 getvvcol(curwin, fp, NULL, NULL, &vcol);
15528 ++vcol;
15529 }
15530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532}
15533
15534/*
15535 * "visualmode()" function
15536 */
15537/*ARGSUSED*/
15538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015539f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015540 typval_T *argvars;
15541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542{
15543#ifdef FEAT_VISUAL
15544 char_u str[2];
15545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015546 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 str[0] = curbuf->b_visual_mode_eval;
15548 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550
15551 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015552 if ((argvars[0].v_type == VAR_NUMBER
15553 && argvars[0].vval.v_number != 0)
15554 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 curbuf->b_visual_mode_eval = NUL;
15557#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559#endif
15560}
15561
15562/*
15563 * "winbufnr(nr)" function
15564 */
15565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 typval_T *argvars;
15568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569{
15570 win_T *wp;
15571
15572 wp = find_win_by_nr(&argvars[0]);
15573 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577}
15578
15579/*
15580 * "wincol()" function
15581 */
15582/*ARGSUSED*/
15583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015584f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 typval_T *argvars;
15586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587{
15588 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015589 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590}
15591
15592/*
15593 * "winheight(nr)" function
15594 */
15595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015596f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015597 typval_T *argvars;
15598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599{
15600 win_T *wp;
15601
15602 wp = find_win_by_nr(&argvars[0]);
15603 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015604 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015606 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
15609/*
15610 * "winline()" function
15611 */
15612/*ARGSUSED*/
15613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015614f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 typval_T *argvars;
15616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617{
15618 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620}
15621
15622/*
15623 * "winnr()" function
15624 */
15625/* ARGSUSED */
15626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015628 typval_T *argvars;
15629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630{
15631 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015632
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015634 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637}
15638
15639/*
15640 * "winrestcmd()" function
15641 */
15642/* ARGSUSED */
15643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015644f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015645 typval_T *argvars;
15646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647{
15648#ifdef FEAT_WINDOWS
15649 win_T *wp;
15650 int winnr = 1;
15651 garray_T ga;
15652 char_u buf[50];
15653
15654 ga_init2(&ga, (int)sizeof(char), 70);
15655 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15656 {
15657 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15658 ga_concat(&ga, buf);
15659# ifdef FEAT_VERTSPLIT
15660 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15661 ga_concat(&ga, buf);
15662# endif
15663 ++winnr;
15664 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015665 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015667 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015669 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015671 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672}
15673
15674/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015675 * "winrestview()" function
15676 */
15677/* ARGSUSED */
15678 static void
15679f_winrestview(argvars, rettv)
15680 typval_T *argvars;
15681 typval_T *rettv;
15682{
15683 dict_T *dict;
15684
15685 if (argvars[0].v_type != VAR_DICT
15686 || (dict = argvars[0].vval.v_dict) == NULL)
15687 EMSG(_(e_invarg));
15688 else
15689 {
15690 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
15691 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
15692#ifdef FEAT_VIRTUALEDIT
15693 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
15694#endif
15695 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015696 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015697
15698 curwin->w_topline = get_dict_number(dict, (char_u *)"topline");
15699#ifdef FEAT_DIFF
15700 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
15701#endif
15702 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
15703 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
15704
15705 check_cursor();
15706 changed_cline_bef_curs();
15707 invalidate_botline();
15708 redraw_later(VALID);
15709
15710 if (curwin->w_topline == 0)
15711 curwin->w_topline = 1;
15712 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15713 curwin->w_topline = curbuf->b_ml.ml_line_count;
15714#ifdef FEAT_DIFF
15715 check_topfill(curwin, TRUE);
15716#endif
15717 }
15718}
15719
15720/*
15721 * "winsaveview()" function
15722 */
15723/* ARGSUSED */
15724 static void
15725f_winsaveview(argvars, rettv)
15726 typval_T *argvars;
15727 typval_T *rettv;
15728{
15729 dict_T *dict;
15730
15731 dict = dict_alloc();
15732 if (dict == NULL)
15733 return;
15734 rettv->v_type = VAR_DICT;
15735 rettv->vval.v_dict = dict;
15736 ++dict->dv_refcount;
15737
15738 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
15739 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
15740#ifdef FEAT_VIRTUALEDIT
15741 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
15742#endif
15743 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
15744
15745 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
15746#ifdef FEAT_DIFF
15747 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
15748#endif
15749 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
15750 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
15751}
15752
15753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 * "winwidth(nr)" function
15755 */
15756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015757f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015758 typval_T *argvars;
15759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760{
15761 win_T *wp;
15762
15763 wp = find_win_by_nr(&argvars[0]);
15764 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 else
15767#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015770 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771#endif
15772}
15773
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015775 * "writefile()" function
15776 */
15777 static void
15778f_writefile(argvars, rettv)
15779 typval_T *argvars;
15780 typval_T *rettv;
15781{
15782 int binary = FALSE;
15783 char_u *fname;
15784 FILE *fd;
15785 listitem_T *li;
15786 char_u *s;
15787 int ret = 0;
15788 int c;
15789
15790 if (argvars[0].v_type != VAR_LIST)
15791 {
15792 EMSG2(_(e_listarg), "writefile()");
15793 return;
15794 }
15795 if (argvars[0].vval.v_list == NULL)
15796 return;
15797
15798 if (argvars[2].v_type != VAR_UNKNOWN
15799 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15800 binary = TRUE;
15801
15802 /* Always open the file in binary mode, library functions have a mind of
15803 * their own about CR-LF conversion. */
15804 fname = get_tv_string(&argvars[1]);
15805 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15806 {
15807 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15808 ret = -1;
15809 }
15810 else
15811 {
15812 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15813 li = li->li_next)
15814 {
15815 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15816 {
15817 if (*s == '\n')
15818 c = putc(NUL, fd);
15819 else
15820 c = putc(*s, fd);
15821 if (c == EOF)
15822 {
15823 ret = -1;
15824 break;
15825 }
15826 }
15827 if (!binary || li->li_next != NULL)
15828 if (putc('\n', fd) == EOF)
15829 {
15830 ret = -1;
15831 break;
15832 }
15833 if (ret < 0)
15834 {
15835 EMSG(_(e_write));
15836 break;
15837 }
15838 }
15839 fclose(fd);
15840 }
15841
15842 rettv->vval.v_number = ret;
15843}
15844
15845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015847 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 */
15849 static pos_T *
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015850var2fpos(varp, lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015851 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 int lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015853 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015855 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015857 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858
Bram Moolenaara5525202006-03-02 22:52:09 +000015859 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015860 if (varp->v_type == VAR_LIST)
15861 {
15862 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015863 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000015864 int error = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015865
15866 l = varp->vval.v_list;
15867 if (l == NULL)
15868 return NULL;
15869
15870 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015871 pos.lnum = list_find_nr(l, 0L, &error);
15872 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015873 return NULL; /* invalid line number */
15874
15875 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015876 pos.col = list_find_nr(l, 1L, &error);
15877 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015878 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015879 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaara5525202006-03-02 22:52:09 +000015880 /* Accept a position up to the NUL after the line. */
15881 if (pos.col <= 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015882 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000015883 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015884
Bram Moolenaara5525202006-03-02 22:52:09 +000015885#ifdef FEAT_VIRTUALEDIT
15886 /* Get the virtual offset. Defaults to zero. */
15887 pos.coladd = list_find_nr(l, 2L, &error);
15888 if (error)
15889 pos.coladd = 0;
15890#endif
15891
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015892 return &pos;
15893 }
15894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 name = get_tv_string_chk(varp);
15896 if (name == NULL)
15897 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 if (name[0] == '.') /* cursor */
15899 return &curwin->w_cursor;
15900 if (name[0] == '\'') /* mark */
15901 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015902 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15904 return NULL;
15905 return pp;
15906 }
Bram Moolenaara5525202006-03-02 22:52:09 +000015907
15908#ifdef FEAT_VIRTUALEDIT
15909 pos.coladd = 0;
15910#endif
15911
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015912 if (name[0] == 'w' && lnum)
15913 {
15914 pos.col = 0;
15915 if (name[1] == '0') /* "w0": first visible line */
15916 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015917 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015918 pos.lnum = curwin->w_topline;
15919 return &pos;
15920 }
15921 else if (name[1] == '$') /* "w$": last visible line */
15922 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000015923 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000015924 pos.lnum = curwin->w_botline - 1;
15925 return &pos;
15926 }
15927 }
15928 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 {
15930 if (lnum)
15931 {
15932 pos.lnum = curbuf->b_ml.ml_line_count;
15933 pos.col = 0;
15934 }
15935 else
15936 {
15937 pos.lnum = curwin->w_cursor.lnum;
15938 pos.col = (colnr_T)STRLEN(ml_get_curline());
15939 }
15940 return &pos;
15941 }
15942 return NULL;
15943}
15944
15945/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015946 * Convert list in "arg" into a position and optional file number.
15947 * When "fnump" is NULL there is no file number, only 3 items.
15948 * Note that the column is passed on as-is, the caller may want to decrement
15949 * it to use 1 for the first column.
15950 * Return FAIL when conversion is not possible, doesn't check the position for
15951 * validity.
15952 */
15953 static int
15954list2fpos(arg, posp, fnump)
15955 typval_T *arg;
15956 pos_T *posp;
15957 int *fnump;
15958{
15959 list_T *l = arg->vval.v_list;
15960 long i = 0;
15961 long n;
15962
15963 /* List must be: [fnum, lnum, col, coladd] */
15964 if (arg->v_type != VAR_LIST || l == NULL
15965 || l->lv_len != (fnump == NULL ? 3 : 4))
15966 return FAIL;
15967
15968 if (fnump != NULL)
15969 {
15970 n = list_find_nr(l, i++, NULL); /* fnum */
15971 if (n < 0)
15972 return FAIL;
15973 if (n == 0)
15974 n = curbuf->b_fnum; /* current buffer */
15975 *fnump = n;
15976 }
15977
15978 n = list_find_nr(l, i++, NULL); /* lnum */
15979 if (n < 0)
15980 return FAIL;
15981 posp->lnum = n;
15982
15983 n = list_find_nr(l, i++, NULL); /* col */
15984 if (n < 0)
15985 return FAIL;
15986 posp->col = n;
15987
15988#ifdef FEAT_VIRTUALEDIT
15989 n = list_find_nr(l, i, NULL);
15990 if (n < 0)
15991 return FAIL;
15992 posp->coladd = n;
15993#endif
15994
15995 return OK;
15996}
15997
15998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 * Get the length of an environment variable name.
16000 * Advance "arg" to the first character after the name.
16001 * Return 0 for error.
16002 */
16003 static int
16004get_env_len(arg)
16005 char_u **arg;
16006{
16007 char_u *p;
16008 int len;
16009
16010 for (p = *arg; vim_isIDc(*p); ++p)
16011 ;
16012 if (p == *arg) /* no name found */
16013 return 0;
16014
16015 len = (int)(p - *arg);
16016 *arg = p;
16017 return len;
16018}
16019
16020/*
16021 * Get the length of the name of a function or internal variable.
16022 * "arg" is advanced to the first non-white character after the name.
16023 * Return 0 if something is wrong.
16024 */
16025 static int
16026get_id_len(arg)
16027 char_u **arg;
16028{
16029 char_u *p;
16030 int len;
16031
16032 /* Find the end of the name. */
16033 for (p = *arg; eval_isnamec(*p); ++p)
16034 ;
16035 if (p == *arg) /* no name found */
16036 return 0;
16037
16038 len = (int)(p - *arg);
16039 *arg = skipwhite(p);
16040
16041 return len;
16042}
16043
16044/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016045 * Get the length of the name of a variable or function.
16046 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016048 * Return -1 if curly braces expansion failed.
16049 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 * If the name contains 'magic' {}'s, expand them and return the
16051 * expanded name in an allocated string via 'alias' - caller must free.
16052 */
16053 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016054get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 char_u **arg;
16056 char_u **alias;
16057 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016058 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059{
16060 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 char_u *p;
16062 char_u *expr_start;
16063 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064
16065 *alias = NULL; /* default to no alias */
16066
16067 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16068 && (*arg)[2] == (int)KE_SNR)
16069 {
16070 /* hard coded <SNR>, already translated */
16071 *arg += 3;
16072 return get_id_len(arg) + 3;
16073 }
16074 len = eval_fname_script(*arg);
16075 if (len > 0)
16076 {
16077 /* literal "<SID>", "s:" or "<SNR>" */
16078 *arg += len;
16079 }
16080
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016082 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016084 p = find_name_end(*arg, &expr_start, &expr_end,
16085 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 if (expr_start != NULL)
16087 {
16088 char_u *temp_string;
16089
16090 if (!evaluate)
16091 {
16092 len += (int)(p - *arg);
16093 *arg = skipwhite(p);
16094 return len;
16095 }
16096
16097 /*
16098 * Include any <SID> etc in the expanded string:
16099 * Thus the -len here.
16100 */
16101 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16102 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016103 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016104 *alias = temp_string;
16105 *arg = skipwhite(p);
16106 return (int)STRLEN(temp_string);
16107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108
16109 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016110 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 EMSG2(_(e_invexpr2), *arg);
16112
16113 return len;
16114}
16115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016116/*
16117 * Find the end of a variable or function name, taking care of magic braces.
16118 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16119 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016120 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016121 * Return a pointer to just after the name. Equal to "arg" if there is no
16122 * valid name.
16123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016125find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126 char_u *arg;
16127 char_u **expr_start;
16128 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016129 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016131 int mb_nest = 0;
16132 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 char_u *p;
16134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016135 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016137 *expr_start = NULL;
16138 *expr_end = NULL;
16139 }
16140
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016141 /* Quick check for valid starting character. */
16142 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
16143 return arg;
16144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145 for (p = arg; *p != NUL
16146 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016147 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016148 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016149 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000016150 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016151 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000016152 if (*p == '\'')
16153 {
16154 /* skip over 'string' to avoid counting [ and ] inside it. */
16155 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
16156 ;
16157 if (*p == NUL)
16158 break;
16159 }
16160 else if (*p == '"')
16161 {
16162 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
16163 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
16164 if (*p == '\\' && p[1] != NUL)
16165 ++p;
16166 if (*p == NUL)
16167 break;
16168 }
16169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016172 if (*p == '[')
16173 ++br_nest;
16174 else if (*p == ']')
16175 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000016177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016178 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016180 if (*p == '{')
16181 {
16182 mb_nest++;
16183 if (expr_start != NULL && *expr_start == NULL)
16184 *expr_start = p;
16185 }
16186 else if (*p == '}')
16187 {
16188 mb_nest--;
16189 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
16190 *expr_end = p;
16191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 }
16194
16195 return p;
16196}
16197
16198/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016199 * Expands out the 'magic' {}'s in a variable/function name.
16200 * Note that this can call itself recursively, to deal with
16201 * constructs like foo{bar}{baz}{bam}
16202 * The four pointer arguments point to "foo{expre}ss{ion}bar"
16203 * "in_start" ^
16204 * "expr_start" ^
16205 * "expr_end" ^
16206 * "in_end" ^
16207 *
16208 * Returns a new allocated string, which the caller must free.
16209 * Returns NULL for failure.
16210 */
16211 static char_u *
16212make_expanded_name(in_start, expr_start, expr_end, in_end)
16213 char_u *in_start;
16214 char_u *expr_start;
16215 char_u *expr_end;
16216 char_u *in_end;
16217{
16218 char_u c1;
16219 char_u *retval = NULL;
16220 char_u *temp_result;
16221 char_u *nextcmd = NULL;
16222
16223 if (expr_end == NULL || in_end == NULL)
16224 return NULL;
16225 *expr_start = NUL;
16226 *expr_end = NUL;
16227 c1 = *in_end;
16228 *in_end = NUL;
16229
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016230 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231 if (temp_result != NULL && nextcmd == NULL)
16232 {
16233 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
16234 + (in_end - expr_end) + 1));
16235 if (retval != NULL)
16236 {
16237 STRCPY(retval, in_start);
16238 STRCAT(retval, temp_result);
16239 STRCAT(retval, expr_end + 1);
16240 }
16241 }
16242 vim_free(temp_result);
16243
16244 *in_end = c1; /* put char back for error messages */
16245 *expr_start = '{';
16246 *expr_end = '}';
16247
16248 if (retval != NULL)
16249 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016250 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016251 if (expr_start != NULL)
16252 {
16253 /* Further expansion! */
16254 temp_result = make_expanded_name(retval, expr_start,
16255 expr_end, temp_result);
16256 vim_free(retval);
16257 retval = temp_result;
16258 }
16259 }
16260
16261 return retval;
16262}
16263
16264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016266 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 */
16268 static int
16269eval_isnamec(c)
16270 int c;
16271{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016272 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
16273}
16274
16275/*
16276 * Return TRUE if character "c" can be used as the first character in a
16277 * variable or function name (excluding '{' and '}').
16278 */
16279 static int
16280eval_isnamec1(c)
16281 int c;
16282{
16283 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284}
16285
16286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 * Set number v: variable to "val".
16288 */
16289 void
16290set_vim_var_nr(idx, val)
16291 int idx;
16292 long val;
16293{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016294 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295}
16296
16297/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016298 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 */
16300 long
16301get_vim_var_nr(idx)
16302 int idx;
16303{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016304 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305}
16306
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016307#if defined(FEAT_AUTOCMD) || defined(PROTO)
16308/*
16309 * Get string v: variable value. Uses a static buffer, can only be used once.
16310 */
16311 char_u *
16312get_vim_var_str(idx)
16313 int idx;
16314{
16315 return get_tv_string(&vimvars[idx].vv_tv);
16316}
16317#endif
16318
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319/*
16320 * Set v:count, v:count1 and v:prevcount.
16321 */
16322 void
16323set_vcount(count, count1)
16324 long count;
16325 long count1;
16326{
Bram Moolenaare9a41262005-01-15 22:18:47 +000016327 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
16328 vimvars[VV_COUNT].vv_nr = count;
16329 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330}
16331
16332/*
16333 * Set string v: variable to a copy of "val".
16334 */
16335 void
16336set_vim_var_string(idx, val, len)
16337 int idx;
16338 char_u *val;
16339 int len; /* length of "val" to use or -1 (whole string) */
16340{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016341 /* Need to do this (at least) once, since we can't initialize a union.
16342 * Will always be invoked when "v:progname" is set. */
16343 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
16344
Bram Moolenaare9a41262005-01-15 22:18:47 +000016345 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016347 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016349 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000016351 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352}
16353
16354/*
16355 * Set v:register if needed.
16356 */
16357 void
16358set_reg_var(c)
16359 int c;
16360{
16361 char_u regname;
16362
16363 if (c == 0 || c == ' ')
16364 regname = '"';
16365 else
16366 regname = c;
16367 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000016368 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 set_vim_var_string(VV_REG, &regname, 1);
16370}
16371
16372/*
16373 * Get or set v:exception. If "oldval" == NULL, return the current value.
16374 * Otherwise, restore the value to "oldval" and return NULL.
16375 * Must always be called in pairs to save and restore v:exception! Does not
16376 * take care of memory allocations.
16377 */
16378 char_u *
16379v_exception(oldval)
16380 char_u *oldval;
16381{
16382 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016383 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384
Bram Moolenaare9a41262005-01-15 22:18:47 +000016385 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 return NULL;
16387}
16388
16389/*
16390 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
16391 * Otherwise, restore the value to "oldval" and return NULL.
16392 * Must always be called in pairs to save and restore v:throwpoint! Does not
16393 * take care of memory allocations.
16394 */
16395 char_u *
16396v_throwpoint(oldval)
16397 char_u *oldval;
16398{
16399 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016400 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401
Bram Moolenaare9a41262005-01-15 22:18:47 +000016402 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 return NULL;
16404}
16405
16406#if defined(FEAT_AUTOCMD) || defined(PROTO)
16407/*
16408 * Set v:cmdarg.
16409 * If "eap" != NULL, use "eap" to generate the value and return the old value.
16410 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
16411 * Must always be called in pairs!
16412 */
16413 char_u *
16414set_cmdarg(eap, oldarg)
16415 exarg_T *eap;
16416 char_u *oldarg;
16417{
16418 char_u *oldval;
16419 char_u *newval;
16420 unsigned len;
16421
Bram Moolenaare9a41262005-01-15 22:18:47 +000016422 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016423 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016425 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000016426 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016427 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 }
16429
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016430 if (eap->force_bin == FORCE_BIN)
16431 len = 6;
16432 else if (eap->force_bin == FORCE_NOBIN)
16433 len = 8;
16434 else
16435 len = 0;
16436 if (eap->force_ff != 0)
16437 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
16438# ifdef FEAT_MBYTE
16439 if (eap->force_enc != 0)
16440 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016441 if (eap->bad_char != 0)
16442 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016443# endif
16444
16445 newval = alloc(len + 1);
16446 if (newval == NULL)
16447 return NULL;
16448
16449 if (eap->force_bin == FORCE_BIN)
16450 sprintf((char *)newval, " ++bin");
16451 else if (eap->force_bin == FORCE_NOBIN)
16452 sprintf((char *)newval, " ++nobin");
16453 else
16454 *newval = NUL;
16455 if (eap->force_ff != 0)
16456 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
16457 eap->cmd + eap->force_ff);
16458# ifdef FEAT_MBYTE
16459 if (eap->force_enc != 0)
16460 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
16461 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000016462 if (eap->bad_char != 0)
16463 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
16464 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016465# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000016466 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000016467 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468}
16469#endif
16470
16471/*
16472 * Get the value of internal variable "name".
16473 * Return OK or FAIL.
16474 */
16475 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016476get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477 char_u *name;
16478 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000016479 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016480 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016481{
16482 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000016483 typval_T *tv = NULL;
16484 typval_T atv;
16485 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487
16488 /* truncate the name, so that we can use strcmp() */
16489 cc = name[len];
16490 name[len] = NUL;
16491
16492 /*
16493 * Check for "b:changedtick".
16494 */
16495 if (STRCMP(name, "b:changedtick") == 0)
16496 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000016497 atv.v_type = VAR_NUMBER;
16498 atv.vval.v_number = curbuf->b_changedtick;
16499 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 }
16501
16502 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 * Check for user-defined variables.
16504 */
16505 else
16506 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016507 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016509 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 }
16511
Bram Moolenaare9a41262005-01-15 22:18:47 +000016512 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016514 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016515 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 ret = FAIL;
16517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016518 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016519 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520
16521 name[len] = cc;
16522
16523 return ret;
16524}
16525
16526/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016527 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
16528 * Also handle function call with Funcref variable: func(expr)
16529 * Can all be combined: dict.func(expr)[idx]['func'](expr)
16530 */
16531 static int
16532handle_subscript(arg, rettv, evaluate, verbose)
16533 char_u **arg;
16534 typval_T *rettv;
16535 int evaluate; /* do more than finding the end */
16536 int verbose; /* give error messages */
16537{
16538 int ret = OK;
16539 dict_T *selfdict = NULL;
16540 char_u *s;
16541 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000016542 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016543
16544 while (ret == OK
16545 && (**arg == '['
16546 || (**arg == '.' && rettv->v_type == VAR_DICT)
16547 || (**arg == '(' && rettv->v_type == VAR_FUNC))
16548 && !vim_iswhite(*(*arg - 1)))
16549 {
16550 if (**arg == '(')
16551 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000016552 /* need to copy the funcref so that we can clear rettv */
16553 functv = *rettv;
16554 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016555
16556 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000016557 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016558 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000016559 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
16560 &len, evaluate, selfdict);
16561
16562 /* Clear the funcref afterwards, so that deleting it while
16563 * evaluating the arguments is possible (see test55). */
16564 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016565
16566 /* Stop the expression evaluation when immediately aborting on
16567 * error, or when an interrupt occurred or an exception was thrown
16568 * but not caught. */
16569 if (aborting())
16570 {
16571 if (ret == OK)
16572 clear_tv(rettv);
16573 ret = FAIL;
16574 }
16575 dict_unref(selfdict);
16576 selfdict = NULL;
16577 }
16578 else /* **arg == '[' || **arg == '.' */
16579 {
16580 dict_unref(selfdict);
16581 if (rettv->v_type == VAR_DICT)
16582 {
16583 selfdict = rettv->vval.v_dict;
16584 if (selfdict != NULL)
16585 ++selfdict->dv_refcount;
16586 }
16587 else
16588 selfdict = NULL;
16589 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
16590 {
16591 clear_tv(rettv);
16592 ret = FAIL;
16593 }
16594 }
16595 }
16596 dict_unref(selfdict);
16597 return ret;
16598}
16599
16600/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016601 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
16602 * value).
16603 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016604 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016605alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016606{
Bram Moolenaar33570922005-01-25 22:26:29 +000016607 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016608}
16609
16610/*
16611 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 * The string "s" must have been allocated, it is consumed.
16613 * Return NULL for out of memory, the variable otherwise.
16614 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016616alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016617 char_u *s;
16618{
Bram Moolenaar33570922005-01-25 22:26:29 +000016619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016621 rettv = alloc_tv();
16622 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016624 rettv->v_type = VAR_STRING;
16625 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 }
16627 else
16628 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016629 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630}
16631
16632/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016633 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000016635 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016637 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638{
16639 if (varp != NULL)
16640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016641 switch (varp->v_type)
16642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016643 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016644 func_unref(varp->vval.v_string);
16645 /*FALLTHROUGH*/
16646 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016647 vim_free(varp->vval.v_string);
16648 break;
16649 case VAR_LIST:
16650 list_unref(varp->vval.v_list);
16651 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016652 case VAR_DICT:
16653 dict_unref(varp->vval.v_dict);
16654 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016655 case VAR_NUMBER:
16656 case VAR_UNKNOWN:
16657 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016658 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016659 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016660 break;
16661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 vim_free(varp);
16663 }
16664}
16665
16666/*
16667 * Free the memory for a variable value and set the value to NULL or 0.
16668 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016669 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672{
16673 if (varp != NULL)
16674 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016675 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016677 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016678 func_unref(varp->vval.v_string);
16679 /*FALLTHROUGH*/
16680 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016681 vim_free(varp->vval.v_string);
16682 varp->vval.v_string = NULL;
16683 break;
16684 case VAR_LIST:
16685 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016686 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016687 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016688 case VAR_DICT:
16689 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016690 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016691 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016693 varp->vval.v_number = 0;
16694 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016695 case VAR_UNKNOWN:
16696 break;
16697 default:
16698 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016700 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 }
16702}
16703
16704/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016705 * Set the value of a variable to NULL without freeing items.
16706 */
16707 static void
16708init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016709 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016710{
16711 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016712 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016713}
16714
16715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 * Get the number value of a variable.
16717 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016718 * For incompatible types, return 0.
16719 * get_tv_number_chk() is similar to get_tv_number(), but informs the
16720 * caller of incompatible types: it sets *denote to TRUE if "denote"
16721 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 */
16723 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016724get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016725 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016727 int error = FALSE;
16728
16729 return get_tv_number_chk(varp, &error); /* return 0L on error */
16730}
16731
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016732 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016733get_tv_number_chk(varp, denote)
16734 typval_T *varp;
16735 int *denote;
16736{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016737 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016739 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016741 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016743 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016744 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016745 break;
16746 case VAR_STRING:
16747 if (varp->vval.v_string != NULL)
16748 vim_str2nr(varp->vval.v_string, NULL, NULL,
16749 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016750 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016751 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000016752 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016753 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016754 case VAR_DICT:
16755 EMSG(_("E728: Using a Dictionary as a number"));
16756 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016757 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016758 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016759 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 if (denote == NULL) /* useful for values that must be unsigned */
16762 n = -1;
16763 else
16764 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016765 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766}
16767
16768/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016769 * Get the lnum from the first argument.
16770 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016771 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 */
16773 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016774get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000016775 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776{
Bram Moolenaar33570922005-01-25 22:26:29 +000016777 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 linenr_T lnum;
16779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 if (lnum == 0) /* no valid number, try using line() */
16782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016783 rettv.v_type = VAR_NUMBER;
16784 f_line(argvars, &rettv);
16785 lnum = rettv.vval.v_number;
16786 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 }
16788 return lnum;
16789}
16790
16791/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000016792 * Get the lnum from the first argument.
16793 * Also accepts "$", then "buf" is used.
16794 * Returns 0 on error.
16795 */
16796 static linenr_T
16797get_tv_lnum_buf(argvars, buf)
16798 typval_T *argvars;
16799 buf_T *buf;
16800{
16801 if (argvars[0].v_type == VAR_STRING
16802 && argvars[0].vval.v_string != NULL
16803 && argvars[0].vval.v_string[0] == '$'
16804 && buf != NULL)
16805 return buf->b_ml.ml_line_count;
16806 return get_tv_number_chk(&argvars[0], NULL);
16807}
16808
16809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 * Get the string value of a variable.
16811 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000016812 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
16813 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814 * If the String variable has never been set, return an empty string.
16815 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016816 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
16817 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 */
16819 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016820get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016821 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822{
16823 static char_u mybuf[NUMBUFLEN];
16824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016825 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826}
16827
16828 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016829get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000016830 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016831 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016833 char_u *res = get_tv_string_buf_chk(varp, buf);
16834
16835 return res != NULL ? res : (char_u *)"";
16836}
16837
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016838 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016839get_tv_string_chk(varp)
16840 typval_T *varp;
16841{
16842 static char_u mybuf[NUMBUFLEN];
16843
16844 return get_tv_string_buf_chk(varp, mybuf);
16845}
16846
16847 static char_u *
16848get_tv_string_buf_chk(varp, buf)
16849 typval_T *varp;
16850 char_u *buf;
16851{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016852 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016854 case VAR_NUMBER:
16855 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16856 return buf;
16857 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016858 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016859 break;
16860 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016861 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016862 break;
16863 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016864 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016865 break;
16866 case VAR_STRING:
16867 if (varp->vval.v_string != NULL)
16868 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016869 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016870 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016871 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016872 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016874 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875}
16876
16877/*
16878 * Find variable "name" in the list of variables.
16879 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016880 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016881 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016882 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016884 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016885find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016887 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891
Bram Moolenaara7043832005-01-21 11:56:39 +000016892 ht = find_var_ht(name, &varname);
16893 if (htp != NULL)
16894 *htp = ht;
16895 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016897 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898}
16899
16900/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016901 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016902 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016905find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016907 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016908 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016909{
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 hashitem_T *hi;
16911
16912 if (*varname == NUL)
16913 {
16914 /* Must be something like "s:", otherwise "ht" would be NULL. */
16915 switch (varname[-2])
16916 {
16917 case 's': return &SCRIPT_SV(current_SID).sv_var;
16918 case 'g': return &globvars_var;
16919 case 'v': return &vimvars_var;
16920 case 'b': return &curbuf->b_bufvar;
16921 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016922 case 'l': return current_funccal == NULL
16923 ? NULL : &current_funccal->l_vars_var;
16924 case 'a': return current_funccal == NULL
16925 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016926 }
16927 return NULL;
16928 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016929
16930 hi = hash_find(ht, varname);
16931 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016932 {
16933 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016934 * worked find the variable again. Don't auto-load a script if it was
16935 * loaded already, otherwise it would be loaded every time when
16936 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016937 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016938 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016939 hi = hash_find(ht, varname);
16940 if (HASHITEM_EMPTY(hi))
16941 return NULL;
16942 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016943 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016944}
16945
16946/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016947 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016948 * Set "varname" to the start of name without ':'.
16949 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016950 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016951find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 char_u *name;
16953 char_u **varname;
16954{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016955 hashitem_T *hi;
16956
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 if (name[1] != ':')
16958 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016959 /* The name must not start with a colon or #. */
16960 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961 return NULL;
16962 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016963
16964 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016965 hi = hash_find(&compat_hashtab, name);
16966 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016967 return &compat_hashtab;
16968
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016970 return &globvarht; /* global variable */
16971 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972 }
16973 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016974 if (*name == 'g') /* global variable */
16975 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016976 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16977 */
16978 if (vim_strchr(name + 2, ':') != NULL
16979 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016980 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016985 if (*name == 'v') /* v: variable */
16986 return &vimvarht;
16987 if (*name == 'a' && current_funccal != NULL) /* function argument */
16988 return &current_funccal->l_avars.dv_hashtab;
16989 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16990 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991 if (*name == 's' /* script variable */
16992 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16993 return &SCRIPT_VARS(current_SID);
16994 return NULL;
16995}
16996
16997/*
16998 * Get the string value of a (global/local) variable.
16999 * Returns NULL when it doesn't exist.
17000 */
17001 char_u *
17002get_var_value(name)
17003 char_u *name;
17004{
Bram Moolenaar33570922005-01-25 22:26:29 +000017005 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006
Bram Moolenaara7043832005-01-21 11:56:39 +000017007 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008 if (v == NULL)
17009 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017010 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011}
17012
17013/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017014 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 * sourcing this script and when executing functions defined in the script.
17016 */
17017 void
17018new_script_vars(id)
17019 scid_T id;
17020{
Bram Moolenaara7043832005-01-21 11:56:39 +000017021 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017022 hashtab_T *ht;
17023 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017024
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17026 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017027 /* Re-allocating ga_data means that an ht_array pointing to
17028 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017029 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017030 for (i = 1; i <= ga_scripts.ga_len; ++i)
17031 {
17032 ht = &SCRIPT_VARS(i);
17033 if (ht->ht_mask == HT_INIT_SIZE - 1)
17034 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017035 sv = &SCRIPT_SV(i);
17036 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017037 }
17038
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 while (ga_scripts.ga_len < id)
17040 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017041 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17042 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 }
17045 }
17046}
17047
17048/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017049 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17050 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051 */
17052 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017053init_var_dict(dict, dict_var)
17054 dict_T *dict;
17055 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056{
Bram Moolenaar33570922005-01-25 22:26:29 +000017057 hash_init(&dict->dv_hashtab);
17058 dict->dv_refcount = 99999;
17059 dict_var->di_tv.vval.v_dict = dict;
17060 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017061 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017062 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17063 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064}
17065
17066/*
17067 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017068 * Frees all allocated variables and the value they contain.
17069 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 */
17071 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017072vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017073 hashtab_T *ht;
17074{
17075 vars_clear_ext(ht, TRUE);
17076}
17077
17078/*
17079 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17080 */
17081 static void
17082vars_clear_ext(ht, free_val)
17083 hashtab_T *ht;
17084 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085{
Bram Moolenaara7043832005-01-21 11:56:39 +000017086 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017087 hashitem_T *hi;
17088 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000017091 todo = ht->ht_used;
17092 for (hi = ht->ht_array; todo > 0; ++hi)
17093 {
17094 if (!HASHITEM_EMPTY(hi))
17095 {
17096 --todo;
17097
Bram Moolenaar33570922005-01-25 22:26:29 +000017098 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000017099 * ht_array might change then. hash_clear() takes care of it
17100 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017101 v = HI2DI(hi);
17102 if (free_val)
17103 clear_tv(&v->di_tv);
17104 if ((v->di_flags & DI_FLAGS_FIX) == 0)
17105 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000017106 }
17107 }
17108 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017109 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110}
17111
Bram Moolenaara7043832005-01-21 11:56:39 +000017112/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 * Delete a variable from hashtab "ht" at item "hi".
17114 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000017115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000017117delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000017118 hashtab_T *ht;
17119 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120{
Bram Moolenaar33570922005-01-25 22:26:29 +000017121 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017122
17123 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000017124 clear_tv(&di->di_tv);
17125 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126}
17127
17128/*
17129 * List the value of one internal variable.
17130 */
17131 static void
17132list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000017133 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 char_u *prefix;
17135{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017136 char_u *tofree;
17137 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017138 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017139
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017140 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000017141 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017142 s == NULL ? (char_u *)"" : s);
17143 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144}
17145
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 static void
17147list_one_var_a(prefix, name, type, string)
17148 char_u *prefix;
17149 char_u *name;
17150 int type;
17151 char_u *string;
17152{
17153 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
17154 if (name != NULL) /* "a:" vars don't have a name stored */
17155 msg_puts(name);
17156 msg_putchar(' ');
17157 msg_advance(22);
17158 if (type == VAR_NUMBER)
17159 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017160 else if (type == VAR_FUNC)
17161 msg_putchar('*');
17162 else if (type == VAR_LIST)
17163 {
17164 msg_putchar('[');
17165 if (*string == '[')
17166 ++string;
17167 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017168 else if (type == VAR_DICT)
17169 {
17170 msg_putchar('{');
17171 if (*string == '{')
17172 ++string;
17173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 else
17175 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017176
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017178
17179 if (type == VAR_FUNC)
17180 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181}
17182
17183/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017184 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 * If the variable already exists, the value is updated.
17186 * Otherwise the variable is created.
17187 */
17188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017189set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017191 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017192 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193{
Bram Moolenaar33570922005-01-25 22:26:29 +000017194 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017196 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017197 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017200 {
17201 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
17202 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
17203 ? name[2] : name[0]))
17204 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017205 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017206 return;
17207 }
17208 if (function_exists(name))
17209 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017210 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017211 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017212 return;
17213 }
17214 }
17215
Bram Moolenaara7043832005-01-21 11:56:39 +000017216 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017217 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000017218 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017219 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000017220 return;
17221 }
17222
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017223 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017224 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017226 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017227 if (var_check_ro(v->di_flags, name)
17228 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000017229 return;
17230 if (v->di_tv.v_type != tv->v_type
17231 && !((v->di_tv.v_type == VAR_STRING
17232 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017233 && (tv->v_type == VAR_STRING
17234 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017235 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017236 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017237 return;
17238 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017239
17240 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000017241 * Handle setting internal v: variables separately: we don't change
17242 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000017243 */
17244 if (ht == &vimvarht)
17245 {
17246 if (v->di_tv.v_type == VAR_STRING)
17247 {
17248 vim_free(v->di_tv.vval.v_string);
17249 if (copy || tv->v_type != VAR_STRING)
17250 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
17251 else
17252 {
17253 /* Take over the string to avoid an extra alloc/free. */
17254 v->di_tv.vval.v_string = tv->vval.v_string;
17255 tv->vval.v_string = NULL;
17256 }
17257 }
17258 else if (v->di_tv.v_type != VAR_NUMBER)
17259 EMSG2(_(e_intern2), "set_var()");
17260 else
17261 v->di_tv.vval.v_number = get_tv_number(tv);
17262 return;
17263 }
17264
17265 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 }
17267 else /* add a new variable */
17268 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000017269 /* Make sure the variable name is valid. */
17270 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000017271 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
17272 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000017273 {
17274 EMSG2(_(e_illvar), varname);
17275 return;
17276 }
17277
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017278 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17279 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000017280 if (v == NULL)
17281 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000017282 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017285 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 return;
17287 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017288 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017291 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000017292 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017293 else
17294 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017295 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017296 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299}
17300
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017301/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 * Return TRUE if di_flags "flags" indicate read-only variable "name".
17303 * Also give an error message.
17304 */
17305 static int
17306var_check_ro(flags, name)
17307 int flags;
17308 char_u *name;
17309{
17310 if (flags & DI_FLAGS_RO)
17311 {
17312 EMSG2(_(e_readonlyvar), name);
17313 return TRUE;
17314 }
17315 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
17316 {
17317 EMSG2(_(e_readonlysbx), name);
17318 return TRUE;
17319 }
17320 return FALSE;
17321}
17322
17323/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017324 * Return TRUE if typeval "tv" is set to be locked (immutable).
17325 * Also give an error message, using "name".
17326 */
17327 static int
17328tv_check_lock(lock, name)
17329 int lock;
17330 char_u *name;
17331{
17332 if (lock & VAR_LOCKED)
17333 {
17334 EMSG2(_("E741: Value is locked: %s"),
17335 name == NULL ? (char_u *)_("Unknown") : name);
17336 return TRUE;
17337 }
17338 if (lock & VAR_FIXED)
17339 {
17340 EMSG2(_("E742: Cannot change value of %s"),
17341 name == NULL ? (char_u *)_("Unknown") : name);
17342 return TRUE;
17343 }
17344 return FALSE;
17345}
17346
17347/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017348 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017349 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017350 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017353copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 typval_T *from;
17355 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017357 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017358 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017359 switch (from->v_type)
17360 {
17361 case VAR_NUMBER:
17362 to->vval.v_number = from->vval.v_number;
17363 break;
17364 case VAR_STRING:
17365 case VAR_FUNC:
17366 if (from->vval.v_string == NULL)
17367 to->vval.v_string = NULL;
17368 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017369 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017370 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017371 if (from->v_type == VAR_FUNC)
17372 func_ref(to->vval.v_string);
17373 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017374 break;
17375 case VAR_LIST:
17376 if (from->vval.v_list == NULL)
17377 to->vval.v_list = NULL;
17378 else
17379 {
17380 to->vval.v_list = from->vval.v_list;
17381 ++to->vval.v_list->lv_refcount;
17382 }
17383 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017384 case VAR_DICT:
17385 if (from->vval.v_dict == NULL)
17386 to->vval.v_dict = NULL;
17387 else
17388 {
17389 to->vval.v_dict = from->vval.v_dict;
17390 ++to->vval.v_dict->dv_refcount;
17391 }
17392 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017393 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017394 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017395 break;
17396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397}
17398
17399/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000017400 * Make a copy of an item.
17401 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017402 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
17403 * reference to an already copied list/dict can be used.
17404 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017405 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017406 static int
17407item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000017408 typval_T *from;
17409 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017410 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017411 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017412{
17413 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017414 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017415
Bram Moolenaar33570922005-01-25 22:26:29 +000017416 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017417 {
17418 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017419 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017420 }
17421 ++recurse;
17422
17423 switch (from->v_type)
17424 {
17425 case VAR_NUMBER:
17426 case VAR_STRING:
17427 case VAR_FUNC:
17428 copy_tv(from, to);
17429 break;
17430 case VAR_LIST:
17431 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017432 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017433 if (from->vval.v_list == NULL)
17434 to->vval.v_list = NULL;
17435 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
17436 {
17437 /* use the copy made earlier */
17438 to->vval.v_list = from->vval.v_list->lv_copylist;
17439 ++to->vval.v_list->lv_refcount;
17440 }
17441 else
17442 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
17443 if (to->vval.v_list == NULL)
17444 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017445 break;
17446 case VAR_DICT:
17447 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017448 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017449 if (from->vval.v_dict == NULL)
17450 to->vval.v_dict = NULL;
17451 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
17452 {
17453 /* use the copy made earlier */
17454 to->vval.v_dict = from->vval.v_dict->dv_copydict;
17455 ++to->vval.v_dict->dv_refcount;
17456 }
17457 else
17458 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
17459 if (to->vval.v_dict == NULL)
17460 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017461 break;
17462 default:
17463 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017464 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017465 }
17466 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017467 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017468}
17469
17470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 * ":echo expr1 ..." print each argument separated with a space, add a
17472 * newline at the end.
17473 * ":echon expr1 ..." print each argument plain.
17474 */
17475 void
17476ex_echo(eap)
17477 exarg_T *eap;
17478{
17479 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017481 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 char_u *p;
17483 int needclr = TRUE;
17484 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486
17487 if (eap->skip)
17488 ++emsg_skip;
17489 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
17490 {
17491 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 {
17494 /*
17495 * Report the invalid expression unless the expression evaluation
17496 * has been cancelled due to an aborting error, an interrupt, or an
17497 * exception.
17498 */
17499 if (!aborting())
17500 EMSG2(_(e_invexpr2), p);
17501 break;
17502 }
17503 if (!eap->skip)
17504 {
17505 if (atstart)
17506 {
17507 atstart = FALSE;
17508 /* Call msg_start() after eval1(), evaluating the expression
17509 * may cause a message to appear. */
17510 if (eap->cmdidx == CMD_echo)
17511 msg_start();
17512 }
17513 else if (eap->cmdidx == CMD_echo)
17514 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017515 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017516 if (p != NULL)
17517 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017519 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017521 if (*p != TAB && needclr)
17522 {
17523 /* remove any text still there from the command */
17524 msg_clr_eos();
17525 needclr = FALSE;
17526 }
17527 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 }
17529 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017530 {
17531#ifdef FEAT_MBYTE
17532 if (has_mbyte)
17533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017534 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017535
17536 (void)msg_outtrans_len_attr(p, i, echo_attr);
17537 p += i - 1;
17538 }
17539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017541 (void)msg_outtrans_len_attr(p, 1, echo_attr);
17542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017544 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 arg = skipwhite(arg);
17548 }
17549 eap->nextcmd = check_nextcmd(arg);
17550
17551 if (eap->skip)
17552 --emsg_skip;
17553 else
17554 {
17555 /* remove text that may still be there from the command */
17556 if (needclr)
17557 msg_clr_eos();
17558 if (eap->cmdidx == CMD_echo)
17559 msg_end();
17560 }
17561}
17562
17563/*
17564 * ":echohl {name}".
17565 */
17566 void
17567ex_echohl(eap)
17568 exarg_T *eap;
17569{
17570 int id;
17571
17572 id = syn_name2id(eap->arg);
17573 if (id == 0)
17574 echo_attr = 0;
17575 else
17576 echo_attr = syn_id2attr(id);
17577}
17578
17579/*
17580 * ":execute expr1 ..." execute the result of an expression.
17581 * ":echomsg expr1 ..." Print a message
17582 * ":echoerr expr1 ..." Print an error
17583 * Each gets spaces around each argument and a newline at the end for
17584 * echo commands
17585 */
17586 void
17587ex_execute(eap)
17588 exarg_T *eap;
17589{
17590 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000017591 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 int ret = OK;
17593 char_u *p;
17594 garray_T ga;
17595 int len;
17596 int save_did_emsg;
17597
17598 ga_init2(&ga, 1, 80);
17599
17600 if (eap->skip)
17601 ++emsg_skip;
17602 while (*arg != NUL && *arg != '|' && *arg != '\n')
17603 {
17604 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017605 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 {
17607 /*
17608 * Report the invalid expression unless the expression evaluation
17609 * has been cancelled due to an aborting error, an interrupt, or an
17610 * exception.
17611 */
17612 if (!aborting())
17613 EMSG2(_(e_invexpr2), p);
17614 ret = FAIL;
17615 break;
17616 }
17617
17618 if (!eap->skip)
17619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017620 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 len = (int)STRLEN(p);
17622 if (ga_grow(&ga, len + 2) == FAIL)
17623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017624 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 ret = FAIL;
17626 break;
17627 }
17628 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 ga.ga_len += len;
17632 }
17633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017634 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 arg = skipwhite(arg);
17636 }
17637
17638 if (ret != FAIL && ga.ga_data != NULL)
17639 {
17640 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000017641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000017643 out_flush();
17644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 else if (eap->cmdidx == CMD_echoerr)
17646 {
17647 /* We don't want to abort following commands, restore did_emsg. */
17648 save_did_emsg = did_emsg;
17649 EMSG((char_u *)ga.ga_data);
17650 if (!force_abort)
17651 did_emsg = save_did_emsg;
17652 }
17653 else if (eap->cmdidx == CMD_execute)
17654 do_cmdline((char_u *)ga.ga_data,
17655 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
17656 }
17657
17658 ga_clear(&ga);
17659
17660 if (eap->skip)
17661 --emsg_skip;
17662
17663 eap->nextcmd = check_nextcmd(arg);
17664}
17665
17666/*
17667 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
17668 * "arg" points to the "&" or '+' when called, to "option" when returning.
17669 * Returns NULL when no option name found. Otherwise pointer to the char
17670 * after the option name.
17671 */
17672 static char_u *
17673find_option_end(arg, opt_flags)
17674 char_u **arg;
17675 int *opt_flags;
17676{
17677 char_u *p = *arg;
17678
17679 ++p;
17680 if (*p == 'g' && p[1] == ':')
17681 {
17682 *opt_flags = OPT_GLOBAL;
17683 p += 2;
17684 }
17685 else if (*p == 'l' && p[1] == ':')
17686 {
17687 *opt_flags = OPT_LOCAL;
17688 p += 2;
17689 }
17690 else
17691 *opt_flags = 0;
17692
17693 if (!ASCII_ISALPHA(*p))
17694 return NULL;
17695 *arg = p;
17696
17697 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
17698 p += 4; /* termcap option */
17699 else
17700 while (ASCII_ISALPHA(*p))
17701 ++p;
17702 return p;
17703}
17704
17705/*
17706 * ":function"
17707 */
17708 void
17709ex_function(eap)
17710 exarg_T *eap;
17711{
17712 char_u *theline;
17713 int j;
17714 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716 char_u *name = NULL;
17717 char_u *p;
17718 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017719 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 garray_T newargs;
17721 garray_T newlines;
17722 int varargs = FALSE;
17723 int mustend = FALSE;
17724 int flags = 0;
17725 ufunc_T *fp;
17726 int indent;
17727 int nesting;
17728 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 dictitem_T *v;
17730 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017731 static int func_nr = 0; /* number for nameless function */
17732 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017733 hashtab_T *ht;
17734 int todo;
17735 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017736 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
17738 /*
17739 * ":function" without argument: list functions.
17740 */
17741 if (ends_excmd(*eap->arg))
17742 {
17743 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017744 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000017745 todo = func_hashtab.ht_used;
17746 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017747 {
17748 if (!HASHITEM_EMPTY(hi))
17749 {
17750 --todo;
17751 fp = HI2UF(hi);
17752 if (!isdigit(*fp->uf_name))
17753 list_func_head(fp, FALSE);
17754 }
17755 }
17756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 eap->nextcmd = check_nextcmd(eap->arg);
17758 return;
17759 }
17760
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017761 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017762 * ":function /pat": list functions matching pattern.
17763 */
17764 if (*eap->arg == '/')
17765 {
17766 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
17767 if (!eap->skip)
17768 {
17769 regmatch_T regmatch;
17770
17771 c = *p;
17772 *p = NUL;
17773 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
17774 *p = c;
17775 if (regmatch.regprog != NULL)
17776 {
17777 regmatch.rm_ic = p_ic;
17778
17779 todo = func_hashtab.ht_used;
17780 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
17781 {
17782 if (!HASHITEM_EMPTY(hi))
17783 {
17784 --todo;
17785 fp = HI2UF(hi);
17786 if (!isdigit(*fp->uf_name)
17787 && vim_regexec(&regmatch, fp->uf_name, 0))
17788 list_func_head(fp, FALSE);
17789 }
17790 }
17791 }
17792 }
17793 if (*p == '/')
17794 ++p;
17795 eap->nextcmd = check_nextcmd(p);
17796 return;
17797 }
17798
17799 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017800 * Get the function name. There are these situations:
17801 * func normal function name
17802 * "name" == func, "fudi.fd_dict" == NULL
17803 * dict.func new dictionary entry
17804 * "name" == NULL, "fudi.fd_dict" set,
17805 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
17806 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017807 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017808 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17809 * dict.func existing dict entry that's not a Funcref
17810 * "name" == NULL, "fudi.fd_dict" set,
17811 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
17812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017814 name = trans_function_name(&p, eap->skip, 0, &fudi);
17815 paren = (vim_strchr(p, '(') != NULL);
17816 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 {
17818 /*
17819 * Return on an invalid expression in braces, unless the expression
17820 * evaluation has been cancelled due to an aborting error, an
17821 * interrupt, or an exception.
17822 */
17823 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017824 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017825 if (!eap->skip && fudi.fd_newkey != NULL)
17826 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017827 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 else
17831 eap->skip = TRUE;
17832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 /* An error in a function call during evaluation of an expression in magic
17834 * braces should not cause the function not to be defined. */
17835 saved_did_emsg = did_emsg;
17836 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837
17838 /*
17839 * ":function func" with only function name: list function.
17840 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017841 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 {
17843 if (!ends_excmd(*skipwhite(p)))
17844 {
17845 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017846 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 }
17848 eap->nextcmd = check_nextcmd(p);
17849 if (eap->nextcmd != NULL)
17850 *p = NUL;
17851 if (!eap->skip && !got_int)
17852 {
17853 fp = find_func(name);
17854 if (fp != NULL)
17855 {
17856 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017857 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000017859 if (FUNCLINE(fp, j) == NULL)
17860 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861 msg_putchar('\n');
17862 msg_outnum((long)(j + 1));
17863 if (j < 9)
17864 msg_putchar(' ');
17865 if (j < 99)
17866 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017867 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 out_flush(); /* show a line at a time */
17869 ui_breakcheck();
17870 }
17871 if (!got_int)
17872 {
17873 msg_putchar('\n');
17874 msg_puts((char_u *)" endfunction");
17875 }
17876 }
17877 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017878 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017880 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 }
17882
17883 /*
17884 * ":function name(arg1, arg2)" Define function.
17885 */
17886 p = skipwhite(p);
17887 if (*p != '(')
17888 {
17889 if (!eap->skip)
17890 {
17891 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017892 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 }
17894 /* attempt to continue by skipping some text */
17895 if (vim_strchr(p, '(') != NULL)
17896 p = vim_strchr(p, '(');
17897 }
17898 p = skipwhite(p + 1);
17899
17900 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17901 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17902
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017903 if (!eap->skip)
17904 {
17905 /* Check the name of the function. */
17906 if (name != NULL)
17907 arg = name;
17908 else
17909 arg = fudi.fd_newkey;
17910 if (arg != NULL)
17911 {
17912 if (*arg == K_SPECIAL)
17913 j = 3;
17914 else
17915 j = 0;
17916 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17917 : eval_isnamec(arg[j])))
17918 ++j;
17919 if (arg[j] != NUL)
17920 emsg_funcname(_(e_invarg2), arg);
17921 }
17922 }
17923
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 /*
17925 * Isolate the arguments: "arg1, arg2, ...)"
17926 */
17927 while (*p != ')')
17928 {
17929 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17930 {
17931 varargs = TRUE;
17932 p += 3;
17933 mustend = TRUE;
17934 }
17935 else
17936 {
17937 arg = p;
17938 while (ASCII_ISALNUM(*p) || *p == '_')
17939 ++p;
17940 if (arg == p || isdigit(*arg)
17941 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17942 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17943 {
17944 if (!eap->skip)
17945 EMSG2(_("E125: Illegal argument: %s"), arg);
17946 break;
17947 }
17948 if (ga_grow(&newargs, 1) == FAIL)
17949 goto erret;
17950 c = *p;
17951 *p = NUL;
17952 arg = vim_strsave(arg);
17953 if (arg == NULL)
17954 goto erret;
17955 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17956 *p = c;
17957 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 if (*p == ',')
17959 ++p;
17960 else
17961 mustend = TRUE;
17962 }
17963 p = skipwhite(p);
17964 if (mustend && *p != ')')
17965 {
17966 if (!eap->skip)
17967 EMSG2(_(e_invarg2), eap->arg);
17968 break;
17969 }
17970 }
17971 ++p; /* skip the ')' */
17972
Bram Moolenaare9a41262005-01-15 22:18:47 +000017973 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 for (;;)
17975 {
17976 p = skipwhite(p);
17977 if (STRNCMP(p, "range", 5) == 0)
17978 {
17979 flags |= FC_RANGE;
17980 p += 5;
17981 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017982 else if (STRNCMP(p, "dict", 4) == 0)
17983 {
17984 flags |= FC_DICT;
17985 p += 4;
17986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987 else if (STRNCMP(p, "abort", 5) == 0)
17988 {
17989 flags |= FC_ABORT;
17990 p += 5;
17991 }
17992 else
17993 break;
17994 }
17995
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000017996 /* When there is a line break use what follows for the function body.
17997 * Makes 'exe "func Test()\n...\nendfunc"' work. */
17998 if (*p == '\n')
17999 line_arg = p + 1;
18000 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 EMSG(_(e_trailing));
18002
18003 /*
18004 * Read the body of the function, until ":endfunction" is found.
18005 */
18006 if (KeyTyped)
18007 {
18008 /* Check if the function already exists, don't let the user type the
18009 * whole function before telling him it doesn't work! For a script we
18010 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018011 if (!eap->skip && !eap->forceit)
18012 {
18013 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18014 EMSG(_(e_funcdict));
18015 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018016 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018019 if (!eap->skip && did_emsg)
18020 goto erret;
18021
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 msg_putchar('\n'); /* don't overwrite the function name */
18023 cmdline_row = msg_row;
18024 }
18025
18026 indent = 2;
18027 nesting = 0;
18028 for (;;)
18029 {
18030 msg_scroll = TRUE;
18031 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018032 sourcing_lnum_off = sourcing_lnum;
18033
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018034 if (line_arg != NULL)
18035 {
18036 /* Use eap->arg, split up in parts by line breaks. */
18037 theline = line_arg;
18038 p = vim_strchr(theline, '\n');
18039 if (p == NULL)
18040 line_arg += STRLEN(line_arg);
18041 else
18042 {
18043 *p = NUL;
18044 line_arg = p + 1;
18045 }
18046 }
18047 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 theline = getcmdline(':', 0L, indent);
18049 else
18050 theline = eap->getline(':', eap->cookie, indent);
18051 if (KeyTyped)
18052 lines_left = Rows - 1;
18053 if (theline == NULL)
18054 {
18055 EMSG(_("E126: Missing :endfunction"));
18056 goto erret;
18057 }
18058
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018059 /* Detect line continuation: sourcing_lnum increased more than one. */
18060 if (sourcing_lnum > sourcing_lnum_off + 1)
18061 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
18062 else
18063 sourcing_lnum_off = 0;
18064
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 if (skip_until != NULL)
18066 {
18067 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18068 * don't check for ":endfunc". */
18069 if (STRCMP(theline, skip_until) == 0)
18070 {
18071 vim_free(skip_until);
18072 skip_until = NULL;
18073 }
18074 }
18075 else
18076 {
18077 /* skip ':' and blanks*/
18078 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
18079 ;
18080
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018081 /* Check for "endfunction". */
18082 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018084 if (line_arg == NULL)
18085 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 break;
18087 }
18088
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018089 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 * at "end". */
18091 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
18092 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018093 else if (STRNCMP(p, "if", 2) == 0
18094 || STRNCMP(p, "wh", 2) == 0
18095 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 || STRNCMP(p, "try", 3) == 0)
18097 indent += 2;
18098
18099 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018100 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018102 if (*p == '!')
18103 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 p += eval_fname_script(p);
18105 if (ASCII_ISALPHA(*p))
18106 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018107 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 if (*skipwhite(p) == '(')
18109 {
18110 ++nesting;
18111 indent += 2;
18112 }
18113 }
18114 }
18115
18116 /* Check for ":append" or ":insert". */
18117 p = skip_range(p, NULL);
18118 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
18119 || (p[0] == 'i'
18120 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
18121 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
18122 skip_until = vim_strsave((char_u *)".");
18123
18124 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
18125 arg = skipwhite(skiptowhite(p));
18126 if (arg[0] == '<' && arg[1] =='<'
18127 && ((p[0] == 'p' && p[1] == 'y'
18128 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
18129 || (p[0] == 'p' && p[1] == 'e'
18130 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
18131 || (p[0] == 't' && p[1] == 'c'
18132 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
18133 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
18134 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000018135 || (p[0] == 'm' && p[1] == 'z'
18136 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 ))
18138 {
18139 /* ":python <<" continues until a dot, like ":append" */
18140 p = skipwhite(arg + 2);
18141 if (*p == NUL)
18142 skip_until = vim_strsave((char_u *)".");
18143 else
18144 skip_until = vim_strsave(p);
18145 }
18146 }
18147
18148 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018149 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018150 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018151 if (line_arg == NULL)
18152 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018154 }
18155
18156 /* Copy the line to newly allocated memory. get_one_sourceline()
18157 * allocates 250 bytes per line, this saves 80% on average. The cost
18158 * is an extra alloc/free. */
18159 p = vim_strsave(theline);
18160 if (p != NULL)
18161 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018162 if (line_arg == NULL)
18163 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018164 theline = p;
18165 }
18166
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018167 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
18168
18169 /* Add NULL lines for continuation lines, so that the line count is
18170 * equal to the index in the growarray. */
18171 while (sourcing_lnum_off-- > 0)
18172 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018173
18174 /* Check for end of eap->arg. */
18175 if (line_arg != NULL && *line_arg == NUL)
18176 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 }
18178
18179 /* Don't define the function when skipping commands or when an error was
18180 * detected. */
18181 if (eap->skip || did_emsg)
18182 goto erret;
18183
18184 /*
18185 * If there are no errors, add the function
18186 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018187 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018188 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018189 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000018190 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018191 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018192 emsg_funcname("E707: Function name conflicts with variable: %s",
18193 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018194 goto erret;
18195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018196
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018197 fp = find_func(name);
18198 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018200 if (!eap->forceit)
18201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018202 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018203 goto erret;
18204 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018205 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018206 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018207 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018208 name);
18209 goto erret;
18210 }
18211 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018212 ga_clear_strings(&(fp->uf_args));
18213 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018214 vim_free(name);
18215 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 }
18218 else
18219 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018220 char numbuf[20];
18221
18222 fp = NULL;
18223 if (fudi.fd_newkey == NULL && !eap->forceit)
18224 {
18225 EMSG(_(e_funcdict));
18226 goto erret;
18227 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000018228 if (fudi.fd_di == NULL)
18229 {
18230 /* Can't add a function to a locked dictionary */
18231 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
18232 goto erret;
18233 }
18234 /* Can't change an existing function if it is locked */
18235 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
18236 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018237
18238 /* Give the function a sequential number. Can only be used with a
18239 * Funcref! */
18240 vim_free(name);
18241 sprintf(numbuf, "%d", ++func_nr);
18242 name = vim_strsave((char_u *)numbuf);
18243 if (name == NULL)
18244 goto erret;
18245 }
18246
18247 if (fp == NULL)
18248 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018249 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018250 {
18251 int slen, plen;
18252 char_u *scriptname;
18253
18254 /* Check that the autoload name matches the script name. */
18255 j = FAIL;
18256 if (sourcing_name != NULL)
18257 {
18258 scriptname = autoload_name(name);
18259 if (scriptname != NULL)
18260 {
18261 p = vim_strchr(scriptname, '/');
18262 plen = STRLEN(p);
18263 slen = STRLEN(sourcing_name);
18264 if (slen > plen && fnamecmp(p,
18265 sourcing_name + slen - plen) == 0)
18266 j = OK;
18267 vim_free(scriptname);
18268 }
18269 }
18270 if (j == FAIL)
18271 {
18272 EMSG2(_("E746: Function name does not match script file name: %s"), name);
18273 goto erret;
18274 }
18275 }
18276
18277 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278 if (fp == NULL)
18279 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018280
18281 if (fudi.fd_dict != NULL)
18282 {
18283 if (fudi.fd_di == NULL)
18284 {
18285 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018286 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018287 if (fudi.fd_di == NULL)
18288 {
18289 vim_free(fp);
18290 goto erret;
18291 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018292 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
18293 {
18294 vim_free(fudi.fd_di);
18295 goto erret;
18296 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018297 }
18298 else
18299 /* overwrite existing dict entry */
18300 clear_tv(&fudi.fd_di->di_tv);
18301 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018302 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018303 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018304 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018305 }
18306
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018308 STRCPY(fp->uf_name, name);
18309 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018311 fp->uf_args = newargs;
18312 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018313#ifdef FEAT_PROFILE
18314 fp->uf_tml_count = NULL;
18315 fp->uf_tml_total = NULL;
18316 fp->uf_tml_self = NULL;
18317 fp->uf_profiling = FALSE;
18318 if (prof_def_func())
18319 func_do_profile(fp);
18320#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018321 fp->uf_varargs = varargs;
18322 fp->uf_flags = flags;
18323 fp->uf_calls = 0;
18324 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018325 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326
18327erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 ga_clear_strings(&newargs);
18329 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018330ret_free:
18331 vim_free(skip_until);
18332 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335}
18336
18337/*
18338 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000018339 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018341 * flags:
18342 * TFN_INT: internal function name OK
18343 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 * Advances "pp" to just after the function name (if no error).
18345 */
18346 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018347trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 char_u **pp;
18349 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018350 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000018351 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018353 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 char_u *start;
18355 char_u *end;
18356 int lead;
18357 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018359 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018360
18361 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018362 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000018364
18365 /* Check for hard coded <SNR>: already translated function ID (from a user
18366 * command). */
18367 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
18368 && (*pp)[2] == (int)KE_SNR)
18369 {
18370 *pp += 3;
18371 len = get_id_len(pp) + 3;
18372 return vim_strnsave(start, len);
18373 }
18374
18375 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
18376 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000018378 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018380
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018381 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
18382 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018383 if (end == start)
18384 {
18385 if (!skip)
18386 EMSG(_("E129: Function name required"));
18387 goto theend;
18388 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018389 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018390 {
18391 /*
18392 * Report an invalid expression in braces, unless the expression
18393 * evaluation has been cancelled due to an aborting error, an
18394 * interrupt, or an exception.
18395 */
18396 if (!aborting())
18397 {
18398 if (end != NULL)
18399 EMSG2(_(e_invarg2), start);
18400 }
18401 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018402 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018403 goto theend;
18404 }
18405
18406 if (lv.ll_tv != NULL)
18407 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018408 if (fdp != NULL)
18409 {
18410 fdp->fd_dict = lv.ll_dict;
18411 fdp->fd_newkey = lv.ll_newkey;
18412 lv.ll_newkey = NULL;
18413 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018414 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018415 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
18416 {
18417 name = vim_strsave(lv.ll_tv->vval.v_string);
18418 *pp = end;
18419 }
18420 else
18421 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018422 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
18423 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018424 EMSG(_(e_funcref));
18425 else
18426 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018427 name = NULL;
18428 }
18429 goto theend;
18430 }
18431
18432 if (lv.ll_name == NULL)
18433 {
18434 /* Error found, but continue after the function name. */
18435 *pp = end;
18436 goto theend;
18437 }
18438
18439 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018440 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018441 len = STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000018442 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
18443 && STRNCMP(lv.ll_name, "s:", 2) == 0)
18444 {
18445 /* When there was "s:" already or the name expanded to get a
18446 * leading "s:" then remove it. */
18447 lv.ll_name += 2;
18448 len -= 2;
18449 lead = 2;
18450 }
18451 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018452 else
Bram Moolenaara7043832005-01-21 11:56:39 +000018453 {
18454 if (lead == 2) /* skip over "s:" */
18455 lv.ll_name += 2;
18456 len = (int)(end - lv.ll_name);
18457 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018458
18459 /*
18460 * Copy the function name to allocated memory.
18461 * Accept <SID>name() inside a script, translate into <SNR>123_name().
18462 * Accept <SNR>123_name() outside a script.
18463 */
18464 if (skip)
18465 lead = 0; /* do nothing */
18466 else if (lead > 0)
18467 {
18468 lead = 3;
18469 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
18470 {
18471 if (current_SID <= 0)
18472 {
18473 EMSG(_(e_usingsid));
18474 goto theend;
18475 }
18476 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
18477 lead += (int)STRLEN(sid_buf);
18478 }
18479 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018480 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018481 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018482 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018483 goto theend;
18484 }
18485 name = alloc((unsigned)(len + lead + 1));
18486 if (name != NULL)
18487 {
18488 if (lead > 0)
18489 {
18490 name[0] = K_SPECIAL;
18491 name[1] = KS_EXTRA;
18492 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000018493 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000018494 STRCPY(name + 3, sid_buf);
18495 }
18496 mch_memmove(name + lead, lv.ll_name, (size_t)len);
18497 name[len + lead] = NUL;
18498 }
18499 *pp = end;
18500
18501theend:
18502 clear_lval(&lv);
18503 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504}
18505
18506/*
18507 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
18508 * Return 2 if "p" starts with "s:".
18509 * Return 0 otherwise.
18510 */
18511 static int
18512eval_fname_script(p)
18513 char_u *p;
18514{
18515 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
18516 || STRNICMP(p + 1, "SNR>", 4) == 0))
18517 return 5;
18518 if (p[0] == 's' && p[1] == ':')
18519 return 2;
18520 return 0;
18521}
18522
18523/*
18524 * Return TRUE if "p" starts with "<SID>" or "s:".
18525 * Only works if eval_fname_script() returned non-zero for "p"!
18526 */
18527 static int
18528eval_fname_sid(p)
18529 char_u *p;
18530{
18531 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
18532}
18533
18534/*
18535 * List the head of the function: "name(arg1, arg2)".
18536 */
18537 static void
18538list_func_head(fp, indent)
18539 ufunc_T *fp;
18540 int indent;
18541{
18542 int j;
18543
18544 msg_start();
18545 if (indent)
18546 MSG_PUTS(" ");
18547 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018548 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 {
18550 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018551 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 }
18553 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018554 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018556 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 {
18558 if (j)
18559 MSG_PUTS(", ");
18560 msg_puts(FUNCARG(fp, j));
18561 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018562 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 {
18564 if (j)
18565 MSG_PUTS(", ");
18566 MSG_PUTS("...");
18567 }
18568 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018569 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000018570 if (p_verbose > 0)
18571 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572}
18573
18574/*
18575 * Find a function by name, return pointer to it in ufuncs.
18576 * Return NULL for unknown function.
18577 */
18578 static ufunc_T *
18579find_func(name)
18580 char_u *name;
18581{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018582 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018584 hi = hash_find(&func_hashtab, name);
18585 if (!HASHITEM_EMPTY(hi))
18586 return HI2UF(hi);
18587 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588}
18589
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000018590#if defined(EXITFREE) || defined(PROTO)
18591 void
18592free_all_functions()
18593{
18594 hashitem_T *hi;
18595
18596 /* Need to start all over every time, because func_free() may change the
18597 * hash table. */
18598 while (func_hashtab.ht_used > 0)
18599 for (hi = func_hashtab.ht_array; ; ++hi)
18600 if (!HASHITEM_EMPTY(hi))
18601 {
18602 func_free(HI2UF(hi));
18603 break;
18604 }
18605}
18606#endif
18607
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018608/*
18609 * Return TRUE if a function "name" exists.
18610 */
18611 static int
18612function_exists(name)
18613 char_u *name;
18614{
18615 char_u *p = name;
18616 int n = FALSE;
18617
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018618 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018619 if (p != NULL)
18620 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018621 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018622 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018623 else
18624 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018625 vim_free(p);
18626 }
18627 return n;
18628}
18629
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018630/*
18631 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018632 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018633 */
18634 static int
18635builtin_function(name)
18636 char_u *name;
18637{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018638 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
18639 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018640}
18641
Bram Moolenaar05159a02005-02-26 23:04:13 +000018642#if defined(FEAT_PROFILE) || defined(PROTO)
18643/*
18644 * Start profiling function "fp".
18645 */
18646 static void
18647func_do_profile(fp)
18648 ufunc_T *fp;
18649{
18650 fp->uf_tm_count = 0;
18651 profile_zero(&fp->uf_tm_self);
18652 profile_zero(&fp->uf_tm_total);
18653 if (fp->uf_tml_count == NULL)
18654 fp->uf_tml_count = (int *)alloc_clear((unsigned)
18655 (sizeof(int) * fp->uf_lines.ga_len));
18656 if (fp->uf_tml_total == NULL)
18657 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
18658 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18659 if (fp->uf_tml_self == NULL)
18660 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
18661 (sizeof(proftime_T) * fp->uf_lines.ga_len));
18662 fp->uf_tml_idx = -1;
18663 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
18664 || fp->uf_tml_self == NULL)
18665 return; /* out of memory */
18666
18667 fp->uf_profiling = TRUE;
18668}
18669
18670/*
18671 * Dump the profiling results for all functions in file "fd".
18672 */
18673 void
18674func_dump_profile(fd)
18675 FILE *fd;
18676{
18677 hashitem_T *hi;
18678 int todo;
18679 ufunc_T *fp;
18680 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000018681 ufunc_T **sorttab;
18682 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018683
18684 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000018685 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
18686
Bram Moolenaar05159a02005-02-26 23:04:13 +000018687 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
18688 {
18689 if (!HASHITEM_EMPTY(hi))
18690 {
18691 --todo;
18692 fp = HI2UF(hi);
18693 if (fp->uf_profiling)
18694 {
Bram Moolenaar73830342005-02-28 22:48:19 +000018695 if (sorttab != NULL)
18696 sorttab[st_len++] = fp;
18697
Bram Moolenaar05159a02005-02-26 23:04:13 +000018698 if (fp->uf_name[0] == K_SPECIAL)
18699 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
18700 else
18701 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
18702 if (fp->uf_tm_count == 1)
18703 fprintf(fd, "Called 1 time\n");
18704 else
18705 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
18706 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
18707 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
18708 fprintf(fd, "\n");
18709 fprintf(fd, "count total (s) self (s)\n");
18710
18711 for (i = 0; i < fp->uf_lines.ga_len; ++i)
18712 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018713 if (FUNCLINE(fp, i) == NULL)
18714 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000018715 prof_func_line(fd, fp->uf_tml_count[i],
18716 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018717 fprintf(fd, "%s\n", FUNCLINE(fp, i));
18718 }
18719 fprintf(fd, "\n");
18720 }
18721 }
18722 }
Bram Moolenaar73830342005-02-28 22:48:19 +000018723
18724 if (sorttab != NULL && st_len > 0)
18725 {
18726 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18727 prof_total_cmp);
18728 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
18729 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
18730 prof_self_cmp);
18731 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
18732 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018733}
Bram Moolenaar73830342005-02-28 22:48:19 +000018734
18735 static void
18736prof_sort_list(fd, sorttab, st_len, title, prefer_self)
18737 FILE *fd;
18738 ufunc_T **sorttab;
18739 int st_len;
18740 char *title;
18741 int prefer_self; /* when equal print only self time */
18742{
18743 int i;
18744 ufunc_T *fp;
18745
18746 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
18747 fprintf(fd, "count total (s) self (s) function\n");
18748 for (i = 0; i < 20 && i < st_len; ++i)
18749 {
18750 fp = sorttab[i];
18751 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
18752 prefer_self);
18753 if (fp->uf_name[0] == K_SPECIAL)
18754 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
18755 else
18756 fprintf(fd, " %s()\n", fp->uf_name);
18757 }
18758 fprintf(fd, "\n");
18759}
18760
18761/*
18762 * Print the count and times for one function or function line.
18763 */
18764 static void
18765prof_func_line(fd, count, total, self, prefer_self)
18766 FILE *fd;
18767 int count;
18768 proftime_T *total;
18769 proftime_T *self;
18770 int prefer_self; /* when equal print only self time */
18771{
18772 if (count > 0)
18773 {
18774 fprintf(fd, "%5d ", count);
18775 if (prefer_self && profile_equal(total, self))
18776 fprintf(fd, " ");
18777 else
18778 fprintf(fd, "%s ", profile_msg(total));
18779 if (!prefer_self && profile_equal(total, self))
18780 fprintf(fd, " ");
18781 else
18782 fprintf(fd, "%s ", profile_msg(self));
18783 }
18784 else
18785 fprintf(fd, " ");
18786}
18787
18788/*
18789 * Compare function for total time sorting.
18790 */
18791 static int
18792#ifdef __BORLANDC__
18793_RTLENTRYF
18794#endif
18795prof_total_cmp(s1, s2)
18796 const void *s1;
18797 const void *s2;
18798{
18799 ufunc_T *p1, *p2;
18800
18801 p1 = *(ufunc_T **)s1;
18802 p2 = *(ufunc_T **)s2;
18803 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
18804}
18805
18806/*
18807 * Compare function for self time sorting.
18808 */
18809 static int
18810#ifdef __BORLANDC__
18811_RTLENTRYF
18812#endif
18813prof_self_cmp(s1, s2)
18814 const void *s1;
18815 const void *s2;
18816{
18817 ufunc_T *p1, *p2;
18818
18819 p1 = *(ufunc_T **)s1;
18820 p2 = *(ufunc_T **)s2;
18821 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
18822}
18823
Bram Moolenaar05159a02005-02-26 23:04:13 +000018824#endif
18825
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018826/* The names of packages that once were loaded is remembered. */
18827static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
18828
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018829/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018830 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018831 * Return TRUE if a package was loaded.
18832 */
18833 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018834script_autoload(name, reload)
18835 char_u *name;
18836 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018837{
18838 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018839 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018840 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018841 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018842
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018843 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018844 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018845 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018846 return FALSE;
18847
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018848 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018849
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018850 /* Find the name in the list of previously loaded package names. Skip
18851 * "autoload/", it's always the same. */
18852 for (i = 0; i < ga_loaded.ga_len; ++i)
18853 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
18854 break;
18855 if (!reload && i < ga_loaded.ga_len)
18856 ret = FALSE; /* was loaded already */
18857 else
18858 {
18859 /* Remember the name if it wasn't loaded already. */
18860 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
18861 {
18862 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
18863 tofree = NULL;
18864 }
18865
18866 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000018867 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018868 ret = TRUE;
18869 }
18870
18871 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018872 return ret;
18873}
18874
18875/*
18876 * Return the autoload script name for a function or variable name.
18877 * Returns NULL when out of memory.
18878 */
18879 static char_u *
18880autoload_name(name)
18881 char_u *name;
18882{
18883 char_u *p;
18884 char_u *scriptname;
18885
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018886 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018887 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18888 if (scriptname == NULL)
18889 return FALSE;
18890 STRCPY(scriptname, "autoload/");
18891 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018892 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018893 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018894 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018895 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018896 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018897}
18898
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18900
18901/*
18902 * Function given to ExpandGeneric() to obtain the list of user defined
18903 * function names.
18904 */
18905 char_u *
18906get_user_func_name(xp, idx)
18907 expand_T *xp;
18908 int idx;
18909{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018910 static long_u done;
18911 static hashitem_T *hi;
18912 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913
18914 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018916 done = 0;
18917 hi = func_hashtab.ht_array;
18918 }
18919 if (done < func_hashtab.ht_used)
18920 {
18921 if (done++ > 0)
18922 ++hi;
18923 while (HASHITEM_EMPTY(hi))
18924 ++hi;
18925 fp = HI2UF(hi);
18926
18927 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18928 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929
18930 cat_func_name(IObuff, fp);
18931 if (xp->xp_context != EXPAND_USER_FUNC)
18932 {
18933 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018934 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 STRCAT(IObuff, ")");
18936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 return IObuff;
18938 }
18939 return NULL;
18940}
18941
18942#endif /* FEAT_CMDL_COMPL */
18943
18944/*
18945 * Copy the function name of "fp" to buffer "buf".
18946 * "buf" must be able to hold the function name plus three bytes.
18947 * Takes care of script-local function names.
18948 */
18949 static void
18950cat_func_name(buf, fp)
18951 char_u *buf;
18952 ufunc_T *fp;
18953{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018954 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 {
18956 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018957 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 }
18959 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018960 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961}
18962
18963/*
18964 * ":delfunction {name}"
18965 */
18966 void
18967ex_delfunction(eap)
18968 exarg_T *eap;
18969{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018970 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 char_u *p;
18972 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018973 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974
18975 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018976 name = trans_function_name(&p, eap->skip, 0, &fudi);
18977 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018979 {
18980 if (fudi.fd_dict != NULL && !eap->skip)
18981 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 if (!ends_excmd(*skipwhite(p)))
18985 {
18986 vim_free(name);
18987 EMSG(_(e_trailing));
18988 return;
18989 }
18990 eap->nextcmd = check_nextcmd(p);
18991 if (eap->nextcmd != NULL)
18992 *p = NUL;
18993
18994 if (!eap->skip)
18995 fp = find_func(name);
18996 vim_free(name);
18997
18998 if (!eap->skip)
18999 {
19000 if (fp == NULL)
19001 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019002 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 return;
19004 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019005 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 {
19007 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19008 return;
19009 }
19010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019011 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019013 /* Delete the dict item that refers to the function, it will
19014 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019015 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019017 else
19018 func_free(fp);
19019 }
19020}
19021
19022/*
19023 * Free a function and remove it from the list of functions.
19024 */
19025 static void
19026func_free(fp)
19027 ufunc_T *fp;
19028{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019029 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019030
19031 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019032 ga_clear_strings(&(fp->uf_args));
19033 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000019034#ifdef FEAT_PROFILE
19035 vim_free(fp->uf_tml_count);
19036 vim_free(fp->uf_tml_total);
19037 vim_free(fp->uf_tml_self);
19038#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019039
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019040 /* remove the function from the function hashtable */
19041 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
19042 if (HASHITEM_EMPTY(hi))
19043 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019044 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019045 hash_remove(&func_hashtab, hi);
19046
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019047 vim_free(fp);
19048}
19049
19050/*
19051 * Unreference a Function: decrement the reference count and free it when it
19052 * becomes zero. Only for numbered functions.
19053 */
19054 static void
19055func_unref(name)
19056 char_u *name;
19057{
19058 ufunc_T *fp;
19059
19060 if (name != NULL && isdigit(*name))
19061 {
19062 fp = find_func(name);
19063 if (fp == NULL)
19064 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019065 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019066 {
19067 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019068 * when "uf_calls" becomes zero. */
19069 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019070 func_free(fp);
19071 }
19072 }
19073}
19074
19075/*
19076 * Count a reference to a Function.
19077 */
19078 static void
19079func_ref(name)
19080 char_u *name;
19081{
19082 ufunc_T *fp;
19083
19084 if (name != NULL && isdigit(*name))
19085 {
19086 fp = find_func(name);
19087 if (fp == NULL)
19088 EMSG2(_(e_intern2), "func_ref()");
19089 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019090 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 }
19092}
19093
19094/*
19095 * Call a user function.
19096 */
19097 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000019098call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 ufunc_T *fp; /* pointer to function */
19100 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 typval_T *argvars; /* arguments */
19102 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 linenr_T firstline; /* first line of range */
19104 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000019105 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106{
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 char_u *save_sourcing_name;
19108 linenr_T save_sourcing_lnum;
19109 scid_T save_current_SID;
19110 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 int save_did_emsg;
19112 static int depth = 0;
19113 dictitem_T *v;
19114 int fixvar_idx = 0; /* index in fixvar[] */
19115 int i;
19116 int ai;
19117 char_u numbuf[NUMBUFLEN];
19118 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019119#ifdef FEAT_PROFILE
19120 proftime_T wait_start;
19121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122
19123 /* If depth of calling is getting too high, don't execute the function */
19124 if (depth >= p_mfd)
19125 {
19126 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127 rettv->v_type = VAR_NUMBER;
19128 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 return;
19130 }
19131 ++depth;
19132
19133 line_breakcheck(); /* check for CTRL-C hit */
19134
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019135 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019138 fc.rettv = rettv;
19139 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 fc.linenr = 0;
19141 fc.returned = FALSE;
19142 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019144 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 fc.dbg_tick = debug_tick;
19146
Bram Moolenaar33570922005-01-25 22:26:29 +000019147 /*
19148 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
19149 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
19150 * each argument variable and saves a lot of time.
19151 */
19152 /*
19153 * Init l: variables.
19154 */
19155 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000019156 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019157 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 /* Set l:self to "selfdict". */
19159 v = &fc.fixvar[fixvar_idx++].var;
19160 STRCPY(v->di_key, "self");
19161 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
19162 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
19163 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019164 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019165 v->di_tv.vval.v_dict = selfdict;
19166 ++selfdict->dv_refcount;
19167 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019168
Bram Moolenaar33570922005-01-25 22:26:29 +000019169 /*
19170 * Init a: variables.
19171 * Set a:0 to "argcount".
19172 * Set a:000 to a list with room for the "..." arguments.
19173 */
19174 init_var_dict(&fc.l_avars, &fc.l_avars_var);
19175 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019176 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000019177 v = &fc.fixvar[fixvar_idx++].var;
19178 STRCPY(v->di_key, "000");
19179 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19180 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19181 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019182 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019183 v->di_tv.vval.v_list = &fc.l_varlist;
19184 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
19185 fc.l_varlist.lv_refcount = 99999;
19186
19187 /*
19188 * Set a:firstline to "firstline" and a:lastline to "lastline".
19189 * Set a:name to named arguments.
19190 * Set a:N to the "..." arguments.
19191 */
19192 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
19193 (varnumber_T)firstline);
19194 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
19195 (varnumber_T)lastline);
19196 for (i = 0; i < argcount; ++i)
19197 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019198 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019199 if (ai < 0)
19200 /* named argument a:name */
19201 name = FUNCARG(fp, i);
19202 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019203 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019204 /* "..." argument a:1, a:2, etc. */
19205 sprintf((char *)numbuf, "%d", ai + 1);
19206 name = numbuf;
19207 }
19208 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
19209 {
19210 v = &fc.fixvar[fixvar_idx++].var;
19211 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19212 }
19213 else
19214 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019215 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19216 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000019217 if (v == NULL)
19218 break;
19219 v->di_flags = DI_FLAGS_RO;
19220 }
19221 STRCPY(v->di_key, name);
19222 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
19223
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019224 /* Note: the values are copied directly to avoid alloc/free.
19225 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019226 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019227 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019228
19229 if (ai >= 0 && ai < MAX_FUNC_ARGS)
19230 {
19231 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
19232 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019233 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019234 }
19235 }
19236
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 /* Don't redraw while executing the function. */
19238 ++RedrawingDisabled;
19239 save_sourcing_name = sourcing_name;
19240 save_sourcing_lnum = sourcing_lnum;
19241 sourcing_lnum = 1;
19242 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019243 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 if (sourcing_name != NULL)
19245 {
19246 if (save_sourcing_name != NULL
19247 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
19248 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
19249 else
19250 STRCPY(sourcing_name, "function ");
19251 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
19252
19253 if (p_verbose >= 12)
19254 {
19255 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019256 verbose_enter_scroll();
19257
Bram Moolenaar555b2802005-05-19 21:08:39 +000019258 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259 if (p_verbose >= 14)
19260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000019262 char_u numbuf[NUMBUFLEN];
19263 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264
19265 msg_puts((char_u *)"(");
19266 for (i = 0; i < argcount; ++i)
19267 {
19268 if (i > 0)
19269 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019270 if (argvars[i].v_type == VAR_NUMBER)
19271 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 else
19273 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019274 trunc_string(tv2string(&argvars[i], &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019275 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019277 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 }
19279 }
19280 msg_puts((char_u *)")");
19281 }
19282 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019283
19284 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 --no_wait_return;
19286 }
19287 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019288#ifdef FEAT_PROFILE
19289 if (do_profiling)
19290 {
19291 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
19292 func_do_profile(fp);
19293 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019294 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019295 {
19296 ++fp->uf_tm_count;
19297 profile_start(&fp->uf_tm_start);
19298 profile_zero(&fp->uf_tm_children);
19299 }
19300 script_prof_save(&wait_start);
19301 }
19302#endif
19303
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019305 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 save_did_emsg = did_emsg;
19307 did_emsg = FALSE;
19308
19309 /* call do_cmdline() to execute the lines */
19310 do_cmdline(NULL, get_func_line, (void *)&fc,
19311 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
19312
19313 --RedrawingDisabled;
19314
19315 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019316 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 clear_tv(rettv);
19319 rettv->v_type = VAR_NUMBER;
19320 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 }
19322
Bram Moolenaar05159a02005-02-26 23:04:13 +000019323#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019324 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000019325 {
19326 profile_end(&fp->uf_tm_start);
19327 profile_sub_wait(&wait_start, &fp->uf_tm_start);
19328 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
19329 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
19330 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019331 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019332 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019333 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
19334 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019335 }
19336 }
19337#endif
19338
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 /* when being verbose, mention the return value */
19340 if (p_verbose >= 12)
19341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019343 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000019346 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000019348 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
19349 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019350 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000019352 char_u buf[MSG_BUF_LEN];
19353 char_u numbuf[NUMBUFLEN];
19354 char_u *tofree;
19355
Bram Moolenaar555b2802005-05-19 21:08:39 +000019356 /* The value may be very long. Skip the middle part, so that we
19357 * have some idea how it starts and ends. smsg() would always
19358 * truncate it at the end. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019359 trunc_string(tv2string(fc.rettv, &tofree, numbuf, 0),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019360 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000019361 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000019362 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 }
19364 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019365
19366 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 --no_wait_return;
19368 }
19369
19370 vim_free(sourcing_name);
19371 sourcing_name = save_sourcing_name;
19372 sourcing_lnum = save_sourcing_lnum;
19373 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019374#ifdef FEAT_PROFILE
19375 if (do_profiling)
19376 script_prof_restore(&wait_start);
19377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378
19379 if (p_verbose >= 12 && sourcing_name != NULL)
19380 {
19381 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019382 verbose_enter_scroll();
19383
Bram Moolenaar555b2802005-05-19 21:08:39 +000019384 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000019386
19387 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 --no_wait_return;
19389 }
19390
19391 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019392 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393
Bram Moolenaar33570922005-01-25 22:26:29 +000019394 /* The a: variables typevals were not alloced, only free the allocated
19395 * variables. */
19396 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
19397
19398 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 --depth;
19400}
19401
19402/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019403 * Add a number variable "name" to dict "dp" with value "nr".
19404 */
19405 static void
19406add_nr_var(dp, v, name, nr)
19407 dict_T *dp;
19408 dictitem_T *v;
19409 char *name;
19410 varnumber_T nr;
19411{
19412 STRCPY(v->di_key, name);
19413 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19414 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
19415 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019416 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019417 v->di_tv.vval.v_number = nr;
19418}
19419
19420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 * ":return [expr]"
19422 */
19423 void
19424ex_return(eap)
19425 exarg_T *eap;
19426{
19427 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019428 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 int returning = FALSE;
19430
19431 if (current_funccal == NULL)
19432 {
19433 EMSG(_("E133: :return not inside a function"));
19434 return;
19435 }
19436
19437 if (eap->skip)
19438 ++emsg_skip;
19439
19440 eap->nextcmd = NULL;
19441 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019442 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 {
19444 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019445 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019447 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 }
19449 /* It's safer to return also on error. */
19450 else if (!eap->skip)
19451 {
19452 /*
19453 * Return unless the expression evaluation has been cancelled due to an
19454 * aborting error, an interrupt, or an exception.
19455 */
19456 if (!aborting())
19457 returning = do_return(eap, FALSE, TRUE, NULL);
19458 }
19459
19460 /* When skipping or the return gets pending, advance to the next command
19461 * in this line (!returning). Otherwise, ignore the rest of the line.
19462 * Following lines will be ignored by get_func_line(). */
19463 if (returning)
19464 eap->nextcmd = NULL;
19465 else if (eap->nextcmd == NULL) /* no argument */
19466 eap->nextcmd = check_nextcmd(arg);
19467
19468 if (eap->skip)
19469 --emsg_skip;
19470}
19471
19472/*
19473 * Return from a function. Possibly makes the return pending. Also called
19474 * for a pending return at the ":endtry" or after returning from an extra
19475 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000019476 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 * FALSE when the return gets pending.
19479 */
19480 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 exarg_T *eap;
19483 int reanimate;
19484 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019485 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486{
19487 int idx;
19488 struct condstack *cstack = eap->cstack;
19489
19490 if (reanimate)
19491 /* Undo the return. */
19492 current_funccal->returned = FALSE;
19493
19494 /*
19495 * Cleanup (and inactivate) conditionals, but stop when a try conditional
19496 * not in its finally clause (which then is to be executed next) is found.
19497 * In this case, make the ":return" pending for execution at the ":endtry".
19498 * Otherwise, return normally.
19499 */
19500 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
19501 if (idx >= 0)
19502 {
19503 cstack->cs_pending[idx] = CSTP_RETURN;
19504
19505 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019506 /* A pending return again gets pending. "rettv" points to an
19507 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 else
19511 {
19512 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019513 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019517 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 {
19519 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019520 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019521 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 else
19523 EMSG(_(e_outofmem));
19524 }
19525 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019526 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527
19528 if (reanimate)
19529 {
19530 /* The pending return value could be overwritten by a ":return"
19531 * without argument in a finally clause; reset the default
19532 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019533 current_funccal->rettv->v_type = VAR_NUMBER;
19534 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 }
19536 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 }
19539 else
19540 {
19541 current_funccal->returned = TRUE;
19542
19543 /* If the return is carried out now, store the return value. For
19544 * a return immediately after reanimation, the value is already
19545 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019546 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000019549 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019551 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 }
19553 }
19554
19555 return idx < 0;
19556}
19557
19558/*
19559 * Free the variable with a pending return value.
19560 */
19561 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019562discard_pending_return(rettv)
19563 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564{
Bram Moolenaar33570922005-01-25 22:26:29 +000019565 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566}
19567
19568/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019569 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 * is an allocated string. Used by report_pending() for verbose messages.
19571 */
19572 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019573get_return_cmd(rettv)
19574 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019576 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019578 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019580 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019581 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019582 if (s == NULL)
19583 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584
19585 STRCPY(IObuff, ":return ");
19586 STRNCPY(IObuff + 8, s, IOSIZE - 8);
19587 if (STRLEN(s) + 8 >= IOSIZE)
19588 STRCPY(IObuff + IOSIZE - 4, "...");
19589 vim_free(tofree);
19590 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591}
19592
19593/*
19594 * Get next function line.
19595 * Called by do_cmdline() to get the next line.
19596 * Returns allocated string, or NULL for end of function.
19597 */
19598/* ARGSUSED */
19599 char_u *
19600get_func_line(c, cookie, indent)
19601 int c; /* not used */
19602 void *cookie;
19603 int indent; /* not used */
19604{
Bram Moolenaar33570922005-01-25 22:26:29 +000019605 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019606 ufunc_T *fp = fcp->func;
19607 char_u *retval;
19608 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609
19610 /* If breakpoints have been added/deleted need to check for it. */
19611 if (fcp->dbg_tick != debug_tick)
19612 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019613 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 sourcing_lnum);
19615 fcp->dbg_tick = debug_tick;
19616 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019617#ifdef FEAT_PROFILE
19618 if (do_profiling)
19619 func_line_end(cookie);
19620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621
Bram Moolenaar05159a02005-02-26 23:04:13 +000019622 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019623 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
19624 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 retval = NULL;
19626 else
19627 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019628 /* Skip NULL lines (continuation lines). */
19629 while (fcp->linenr < gap->ga_len
19630 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
19631 ++fcp->linenr;
19632 if (fcp->linenr >= gap->ga_len)
19633 retval = NULL;
19634 else
19635 {
19636 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
19637 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019638#ifdef FEAT_PROFILE
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019639 if (do_profiling)
19640 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019641#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 }
19644
19645 /* Did we encounter a breakpoint? */
19646 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
19647 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019648 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019650 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 sourcing_lnum);
19652 fcp->dbg_tick = debug_tick;
19653 }
19654
19655 return retval;
19656}
19657
Bram Moolenaar05159a02005-02-26 23:04:13 +000019658#if defined(FEAT_PROFILE) || defined(PROTO)
19659/*
19660 * Called when starting to read a function line.
19661 * "sourcing_lnum" must be correct!
19662 * When skipping lines it may not actually be executed, but we won't find out
19663 * until later and we need to store the time now.
19664 */
19665 void
19666func_line_start(cookie)
19667 void *cookie;
19668{
19669 funccall_T *fcp = (funccall_T *)cookie;
19670 ufunc_T *fp = fcp->func;
19671
19672 if (fp->uf_profiling && sourcing_lnum >= 1
19673 && sourcing_lnum <= fp->uf_lines.ga_len)
19674 {
19675 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019676 /* Skip continuation lines. */
19677 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
19678 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019679 fp->uf_tml_execed = FALSE;
19680 profile_start(&fp->uf_tml_start);
19681 profile_zero(&fp->uf_tml_children);
19682 profile_get_wait(&fp->uf_tml_wait);
19683 }
19684}
19685
19686/*
19687 * Called when actually executing a function line.
19688 */
19689 void
19690func_line_exec(cookie)
19691 void *cookie;
19692{
19693 funccall_T *fcp = (funccall_T *)cookie;
19694 ufunc_T *fp = fcp->func;
19695
19696 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19697 fp->uf_tml_execed = TRUE;
19698}
19699
19700/*
19701 * Called when done with a function line.
19702 */
19703 void
19704func_line_end(cookie)
19705 void *cookie;
19706{
19707 funccall_T *fcp = (funccall_T *)cookie;
19708 ufunc_T *fp = fcp->func;
19709
19710 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
19711 {
19712 if (fp->uf_tml_execed)
19713 {
19714 ++fp->uf_tml_count[fp->uf_tml_idx];
19715 profile_end(&fp->uf_tml_start);
19716 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
19717 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
19718 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
19719 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
19720 }
19721 fp->uf_tml_idx = -1;
19722 }
19723}
19724#endif
19725
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726/*
19727 * Return TRUE if the currently active function should be ended, because a
19728 * return was encountered or an error occured. Used inside a ":while".
19729 */
19730 int
19731func_has_ended(cookie)
19732 void *cookie;
19733{
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735
19736 /* Ignore the "abort" flag if the abortion behavior has been changed due to
19737 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019738 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 || fcp->returned);
19740}
19741
19742/*
19743 * return TRUE if cookie indicates a function which "abort"s on errors.
19744 */
19745 int
19746func_has_abort(cookie)
19747 void *cookie;
19748{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019749 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750}
19751
19752#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
19753typedef enum
19754{
19755 VAR_FLAVOUR_DEFAULT,
19756 VAR_FLAVOUR_SESSION,
19757 VAR_FLAVOUR_VIMINFO
19758} var_flavour_T;
19759
19760static var_flavour_T var_flavour __ARGS((char_u *varname));
19761
19762 static var_flavour_T
19763var_flavour(varname)
19764 char_u *varname;
19765{
19766 char_u *p = varname;
19767
19768 if (ASCII_ISUPPER(*p))
19769 {
19770 while (*(++p))
19771 if (ASCII_ISLOWER(*p))
19772 return VAR_FLAVOUR_SESSION;
19773 return VAR_FLAVOUR_VIMINFO;
19774 }
19775 else
19776 return VAR_FLAVOUR_DEFAULT;
19777}
19778#endif
19779
19780#if defined(FEAT_VIMINFO) || defined(PROTO)
19781/*
19782 * Restore global vars that start with a capital from the viminfo file
19783 */
19784 int
19785read_viminfo_varlist(virp, writing)
19786 vir_T *virp;
19787 int writing;
19788{
19789 char_u *tab;
19790 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000019791 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
19793 if (!writing && (find_viminfo_parameter('!') != NULL))
19794 {
19795 tab = vim_strchr(virp->vir_line + 1, '\t');
19796 if (tab != NULL)
19797 {
19798 *tab++ = '\0'; /* isolate the variable name */
19799 if (*tab == 'S') /* string var */
19800 is_string = TRUE;
19801
19802 tab = vim_strchr(tab, '\t');
19803 if (tab != NULL)
19804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 if (is_string)
19806 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019807 tv.v_type = VAR_STRING;
19808 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 }
19811 else
19812 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019813 tv.v_type = VAR_NUMBER;
19814 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019816 set_var(virp->vir_line + 1, &tv, FALSE);
19817 if (is_string)
19818 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 }
19820 }
19821 }
19822
19823 return viminfo_readline(virp);
19824}
19825
19826/*
19827 * Write global vars that start with a capital to the viminfo file
19828 */
19829 void
19830write_viminfo_varlist(fp)
19831 FILE *fp;
19832{
Bram Moolenaar33570922005-01-25 22:26:29 +000019833 hashitem_T *hi;
19834 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019835 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019837 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019839 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840
19841 if (find_viminfo_parameter('!') == NULL)
19842 return;
19843
19844 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000019845
Bram Moolenaar33570922005-01-25 22:26:29 +000019846 todo = globvarht.ht_used;
19847 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019849 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019851 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019852 this_var = HI2DI(hi);
19853 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019854 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019855 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000019856 {
19857 case VAR_STRING: s = "STR"; break;
19858 case VAR_NUMBER: s = "NUM"; break;
19859 default: continue;
19860 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019862 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019863 if (p != NULL)
19864 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000019865 vim_free(tofree);
19866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 }
19868 }
19869}
19870#endif
19871
19872#if defined(FEAT_SESSION) || defined(PROTO)
19873 int
19874store_session_globals(fd)
19875 FILE *fd;
19876{
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 hashitem_T *hi;
19878 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000019879 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 char_u *p, *t;
19881
Bram Moolenaar33570922005-01-25 22:26:29 +000019882 todo = globvarht.ht_used;
19883 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019885 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019887 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019888 this_var = HI2DI(hi);
19889 if ((this_var->di_tv.v_type == VAR_NUMBER
19890 || this_var->di_tv.v_type == VAR_STRING)
19891 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000019892 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019893 /* Escape special characters with a backslash. Turn a LF and
19894 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000019896 (char_u *)"\\\"\n\r");
19897 if (p == NULL) /* out of memory */
19898 break;
19899 for (t = p; *t != NUL; ++t)
19900 if (*t == '\n')
19901 *t = 'n';
19902 else if (*t == '\r')
19903 *t = 'r';
19904 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019905 this_var->di_key,
19906 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19907 : ' ',
19908 p,
19909 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19910 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019911 || put_eol(fd) == FAIL)
19912 {
19913 vim_free(p);
19914 return FAIL;
19915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 vim_free(p);
19917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 }
19919 }
19920 return OK;
19921}
19922#endif
19923
Bram Moolenaar661b1822005-07-28 22:36:45 +000019924/*
19925 * Display script name where an item was last set.
19926 * Should only be invoked when 'verbose' is non-zero.
19927 */
19928 void
19929last_set_msg(scriptID)
19930 scid_T scriptID;
19931{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019932 char_u *p;
19933
Bram Moolenaar661b1822005-07-28 22:36:45 +000019934 if (scriptID != 0)
19935 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019936 p = home_replace_save(NULL, get_scriptname(scriptID));
19937 if (p != NULL)
19938 {
19939 verbose_enter();
19940 MSG_PUTS(_("\n\tLast set from "));
19941 MSG_PUTS(p);
19942 vim_free(p);
19943 verbose_leave();
19944 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019945 }
19946}
19947
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948#endif /* FEAT_EVAL */
19949
19950#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19951
19952
19953#ifdef WIN3264
19954/*
19955 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19956 */
19957static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19958static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19959static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19960
19961/*
19962 * Get the short pathname of a file.
19963 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19964 */
19965 static int
19966get_short_pathname(fnamep, bufp, fnamelen)
19967 char_u **fnamep;
19968 char_u **bufp;
19969 int *fnamelen;
19970{
19971 int l,len;
19972 char_u *newbuf;
19973
19974 len = *fnamelen;
19975
19976 l = GetShortPathName(*fnamep, *fnamep, len);
19977 if (l > len - 1)
19978 {
19979 /* If that doesn't work (not enough space), then save the string
19980 * and try again with a new buffer big enough
19981 */
19982 newbuf = vim_strnsave(*fnamep, l);
19983 if (newbuf == NULL)
19984 return 0;
19985
19986 vim_free(*bufp);
19987 *fnamep = *bufp = newbuf;
19988
19989 l = GetShortPathName(*fnamep,*fnamep,l+1);
19990
19991 /* Really should always succeed, as the buffer is big enough */
19992 }
19993
19994 *fnamelen = l;
19995 return 1;
19996}
19997
19998/*
19999 * Create a short path name. Returns the length of the buffer it needs.
20000 * Doesn't copy over the end of the buffer passed in.
20001 */
20002 static int
20003shortpath_for_invalid_fname(fname, bufp, fnamelen)
20004 char_u **fname;
20005 char_u **bufp;
20006 int *fnamelen;
20007{
20008 char_u *s, *p, *pbuf2, *pbuf3;
20009 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020010 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011
20012 /* Make a copy */
20013 len2 = *fnamelen;
20014 pbuf2 = vim_strnsave(*fname, len2);
20015 pbuf3 = NULL;
20016
20017 s = pbuf2 + len2 - 1; /* Find the end */
20018 slen = 1;
20019 plen = len2;
20020
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020021 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 {
20023 --s;
20024 ++slen;
20025 --plen;
20026 }
20027
20028 do
20029 {
20030 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020031 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 {
20033 --s;
20034 ++slen;
20035 --plen;
20036 }
20037 if (s <= pbuf2)
20038 break;
20039
20040 /* Remeber the character that is about to be blatted */
20041 ch = *s;
20042 *s = 0; /* get_short_pathname requires a null-terminated string */
20043
20044 /* Try it in situ */
20045 p = pbuf2;
20046 if (!get_short_pathname(&p, &pbuf3, &plen))
20047 {
20048 vim_free(pbuf2);
20049 return -1;
20050 }
20051 *s = ch; /* Preserve the string */
20052 } while (plen == 0);
20053
20054 if (plen > 0)
20055 {
20056 /* Remeber the length of the new string. */
20057 *fnamelen = len = plen + slen;
20058 vim_free(*bufp);
20059 if (len > len2)
20060 {
20061 /* If there's not enough space in the currently allocated string,
20062 * then copy it to a buffer big enough.
20063 */
20064 *fname= *bufp = vim_strnsave(p, len);
20065 if (*fname == NULL)
20066 return -1;
20067 }
20068 else
20069 {
20070 /* Transfer pbuf2 to being the main buffer (it's big enough) */
20071 *fname = *bufp = pbuf2;
20072 if (p != pbuf2)
20073 strncpy(*fname, p, plen);
20074 pbuf2 = NULL;
20075 }
20076 /* Concat the next bit */
20077 strncpy(*fname + plen, s, slen);
20078 (*fname)[len] = '\0';
20079 }
20080 vim_free(pbuf3);
20081 vim_free(pbuf2);
20082 return 0;
20083}
20084
20085/*
20086 * Get a pathname for a partial path.
20087 */
20088 static int
20089shortpath_for_partial(fnamep, bufp, fnamelen)
20090 char_u **fnamep;
20091 char_u **bufp;
20092 int *fnamelen;
20093{
20094 int sepcount, len, tflen;
20095 char_u *p;
20096 char_u *pbuf, *tfname;
20097 int hasTilde;
20098
20099 /* Count up the path seperators from the RHS.. so we know which part
20100 * of the path to return.
20101 */
20102 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020103 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020104 if (vim_ispathsep(*p))
20105 ++sepcount;
20106
20107 /* Need full path first (use expand_env() to remove a "~/") */
20108 hasTilde = (**fnamep == '~');
20109 if (hasTilde)
20110 pbuf = tfname = expand_env_save(*fnamep);
20111 else
20112 pbuf = tfname = FullName_save(*fnamep, FALSE);
20113
20114 len = tflen = STRLEN(tfname);
20115
20116 if (!get_short_pathname(&tfname, &pbuf, &len))
20117 return -1;
20118
20119 if (len == 0)
20120 {
20121 /* Don't have a valid filename, so shorten the rest of the
20122 * path if we can. This CAN give us invalid 8.3 filenames, but
20123 * there's not a lot of point in guessing what it might be.
20124 */
20125 len = tflen;
20126 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
20127 return -1;
20128 }
20129
20130 /* Count the paths backward to find the beginning of the desired string. */
20131 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020132 {
20133#ifdef FEAT_MBYTE
20134 if (has_mbyte)
20135 p -= mb_head_off(tfname, p);
20136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 if (vim_ispathsep(*p))
20138 {
20139 if (sepcount == 0 || (hasTilde && sepcount == 1))
20140 break;
20141 else
20142 sepcount --;
20143 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 if (hasTilde)
20146 {
20147 --p;
20148 if (p >= tfname)
20149 *p = '~';
20150 else
20151 return -1;
20152 }
20153 else
20154 ++p;
20155
20156 /* Copy in the string - p indexes into tfname - allocated at pbuf */
20157 vim_free(*bufp);
20158 *fnamelen = (int)STRLEN(p);
20159 *bufp = pbuf;
20160 *fnamep = p;
20161
20162 return 0;
20163}
20164#endif /* WIN3264 */
20165
20166/*
20167 * Adjust a filename, according to a string of modifiers.
20168 * *fnamep must be NUL terminated when called. When returning, the length is
20169 * determined by *fnamelen.
20170 * Returns valid flags.
20171 * When there is an error, *fnamep is set to NULL.
20172 */
20173 int
20174modify_fname(src, usedlen, fnamep, bufp, fnamelen)
20175 char_u *src; /* string with modifiers */
20176 int *usedlen; /* characters after src that are used */
20177 char_u **fnamep; /* file name so far */
20178 char_u **bufp; /* buffer for allocated file name or NULL */
20179 int *fnamelen; /* length of fnamep */
20180{
20181 int valid = 0;
20182 char_u *tail;
20183 char_u *s, *p, *pbuf;
20184 char_u dirname[MAXPATHL];
20185 int c;
20186 int has_fullname = 0;
20187#ifdef WIN3264
20188 int has_shortname = 0;
20189#endif
20190
20191repeat:
20192 /* ":p" - full path/file_name */
20193 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
20194 {
20195 has_fullname = 1;
20196
20197 valid |= VALID_PATH;
20198 *usedlen += 2;
20199
20200 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
20201 if ((*fnamep)[0] == '~'
20202#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
20203 && ((*fnamep)[1] == '/'
20204# ifdef BACKSLASH_IN_FILENAME
20205 || (*fnamep)[1] == '\\'
20206# endif
20207 || (*fnamep)[1] == NUL)
20208
20209#endif
20210 )
20211 {
20212 *fnamep = expand_env_save(*fnamep);
20213 vim_free(*bufp); /* free any allocated file name */
20214 *bufp = *fnamep;
20215 if (*fnamep == NULL)
20216 return -1;
20217 }
20218
20219 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020220 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 {
20222 if (vim_ispathsep(*p)
20223 && p[1] == '.'
20224 && (p[2] == NUL
20225 || vim_ispathsep(p[2])
20226 || (p[2] == '.'
20227 && (p[3] == NUL || vim_ispathsep(p[3])))))
20228 break;
20229 }
20230
20231 /* FullName_save() is slow, don't use it when not needed. */
20232 if (*p != NUL || !vim_isAbsName(*fnamep))
20233 {
20234 *fnamep = FullName_save(*fnamep, *p != NUL);
20235 vim_free(*bufp); /* free any allocated file name */
20236 *bufp = *fnamep;
20237 if (*fnamep == NULL)
20238 return -1;
20239 }
20240
20241 /* Append a path separator to a directory. */
20242 if (mch_isdir(*fnamep))
20243 {
20244 /* Make room for one or two extra characters. */
20245 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
20246 vim_free(*bufp); /* free any allocated file name */
20247 *bufp = *fnamep;
20248 if (*fnamep == NULL)
20249 return -1;
20250 add_pathsep(*fnamep);
20251 }
20252 }
20253
20254 /* ":." - path relative to the current directory */
20255 /* ":~" - path relative to the home directory */
20256 /* ":8" - shortname path - postponed till after */
20257 while (src[*usedlen] == ':'
20258 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
20259 {
20260 *usedlen += 2;
20261 if (c == '8')
20262 {
20263#ifdef WIN3264
20264 has_shortname = 1; /* Postpone this. */
20265#endif
20266 continue;
20267 }
20268 pbuf = NULL;
20269 /* Need full path first (use expand_env() to remove a "~/") */
20270 if (!has_fullname)
20271 {
20272 if (c == '.' && **fnamep == '~')
20273 p = pbuf = expand_env_save(*fnamep);
20274 else
20275 p = pbuf = FullName_save(*fnamep, FALSE);
20276 }
20277 else
20278 p = *fnamep;
20279
20280 has_fullname = 0;
20281
20282 if (p != NULL)
20283 {
20284 if (c == '.')
20285 {
20286 mch_dirname(dirname, MAXPATHL);
20287 s = shorten_fname(p, dirname);
20288 if (s != NULL)
20289 {
20290 *fnamep = s;
20291 if (pbuf != NULL)
20292 {
20293 vim_free(*bufp); /* free any allocated file name */
20294 *bufp = pbuf;
20295 pbuf = NULL;
20296 }
20297 }
20298 }
20299 else
20300 {
20301 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
20302 /* Only replace it when it starts with '~' */
20303 if (*dirname == '~')
20304 {
20305 s = vim_strsave(dirname);
20306 if (s != NULL)
20307 {
20308 *fnamep = s;
20309 vim_free(*bufp);
20310 *bufp = s;
20311 }
20312 }
20313 }
20314 vim_free(pbuf);
20315 }
20316 }
20317
20318 tail = gettail(*fnamep);
20319 *fnamelen = (int)STRLEN(*fnamep);
20320
20321 /* ":h" - head, remove "/file_name", can be repeated */
20322 /* Don't remove the first "/" or "c:\" */
20323 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
20324 {
20325 valid |= VALID_HEAD;
20326 *usedlen += 2;
20327 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020328 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 --tail;
20330 *fnamelen = (int)(tail - *fnamep);
20331#ifdef VMS
20332 if (*fnamelen > 0)
20333 *fnamelen += 1; /* the path separator is part of the path */
20334#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000020335 while (tail > s && !after_pathsep(s, tail))
20336 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 }
20338
20339 /* ":8" - shortname */
20340 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
20341 {
20342 *usedlen += 2;
20343#ifdef WIN3264
20344 has_shortname = 1;
20345#endif
20346 }
20347
20348#ifdef WIN3264
20349 /* Check shortname after we have done 'heads' and before we do 'tails'
20350 */
20351 if (has_shortname)
20352 {
20353 pbuf = NULL;
20354 /* Copy the string if it is shortened by :h */
20355 if (*fnamelen < (int)STRLEN(*fnamep))
20356 {
20357 p = vim_strnsave(*fnamep, *fnamelen);
20358 if (p == 0)
20359 return -1;
20360 vim_free(*bufp);
20361 *bufp = *fnamep = p;
20362 }
20363
20364 /* Split into two implementations - makes it easier. First is where
20365 * there isn't a full name already, second is where there is.
20366 */
20367 if (!has_fullname && !vim_isAbsName(*fnamep))
20368 {
20369 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
20370 return -1;
20371 }
20372 else
20373 {
20374 int l;
20375
20376 /* Simple case, already have the full-name
20377 * Nearly always shorter, so try first time. */
20378 l = *fnamelen;
20379 if (!get_short_pathname(fnamep, bufp, &l))
20380 return -1;
20381
20382 if (l == 0)
20383 {
20384 /* Couldn't find the filename.. search the paths.
20385 */
20386 l = *fnamelen;
20387 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
20388 return -1;
20389 }
20390 *fnamelen = l;
20391 }
20392 }
20393#endif /* WIN3264 */
20394
20395 /* ":t" - tail, just the basename */
20396 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
20397 {
20398 *usedlen += 2;
20399 *fnamelen -= (int)(tail - *fnamep);
20400 *fnamep = tail;
20401 }
20402
20403 /* ":e" - extension, can be repeated */
20404 /* ":r" - root, without extension, can be repeated */
20405 while (src[*usedlen] == ':'
20406 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
20407 {
20408 /* find a '.' in the tail:
20409 * - for second :e: before the current fname
20410 * - otherwise: The last '.'
20411 */
20412 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
20413 s = *fnamep - 2;
20414 else
20415 s = *fnamep + *fnamelen - 1;
20416 for ( ; s > tail; --s)
20417 if (s[0] == '.')
20418 break;
20419 if (src[*usedlen + 1] == 'e') /* :e */
20420 {
20421 if (s > tail)
20422 {
20423 *fnamelen += (int)(*fnamep - (s + 1));
20424 *fnamep = s + 1;
20425#ifdef VMS
20426 /* cut version from the extension */
20427 s = *fnamep + *fnamelen - 1;
20428 for ( ; s > *fnamep; --s)
20429 if (s[0] == ';')
20430 break;
20431 if (s > *fnamep)
20432 *fnamelen = s - *fnamep;
20433#endif
20434 }
20435 else if (*fnamep <= tail)
20436 *fnamelen = 0;
20437 }
20438 else /* :r */
20439 {
20440 if (s > tail) /* remove one extension */
20441 *fnamelen = (int)(s - *fnamep);
20442 }
20443 *usedlen += 2;
20444 }
20445
20446 /* ":s?pat?foo?" - substitute */
20447 /* ":gs?pat?foo?" - global substitute */
20448 if (src[*usedlen] == ':'
20449 && (src[*usedlen + 1] == 's'
20450 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
20451 {
20452 char_u *str;
20453 char_u *pat;
20454 char_u *sub;
20455 int sep;
20456 char_u *flags;
20457 int didit = FALSE;
20458
20459 flags = (char_u *)"";
20460 s = src + *usedlen + 2;
20461 if (src[*usedlen + 1] == 'g')
20462 {
20463 flags = (char_u *)"g";
20464 ++s;
20465 }
20466
20467 sep = *s++;
20468 if (sep)
20469 {
20470 /* find end of pattern */
20471 p = vim_strchr(s, sep);
20472 if (p != NULL)
20473 {
20474 pat = vim_strnsave(s, (int)(p - s));
20475 if (pat != NULL)
20476 {
20477 s = p + 1;
20478 /* find end of substitution */
20479 p = vim_strchr(s, sep);
20480 if (p != NULL)
20481 {
20482 sub = vim_strnsave(s, (int)(p - s));
20483 str = vim_strnsave(*fnamep, *fnamelen);
20484 if (sub != NULL && str != NULL)
20485 {
20486 *usedlen = (int)(p + 1 - src);
20487 s = do_string_sub(str, pat, sub, flags);
20488 if (s != NULL)
20489 {
20490 *fnamep = s;
20491 *fnamelen = (int)STRLEN(s);
20492 vim_free(*bufp);
20493 *bufp = s;
20494 didit = TRUE;
20495 }
20496 }
20497 vim_free(sub);
20498 vim_free(str);
20499 }
20500 vim_free(pat);
20501 }
20502 }
20503 /* after using ":s", repeat all the modifiers */
20504 if (didit)
20505 goto repeat;
20506 }
20507 }
20508
20509 return valid;
20510}
20511
20512/*
20513 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
20514 * "flags" can be "g" to do a global substitute.
20515 * Returns an allocated string, NULL for error.
20516 */
20517 char_u *
20518do_string_sub(str, pat, sub, flags)
20519 char_u *str;
20520 char_u *pat;
20521 char_u *sub;
20522 char_u *flags;
20523{
20524 int sublen;
20525 regmatch_T regmatch;
20526 int i;
20527 int do_all;
20528 char_u *tail;
20529 garray_T ga;
20530 char_u *ret;
20531 char_u *save_cpo;
20532
20533 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
20534 save_cpo = p_cpo;
20535 p_cpo = (char_u *)"";
20536
20537 ga_init2(&ga, 1, 200);
20538
20539 do_all = (flags[0] == 'g');
20540
20541 regmatch.rm_ic = p_ic;
20542 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
20543 if (regmatch.regprog != NULL)
20544 {
20545 tail = str;
20546 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
20547 {
20548 /*
20549 * Get some space for a temporary buffer to do the substitution
20550 * into. It will contain:
20551 * - The text up to where the match is.
20552 * - The substituted text.
20553 * - The text after the match.
20554 */
20555 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
20556 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
20557 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
20558 {
20559 ga_clear(&ga);
20560 break;
20561 }
20562
20563 /* copy the text up to where the match is */
20564 i = (int)(regmatch.startp[0] - tail);
20565 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
20566 /* add the substituted text */
20567 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
20568 + ga.ga_len + i, TRUE, TRUE, FALSE);
20569 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 /* avoid getting stuck on a match with an empty string */
20571 if (tail == regmatch.endp[0])
20572 {
20573 if (*tail == NUL)
20574 break;
20575 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
20576 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 }
20578 else
20579 {
20580 tail = regmatch.endp[0];
20581 if (*tail == NUL)
20582 break;
20583 }
20584 if (!do_all)
20585 break;
20586 }
20587
20588 if (ga.ga_data != NULL)
20589 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
20590
20591 vim_free(regmatch.regprog);
20592 }
20593
20594 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
20595 ga_clear(&ga);
20596 p_cpo = save_cpo;
20597
20598 return ret;
20599}
20600
20601#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */