blob: c03da196c32e3fd0135e180f3806709fd14d3623 [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 */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169 proftime_T uf_tm_children; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count; /* nr of times line was executed */
172 proftime_T *uf_tml_total; /* time spend in a line + children */
173 proftime_T *uf_tml_self; /* time spend in a line itself */
174 proftime_T uf_tml_start; /* start time for current line */
175 proftime_T uf_tml_children; /* time spent in children for this line */
176 proftime_T uf_tml_wait; /* start wait time for current line */
177 int uf_tml_idx; /* index of line being timed; -1 if none */
178 int uf_tml_execed; /* line being timed was executed */
179#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000180 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000182 int uf_refcount; /* for numbered function: reference count */
183 char_u uf_name[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
185 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186};
187
188/* function flags */
189#define FC_ABORT 1 /* abort function on error */
190#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000191#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000194 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000196static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000198/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000199static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000340 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000341 {VV_NAME("swapname", VAR_STRING), VV_RO},
342 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000343 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000344 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000345 {VV_NAME("mouse_win", VAR_NUMBER), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000348 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000349};
350
351/* shorthand */
352#define vv_type vv_di.di_tv.v_type
353#define vv_nr vv_di.di_tv.vval.v_number
354#define vv_str vv_di.di_tv.vval.v_string
355#define vv_tv vv_di.di_tv
356
357/*
358 * The v: variables are stored in dictionary "vimvardict".
359 * "vimvars_var" is the variable that is used for the "l:" scope.
360 */
361static dict_T vimvardict;
362static dictitem_T vimvars_var;
363#define vimvarht vimvardict.dv_hashtab
364
Bram Moolenaara40058a2005-07-11 22:42:07 +0000365static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
366static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
367#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
368static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
369#endif
370static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
371static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
372static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000373static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
374static void list_glob_vars __ARGS((int *first));
375static void list_buf_vars __ARGS((int *first));
376static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000377#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000378static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000379#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000380static void list_vim_vars __ARGS((int *first));
381static void list_script_vars __ARGS((int *first));
382static void list_func_vars __ARGS((int *first));
383static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000384static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
385static int check_changedtick __ARGS((char_u *arg));
386static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
387static void clear_lval __ARGS((lval_T *lp));
388static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
389static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
390static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
391static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
392static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
393static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
394static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
395static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
396static void item_lock __ARGS((typval_T *tv, int deep, int lock));
397static int tv_islocked __ARGS((typval_T *tv));
398
Bram Moolenaar33570922005-01-25 22:26:29 +0000399static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
400static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
401static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000407
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000408static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000409static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000413static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static listitem_T *listitem_alloc __ARGS((void));
415static void listitem_free __ARGS((listitem_T *item));
416static void listitem_remove __ARGS((list_T *l, listitem_T *item));
417static long list_len __ARGS((list_T *l));
418static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
419static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
420static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static void list_append __ARGS((list_T *l, listitem_T *item));
425static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000426static int list_append_string __ARGS((list_T *l, char_u *str, int len));
427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000433static char_u *list2string __ARGS((typval_T *tv, int copyID));
434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000435static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
436static void set_ref_in_list __ARGS((list_T *l, int copyID));
437static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000439static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_alloc __ARGS((char_u *key));
441static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
442static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
443static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000444static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int dict_add __ARGS((dict_T *d, dictitem_T *item));
446static long dict_len __ARGS((dict_T *d));
447static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000448static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
451static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static char_u *string_quote __ARGS((char_u *str, int function));
453static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
454static int find_internal_func __ARGS((char_u *name));
455static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
456static 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));
457static 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 Moolenaar89d40322006-08-29 15:30:07 +0000458static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000476static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000479static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000481#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000482static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000483static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
491static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000504static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000518static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000520static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000526static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000527static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000534static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000535static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000536static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000539static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000547static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000561static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000567static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000583static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000584static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000585static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000587static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000591#ifdef vim_mkdir
592static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
593#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000597static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000599static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000600static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000602static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000603static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000616static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000618static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000625static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000626static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000627static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000628static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000630static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000632static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000635static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000636static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
637static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2c932302006-03-18 21:42:09 +0000639static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640#ifdef HAVE_STRFTIME
641static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
643static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000655static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000656static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000657static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000658static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000659static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000661static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000675static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000678static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000680static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000681static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static int get_env_len __ARGS((char_u **arg));
683static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000684static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000685static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
686#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
687#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
688 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000689static 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 +0000690static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000691static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000692static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
693static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static typval_T *alloc_tv __ARGS((void));
695static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void init_tv __ARGS((typval_T *varp));
697static long get_tv_number __ARGS((typval_T *varp));
698static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000699static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static char_u *get_tv_string __ARGS((typval_T *varp));
701static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000702static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000704static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
706static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
707static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000708static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
709static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
711static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000712static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000713static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000715static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
717static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
718static int eval_fname_script __ARGS((char_u *p));
719static int eval_fname_sid __ARGS((char_u *p));
720static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static ufunc_T *find_func __ARGS((char_u *name));
722static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000723static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000724#ifdef FEAT_PROFILE
725static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000726static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
727static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
728static int
729# ifdef __BORLANDC__
730 _RTLENTRYF
731# endif
732 prof_total_cmp __ARGS((const void *s1, const void *s2));
733static int
734# ifdef __BORLANDC__
735 _RTLENTRYF
736# endif
737 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000738#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000739static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000740static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000741static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void func_free __ARGS((ufunc_T *fp));
743static void func_unref __ARGS((char_u *name));
744static void func_ref __ARGS((char_u *name));
745static 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));
746static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000747static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
748static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000749static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000750static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000751static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000753/* Character used as separated in autoload function/variable names. */
754#define AUTOLOAD_CHAR '#'
755
Bram Moolenaar33570922005-01-25 22:26:29 +0000756/*
757 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000758 */
759 void
760eval_init()
761{
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 int i;
763 struct vimvar *p;
764
765 init_var_dict(&globvardict, &globvars_var);
766 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000767 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000768 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769
770 for (i = 0; i < VV_LEN; ++i)
771 {
772 p = &vimvars[i];
773 STRCPY(p->vv_di.di_key, p->vv_name);
774 if (p->vv_flags & VV_RO)
775 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
776 else if (p->vv_flags & VV_RO_SBX)
777 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
778 else
779 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780
781 /* add to v: scope dict, unless the value is not always available */
782 if (p->vv_type != VAR_UNKNOWN)
783 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000784 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000785 /* add to compat scope dict */
786 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000788}
789
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000790#if defined(EXITFREE) || defined(PROTO)
791 void
792eval_clear()
793{
794 int i;
795 struct vimvar *p;
796
797 for (i = 0; i < VV_LEN; ++i)
798 {
799 p = &vimvars[i];
800 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000801 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000802 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000803 p->vv_di.di_tv.vval.v_string = NULL;
804 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000805 }
806 hash_clear(&vimvarht);
807 hash_clear(&compat_hashtab);
808
809 /* script-local variables */
810 for (i = 1; i <= ga_scripts.ga_len; ++i)
811 vars_clear(&SCRIPT_VARS(i));
812 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000813 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000814
815 /* global variables */
816 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000817
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000818 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000819 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000820 hash_clear(&func_hashtab);
821
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000822 /* autoloaded script names */
823 ga_clear_strings(&ga_loaded);
824
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000825 /* unreferenced lists and dicts */
826 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000827}
828#endif
829
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 * Return the name of the executed function.
832 */
833 char_u *
834func_name(cookie)
835 void *cookie;
836{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000837 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
839
840/*
841 * Return the address holding the next breakpoint line for a funccall cookie.
842 */
843 linenr_T *
844func_breakpoint(cookie)
845 void *cookie;
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848}
849
850/*
851 * Return the address holding the debug tick for a funccall cookie.
852 */
853 int *
854func_dbg_tick(cookie)
855 void *cookie;
856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858}
859
860/*
861 * Return the nesting level for a funccall cookie.
862 */
863 int
864func_level(cookie)
865 void *cookie;
866{
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868}
869
870/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000871funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873/*
874 * Return TRUE when a function was ended by a ":return" command.
875 */
876 int
877current_func_returned()
878{
879 return current_funccal->returned;
880}
881
882
883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * Set an internal variable to a string value. Creates the variable if it does
885 * not already exist.
886 */
887 void
888set_internal_string_var(name, value)
889 char_u *name;
890 char_u *value;
891{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000892 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000893 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
895 val = vim_strsave(value);
896 if (val != NULL)
897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000898 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000899 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000901 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000902 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904 }
905}
906
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000907static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000908static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000909static char_u *redir_endp = NULL;
910static char_u *redir_varname = NULL;
911
912/*
913 * Start recording command output to a variable
914 * Returns OK if successfully completed the setup. FAIL otherwise.
915 */
916 int
917var_redir_start(name, append)
918 char_u *name;
919 int append; /* append to an existing variable */
920{
921 int save_emsg;
922 int err;
923 typval_T tv;
924
925 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000926 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000927 {
928 EMSG(_(e_invarg));
929 return FAIL;
930 }
931
932 redir_varname = vim_strsave(name);
933 if (redir_varname == NULL)
934 return FAIL;
935
936 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
937 if (redir_lval == NULL)
938 {
939 var_redir_stop();
940 return FAIL;
941 }
942
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000943 /* The output is stored in growarray "redir_ga" until redirection ends. */
944 ga_init2(&redir_ga, (int)sizeof(char), 500);
945
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000946 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000947 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
948 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
950 {
951 if (redir_endp != NULL && *redir_endp != NUL)
952 /* Trailing characters are present after the variable name */
953 EMSG(_(e_trailing));
954 else
955 EMSG(_(e_invarg));
956 var_redir_stop();
957 return FAIL;
958 }
959
960 /* check if we can write to the variable: set it to or append an empty
961 * string */
962 save_emsg = did_emsg;
963 did_emsg = FALSE;
964 tv.v_type = VAR_STRING;
965 tv.vval.v_string = (char_u *)"";
966 if (append)
967 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
968 else
969 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
970 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000971 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000972 if (err)
973 {
974 var_redir_stop();
975 return FAIL;
976 }
977 if (redir_lval->ll_newkey != NULL)
978 {
979 /* Dictionary item was created, don't do it again. */
980 vim_free(redir_lval->ll_newkey);
981 redir_lval->ll_newkey = NULL;
982 }
983
984 return OK;
985}
986
987/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000988 * Append "value[value_len]" to the variable set by var_redir_start().
989 * The actual appending is postponed until redirection ends, because the value
990 * appended may in fact be the string we write to, changing it may cause freed
991 * memory to be used:
992 * :redir => foo
993 * :let foo
994 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000995 */
996 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000997var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000998 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000999 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001000{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001001 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002
1003 if (redir_lval == NULL)
1004 return;
1005
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001006 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001007 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001009 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001011 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001012 {
1013 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001014 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015 }
1016 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018}
1019
1020/*
1021 * Stop redirecting command output to a variable.
1022 */
1023 void
1024var_redir_stop()
1025{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001026 typval_T tv;
1027
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028 if (redir_lval != NULL)
1029 {
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030 /* Append the trailing NUL. */
1031 ga_append(&redir_ga, NUL);
1032
1033 /* Assign the text to the variable. */
1034 tv.v_type = VAR_STRING;
1035 tv.vval.v_string = redir_ga.ga_data;
1036 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1037 vim_free(tv.vval.v_string);
1038
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 clear_lval(redir_lval);
1040 vim_free(redir_lval);
1041 redir_lval = NULL;
1042 }
1043 vim_free(redir_varname);
1044 redir_varname = NULL;
1045}
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047# if defined(FEAT_MBYTE) || defined(PROTO)
1048 int
1049eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1050 char_u *enc_from;
1051 char_u *enc_to;
1052 char_u *fname_from;
1053 char_u *fname_to;
1054{
1055 int err = FALSE;
1056
1057 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1058 set_vim_var_string(VV_CC_TO, enc_to, -1);
1059 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1060 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1061 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1062 err = TRUE;
1063 set_vim_var_string(VV_CC_FROM, NULL, -1);
1064 set_vim_var_string(VV_CC_TO, NULL, -1);
1065 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1066 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1067
1068 if (err)
1069 return FAIL;
1070 return OK;
1071}
1072# endif
1073
1074# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1075 int
1076eval_printexpr(fname, args)
1077 char_u *fname;
1078 char_u *args;
1079{
1080 int err = FALSE;
1081
1082 set_vim_var_string(VV_FNAME_IN, fname, -1);
1083 set_vim_var_string(VV_CMDARG, args, -1);
1084 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1085 err = TRUE;
1086 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1087 set_vim_var_string(VV_CMDARG, NULL, -1);
1088
1089 if (err)
1090 {
1091 mch_remove(fname);
1092 return FAIL;
1093 }
1094 return OK;
1095}
1096# endif
1097
1098# if defined(FEAT_DIFF) || defined(PROTO)
1099 void
1100eval_diff(origfile, newfile, outfile)
1101 char_u *origfile;
1102 char_u *newfile;
1103 char_u *outfile;
1104{
1105 int err = FALSE;
1106
1107 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1108 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1109 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1110 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1111 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1112 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1113 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1114}
1115
1116 void
1117eval_patch(origfile, difffile, outfile)
1118 char_u *origfile;
1119 char_u *difffile;
1120 char_u *outfile;
1121{
1122 int err;
1123
1124 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1125 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1126 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1127 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1128 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1129 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1130 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1131}
1132# endif
1133
1134/*
1135 * Top level evaluation function, returning a boolean.
1136 * Sets "error" to TRUE if there was an error.
1137 * Return TRUE or FALSE.
1138 */
1139 int
1140eval_to_bool(arg, error, nextcmd, skip)
1141 char_u *arg;
1142 int *error;
1143 char_u **nextcmd;
1144 int skip; /* only parse, don't execute */
1145{
Bram Moolenaar33570922005-01-25 22:26:29 +00001146 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 int retval = FALSE;
1148
1149 if (skip)
1150 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 else
1154 {
1155 *error = FALSE;
1156 if (!skip)
1157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001158 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 }
1162 if (skip)
1163 --emsg_skip;
1164
1165 return retval;
1166}
1167
1168/*
1169 * Top level evaluation function, returning a string. If "skip" is TRUE,
1170 * only parsing to "nextcmd" is done, without reporting errors. Return
1171 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1172 */
1173 char_u *
1174eval_to_string_skip(arg, nextcmd, skip)
1175 char_u *arg;
1176 char_u **nextcmd;
1177 int skip; /* only parse, don't execute */
1178{
Bram Moolenaar33570922005-01-25 22:26:29 +00001179 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 char_u *retval;
1181
1182 if (skip)
1183 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001184 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 retval = NULL;
1186 else
1187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001188 retval = vim_strsave(get_tv_string(&tv));
1189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191 if (skip)
1192 --emsg_skip;
1193
1194 return retval;
1195}
1196
1197/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001198 * Skip over an expression at "*pp".
1199 * Return FAIL for an error, OK otherwise.
1200 */
1201 int
1202skip_expr(pp)
1203 char_u **pp;
1204{
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001206
1207 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001208 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001209}
1210
1211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 * Top level evaluation function, returning a string.
1213 * Return pointer to allocated memory, or NULL for failure.
1214 */
1215 char_u *
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001216eval_to_string(arg, nextcmd, dolist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 char_u *arg;
1218 char_u **nextcmd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001219 int dolist; /* turn List into sequence of lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
Bram Moolenaar33570922005-01-25 22:26:29 +00001221 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001223 garray_T ga;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 retval = NULL;
1227 else
1228 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001229 if (dolist && tv.v_type == VAR_LIST)
1230 {
1231 ga_init2(&ga, (int)sizeof(char), 80);
1232 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
1233 ga_append(&ga, NUL);
1234 retval = (char_u *)ga.ga_data;
1235 }
1236 else
1237 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001238 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240
1241 return retval;
1242}
1243
1244/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001245 * Call eval_to_string() without using current local variables and using
1246 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
1248 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001249eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 char_u *arg;
1251 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001252 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
1254 char_u *retval;
1255 void *save_funccalp;
1256
1257 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001258 if (use_sandbox)
1259 ++sandbox;
1260 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001261 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001262 if (use_sandbox)
1263 --sandbox;
1264 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 restore_funccal(save_funccalp);
1266 return retval;
1267}
1268
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269/*
1270 * Top level evaluation function, returning a number.
1271 * Evaluates "expr" silently.
1272 * Returns -1 for an error.
1273 */
1274 int
1275eval_to_number(expr)
1276 char_u *expr;
1277{
Bram Moolenaar33570922005-01-25 22:26:29 +00001278 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001280 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 ++emsg_off;
1283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 retval = -1;
1286 else
1287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001288 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291 --emsg_off;
1292
1293 return retval;
1294}
1295
Bram Moolenaara40058a2005-07-11 22:42:07 +00001296/*
1297 * Prepare v: variable "idx" to be used.
1298 * Save the current typeval in "save_tv".
1299 * When not used yet add the variable to the v: hashtable.
1300 */
1301 static void
1302prepare_vimvar(idx, save_tv)
1303 int idx;
1304 typval_T *save_tv;
1305{
1306 *save_tv = vimvars[idx].vv_tv;
1307 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1308 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1309}
1310
1311/*
1312 * Restore v: variable "idx" to typeval "save_tv".
1313 * When no longer defined, remove the variable from the v: hashtable.
1314 */
1315 static void
1316restore_vimvar(idx, save_tv)
1317 int idx;
1318 typval_T *save_tv;
1319{
1320 hashitem_T *hi;
1321
Bram Moolenaara40058a2005-07-11 22:42:07 +00001322 vimvars[idx].vv_tv = *save_tv;
1323 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1324 {
1325 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1326 if (HASHITEM_EMPTY(hi))
1327 EMSG2(_(e_intern2), "restore_vimvar()");
1328 else
1329 hash_remove(&vimvarht, hi);
1330 }
1331}
1332
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001333#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001334/*
1335 * Evaluate an expression to a list with suggestions.
1336 * For the "expr:" part of 'spellsuggest'.
1337 */
1338 list_T *
1339eval_spell_expr(badword, expr)
1340 char_u *badword;
1341 char_u *expr;
1342{
1343 typval_T save_val;
1344 typval_T rettv;
1345 list_T *list = NULL;
1346 char_u *p = skipwhite(expr);
1347
1348 /* Set "v:val" to the bad word. */
1349 prepare_vimvar(VV_VAL, &save_val);
1350 vimvars[VV_VAL].vv_type = VAR_STRING;
1351 vimvars[VV_VAL].vv_str = badword;
1352 if (p_verbose == 0)
1353 ++emsg_off;
1354
1355 if (eval1(&p, &rettv, TRUE) == OK)
1356 {
1357 if (rettv.v_type != VAR_LIST)
1358 clear_tv(&rettv);
1359 else
1360 list = rettv.vval.v_list;
1361 }
1362
1363 if (p_verbose == 0)
1364 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001365 restore_vimvar(VV_VAL, &save_val);
1366
1367 return list;
1368}
1369
1370/*
1371 * "list" is supposed to contain two items: a word and a number. Return the
1372 * word in "pp" and the number as the return value.
1373 * Return -1 if anything isn't right.
1374 * Used to get the good word and score from the eval_spell_expr() result.
1375 */
1376 int
1377get_spellword(list, pp)
1378 list_T *list;
1379 char_u **pp;
1380{
1381 listitem_T *li;
1382
1383 li = list->lv_first;
1384 if (li == NULL)
1385 return -1;
1386 *pp = get_tv_string(&li->li_tv);
1387
1388 li = li->li_next;
1389 if (li == NULL)
1390 return -1;
1391 return get_tv_number(&li->li_tv);
1392}
1393#endif
1394
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001395/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001396 * Top level evaluation function.
1397 * Returns an allocated typval_T with the result.
1398 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001399 */
1400 typval_T *
1401eval_expr(arg, nextcmd)
1402 char_u *arg;
1403 char_u **nextcmd;
1404{
1405 typval_T *tv;
1406
1407 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001408 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001409 {
1410 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001411 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001412 }
1413
1414 return tv;
1415}
1416
1417
Bram Moolenaar4f688582007-07-24 12:34:30 +00001418#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1419 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001421 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001423 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001425 static int
1426call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 char_u *func;
1428 int argc;
1429 char_u **argv;
1430 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 long n;
1435 int len;
1436 int i;
1437 int doesrange;
1438 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001439 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001441 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001443 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 for (i = 0; i < argc; i++)
1446 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001447 /* Pass a NULL or empty argument as an empty string */
1448 if (argv[i] == NULL || *argv[i] == NUL)
1449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001450 argvars[i].v_type = VAR_STRING;
1451 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001452 continue;
1453 }
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /* Recognize a number argument, the others must be strings. */
1456 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1457 if (len != 0 && len == (int)STRLEN(argv[i]))
1458 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001459 argvars[i].v_type = VAR_NUMBER;
1460 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 else
1463 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001464 argvars[i].v_type = VAR_STRING;
1465 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 }
1467 }
1468
1469 if (safe)
1470 {
1471 save_funccalp = save_funccal();
1472 ++sandbox;
1473 }
1474
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001475 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1476 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001478 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (safe)
1480 {
1481 --sandbox;
1482 restore_funccal(save_funccalp);
1483 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001484 vim_free(argvars);
1485
1486 if (ret == FAIL)
1487 clear_tv(rettv);
1488
1489 return ret;
1490}
1491
Bram Moolenaar4f688582007-07-24 12:34:30 +00001492# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001493/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001494 * Call vimL function "func" and return the result as a string.
1495 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001496 * Uses argv[argc] for the function arguments.
1497 */
1498 void *
1499call_func_retstr(func, argc, argv, safe)
1500 char_u *func;
1501 int argc;
1502 char_u **argv;
1503 int safe; /* use the sandbox */
1504{
1505 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001506 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001507
1508 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1509 return NULL;
1510
1511 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001512 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 return retval;
1514}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001515# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001516
Bram Moolenaar4f688582007-07-24 12:34:30 +00001517# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001518/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001519 * Call vimL function "func" and return the result as a number.
1520 * Returns -1 when calling the function fails.
1521 * Uses argv[argc] for the function arguments.
1522 */
1523 long
1524call_func_retnr(func, argc, argv, safe)
1525 char_u *func;
1526 int argc;
1527 char_u **argv;
1528 int safe; /* use the sandbox */
1529{
1530 typval_T rettv;
1531 long retval;
1532
1533 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1534 return -1;
1535
1536 retval = get_tv_number_chk(&rettv, NULL);
1537 clear_tv(&rettv);
1538 return retval;
1539}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001540# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001541
1542/*
1543 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001544 * Uses argv[argc] for the function arguments.
1545 */
1546 void *
1547call_func_retlist(func, argc, argv, safe)
1548 char_u *func;
1549 int argc;
1550 char_u **argv;
1551 int safe; /* use the sandbox */
1552{
1553 typval_T rettv;
1554
1555 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1556 return NULL;
1557
1558 if (rettv.v_type != VAR_LIST)
1559 {
1560 clear_tv(&rettv);
1561 return NULL;
1562 }
1563
1564 return rettv.vval.v_list;
1565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566#endif
1567
Bram Moolenaar4f688582007-07-24 12:34:30 +00001568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569/*
1570 * Save the current function call pointer, and set it to NULL.
1571 * Used when executing autocommands and for ":source".
1572 */
1573 void *
1574save_funccal()
1575{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001576 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 current_funccal = NULL;
1579 return (void *)fc;
1580}
1581
1582 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001583restore_funccal(vfc)
1584 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001586 funccall_T *fc = (funccall_T *)vfc;
1587
1588 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
Bram Moolenaar05159a02005-02-26 23:04:13 +00001591#if defined(FEAT_PROFILE) || defined(PROTO)
1592/*
1593 * Prepare profiling for entering a child or something else that is not
1594 * counted for the script/function itself.
1595 * Should always be called in pair with prof_child_exit().
1596 */
1597 void
1598prof_child_enter(tm)
1599 proftime_T *tm; /* place to store waittime */
1600{
1601 funccall_T *fc = current_funccal;
1602
1603 if (fc != NULL && fc->func->uf_profiling)
1604 profile_start(&fc->prof_child);
1605 script_prof_save(tm);
1606}
1607
1608/*
1609 * Take care of time spent in a child.
1610 * Should always be called after prof_child_enter().
1611 */
1612 void
1613prof_child_exit(tm)
1614 proftime_T *tm; /* where waittime was stored */
1615{
1616 funccall_T *fc = current_funccal;
1617
1618 if (fc != NULL && fc->func->uf_profiling)
1619 {
1620 profile_end(&fc->prof_child);
1621 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1622 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1623 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1624 }
1625 script_prof_restore(tm);
1626}
1627#endif
1628
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#ifdef FEAT_FOLDING
1631/*
1632 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1633 * it in "*cp". Doesn't give error messages.
1634 */
1635 int
1636eval_foldexpr(arg, cp)
1637 char_u *arg;
1638 int *cp;
1639{
Bram Moolenaar33570922005-01-25 22:26:29 +00001640 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 int retval;
1642 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001643 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1644 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001647 if (use_sandbox)
1648 ++sandbox;
1649 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 retval = 0;
1653 else
1654 {
1655 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 if (tv.v_type == VAR_NUMBER)
1657 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001658 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 retval = 0;
1660 else
1661 {
1662 /* If the result is a string, check if there is a non-digit before
1663 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (!VIM_ISDIGIT(*s) && *s != '-')
1666 *cp = *s++;
1667 retval = atol((char *)s);
1668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001672 if (use_sandbox)
1673 --sandbox;
1674 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676 return retval;
1677}
1678#endif
1679
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681 * ":let" list all variable values
1682 * ":let var1 var2" list variable values
1683 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 * ":let var += expr" assignment command.
1685 * ":let var -= expr" assignment command.
1686 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001687 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 */
1689 void
1690ex_let(eap)
1691 exarg_T *eap;
1692{
1693 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001694 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001695 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 int var_count = 0;
1698 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001700 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001701 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaardb552d602006-03-23 22:59:57 +00001703 argend = skip_var_list(arg, &var_count, &semicolon);
1704 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001706 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1707 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001708 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001711 /*
1712 * ":let" without "=": list variables
1713 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 if (*arg == '[')
1715 EMSG(_(e_invarg));
1716 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001717 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001718 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001719 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001721 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001722 list_glob_vars(&first);
1723 list_buf_vars(&first);
1724 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001725#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001726 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001727#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001728 list_script_vars(&first);
1729 list_func_vars(&first);
1730 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 eap->nextcmd = check_nextcmd(arg);
1733 }
1734 else
1735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001736 op[0] = '=';
1737 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001738 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001739 {
1740 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1741 op[0] = expr[-1]; /* +=, -= or .= */
1742 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 if (eap->skip)
1746 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (eap->skip)
1749 {
1750 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001751 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 --emsg_skip;
1753 }
1754 else if (i != FAIL)
1755 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 }
1761}
1762
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763/*
1764 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1765 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 * When "nextchars" is not NULL it points to a string with characters that
1767 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1768 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 * Returns OK or FAIL;
1770 */
1771 static int
1772ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1773 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001774 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 int copy; /* copy values from "tv", don't move */
1776 int semicolon; /* from skip_var_list() */
1777 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001778 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779{
1780 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 listitem_T *item;
1784 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
1786 if (*arg != '[')
1787 {
1788 /*
1789 * ":let var = expr" or ":for var in list"
1790 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001791 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 return FAIL;
1793 return OK;
1794 }
1795
1796 /*
1797 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1798 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001799 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800 {
1801 EMSG(_(e_listreq));
1802 return FAIL;
1803 }
1804
1805 i = list_len(l);
1806 if (semicolon == 0 && var_count < i)
1807 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001808 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 return FAIL;
1810 }
1811 if (var_count - semicolon > i)
1812 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001813 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 return FAIL;
1815 }
1816
1817 item = l->lv_first;
1818 while (*arg != ']')
1819 {
1820 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 item = item->li_next;
1823 if (arg == NULL)
1824 return FAIL;
1825
1826 arg = skipwhite(arg);
1827 if (*arg == ';')
1828 {
1829 /* Put the rest of the list (may be empty) in the var after ';'.
1830 * Create a new list for this. */
1831 l = list_alloc();
1832 if (l == NULL)
1833 return FAIL;
1834 while (item != NULL)
1835 {
1836 list_append_tv(l, &item->li_tv);
1837 item = item->li_next;
1838 }
1839
1840 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001841 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 ltv.vval.v_list = l;
1843 l->lv_refcount = 1;
1844
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1846 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 clear_tv(&ltv);
1848 if (arg == NULL)
1849 return FAIL;
1850 break;
1851 }
1852 else if (*arg != ',' && *arg != ']')
1853 {
1854 EMSG2(_(e_intern2), "ex_let_vars()");
1855 return FAIL;
1856 }
1857 }
1858
1859 return OK;
1860}
1861
1862/*
1863 * Skip over assignable variable "var" or list of variables "[var, var]".
1864 * Used for ":let varvar = expr" and ":for varvar in expr".
1865 * For "[var, var]" increment "*var_count" for each variable.
1866 * for "[var, var; var]" set "semicolon".
1867 * Return NULL for an error.
1868 */
1869 static char_u *
1870skip_var_list(arg, var_count, semicolon)
1871 char_u *arg;
1872 int *var_count;
1873 int *semicolon;
1874{
1875 char_u *p, *s;
1876
1877 if (*arg == '[')
1878 {
1879 /* "[var, var]": find the matching ']'. */
1880 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001881 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 {
1883 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1884 s = skip_var_one(p);
1885 if (s == p)
1886 {
1887 EMSG2(_(e_invarg2), p);
1888 return NULL;
1889 }
1890 ++*var_count;
1891
1892 p = skipwhite(s);
1893 if (*p == ']')
1894 break;
1895 else if (*p == ';')
1896 {
1897 if (*semicolon == 1)
1898 {
1899 EMSG(_("Double ; in list of variables"));
1900 return NULL;
1901 }
1902 *semicolon = 1;
1903 }
1904 else if (*p != ',')
1905 {
1906 EMSG2(_(e_invarg2), p);
1907 return NULL;
1908 }
1909 }
1910 return p + 1;
1911 }
1912 else
1913 return skip_var_one(arg);
1914}
1915
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001916/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001917 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001918 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001919 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 static char_u *
1921skip_var_one(arg)
1922 char_u *arg;
1923{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001924 if (*arg == '@' && arg[1] != NUL)
1925 return arg + 2;
1926 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1927 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928}
1929
Bram Moolenaara7043832005-01-21 11:56:39 +00001930/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 * List variables for hashtab "ht" with prefix "prefix".
1932 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001935list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001939 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001940{
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 hashitem_T *hi;
1942 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001943 int todo;
1944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001945 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001946 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1947 {
1948 if (!HASHITEM_EMPTY(hi))
1949 {
1950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001951 di = HI2DI(hi);
1952 if (empty || di->di_tv.v_type != VAR_STRING
1953 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001955 }
1956 }
1957}
1958
1959/*
1960 * List global variables.
1961 */
1962 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963list_glob_vars(first)
1964 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001965{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001966 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001967}
1968
1969/*
1970 * List buffer variables.
1971 */
1972 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001973list_buf_vars(first)
1974 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001975{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001976 char_u numbuf[NUMBUFLEN];
1977
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001978 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
1979 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001980
1981 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001982 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
1983 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001984}
1985
1986/*
1987 * List window variables.
1988 */
1989 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001990list_win_vars(first)
1991 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00001992{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001993 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
1994 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001995}
1996
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001997#ifdef FEAT_WINDOWS
1998/*
1999 * List tab page variables.
2000 */
2001 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002002list_tab_vars(first)
2003 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002004{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002005 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2006 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002007}
2008#endif
2009
Bram Moolenaara7043832005-01-21 11:56:39 +00002010/*
2011 * List Vim variables.
2012 */
2013 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002014list_vim_vars(first)
2015 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002017 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018}
2019
2020/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002021 * List script-local variables, if there is a script.
2022 */
2023 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002024list_script_vars(first)
2025 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002026{
2027 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002028 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2029 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002030}
2031
2032/*
2033 * List function variables, if there is a function.
2034 */
2035 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002036list_func_vars(first)
2037 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002038{
2039 if (current_funccal != NULL)
2040 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002041 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002042}
2043
2044/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 * List variables in "arg".
2046 */
2047 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002048list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 exarg_T *eap;
2050 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002051 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002052{
2053 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002055 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 char_u *name_start;
2057 char_u *arg_subsc;
2058 char_u *tofree;
2059 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002060
2061 while (!ends_excmd(*arg) && !got_int)
2062 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002063 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002064 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002065 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2067 {
2068 emsg_severe = TRUE;
2069 EMSG(_(e_trailing));
2070 break;
2071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 /* get_name_len() takes care of expanding curly braces */
2076 name_start = name = arg;
2077 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2078 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002079 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002080 /* This is mainly to keep test 49 working: when expanding
2081 * curly braces fails overrule the exception error message. */
2082 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002084 emsg_severe = TRUE;
2085 EMSG2(_(e_invarg2), arg);
2086 break;
2087 }
2088 error = TRUE;
2089 }
2090 else
2091 {
2092 if (tofree != NULL)
2093 name = tofree;
2094 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 else
2097 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002098 /* handle d.key, l[idx], f(expr) */
2099 arg_subsc = arg;
2100 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002102 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002104 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002106 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002107 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002108 case 'g': list_glob_vars(first); break;
2109 case 'b': list_buf_vars(first); break;
2110 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002111#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002113#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 case 'v': list_vim_vars(first); break;
2115 case 's': list_script_vars(first); break;
2116 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117 default:
2118 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002119 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002120 }
2121 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 {
2123 char_u numbuf[NUMBUFLEN];
2124 char_u *tf;
2125 int c;
2126 char_u *s;
2127
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002128 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002129 c = *arg;
2130 *arg = NUL;
2131 list_one_var_a((char_u *)"",
2132 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133 tv.v_type,
2134 s == NULL ? (char_u *)"" : s,
2135 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136 *arg = c;
2137 vim_free(tf);
2138 }
2139 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 }
2142 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002143
2144 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002148 }
2149
2150 return arg;
2151}
2152
2153/*
2154 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2155 * Returns a pointer to the char just after the var name.
2156 * Returns NULL if there is an error.
2157 */
2158 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002161 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002164 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165{
2166 int c1;
2167 char_u *name;
2168 char_u *p;
2169 char_u *arg_end = NULL;
2170 int len;
2171 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002172 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173
2174 /*
2175 * ":let $VAR = expr": Set environment variable.
2176 */
2177 if (*arg == '$')
2178 {
2179 /* Find the end of the name. */
2180 ++arg;
2181 name = arg;
2182 len = get_env_len(&arg);
2183 if (len == 0)
2184 EMSG2(_(e_invarg2), name - 1);
2185 else
2186 {
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)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 EMSG(_(e_letunexp));
2192 else
2193 {
2194 c1 = name[len];
2195 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 p = get_tv_string_chk(tv);
2197 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002198 {
2199 int mustfree = FALSE;
2200 char_u *s = vim_getenv(name, &mustfree);
2201
2202 if (s != NULL)
2203 {
2204 p = tofree = concat_str(s, p);
2205 if (mustfree)
2206 vim_free(s);
2207 }
2208 }
2209 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002210 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002211 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 if (STRICMP(name, "HOME") == 0)
2213 init_homedir();
2214 else if (didset_vim && STRICMP(name, "VIM") == 0)
2215 didset_vim = FALSE;
2216 else if (didset_vimruntime
2217 && STRICMP(name, "VIMRUNTIME") == 0)
2218 didset_vimruntime = FALSE;
2219 arg_end = arg;
2220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002222 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 }
2224 }
2225 }
2226
2227 /*
2228 * ":let &option = expr": Set option value.
2229 * ":let &l:option = expr": Set local option value.
2230 * ":let &g:option = expr": Set global option value.
2231 */
2232 else if (*arg == '&')
2233 {
2234 /* Find the end of the name. */
2235 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002236 if (p == NULL || (endchars != NULL
2237 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 EMSG(_(e_letunexp));
2239 else
2240 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002241 long n;
2242 int opt_type;
2243 long numval;
2244 char_u *stringval = NULL;
2245 char_u *s;
2246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 c1 = *p;
2248 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002249
2250 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002251 s = get_tv_string_chk(tv); /* != NULL if number or string */
2252 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002253 {
2254 opt_type = get_option_value(arg, &numval,
2255 &stringval, opt_flags);
2256 if ((opt_type == 1 && *op == '.')
2257 || (opt_type == 0 && *op != '.'))
2258 EMSG2(_(e_letwrong), op);
2259 else
2260 {
2261 if (opt_type == 1) /* number */
2262 {
2263 if (*op == '+')
2264 n = numval + n;
2265 else
2266 n = numval - n;
2267 }
2268 else if (opt_type == 0 && stringval != NULL) /* string */
2269 {
2270 s = concat_str(stringval, s);
2271 vim_free(stringval);
2272 stringval = s;
2273 }
2274 }
2275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002276 if (s != NULL)
2277 {
2278 set_option_value(arg, n, s, opt_flags);
2279 arg_end = p;
2280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002282 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
2284 }
2285
2286 /*
2287 * ":let @r = expr": Set register contents.
2288 */
2289 else if (*arg == '@')
2290 {
2291 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002292 if (op != NULL && (*op == '+' || *op == '-'))
2293 EMSG2(_(e_letwrong), op);
2294 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002295 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 EMSG(_(e_letunexp));
2297 else
2298 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002299 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 char_u *s;
2301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002302 p = get_tv_string_chk(tv);
2303 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002304 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002305 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002306 if (s != NULL)
2307 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002308 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 vim_free(s);
2310 }
2311 }
2312 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002313 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 arg_end = arg + 1;
2316 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002317 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 }
2319 }
2320
2321 /*
2322 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002325 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002329 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002330 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002332 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2333 EMSG(_(e_letunexp));
2334 else
2335 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 arg_end = p;
2338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002340 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342
2343 else
2344 EMSG2(_(e_invarg2), arg);
2345
2346 return arg_end;
2347}
2348
2349/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2351 */
2352 static int
2353check_changedtick(arg)
2354 char_u *arg;
2355{
2356 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2357 {
2358 EMSG2(_(e_readonlyvar), arg);
2359 return TRUE;
2360 }
2361 return FALSE;
2362}
2363
2364/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 * Get an lval: variable, Dict item or List item that can be assigned a value
2366 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2367 * "name.key", "name.key[expr]" etc.
2368 * Indexing only works if "name" is an existing List or Dictionary.
2369 * "name" points to the start of the name.
2370 * If "rettv" is not NULL it points to the value to be assigned.
2371 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2372 * wrong; must end in space or cmd separator.
2373 *
2374 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002375 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 */
2378 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002379get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002381 typval_T *rettv;
2382 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 int unlet;
2384 int skip;
2385 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002386 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 char_u *expr_start, *expr_end;
2390 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002391 dictitem_T *v;
2392 typval_T var1;
2393 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002395 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002396 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002398 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002400 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002401 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402
2403 if (skip)
2404 {
2405 /* When skipping just find the end of the name. */
2406 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002407 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 }
2409
2410 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002411 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (expr_start != NULL)
2413 {
2414 /* Don't expand the name when we already know there is an error. */
2415 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2416 && *p != '[' && *p != '.')
2417 {
2418 EMSG(_(e_trailing));
2419 return NULL;
2420 }
2421
2422 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2423 if (lp->ll_exp_name == NULL)
2424 {
2425 /* Report an invalid expression in braces, unless the
2426 * expression evaluation has been cancelled due to an
2427 * aborting error, an interrupt, or an exception. */
2428 if (!aborting() && !quiet)
2429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002430 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002431 EMSG2(_(e_invarg2), name);
2432 return NULL;
2433 }
2434 }
2435 lp->ll_name = lp->ll_exp_name;
2436 }
2437 else
2438 lp->ll_name = name;
2439
2440 /* Without [idx] or .key we are done. */
2441 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2442 return p;
2443
2444 cc = *p;
2445 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002446 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 if (v == NULL && !quiet)
2448 EMSG2(_(e_undefvar), lp->ll_name);
2449 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 if (v == NULL)
2451 return NULL;
2452
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 /*
2454 * Loop until no more [idx] or .key is following.
2455 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002456 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2460 && !(lp->ll_tv->v_type == VAR_DICT
2461 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 if (!quiet)
2464 EMSG(_("E689: Can only index a List or Dictionary"));
2465 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (!quiet)
2470 EMSG(_("E708: [:] must come last"));
2471 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002473
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 len = -1;
2475 if (*p == '.')
2476 {
2477 key = p + 1;
2478 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2479 ;
2480 if (len == 0)
2481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (!quiet)
2483 EMSG(_(e_emptykey));
2484 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 }
2486 p = key + len;
2487 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002488 else
2489 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002490 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002491 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 if (*p == ':')
2493 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002494 else
2495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 empty1 = FALSE;
2497 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 if (get_tv_string_chk(&var1) == NULL)
2500 {
2501 /* not a number or string */
2502 clear_tv(&var1);
2503 return NULL;
2504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
2506
2507 /* Optionally get the second index [ :expr]. */
2508 if (*p == ':')
2509 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002511 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002514 if (!empty1)
2515 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (rettv != NULL && (rettv->v_type != VAR_LIST
2519 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (!quiet)
2522 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 if (!empty1)
2524 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002526 }
2527 p = skipwhite(p + 1);
2528 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002530 else
2531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2534 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 if (!empty1)
2536 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002539 if (get_tv_string_chk(&var2) == NULL)
2540 {
2541 /* not a number or string */
2542 if (!empty1)
2543 clear_tv(&var1);
2544 clear_tv(&var2);
2545 return NULL;
2546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002552
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (!quiet)
2556 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 if (!empty1)
2558 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562 }
2563
2564 /* Skip to past ']'. */
2565 ++p;
2566 }
2567
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 {
2570 if (len == -1)
2571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002574 if (*key == NUL)
2575 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (!quiet)
2577 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 }
2581 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002582 lp->ll_list = NULL;
2583 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002584 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002587 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 if (len == -1)
2593 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 }
2596 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 if (len == -1)
2601 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002603 p = NULL;
2604 break;
2605 }
2606 if (len == -1)
2607 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 }
2610 else
2611 {
2612 /*
2613 * Get the number and item for the only or first index of the List.
2614 */
2615 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 else
2618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 clear_tv(&var1);
2621 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002622 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 lp->ll_list = lp->ll_tv->vval.v_list;
2624 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2625 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002627 if (lp->ll_n1 < 0)
2628 {
2629 lp->ll_n1 = 0;
2630 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2631 }
2632 }
2633 if (lp->ll_li == NULL)
2634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 }
2639
2640 /*
2641 * May need to find the item or absolute index for the second
2642 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 * When no index given: "lp->ll_empty2" is TRUE.
2644 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002648 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2659 if (lp->ll_n1 < 0)
2660 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2661 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 }
2664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002666 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
2668
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return p;
2670}
2671
2672/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 */
2675 static void
2676clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002677 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678{
2679 vim_free(lp->ll_exp_name);
2680 vim_free(lp->ll_newkey);
2681}
2682
2683/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002684 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 */
2688 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002690 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002692 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002694 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695{
2696 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002697 listitem_T *ri;
2698 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699
2700 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 cc = *endp;
2705 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 if (op != NULL && *op != '=')
2707 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002708 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002710 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002711 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002712 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 {
2714 if (tv_op(&tv, rettv, op) == OK)
2715 set_var(lp->ll_name, &tv, FALSE);
2716 clear_tv(&tv);
2717 }
2718 }
2719 else
2720 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002722 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002724 else if (tv_check_lock(lp->ll_newkey == NULL
2725 ? lp->ll_tv->v_lock
2726 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2727 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 else if (lp->ll_range)
2729 {
2730 /*
2731 * Assign the List values to the list items.
2732 */
2733 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 if (op != NULL && *op != '=')
2736 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2737 else
2738 {
2739 clear_tv(&lp->ll_li->li_tv);
2740 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2741 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 ri = ri->li_next;
2743 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2744 break;
2745 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002748 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 ri = NULL;
2751 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_li = lp->ll_li->li_next;
2755 ++lp->ll_n1;
2756 }
2757 if (ri != NULL)
2758 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 else if (lp->ll_empty2
2760 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 : lp->ll_n1 != lp->ll_n2)
2762 EMSG(_("E711: List value has not enough items"));
2763 }
2764 else
2765 {
2766 /*
2767 * Assign to a List or Dictionary item.
2768 */
2769 if (lp->ll_newkey != NULL)
2770 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002771 if (op != NULL && *op != '=')
2772 {
2773 EMSG2(_(e_letwrong), op);
2774 return;
2775 }
2776
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002778 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (di == NULL)
2780 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2782 {
2783 vim_free(di);
2784 return;
2785 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002787 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 else if (op != NULL && *op != '=')
2789 {
2790 tv_op(lp->ll_tv, rettv, op);
2791 return;
2792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002793 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 /*
2797 * Assign the value to the variable or list item.
2798 */
2799 if (copy)
2800 copy_tv(rettv, lp->ll_tv);
2801 else
2802 {
2803 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002804 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002806 }
2807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002808}
2809
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002810/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002811 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2812 * Returns OK or FAIL.
2813 */
2814 static int
2815tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 typval_T *tv1;
2817 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 char_u *op;
2819{
2820 long n;
2821 char_u numbuf[NUMBUFLEN];
2822 char_u *s;
2823
2824 /* Can't do anything with a Funcref or a Dict on the right. */
2825 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2826 {
2827 switch (tv1->v_type)
2828 {
2829 case VAR_DICT:
2830 case VAR_FUNC:
2831 break;
2832
2833 case VAR_LIST:
2834 if (*op != '+' || tv2->v_type != VAR_LIST)
2835 break;
2836 /* List += List */
2837 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2838 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2839 return OK;
2840
2841 case VAR_NUMBER:
2842 case VAR_STRING:
2843 if (tv2->v_type == VAR_LIST)
2844 break;
2845 if (*op == '+' || *op == '-')
2846 {
2847 /* nr += nr or nr -= nr*/
2848 n = get_tv_number(tv1);
2849 if (*op == '+')
2850 n += get_tv_number(tv2);
2851 else
2852 n -= get_tv_number(tv2);
2853 clear_tv(tv1);
2854 tv1->v_type = VAR_NUMBER;
2855 tv1->vval.v_number = n;
2856 }
2857 else
2858 {
2859 /* str .= str */
2860 s = get_tv_string(tv1);
2861 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2862 clear_tv(tv1);
2863 tv1->v_type = VAR_STRING;
2864 tv1->vval.v_string = s;
2865 }
2866 return OK;
2867 }
2868 }
2869
2870 EMSG2(_(e_letwrong), op);
2871 return FAIL;
2872}
2873
2874/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002875 * Add a watcher to a list.
2876 */
2877 static void
2878list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 list_T *l;
2880 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002881{
2882 lw->lw_next = l->lv_watch;
2883 l->lv_watch = lw;
2884}
2885
2886/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002887 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888 * No warning when it isn't found...
2889 */
2890 static void
2891list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 list_T *l;
2893 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002894{
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002896
2897 lwp = &l->lv_watch;
2898 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2899 {
2900 if (lw == lwrem)
2901 {
2902 *lwp = lw->lw_next;
2903 break;
2904 }
2905 lwp = &lw->lw_next;
2906 }
2907}
2908
2909/*
2910 * Just before removing an item from a list: advance watchers to the next
2911 * item.
2912 */
2913 static void
2914list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 list_T *l;
2916 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917{
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002919
2920 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2921 if (lw->lw_item == item)
2922 lw->lw_item = item->li_next;
2923}
2924
2925/*
2926 * Evaluate the expression used in a ":for var in expr" command.
2927 * "arg" points to "var".
2928 * Set "*errp" to TRUE for an error, FALSE otherwise;
2929 * Return a pointer that holds the info. Null when there is an error.
2930 */
2931 void *
2932eval_for_line(arg, errp, nextcmdp, skip)
2933 char_u *arg;
2934 int *errp;
2935 char_u **nextcmdp;
2936 int skip;
2937{
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002939 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 typval_T tv;
2941 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002942
2943 *errp = TRUE; /* default: there is an error */
2944
Bram Moolenaar33570922005-01-25 22:26:29 +00002945 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946 if (fi == NULL)
2947 return NULL;
2948
2949 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2950 if (expr == NULL)
2951 return fi;
2952
2953 expr = skipwhite(expr);
2954 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002956 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002957 return fi;
2958 }
2959
2960 if (skip)
2961 ++emsg_skip;
2962 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2963 {
2964 *errp = FALSE;
2965 if (!skip)
2966 {
2967 l = tv.vval.v_list;
2968 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002969 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002970 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002971 clear_tv(&tv);
2972 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002973 else
2974 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002975 /* No need to increment the refcount, it's already set for the
2976 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002977 fi->fi_list = l;
2978 list_add_watch(l, &fi->fi_lw);
2979 fi->fi_lw.lw_item = l->lv_first;
2980 }
2981 }
2982 }
2983 if (skip)
2984 --emsg_skip;
2985
2986 return fi;
2987}
2988
2989/*
2990 * Use the first item in a ":for" list. Advance to the next.
2991 * Assign the values to the variable (list). "arg" points to the first one.
2992 * Return TRUE when a valid item was found, FALSE when at end of list or
2993 * something wrong.
2994 */
2995 int
2996next_for_item(fi_void, arg)
2997 void *fi_void;
2998 char_u *arg;
2999{
Bram Moolenaar33570922005-01-25 22:26:29 +00003000 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003
3004 item = fi->fi_lw.lw_item;
3005 if (item == NULL)
3006 result = FALSE;
3007 else
3008 {
3009 fi->fi_lw.lw_item = item->li_next;
3010 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3011 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3012 }
3013 return result;
3014}
3015
3016/*
3017 * Free the structure used to store info used by ":for".
3018 */
3019 void
3020free_for_info(fi_void)
3021 void *fi_void;
3022{
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003024
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003025 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003026 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003028 list_unref(fi->fi_list);
3029 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003030 vim_free(fi);
3031}
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3034
3035 void
3036set_context_for_expression(xp, arg, cmdidx)
3037 expand_T *xp;
3038 char_u *arg;
3039 cmdidx_T cmdidx;
3040{
3041 int got_eq = FALSE;
3042 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003043 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045 if (cmdidx == CMD_let)
3046 {
3047 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003048 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003049 {
3050 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003051 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052 {
3053 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003054 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003055 if (vim_iswhite(*p))
3056 break;
3057 }
3058 return;
3059 }
3060 }
3061 else
3062 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3063 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 while ((xp->xp_pattern = vim_strpbrk(arg,
3065 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3066 {
3067 c = *xp->xp_pattern;
3068 if (c == '&')
3069 {
3070 c = xp->xp_pattern[1];
3071 if (c == '&')
3072 {
3073 ++xp->xp_pattern;
3074 xp->xp_context = cmdidx != CMD_let || got_eq
3075 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3076 }
3077 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003080 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3081 xp->xp_pattern += 2;
3082
3083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085 else if (c == '$')
3086 {
3087 /* environment variable */
3088 xp->xp_context = EXPAND_ENV_VARS;
3089 }
3090 else if (c == '=')
3091 {
3092 got_eq = TRUE;
3093 xp->xp_context = EXPAND_EXPRESSION;
3094 }
3095 else if (c == '<'
3096 && xp->xp_context == EXPAND_FUNCTIONS
3097 && vim_strchr(xp->xp_pattern, '(') == NULL)
3098 {
3099 /* Function name can start with "<SNR>" */
3100 break;
3101 }
3102 else if (cmdidx != CMD_let || got_eq)
3103 {
3104 if (c == '"') /* string */
3105 {
3106 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3107 if (c == '\\' && xp->xp_pattern[1] != NUL)
3108 ++xp->xp_pattern;
3109 xp->xp_context = EXPAND_NOTHING;
3110 }
3111 else if (c == '\'') /* literal string */
3112 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003113 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3115 /* skip */ ;
3116 xp->xp_context = EXPAND_NOTHING;
3117 }
3118 else if (c == '|')
3119 {
3120 if (xp->xp_pattern[1] == '|')
3121 {
3122 ++xp->xp_pattern;
3123 xp->xp_context = EXPAND_EXPRESSION;
3124 }
3125 else
3126 xp->xp_context = EXPAND_COMMANDS;
3127 }
3128 else
3129 xp->xp_context = EXPAND_EXPRESSION;
3130 }
3131 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 /* Doesn't look like something valid, expand as an expression
3133 * anyway. */
3134 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 arg = xp->xp_pattern;
3136 if (*arg != NUL)
3137 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3138 /* skip */ ;
3139 }
3140 xp->xp_pattern = arg;
3141}
3142
3143#endif /* FEAT_CMDL_COMPL */
3144
3145/*
3146 * ":1,25call func(arg1, arg2)" function call.
3147 */
3148 void
3149ex_call(eap)
3150 exarg_T *eap;
3151{
3152 char_u *arg = eap->arg;
3153 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003155 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003157 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 linenr_T lnum;
3159 int doesrange;
3160 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003163 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003164 if (fudi.fd_newkey != NULL)
3165 {
3166 /* Still need to give an error message for missing key. */
3167 EMSG2(_(e_dictkey), fudi.fd_newkey);
3168 vim_free(fudi.fd_newkey);
3169 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 if (tofree == NULL)
3171 return;
3172
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003173 /* Increase refcount on dictionary, it could get deleted when evaluating
3174 * the arguments. */
3175 if (fudi.fd_dict != NULL)
3176 ++fudi.fd_dict->dv_refcount;
3177
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003178 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003179 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181
Bram Moolenaar532c7802005-01-27 14:44:31 +00003182 /* Skip white space to allow ":call func ()". Not good, but required for
3183 * backward compatibility. */
3184 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003185 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 if (*startarg != '(')
3188 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003189 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 goto end;
3191 }
3192
3193 /*
3194 * When skipping, evaluate the function once, to find the end of the
3195 * arguments.
3196 * When the function takes a range, this is discovered after the first
3197 * call, and the loop is broken.
3198 */
3199 if (eap->skip)
3200 {
3201 ++emsg_skip;
3202 lnum = eap->line2; /* do it once, also with an invalid range */
3203 }
3204 else
3205 lnum = eap->line1;
3206 for ( ; lnum <= eap->line2; ++lnum)
3207 {
3208 if (!eap->skip && eap->addr_count > 0)
3209 {
3210 curwin->w_cursor.lnum = lnum;
3211 curwin->w_cursor.col = 0;
3212 }
3213 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003214 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003215 eap->line1, eap->line2, &doesrange,
3216 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
3218 failed = TRUE;
3219 break;
3220 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003221
3222 /* Handle a function returning a Funcref, Dictionary or List. */
3223 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3224 {
3225 failed = TRUE;
3226 break;
3227 }
3228
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003229 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (doesrange || eap->skip)
3231 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003234 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003235 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (aborting())
3238 break;
3239 }
3240 if (eap->skip)
3241 --emsg_skip;
3242
3243 if (!failed)
3244 {
3245 /* Check for trailing illegal characters and a following command. */
3246 if (!ends_excmd(*arg))
3247 {
3248 emsg_severe = TRUE;
3249 EMSG(_(e_trailing));
3250 }
3251 else
3252 eap->nextcmd = check_nextcmd(arg);
3253 }
3254
3255end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003256 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003257 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258}
3259
3260/*
3261 * ":unlet[!] var1 ... " command.
3262 */
3263 void
3264ex_unlet(eap)
3265 exarg_T *eap;
3266{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003267 ex_unletlock(eap, eap->arg, 0);
3268}
3269
3270/*
3271 * ":lockvar" and ":unlockvar" commands
3272 */
3273 void
3274ex_lockvar(eap)
3275 exarg_T *eap;
3276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003278 int deep = 2;
3279
3280 if (eap->forceit)
3281 deep = -1;
3282 else if (vim_isdigit(*arg))
3283 {
3284 deep = getdigits(&arg);
3285 arg = skipwhite(arg);
3286 }
3287
3288 ex_unletlock(eap, arg, deep);
3289}
3290
3291/*
3292 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3293 */
3294 static void
3295ex_unletlock(eap, argstart, deep)
3296 exarg_T *eap;
3297 char_u *argstart;
3298 int deep;
3299{
3300 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003303 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 do
3306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003307 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003308 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3309 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003310 if (lv.ll_name == NULL)
3311 error = TRUE; /* error but continue parsing */
3312 if (name_end == NULL || (!vim_iswhite(*name_end)
3313 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003315 if (name_end != NULL)
3316 {
3317 emsg_severe = TRUE;
3318 EMSG(_(e_trailing));
3319 }
3320 if (!(eap->skip || error))
3321 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 break;
3323 }
3324
3325 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003326 {
3327 if (eap->cmdidx == CMD_unlet)
3328 {
3329 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3330 error = TRUE;
3331 }
3332 else
3333 {
3334 if (do_lock_var(&lv, name_end, deep,
3335 eap->cmdidx == CMD_lockvar) == FAIL)
3336 error = TRUE;
3337 }
3338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003340 if (!eap->skip)
3341 clear_lval(&lv);
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 arg = skipwhite(name_end);
3344 } while (!ends_excmd(*arg));
3345
3346 eap->nextcmd = check_nextcmd(arg);
3347}
3348
Bram Moolenaar8c711452005-01-14 21:53:12 +00003349 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003350do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003352 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003353 int forceit;
3354{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003355 int ret = OK;
3356 int cc;
3357
3358 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003359 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003360 cc = *name_end;
3361 *name_end = NUL;
3362
3363 /* Normal name or expanded name. */
3364 if (check_changedtick(lp->ll_name))
3365 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003366 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003367 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003368 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003369 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003370 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3371 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003372 else if (lp->ll_range)
3373 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003374 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003375
3376 /* Delete a range of List items. */
3377 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3378 {
3379 li = lp->ll_li->li_next;
3380 listitem_remove(lp->ll_list, lp->ll_li);
3381 lp->ll_li = li;
3382 ++lp->ll_n1;
3383 }
3384 }
3385 else
3386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003387 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003388 /* unlet a List item. */
3389 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003390 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003391 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003392 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003393 }
3394
3395 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003396}
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398/*
3399 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003400 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003403do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003405 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 hashtab_T *ht;
3408 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003409 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003410 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar33570922005-01-25 22:26:29 +00003412 ht = find_var_ht(name, &varname);
3413 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003415 hi = hash_find(ht, varname);
3416 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003417 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003418 di = HI2DI(hi);
3419 if (var_check_fixed(di->di_flags, name)
3420 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003421 return FAIL;
3422 delete_var(ht, hi);
3423 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003426 if (forceit)
3427 return OK;
3428 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 return FAIL;
3430}
3431
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003432/*
3433 * Lock or unlock variable indicated by "lp".
3434 * "deep" is the levels to go (-1 for unlimited);
3435 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3436 */
3437 static int
3438do_lock_var(lp, name_end, deep, lock)
3439 lval_T *lp;
3440 char_u *name_end;
3441 int deep;
3442 int lock;
3443{
3444 int ret = OK;
3445 int cc;
3446 dictitem_T *di;
3447
3448 if (deep == 0) /* nothing to do */
3449 return OK;
3450
3451 if (lp->ll_tv == NULL)
3452 {
3453 cc = *name_end;
3454 *name_end = NUL;
3455
3456 /* Normal name or expanded name. */
3457 if (check_changedtick(lp->ll_name))
3458 ret = FAIL;
3459 else
3460 {
3461 di = find_var(lp->ll_name, NULL);
3462 if (di == NULL)
3463 ret = FAIL;
3464 else
3465 {
3466 if (lock)
3467 di->di_flags |= DI_FLAGS_LOCK;
3468 else
3469 di->di_flags &= ~DI_FLAGS_LOCK;
3470 item_lock(&di->di_tv, deep, lock);
3471 }
3472 }
3473 *name_end = cc;
3474 }
3475 else if (lp->ll_range)
3476 {
3477 listitem_T *li = lp->ll_li;
3478
3479 /* (un)lock a range of List items. */
3480 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3481 {
3482 item_lock(&li->li_tv, deep, lock);
3483 li = li->li_next;
3484 ++lp->ll_n1;
3485 }
3486 }
3487 else if (lp->ll_list != NULL)
3488 /* (un)lock a List item. */
3489 item_lock(&lp->ll_li->li_tv, deep, lock);
3490 else
3491 /* un(lock) a Dictionary item. */
3492 item_lock(&lp->ll_di->di_tv, deep, lock);
3493
3494 return ret;
3495}
3496
3497/*
3498 * Lock or unlock an item. "deep" is nr of levels to go.
3499 */
3500 static void
3501item_lock(tv, deep, lock)
3502 typval_T *tv;
3503 int deep;
3504 int lock;
3505{
3506 static int recurse = 0;
3507 list_T *l;
3508 listitem_T *li;
3509 dict_T *d;
3510 hashitem_T *hi;
3511 int todo;
3512
3513 if (recurse >= DICT_MAXNEST)
3514 {
3515 EMSG(_("E743: variable nested too deep for (un)lock"));
3516 return;
3517 }
3518 if (deep == 0)
3519 return;
3520 ++recurse;
3521
3522 /* lock/unlock the item itself */
3523 if (lock)
3524 tv->v_lock |= VAR_LOCKED;
3525 else
3526 tv->v_lock &= ~VAR_LOCKED;
3527
3528 switch (tv->v_type)
3529 {
3530 case VAR_LIST:
3531 if ((l = tv->vval.v_list) != NULL)
3532 {
3533 if (lock)
3534 l->lv_lock |= VAR_LOCKED;
3535 else
3536 l->lv_lock &= ~VAR_LOCKED;
3537 if (deep < 0 || deep > 1)
3538 /* recursive: lock/unlock the items the List contains */
3539 for (li = l->lv_first; li != NULL; li = li->li_next)
3540 item_lock(&li->li_tv, deep - 1, lock);
3541 }
3542 break;
3543 case VAR_DICT:
3544 if ((d = tv->vval.v_dict) != NULL)
3545 {
3546 if (lock)
3547 d->dv_lock |= VAR_LOCKED;
3548 else
3549 d->dv_lock &= ~VAR_LOCKED;
3550 if (deep < 0 || deep > 1)
3551 {
3552 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003553 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3555 {
3556 if (!HASHITEM_EMPTY(hi))
3557 {
3558 --todo;
3559 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3560 }
3561 }
3562 }
3563 }
3564 }
3565 --recurse;
3566}
3567
Bram Moolenaara40058a2005-07-11 22:42:07 +00003568/*
3569 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3570 * it refers to a List or Dictionary that is locked.
3571 */
3572 static int
3573tv_islocked(tv)
3574 typval_T *tv;
3575{
3576 return (tv->v_lock & VAR_LOCKED)
3577 || (tv->v_type == VAR_LIST
3578 && tv->vval.v_list != NULL
3579 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3580 || (tv->v_type == VAR_DICT
3581 && tv->vval.v_dict != NULL
3582 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3583}
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3586/*
3587 * Delete all "menutrans_" variables.
3588 */
3589 void
3590del_menutrans_vars()
3591{
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003593 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003596 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003598 {
3599 if (!HASHITEM_EMPTY(hi))
3600 {
3601 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3603 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003604 }
3605 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003606 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607}
3608#endif
3609
3610#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3611
3612/*
3613 * Local string buffer for the next two functions to store a variable name
3614 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3615 * get_user_var_name().
3616 */
3617
3618static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3619
3620static char_u *varnamebuf = NULL;
3621static int varnamebuflen = 0;
3622
3623/*
3624 * Function to concatenate a prefix and a variable name.
3625 */
3626 static char_u *
3627cat_prefix_varname(prefix, name)
3628 int prefix;
3629 char_u *name;
3630{
3631 int len;
3632
3633 len = (int)STRLEN(name) + 3;
3634 if (len > varnamebuflen)
3635 {
3636 vim_free(varnamebuf);
3637 len += 10; /* some additional space */
3638 varnamebuf = alloc(len);
3639 if (varnamebuf == NULL)
3640 {
3641 varnamebuflen = 0;
3642 return NULL;
3643 }
3644 varnamebuflen = len;
3645 }
3646 *varnamebuf = prefix;
3647 varnamebuf[1] = ':';
3648 STRCPY(varnamebuf + 2, name);
3649 return varnamebuf;
3650}
3651
3652/*
3653 * Function given to ExpandGeneric() to obtain the list of user defined
3654 * (global/buffer/window/built-in) variable names.
3655 */
3656/*ARGSUSED*/
3657 char_u *
3658get_user_var_name(xp, idx)
3659 expand_T *xp;
3660 int idx;
3661{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003662 static long_u gdone;
3663 static long_u bdone;
3664 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003665#ifdef FEAT_WINDOWS
3666 static long_u tdone;
3667#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003668 static int vidx;
3669 static hashitem_T *hi;
3670 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
3672 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003673 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003675#ifdef FEAT_WINDOWS
3676 tdone = 0;
3677#endif
3678 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003679
3680 /* Global variables */
3681 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003683 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003684 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003685 else
3686 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003687 while (HASHITEM_EMPTY(hi))
3688 ++hi;
3689 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3690 return cat_prefix_varname('g', hi->hi_key);
3691 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003693
3694 /* b: variables */
3695 ht = &curbuf->b_vars.dv_hashtab;
3696 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003698 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003700 else
3701 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003702 while (HASHITEM_EMPTY(hi))
3703 ++hi;
3704 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003708 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return (char_u *)"b:changedtick";
3710 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003711
3712 /* w: variables */
3713 ht = &curwin->w_vars.dv_hashtab;
3714 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003716 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003718 else
3719 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003720 while (HASHITEM_EMPTY(hi))
3721 ++hi;
3722 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003724
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003725#ifdef FEAT_WINDOWS
3726 /* t: variables */
3727 ht = &curtab->tp_vars.dv_hashtab;
3728 if (tdone < ht->ht_used)
3729 {
3730 if (tdone++ == 0)
3731 hi = ht->ht_array;
3732 else
3733 ++hi;
3734 while (HASHITEM_EMPTY(hi))
3735 ++hi;
3736 return cat_prefix_varname('t', hi->hi_key);
3737 }
3738#endif
3739
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 /* v: variables */
3741 if (vidx < VV_LEN)
3742 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 vim_free(varnamebuf);
3745 varnamebuf = NULL;
3746 varnamebuflen = 0;
3747 return NULL;
3748}
3749
3750#endif /* FEAT_CMDL_COMPL */
3751
3752/*
3753 * types for expressions.
3754 */
3755typedef enum
3756{
3757 TYPE_UNKNOWN = 0
3758 , TYPE_EQUAL /* == */
3759 , TYPE_NEQUAL /* != */
3760 , TYPE_GREATER /* > */
3761 , TYPE_GEQUAL /* >= */
3762 , TYPE_SMALLER /* < */
3763 , TYPE_SEQUAL /* <= */
3764 , TYPE_MATCH /* =~ */
3765 , TYPE_NOMATCH /* !~ */
3766} exptype_T;
3767
3768/*
3769 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3772 */
3773
3774/*
3775 * Handle zero level expression.
3776 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003777 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003778 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 * Return OK or FAIL.
3780 */
3781 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003782eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 char_u **nextcmd;
3786 int evaluate;
3787{
3788 int ret;
3789 char_u *p;
3790
3791 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003792 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 if (ret == FAIL || !ends_excmd(*p))
3794 {
3795 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 /*
3798 * Report the invalid expression unless the expression evaluation has
3799 * been cancelled due to an aborting error, an interrupt, or an
3800 * exception.
3801 */
3802 if (!aborting())
3803 EMSG2(_(e_invexpr2), arg);
3804 ret = FAIL;
3805 }
3806 if (nextcmd != NULL)
3807 *nextcmd = check_nextcmd(p);
3808
3809 return ret;
3810}
3811
3812/*
3813 * Handle top level expression:
3814 * expr1 ? expr0 : expr0
3815 *
3816 * "arg" must point to the first non-white of the expression.
3817 * "arg" is advanced to the next non-white after the recognized expression.
3818 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003819 * Note: "rettv.v_lock" is not set.
3820 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 * Return OK or FAIL.
3822 */
3823 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 int evaluate;
3828{
3829 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 /*
3833 * Get the first variable.
3834 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003835 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837
3838 if ((*arg)[0] == '?')
3839 {
3840 result = FALSE;
3841 if (evaluate)
3842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003843 int error = FALSE;
3844
3845 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003847 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003848 if (error)
3849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851
3852 /*
3853 * Get the second variable.
3854 */
3855 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return FAIL;
3858
3859 /*
3860 * Check for the ":".
3861 */
3862 if ((*arg)[0] != ':')
3863 {
3864 EMSG(_("E109: Missing ':' after '?'"));
3865 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return FAIL;
3868 }
3869
3870 /*
3871 * Get the third variable.
3872 */
3873 *arg = skipwhite(*arg + 1);
3874 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3875 {
3876 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 return FAIL;
3879 }
3880 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003881 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883
3884 return OK;
3885}
3886
3887/*
3888 * Handle first level expression:
3889 * expr2 || expr2 || expr2 logical OR
3890 *
3891 * "arg" must point to the first non-white of the expression.
3892 * "arg" is advanced to the next non-white after the recognized expression.
3893 *
3894 * Return OK or FAIL.
3895 */
3896 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003897eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 int evaluate;
3901{
Bram Moolenaar33570922005-01-25 22:26:29 +00003902 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 long result;
3904 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 /*
3908 * Get the first variable.
3909 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003910 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 return FAIL;
3912
3913 /*
3914 * Repeat until there is no following "||".
3915 */
3916 first = TRUE;
3917 result = FALSE;
3918 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3919 {
3920 if (evaluate && first)
3921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003922 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003924 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 if (error)
3926 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 first = FALSE;
3928 }
3929
3930 /*
3931 * Get the second variable.
3932 */
3933 *arg = skipwhite(*arg + 2);
3934 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3935 return FAIL;
3936
3937 /*
3938 * Compute the result.
3939 */
3940 if (evaluate && !result)
3941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 if (error)
3946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 if (evaluate)
3949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003950 rettv->v_type = VAR_NUMBER;
3951 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 }
3954
3955 return OK;
3956}
3957
3958/*
3959 * Handle second level expression:
3960 * expr3 && expr3 && expr3 logical AND
3961 *
3962 * "arg" must point to the first non-white of the expression.
3963 * "arg" is advanced to the next non-white after the recognized expression.
3964 *
3965 * Return OK or FAIL.
3966 */
3967 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 int evaluate;
3972{
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 long result;
3975 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978 /*
3979 * Get the first variable.
3980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003981 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 return FAIL;
3983
3984 /*
3985 * Repeat until there is no following "&&".
3986 */
3987 first = TRUE;
3988 result = TRUE;
3989 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3990 {
3991 if (evaluate && first)
3992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996 if (error)
3997 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 first = FALSE;
3999 }
4000
4001 /*
4002 * Get the second variable.
4003 */
4004 *arg = skipwhite(*arg + 2);
4005 if (eval4(arg, &var2, evaluate && result) == FAIL)
4006 return FAIL;
4007
4008 /*
4009 * Compute the result.
4010 */
4011 if (evaluate && result)
4012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004013 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004016 if (error)
4017 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 if (evaluate)
4020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004021 rettv->v_type = VAR_NUMBER;
4022 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024 }
4025
4026 return OK;
4027}
4028
4029/*
4030 * Handle third level expression:
4031 * var1 == var2
4032 * var1 =~ var2
4033 * var1 != var2
4034 * var1 !~ var2
4035 * var1 > var2
4036 * var1 >= var2
4037 * var1 < var2
4038 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 * var1 is var2
4040 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 *
4042 * "arg" must point to the first non-white of the expression.
4043 * "arg" is advanced to the next non-white after the recognized expression.
4044 *
4045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 int evaluate;
4052{
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *p;
4055 int i;
4056 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004057 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 int len = 2;
4059 long n1, n2;
4060 char_u *s1, *s2;
4061 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4062 regmatch_T regmatch;
4063 int ic;
4064 char_u *save_cpo;
4065
4066 /*
4067 * Get the first variable.
4068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 return FAIL;
4071
4072 p = *arg;
4073 switch (p[0])
4074 {
4075 case '=': if (p[1] == '=')
4076 type = TYPE_EQUAL;
4077 else if (p[1] == '~')
4078 type = TYPE_MATCH;
4079 break;
4080 case '!': if (p[1] == '=')
4081 type = TYPE_NEQUAL;
4082 else if (p[1] == '~')
4083 type = TYPE_NOMATCH;
4084 break;
4085 case '>': if (p[1] != '=')
4086 {
4087 type = TYPE_GREATER;
4088 len = 1;
4089 }
4090 else
4091 type = TYPE_GEQUAL;
4092 break;
4093 case '<': if (p[1] != '=')
4094 {
4095 type = TYPE_SMALLER;
4096 len = 1;
4097 }
4098 else
4099 type = TYPE_SEQUAL;
4100 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004101 case 'i': if (p[1] == 's')
4102 {
4103 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4104 len = 5;
4105 if (!vim_isIDc(p[len]))
4106 {
4107 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4108 type_is = TRUE;
4109 }
4110 }
4111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 /*
4115 * If there is a comparitive operator, use it.
4116 */
4117 if (type != TYPE_UNKNOWN)
4118 {
4119 /* extra question mark appended: ignore case */
4120 if (p[len] == '?')
4121 {
4122 ic = TRUE;
4123 ++len;
4124 }
4125 /* extra '#' appended: match case */
4126 else if (p[len] == '#')
4127 {
4128 ic = FALSE;
4129 ++len;
4130 }
4131 /* nothing appened: use 'ignorecase' */
4132 else
4133 ic = p_ic;
4134
4135 /*
4136 * Get the second variable.
4137 */
4138 *arg = skipwhite(p + len);
4139 if (eval5(arg, &var2, evaluate) == FAIL)
4140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return FAIL;
4143 }
4144
4145 if (evaluate)
4146 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004147 if (type_is && rettv->v_type != var2.v_type)
4148 {
4149 /* For "is" a different type always means FALSE, for "notis"
4150 * it means TRUE. */
4151 n1 = (type == TYPE_NEQUAL);
4152 }
4153 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4154 {
4155 if (type_is)
4156 {
4157 n1 = (rettv->v_type == var2.v_type
4158 && rettv->vval.v_list == var2.vval.v_list);
4159 if (type == TYPE_NEQUAL)
4160 n1 = !n1;
4161 }
4162 else if (rettv->v_type != var2.v_type
4163 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4164 {
4165 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004166 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004168 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 clear_tv(rettv);
4170 clear_tv(&var2);
4171 return FAIL;
4172 }
4173 else
4174 {
4175 /* Compare two Lists for being equal or unequal. */
4176 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4177 if (type == TYPE_NEQUAL)
4178 n1 = !n1;
4179 }
4180 }
4181
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004182 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4183 {
4184 if (type_is)
4185 {
4186 n1 = (rettv->v_type == var2.v_type
4187 && rettv->vval.v_dict == var2.vval.v_dict);
4188 if (type == TYPE_NEQUAL)
4189 n1 = !n1;
4190 }
4191 else if (rettv->v_type != var2.v_type
4192 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4193 {
4194 if (rettv->v_type != var2.v_type)
4195 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4196 else
4197 EMSG(_("E736: Invalid operation for Dictionary"));
4198 clear_tv(rettv);
4199 clear_tv(&var2);
4200 return FAIL;
4201 }
4202 else
4203 {
4204 /* Compare two Dictionaries for being equal or unequal. */
4205 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4206 if (type == TYPE_NEQUAL)
4207 n1 = !n1;
4208 }
4209 }
4210
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004211 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4212 {
4213 if (rettv->v_type != var2.v_type
4214 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4215 {
4216 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004217 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004218 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004219 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004220 clear_tv(rettv);
4221 clear_tv(&var2);
4222 return FAIL;
4223 }
4224 else
4225 {
4226 /* Compare two Funcrefs for being equal or unequal. */
4227 if (rettv->vval.v_string == NULL
4228 || var2.vval.v_string == NULL)
4229 n1 = FALSE;
4230 else
4231 n1 = STRCMP(rettv->vval.v_string,
4232 var2.vval.v_string) == 0;
4233 if (type == TYPE_NEQUAL)
4234 n1 = !n1;
4235 }
4236 }
4237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 /*
4239 * If one of the two variables is a number, compare as a number.
4240 * When using "=~" or "!~", always compare as string.
4241 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004242 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 n1 = get_tv_number(rettv);
4246 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 switch (type)
4248 {
4249 case TYPE_EQUAL: n1 = (n1 == n2); break;
4250 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4251 case TYPE_GREATER: n1 = (n1 > n2); break;
4252 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4253 case TYPE_SMALLER: n1 = (n1 < n2); break;
4254 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4255 case TYPE_UNKNOWN:
4256 case TYPE_MATCH:
4257 case TYPE_NOMATCH: break; /* avoid gcc warning */
4258 }
4259 }
4260 else
4261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 s1 = get_tv_string_buf(rettv, buf1);
4263 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4265 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4266 else
4267 i = 0;
4268 n1 = FALSE;
4269 switch (type)
4270 {
4271 case TYPE_EQUAL: n1 = (i == 0); break;
4272 case TYPE_NEQUAL: n1 = (i != 0); break;
4273 case TYPE_GREATER: n1 = (i > 0); break;
4274 case TYPE_GEQUAL: n1 = (i >= 0); break;
4275 case TYPE_SMALLER: n1 = (i < 0); break;
4276 case TYPE_SEQUAL: n1 = (i <= 0); break;
4277
4278 case TYPE_MATCH:
4279 case TYPE_NOMATCH:
4280 /* avoid 'l' flag in 'cpoptions' */
4281 save_cpo = p_cpo;
4282 p_cpo = (char_u *)"";
4283 regmatch.regprog = vim_regcomp(s2,
4284 RE_MAGIC + RE_STRING);
4285 regmatch.rm_ic = ic;
4286 if (regmatch.regprog != NULL)
4287 {
4288 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4289 vim_free(regmatch.regprog);
4290 if (type == TYPE_NOMATCH)
4291 n1 = !n1;
4292 }
4293 p_cpo = save_cpo;
4294 break;
4295
4296 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4297 }
4298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 clear_tv(rettv);
4300 clear_tv(&var2);
4301 rettv->v_type = VAR_NUMBER;
4302 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 }
4305
4306 return OK;
4307}
4308
4309/*
4310 * Handle fourth level expression:
4311 * + number addition
4312 * - number subtraction
4313 * . string concatenation
4314 *
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4317 *
4318 * Return OK or FAIL.
4319 */
4320 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004321eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int evaluate;
4325{
Bram Moolenaar33570922005-01-25 22:26:29 +00004326 typval_T var2;
4327 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 int op;
4329 long n1, n2;
4330 char_u *s1, *s2;
4331 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4332 char_u *p;
4333
4334 /*
4335 * Get the first variable.
4336 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 return FAIL;
4339
4340 /*
4341 * Repeat computing, until no '+', '-' or '.' is following.
4342 */
4343 for (;;)
4344 {
4345 op = **arg;
4346 if (op != '+' && op != '-' && op != '.')
4347 break;
4348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (op != '+' || rettv->v_type != VAR_LIST)
4350 {
4351 /* For "list + ...", an illegal use of the first operand as
4352 * a number cannot be determined before evaluating the 2nd
4353 * operand: if this is also a list, all is ok.
4354 * For "something . ...", "something - ..." or "non-list + ...",
4355 * we know that the first operand needs to be a string or number
4356 * without evaluating the 2nd operand. So check before to avoid
4357 * side effects after an error. */
4358 if (evaluate && get_tv_string_chk(rettv) == NULL)
4359 {
4360 clear_tv(rettv);
4361 return FAIL;
4362 }
4363 }
4364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 /*
4366 * Get the second variable.
4367 */
4368 *arg = skipwhite(*arg + 1);
4369 if (eval6(arg, &var2, evaluate) == FAIL)
4370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return FAIL;
4373 }
4374
4375 if (evaluate)
4376 {
4377 /*
4378 * Compute the result.
4379 */
4380 if (op == '.')
4381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004382 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4383 s2 = get_tv_string_buf_chk(&var2, buf2);
4384 if (s2 == NULL) /* type error ? */
4385 {
4386 clear_tv(rettv);
4387 clear_tv(&var2);
4388 return FAIL;
4389 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004390 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(rettv);
4392 rettv->v_type = VAR_STRING;
4393 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004395 else if (op == '+' && rettv->v_type == VAR_LIST
4396 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 {
4398 /* concatenate Lists */
4399 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4400 &var3) == FAIL)
4401 {
4402 clear_tv(rettv);
4403 clear_tv(&var2);
4404 return FAIL;
4405 }
4406 clear_tv(rettv);
4407 *rettv = var3;
4408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else
4410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004411 int error = FALSE;
4412
4413 n1 = get_tv_number_chk(rettv, &error);
4414 if (error)
4415 {
4416 /* This can only happen for "list + non-list".
4417 * For "non-list + ..." or "something - ...", we returned
4418 * before evaluating the 2nd operand. */
4419 clear_tv(rettv);
4420 return FAIL;
4421 }
4422 n2 = get_tv_number_chk(&var2, &error);
4423 if (error)
4424 {
4425 clear_tv(rettv);
4426 clear_tv(&var2);
4427 return FAIL;
4428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (op == '+')
4431 n1 = n1 + n2;
4432 else
4433 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 rettv->v_type = VAR_NUMBER;
4435 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 }
4440 return OK;
4441}
4442
4443/*
4444 * Handle fifth level expression:
4445 * * number multiplication
4446 * / number division
4447 * % number modulo
4448 *
4449 * "arg" must point to the first non-white of the expression.
4450 * "arg" is advanced to the next non-white after the recognized expression.
4451 *
4452 * Return OK or FAIL.
4453 */
4454 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 int evaluate;
4459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 int op;
4462 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 /*
4466 * Get the first variable.
4467 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004468 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 return FAIL;
4470
4471 /*
4472 * Repeat computing, until no '*', '/' or '%' is following.
4473 */
4474 for (;;)
4475 {
4476 op = **arg;
4477 if (op != '*' && op != '/' && op != '%')
4478 break;
4479
4480 if (evaluate)
4481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004484 if (error)
4485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else
4488 n1 = 0;
4489
4490 /*
4491 * Get the second variable.
4492 */
4493 *arg = skipwhite(*arg + 1);
4494 if (eval7(arg, &var2, evaluate) == FAIL)
4495 return FAIL;
4496
4497 if (evaluate)
4498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 if (error)
4502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 /*
4505 * Compute the result.
4506 */
4507 if (op == '*')
4508 n1 = n1 * n2;
4509 else if (op == '/')
4510 {
4511 if (n2 == 0) /* give an error message? */
4512 n1 = 0x7fffffffL;
4513 else
4514 n1 = n1 / n2;
4515 }
4516 else
4517 {
4518 if (n2 == 0) /* give an error message? */
4519 n1 = 0;
4520 else
4521 n1 = n1 % n2;
4522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 rettv->v_type = VAR_NUMBER;
4524 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 }
4527
4528 return OK;
4529}
4530
4531/*
4532 * Handle sixth level expression:
4533 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004534 * "string" string constant
4535 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 * &option-name option value
4537 * @r register contents
4538 * identifier variable value
4539 * function() function call
4540 * $VAR environment variable
4541 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004542 * [expr, expr] List
4543 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 *
4545 * Also handle:
4546 * ! in front logical NOT
4547 * - in front unary minus
4548 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004549 * trailing [] subscript in String or List
4550 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 *
4552 * "arg" must point to the first non-white of the expression.
4553 * "arg" is advanced to the next non-white after the recognized expression.
4554 *
4555 * Return OK or FAIL.
4556 */
4557 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004558eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 int evaluate;
4562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 long n;
4564 int len;
4565 char_u *s;
4566 int val;
4567 char_u *start_leader, *end_leader;
4568 int ret = OK;
4569 char_u *alias;
4570
4571 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004575 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 /*
4578 * Skip '!' and '-' characters. They are handled later.
4579 */
4580 start_leader = *arg;
4581 while (**arg == '!' || **arg == '-' || **arg == '+')
4582 *arg = skipwhite(*arg + 1);
4583 end_leader = *arg;
4584
4585 switch (**arg)
4586 {
4587 /*
4588 * Number constant.
4589 */
4590 case '0':
4591 case '1':
4592 case '2':
4593 case '3':
4594 case '4':
4595 case '5':
4596 case '6':
4597 case '7':
4598 case '8':
4599 case '9':
4600 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4601 *arg += len;
4602 if (evaluate)
4603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004604 rettv->v_type = VAR_NUMBER;
4605 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607 break;
4608
4609 /*
4610 * String constant: "string".
4611 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614
4615 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004618 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 break;
4620
4621 /*
4622 * List: [expr, expr]
4623 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626
4627 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 * Dictionary: {key: val, key: val}
4629 */
4630 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4631 break;
4632
4633 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004634 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004636 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 break;
4638
4639 /*
4640 * Environment variable: $VAR.
4641 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004642 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 break;
4644
4645 /*
4646 * Register contents: @r.
4647 */
4648 case '@': ++*arg;
4649 if (evaluate)
4650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004651 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004652 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 if (**arg != NUL)
4655 ++*arg;
4656 break;
4657
4658 /*
4659 * nested expression: (expression).
4660 */
4661 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (**arg == ')')
4664 ++*arg;
4665 else if (ret == OK)
4666 {
4667 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 ret = FAIL;
4670 }
4671 break;
4672
Bram Moolenaar8c711452005-01-14 21:53:12 +00004673 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 break;
4675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004676
4677 if (ret == NOTDONE)
4678 {
4679 /*
4680 * Must be a variable or function name.
4681 * Can also be a curly-braces kind of name: {expr}.
4682 */
4683 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004684 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004685 if (alias != NULL)
4686 s = alias;
4687
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004688 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 ret = FAIL;
4690 else
4691 {
4692 if (**arg == '(') /* recursive! */
4693 {
4694 /* If "s" is the name of a variable of type VAR_FUNC
4695 * use its contents. */
4696 s = deref_func_name(s, &len);
4697
4698 /* Invoke the function. */
4699 ret = get_func_tv(s, len, rettv, arg,
4700 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004701 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 /* Stop the expression evaluation when immediately
4703 * aborting on error, or when an interrupt occurred or
4704 * an exception was thrown but not caught. */
4705 if (aborting())
4706 {
4707 if (ret == OK)
4708 clear_tv(rettv);
4709 ret = FAIL;
4710 }
4711 }
4712 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004713 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004714 else
4715 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004716 }
4717
4718 if (alias != NULL)
4719 vim_free(alias);
4720 }
4721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 *arg = skipwhite(*arg);
4723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004724 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4725 * expr(expr). */
4726 if (ret == OK)
4727 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 /*
4730 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4731 */
4732 if (ret == OK && evaluate && end_leader > start_leader)
4733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004734 int error = FALSE;
4735
4736 val = get_tv_number_chk(rettv, &error);
4737 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 clear_tv(rettv);
4740 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 else
4743 {
4744 while (end_leader > start_leader)
4745 {
4746 --end_leader;
4747 if (*end_leader == '!')
4748 val = !val;
4749 else if (*end_leader == '-')
4750 val = -val;
4751 }
4752 clear_tv(rettv);
4753 rettv->v_type = VAR_NUMBER;
4754 rettv->vval.v_number = val;
4755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757
4758 return ret;
4759}
4760
4761/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004762 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4763 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4765 */
4766 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004769 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004771 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004772{
4773 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004774 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004776 long len = -1;
4777 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004783 if (verbose)
4784 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 return FAIL;
4786 }
4787
Bram Moolenaar8c711452005-01-14 21:53:12 +00004788 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 /*
4791 * dict.name
4792 */
4793 key = *arg + 1;
4794 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4795 ;
4796 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004798 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 }
4800 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802 /*
4803 * something[idx]
4804 *
4805 * Get the (first) variable from inside the [].
4806 */
4807 *arg = skipwhite(*arg + 1);
4808 if (**arg == ':')
4809 empty1 = TRUE;
4810 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4811 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4813 {
4814 /* not a number or string */
4815 clear_tv(&var1);
4816 return FAIL;
4817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004818
4819 /*
4820 * Get the second variable from inside the [:].
4821 */
4822 if (**arg == ':')
4823 {
4824 range = TRUE;
4825 *arg = skipwhite(*arg + 1);
4826 if (**arg == ']')
4827 empty2 = TRUE;
4828 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (!empty1)
4831 clear_tv(&var1);
4832 return FAIL;
4833 }
4834 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4835 {
4836 /* not a number or string */
4837 if (!empty1)
4838 clear_tv(&var1);
4839 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004840 return FAIL;
4841 }
4842 }
4843
4844 /* Check for the ']'. */
4845 if (**arg != ']')
4846 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004847 if (verbose)
4848 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004849 clear_tv(&var1);
4850 if (range)
4851 clear_tv(&var2);
4852 return FAIL;
4853 }
4854 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855 }
4856
4857 if (evaluate)
4858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004859 n1 = 0;
4860 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 n1 = get_tv_number(&var1);
4863 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 }
4865 if (range)
4866 {
4867 if (empty2)
4868 n2 = -1;
4869 else
4870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 n2 = get_tv_number(&var2);
4872 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 }
4874 }
4875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877 {
4878 case VAR_NUMBER:
4879 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 len = (long)STRLEN(s);
4882 if (range)
4883 {
4884 /* The resulting variable is a substring. If the indexes
4885 * are out of range the result is empty. */
4886 if (n1 < 0)
4887 {
4888 n1 = len + n1;
4889 if (n1 < 0)
4890 n1 = 0;
4891 }
4892 if (n2 < 0)
4893 n2 = len + n2;
4894 else if (n2 >= len)
4895 n2 = len;
4896 if (n1 >= len || n2 < 0 || n1 > n2)
4897 s = NULL;
4898 else
4899 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4900 }
4901 else
4902 {
4903 /* The resulting variable is a string of a single
4904 * character. If the index is too big or negative the
4905 * result is empty. */
4906 if (n1 >= len || n1 < 0)
4907 s = NULL;
4908 else
4909 s = vim_strnsave(s + n1, 1);
4910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
4912 rettv->v_type = VAR_STRING;
4913 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 break;
4915
4916 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 if (n1 < 0)
4919 n1 = len + n1;
4920 if (!empty1 && (n1 < 0 || n1 >= len))
4921 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004922 /* For a range we allow invalid values and return an empty
4923 * list. A list index out of range is an error. */
4924 if (!range)
4925 {
4926 if (verbose)
4927 EMSGN(_(e_listidx), n1);
4928 return FAIL;
4929 }
4930 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004931 }
4932 if (range)
4933 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004934 list_T *l;
4935 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004936
4937 if (n2 < 0)
4938 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004939 else if (n2 >= len)
4940 n2 = len - 1;
4941 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004942 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 l = list_alloc();
4944 if (l == NULL)
4945 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004947 n1 <= n2; ++n1)
4948 {
4949 if (list_append_tv(l, &item->li_tv) == FAIL)
4950 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00004951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 return FAIL;
4953 }
4954 item = item->li_next;
4955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004956 clear_tv(rettv);
4957 rettv->v_type = VAR_LIST;
4958 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004959 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004960 }
4961 else
4962 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004963 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
4965 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 }
4967 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968
4969 case VAR_DICT:
4970 if (range)
4971 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004972 if (verbose)
4973 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 if (len == -1)
4975 clear_tv(&var1);
4976 return FAIL;
4977 }
4978 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004979 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004980
4981 if (len == -1)
4982 {
4983 key = get_tv_string(&var1);
4984 if (*key == NUL)
4985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004986 if (verbose)
4987 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004988 clear_tv(&var1);
4989 return FAIL;
4990 }
4991 }
4992
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004993 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004996 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 if (len == -1)
4998 clear_tv(&var1);
4999 if (item == NULL)
5000 return FAIL;
5001
5002 copy_tv(&item->di_tv, &var1);
5003 clear_tv(rettv);
5004 *rettv = var1;
5005 }
5006 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005007 }
5008 }
5009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005010 return OK;
5011}
5012
5013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 * Get an option value.
5015 * "arg" points to the '&' or '+' before the option name.
5016 * "arg" is advanced to character after the option name.
5017 * Return OK or FAIL.
5018 */
5019 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005020get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005022 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 int evaluate;
5024{
5025 char_u *option_end;
5026 long numval;
5027 char_u *stringval;
5028 int opt_type;
5029 int c;
5030 int working = (**arg == '+'); /* has("+option") */
5031 int ret = OK;
5032 int opt_flags;
5033
5034 /*
5035 * Isolate the option name and find its value.
5036 */
5037 option_end = find_option_end(arg, &opt_flags);
5038 if (option_end == NULL)
5039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005040 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 EMSG2(_("E112: Option name missing: %s"), *arg);
5042 return FAIL;
5043 }
5044
5045 if (!evaluate)
5046 {
5047 *arg = option_end;
5048 return OK;
5049 }
5050
5051 c = *option_end;
5052 *option_end = NUL;
5053 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 if (opt_type == -3) /* invalid name */
5057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005058 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 EMSG2(_("E113: Unknown option: %s"), *arg);
5060 ret = FAIL;
5061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005062 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 if (opt_type == -2) /* hidden string option */
5065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 rettv->v_type = VAR_STRING;
5067 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 else if (opt_type == -1) /* hidden number option */
5070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_NUMBER;
5072 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 else if (opt_type == 1) /* number option */
5075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 rettv->v_type = VAR_NUMBER;
5077 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 else /* string option */
5080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 rettv->v_type = VAR_STRING;
5082 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084 }
5085 else if (working && (opt_type == -2 || opt_type == -1))
5086 ret = FAIL;
5087
5088 *option_end = c; /* put back for error messages */
5089 *arg = option_end;
5090
5091 return ret;
5092}
5093
5094/*
5095 * Allocate a variable for a string constant.
5096 * Return OK or FAIL.
5097 */
5098 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 int evaluate;
5103{
5104 char_u *p;
5105 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 int extra = 0;
5107
5108 /*
5109 * Find the end of the string, skipping backslashed characters.
5110 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 if (*p == '\\' && p[1] != NUL)
5114 {
5115 ++p;
5116 /* A "\<x>" form occupies at least 4 characters, and produces up
5117 * to 6 characters: reserve space for 2 extra */
5118 if (*p == '<')
5119 extra += 2;
5120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
5122
5123 if (*p != '"')
5124 {
5125 EMSG2(_("E114: Missing quote: %s"), *arg);
5126 return FAIL;
5127 }
5128
5129 /* If only parsing, set *arg and return here */
5130 if (!evaluate)
5131 {
5132 *arg = p + 1;
5133 return OK;
5134 }
5135
5136 /*
5137 * Copy the string into allocated memory, handling backslashed
5138 * characters.
5139 */
5140 name = alloc((unsigned)(p - *arg + extra));
5141 if (name == NULL)
5142 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 rettv->v_type = VAR_STRING;
5144 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 if (*p == '\\')
5149 {
5150 switch (*++p)
5151 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 case 'b': *name++ = BS; ++p; break;
5153 case 'e': *name++ = ESC; ++p; break;
5154 case 'f': *name++ = FF; ++p; break;
5155 case 'n': *name++ = NL; ++p; break;
5156 case 'r': *name++ = CAR; ++p; break;
5157 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159 case 'X': /* hex: "\x1", "\x12" */
5160 case 'x':
5161 case 'u': /* Unicode: "\u0023" */
5162 case 'U':
5163 if (vim_isxdigit(p[1]))
5164 {
5165 int n, nr;
5166 int c = toupper(*p);
5167
5168 if (c == 'X')
5169 n = 2;
5170 else
5171 n = 4;
5172 nr = 0;
5173 while (--n >= 0 && vim_isxdigit(p[1]))
5174 {
5175 ++p;
5176 nr = (nr << 4) + hex2nr(*p);
5177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#ifdef FEAT_MBYTE
5180 /* For "\u" store the number according to
5181 * 'encoding'. */
5182 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 else
5185#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /* octal: "\1", "\12", "\123" */
5191 case '0':
5192 case '1':
5193 case '2':
5194 case '3':
5195 case '4':
5196 case '5':
5197 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 case '7': *name = *p++ - '0';
5199 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 *name = (*name << 3) + *p++ - '0';
5202 if (*p >= '0' && *p <= '7')
5203 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 break;
5207
5208 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (extra != 0)
5211 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
5215 /* FALLTHROUGH */
5216
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219 }
5220 }
5221 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 *arg = p + 1;
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 return OK;
5229}
5230
5231/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 * Return OK or FAIL.
5234 */
5235 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005236get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 int evaluate;
5240{
5241 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005242 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005243 int reduce = 0;
5244
5245 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005247 */
5248 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005253 break;
5254 ++reduce;
5255 ++p;
5256 }
5257 }
5258
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005262 return FAIL;
5263 }
5264
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005266 if (!evaluate)
5267 {
5268 *arg = p + 1;
5269 return OK;
5270 }
5271
5272 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005274 */
5275 str = alloc((unsigned)((p - *arg) - reduce));
5276 if (str == NULL)
5277 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 rettv->v_type = VAR_STRING;
5279 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005280
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005282 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005286 break;
5287 ++p;
5288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005292 *arg = p + 1;
5293
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005294 return OK;
5295}
5296
5297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 * Allocate a variable for a List and fill it from "*arg".
5299 * Return OK or FAIL.
5300 */
5301 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005302get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005304 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 int evaluate;
5306{
Bram Moolenaar33570922005-01-25 22:26:29 +00005307 list_T *l = NULL;
5308 typval_T tv;
5309 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
5311 if (evaluate)
5312 {
5313 l = list_alloc();
5314 if (l == NULL)
5315 return FAIL;
5316 }
5317
5318 *arg = skipwhite(*arg + 1);
5319 while (**arg != ']' && **arg != NUL)
5320 {
5321 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5322 goto failret;
5323 if (evaluate)
5324 {
5325 item = listitem_alloc();
5326 if (item != NULL)
5327 {
5328 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005329 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 list_append(l, item);
5331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005332 else
5333 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 }
5335
5336 if (**arg == ']')
5337 break;
5338 if (**arg != ',')
5339 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 goto failret;
5342 }
5343 *arg = skipwhite(*arg + 1);
5344 }
5345
5346 if (**arg != ']')
5347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349failret:
5350 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005351 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 return FAIL;
5353 }
5354
5355 *arg = skipwhite(*arg + 1);
5356 if (evaluate)
5357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 rettv->v_type = VAR_LIST;
5359 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 ++l->lv_refcount;
5361 }
5362
5363 return OK;
5364}
5365
5366/*
5367 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005368 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005370 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371list_alloc()
5372{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005373 list_T *l;
5374
5375 l = (list_T *)alloc_clear(sizeof(list_T));
5376 if (l != NULL)
5377 {
5378 /* Prepend the list to the list of lists for garbage collection. */
5379 if (first_list != NULL)
5380 first_list->lv_used_prev = l;
5381 l->lv_used_prev = NULL;
5382 l->lv_used_next = first_list;
5383 first_list = l;
5384 }
5385 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386}
5387
5388/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005389 * Allocate an empty list for a return value.
5390 * Returns OK or FAIL.
5391 */
5392 static int
5393rettv_list_alloc(rettv)
5394 typval_T *rettv;
5395{
5396 list_T *l = list_alloc();
5397
5398 if (l == NULL)
5399 return FAIL;
5400
5401 rettv->vval.v_list = l;
5402 rettv->v_type = VAR_LIST;
5403 ++l->lv_refcount;
5404 return OK;
5405}
5406
5407/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 * Unreference a list: decrement the reference count and free it when it
5409 * becomes zero.
5410 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005411 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005415 if (l != NULL && --l->lv_refcount <= 0)
5416 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417}
5418
5419/*
5420 * Free a list, including all items it points to.
5421 * Ignores the reference count.
5422 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005423 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005424list_free(l, recurse)
5425 list_T *l;
5426 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427{
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005430 /* Remove the list from the list of lists for garbage collection. */
5431 if (l->lv_used_prev == NULL)
5432 first_list = l->lv_used_next;
5433 else
5434 l->lv_used_prev->lv_used_next = l->lv_used_next;
5435 if (l->lv_used_next != NULL)
5436 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5437
Bram Moolenaard9fba312005-06-26 22:34:35 +00005438 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005440 /* Remove the item before deleting it. */
5441 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005442 if (recurse || (item->li_tv.v_type != VAR_LIST
5443 && item->li_tv.v_type != VAR_DICT))
5444 clear_tv(&item->li_tv);
5445 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 }
5447 vim_free(l);
5448}
5449
5450/*
5451 * Allocate a list item.
5452 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454listitem_alloc()
5455{
Bram Moolenaar33570922005-01-25 22:26:29 +00005456 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457}
5458
5459/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005460 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 */
5462 static void
5463listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 vim_free(item);
5468}
5469
5470/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005471 * Remove a list item from a List and free it. Also clears the value.
5472 */
5473 static void
5474listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 list_T *l;
5476 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005477{
5478 list_remove(l, item, item);
5479 listitem_free(item);
5480}
5481
5482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 * Get the number of items in a list.
5484 */
5485 static long
5486list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 if (l == NULL)
5490 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005491 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492}
5493
5494/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005495 * Return TRUE when two lists have exactly the same values.
5496 */
5497 static int
5498list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 list_T *l1;
5500 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005501 int ic; /* ignore case for strings */
5502{
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005504
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005505 if (l1 == l2)
5506 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005507 if (list_len(l1) != list_len(l2))
5508 return FALSE;
5509
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005510 for (item1 = l1->lv_first, item2 = l2->lv_first;
5511 item1 != NULL && item2 != NULL;
5512 item1 = item1->li_next, item2 = item2->li_next)
5513 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5514 return FALSE;
5515 return item1 == NULL && item2 == NULL;
5516}
5517
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005518#if defined(FEAT_PYTHON) || defined(PROTO)
5519/*
5520 * Return the dictitem that an entry in a hashtable points to.
5521 */
5522 dictitem_T *
5523dict_lookup(hi)
5524 hashitem_T *hi;
5525{
5526 return HI2DI(hi);
5527}
5528#endif
5529
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005530/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005531 * Return TRUE when two dictionaries have exactly the same key/values.
5532 */
5533 static int
5534dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 dict_T *d1;
5536 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005537 int ic; /* ignore case for strings */
5538{
Bram Moolenaar33570922005-01-25 22:26:29 +00005539 hashitem_T *hi;
5540 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005541 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005542
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005543 if (d1 == d2)
5544 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005545 if (dict_len(d1) != dict_len(d2))
5546 return FALSE;
5547
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005548 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005549 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 if (!HASHITEM_EMPTY(hi))
5552 {
5553 item2 = dict_find(d2, hi->hi_key, -1);
5554 if (item2 == NULL)
5555 return FALSE;
5556 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5557 return FALSE;
5558 --todo;
5559 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005560 }
5561 return TRUE;
5562}
5563
5564/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005566 * Compares the items just like "==" would compare them, but strings and
5567 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005568 */
5569 static int
5570tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005571 typval_T *tv1;
5572 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005573 int ic; /* ignore case */
5574{
5575 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005576 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005577 static int recursive = 0; /* cach recursive loops */
5578 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005579
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005580 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005581 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005582 /* Catch lists and dicts that have an endless loop by limiting
5583 * recursiveness to 1000. We guess they are equal then. */
5584 if (recursive >= 1000)
5585 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005586
5587 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005588 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005589 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005590 ++recursive;
5591 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5592 --recursive;
5593 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005594
5595 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005596 ++recursive;
5597 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5598 --recursive;
5599 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005600
5601 case VAR_FUNC:
5602 return (tv1->vval.v_string != NULL
5603 && tv2->vval.v_string != NULL
5604 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5605
5606 case VAR_NUMBER:
5607 return tv1->vval.v_number == tv2->vval.v_number;
5608
5609 case VAR_STRING:
5610 s1 = get_tv_string_buf(tv1, buf1);
5611 s2 = get_tv_string_buf(tv2, buf2);
5612 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005614
5615 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005616 return TRUE;
5617}
5618
5619/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 * Locate item with index "n" in list "l" and return it.
5621 * A negative index is counted from the end; -1 is the last item.
5622 * Returns NULL when "n" is out of range.
5623 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005626 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 long n;
5628{
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 long idx;
5631
5632 if (l == NULL)
5633 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005634
5635 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005637 n = l->lv_len + n;
5638
5639 /* Check for index out of range. */
5640 if (n < 0 || n >= l->lv_len)
5641 return NULL;
5642
5643 /* When there is a cached index may start search from there. */
5644 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005646 if (n < l->lv_idx / 2)
5647 {
5648 /* closest to the start of the list */
5649 item = l->lv_first;
5650 idx = 0;
5651 }
5652 else if (n > (l->lv_idx + l->lv_len) / 2)
5653 {
5654 /* closest to the end of the list */
5655 item = l->lv_last;
5656 idx = l->lv_len - 1;
5657 }
5658 else
5659 {
5660 /* closest to the cached index */
5661 item = l->lv_idx_item;
5662 idx = l->lv_idx;
5663 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 }
5665 else
5666 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005667 if (n < l->lv_len / 2)
5668 {
5669 /* closest to the start of the list */
5670 item = l->lv_first;
5671 idx = 0;
5672 }
5673 else
5674 {
5675 /* closest to the end of the list */
5676 item = l->lv_last;
5677 idx = l->lv_len - 1;
5678 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005680
5681 while (n > idx)
5682 {
5683 /* search forward */
5684 item = item->li_next;
5685 ++idx;
5686 }
5687 while (n < idx)
5688 {
5689 /* search backward */
5690 item = item->li_prev;
5691 --idx;
5692 }
5693
5694 /* cache the used index */
5695 l->lv_idx = idx;
5696 l->lv_idx_item = item;
5697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 return item;
5699}
5700
5701/*
Bram Moolenaara5525202006-03-02 22:52:09 +00005702 * Get list item "l[idx]" as a number.
5703 */
5704 static long
5705list_find_nr(l, idx, errorp)
5706 list_T *l;
5707 long idx;
5708 int *errorp; /* set to TRUE when something wrong */
5709{
5710 listitem_T *li;
5711
5712 li = list_find(l, idx);
5713 if (li == NULL)
5714 {
5715 if (errorp != NULL)
5716 *errorp = TRUE;
5717 return -1L;
5718 }
5719 return get_tv_number_chk(&li->li_tv, errorp);
5720}
5721
5722/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005723 * Locate "item" list "l" and return its index.
5724 * Returns -1 when "item" is not in the list.
5725 */
5726 static long
5727list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 list_T *l;
5729 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005730{
5731 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005733
5734 if (l == NULL)
5735 return -1;
5736 idx = 0;
5737 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5738 ++idx;
5739 if (li == NULL)
5740 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005741 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005742}
5743
5744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005745 * Append item "item" to the end of list "l".
5746 */
5747 static void
5748list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005749 list_T *l;
5750 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751{
5752 if (l->lv_last == NULL)
5753 {
5754 /* empty list */
5755 l->lv_first = item;
5756 l->lv_last = item;
5757 item->li_prev = NULL;
5758 }
5759 else
5760 {
5761 l->lv_last->li_next = item;
5762 item->li_prev = l->lv_last;
5763 l->lv_last = item;
5764 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005765 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 item->li_next = NULL;
5767}
5768
5769/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005770 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005771 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005772 */
5773 static int
5774list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005775 list_T *l;
5776 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005778 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779
Bram Moolenaar05159a02005-02-26 23:04:13 +00005780 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005782 copy_tv(tv, &li->li_tv);
5783 list_append(l, li);
5784 return OK;
5785}
5786
5787/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005788 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005789 * Return FAIL when out of memory.
5790 */
5791 int
5792list_append_dict(list, dict)
5793 list_T *list;
5794 dict_T *dict;
5795{
5796 listitem_T *li = listitem_alloc();
5797
5798 if (li == NULL)
5799 return FAIL;
5800 li->li_tv.v_type = VAR_DICT;
5801 li->li_tv.v_lock = 0;
5802 li->li_tv.vval.v_dict = dict;
5803 list_append(list, li);
5804 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 return OK;
5806}
5807
5808/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005809 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00005810 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005811 * Returns FAIL when out of memory.
5812 */
5813 static int
Bram Moolenaar4463f292005-09-25 22:20:24 +00005814list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005815 list_T *l;
5816 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00005817 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005818{
5819 listitem_T *li = listitem_alloc();
5820
5821 if (li == NULL)
5822 return FAIL;
5823 list_append(l, li);
5824 li->li_tv.v_type = VAR_STRING;
5825 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005826 if (str == NULL)
5827 li->li_tv.vval.v_string = NULL;
5828 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005829 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005830 return FAIL;
5831 return OK;
5832}
5833
5834/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00005835 * Append "n" to list "l".
5836 * Returns FAIL when out of memory.
5837 */
5838 static int
5839list_append_number(l, n)
5840 list_T *l;
5841 varnumber_T n;
5842{
5843 listitem_T *li;
5844
5845 li = listitem_alloc();
5846 if (li == NULL)
5847 return FAIL;
5848 li->li_tv.v_type = VAR_NUMBER;
5849 li->li_tv.v_lock = 0;
5850 li->li_tv.vval.v_number = n;
5851 list_append(l, li);
5852 return OK;
5853}
5854
5855/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005857 * If "item" is NULL append at the end.
5858 * Return FAIL when out of memory.
5859 */
5860 static int
5861list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 list_T *l;
5863 typval_T *tv;
5864 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005865{
Bram Moolenaar33570922005-01-25 22:26:29 +00005866 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005867
5868 if (ni == NULL)
5869 return FAIL;
5870 copy_tv(tv, &ni->li_tv);
5871 if (item == NULL)
5872 /* Append new item at end of list. */
5873 list_append(l, ni);
5874 else
5875 {
5876 /* Insert new item before existing item. */
5877 ni->li_prev = item->li_prev;
5878 ni->li_next = item;
5879 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005880 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005881 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 ++l->lv_idx;
5883 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005884 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005886 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005887 l->lv_idx_item = NULL;
5888 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005890 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005891 }
5892 return OK;
5893}
5894
5895/*
5896 * Extend "l1" with "l2".
5897 * If "bef" is NULL append at the end, otherwise insert before this item.
5898 * Returns FAIL when out of memory.
5899 */
5900 static int
5901list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 list_T *l1;
5903 list_T *l2;
5904 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005905{
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005907
5908 for (item = l2->lv_first; item != NULL; item = item->li_next)
5909 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5910 return FAIL;
5911 return OK;
5912}
5913
5914/*
5915 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5916 * Return FAIL when out of memory.
5917 */
5918 static int
5919list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 list_T *l1;
5921 list_T *l2;
5922 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005925
5926 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005928 if (l == NULL)
5929 return FAIL;
5930 tv->v_type = VAR_LIST;
5931 tv->vval.v_list = l;
5932
5933 /* append all items from the second list */
5934 return list_extend(l, l2, NULL);
5935}
5936
5937/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 * Returns NULL when out of memory.
5942 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005944list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005947 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 list_T *copy;
5950 listitem_T *item;
5951 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
5953 if (orig == NULL)
5954 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955
5956 copy = list_alloc();
5957 if (copy != NULL)
5958 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005959 if (copyID != 0)
5960 {
5961 /* Do this before adding the items, because one of the items may
5962 * refer back to this list. */
5963 orig->lv_copyID = copyID;
5964 orig->lv_copylist = copy;
5965 }
5966 for (item = orig->lv_first; item != NULL && !got_int;
5967 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 {
5969 ni = listitem_alloc();
5970 if (ni == NULL)
5971 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005973 {
5974 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5975 {
5976 vim_free(ni);
5977 break;
5978 }
5979 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005981 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 list_append(copy, ni);
5983 }
5984 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005985 if (item != NULL)
5986 {
5987 list_unref(copy);
5988 copy = NULL;
5989 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 }
5991
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 return copy;
5993}
5994
5995/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 list_T *l;
6002 listitem_T *item;
6003 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 /* notify watchers */
6008 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006010 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 list_fix_watch(l, ip);
6012 if (ip == item2)
6013 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015
6016 if (item2->li_next == NULL)
6017 l->lv_last = item->li_prev;
6018 else
6019 item2->li_next->li_prev = item->li_prev;
6020 if (item->li_prev == NULL)
6021 l->lv_first = item2->li_next;
6022 else
6023 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006024 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025}
6026
6027/*
6028 * Return an allocated string with the string representation of a list.
6029 * May return NULL.
6030 */
6031 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006032list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006034 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
6036 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037
6038 if (tv->vval.v_list == NULL)
6039 return NULL;
6040 ga_init2(&ga, (int)sizeof(char), 80);
6041 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006042 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006043 {
6044 vim_free(ga.ga_data);
6045 return NULL;
6046 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 ga_append(&ga, ']');
6048 ga_append(&ga, NUL);
6049 return (char_u *)ga.ga_data;
6050}
6051
6052/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006053 * Join list "l" into a string in "*gap", using separator "sep".
6054 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006058list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006059 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006061 char_u *sep;
6062 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006063 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006064{
6065 int first = TRUE;
6066 char_u *tofree;
6067 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006069 char_u *s;
6070
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006071 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006072 {
6073 if (first)
6074 first = FALSE;
6075 else
6076 ga_concat(gap, sep);
6077
6078 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006079 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006080 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006082 if (s != NULL)
6083 ga_concat(gap, s);
6084 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006085 if (s == NULL)
6086 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006087 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006088 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006089}
6090
6091/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006092 * Garbage collection for lists and dictionaries.
6093 *
6094 * We use reference counts to be able to free most items right away when they
6095 * are no longer used. But for composite items it's possible that it becomes
6096 * unused while the reference count is > 0: When there is a recursive
6097 * reference. Example:
6098 * :let l = [1, 2, 3]
6099 * :let d = {9: l}
6100 * :let l[1] = d
6101 *
6102 * Since this is quite unusual we handle this with garbage collection: every
6103 * once in a while find out which lists and dicts are not referenced from any
6104 * variable.
6105 *
6106 * Here is a good reference text about garbage collection (refers to Python
6107 * but it applies to all reference-counting mechanisms):
6108 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006109 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006110
6111/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112 * Do garbage collection for lists and dicts.
6113 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006114 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006115 int
6116garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006117{
6118 dict_T *dd;
6119 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006120 int copyID = ++current_copyID;
6121 buf_T *buf;
6122 win_T *wp;
6123 int i;
6124 funccall_T *fc;
6125 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006126#ifdef FEAT_WINDOWS
6127 tabpage_T *tp;
6128#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006129
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006130 /* Only do this once. */
6131 want_garbage_collect = FALSE;
6132 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006133 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006134
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006135 /*
6136 * 1. Go through all accessible variables and mark all lists and dicts
6137 * with copyID.
6138 */
6139 /* script-local variables */
6140 for (i = 1; i <= ga_scripts.ga_len; ++i)
6141 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6142
6143 /* buffer-local variables */
6144 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6145 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6146
6147 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006148 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006149 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6150
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006151#ifdef FEAT_WINDOWS
6152 /* tabpage-local variables */
6153 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6154 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6155#endif
6156
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006157 /* global variables */
6158 set_ref_in_ht(&globvarht, copyID);
6159
6160 /* function-local variables */
6161 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6162 {
6163 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6164 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6165 }
6166
6167 /*
6168 * 2. Go through the list of dicts and free items without the copyID.
6169 */
6170 for (dd = first_dict; dd != NULL; )
6171 if (dd->dv_copyID != copyID)
6172 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006173 /* Free the Dictionary and ordinary items it contains, but don't
6174 * recurse into Lists and Dictionaries, they will be in the list
6175 * of dicts or list of lists. */
6176 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006177 did_free = TRUE;
6178
6179 /* restart, next dict may also have been freed */
6180 dd = first_dict;
6181 }
6182 else
6183 dd = dd->dv_used_next;
6184
6185 /*
6186 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006187 * But don't free a list that has a watcher (used in a for loop), these
6188 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006189 */
6190 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006191 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006192 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006193 /* Free the List and ordinary items it contains, but don't recurse
6194 * into Lists and Dictionaries, they will be in the list of dicts
6195 * or list of lists. */
6196 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006197 did_free = TRUE;
6198
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006199 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006200 ll = first_list;
6201 }
6202 else
6203 ll = ll->lv_used_next;
6204
6205 return did_free;
6206}
6207
6208/*
6209 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6210 */
6211 static void
6212set_ref_in_ht(ht, copyID)
6213 hashtab_T *ht;
6214 int copyID;
6215{
6216 int todo;
6217 hashitem_T *hi;
6218
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006219 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006220 for (hi = ht->ht_array; todo > 0; ++hi)
6221 if (!HASHITEM_EMPTY(hi))
6222 {
6223 --todo;
6224 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6225 }
6226}
6227
6228/*
6229 * Mark all lists and dicts referenced through list "l" with "copyID".
6230 */
6231 static void
6232set_ref_in_list(l, copyID)
6233 list_T *l;
6234 int copyID;
6235{
6236 listitem_T *li;
6237
6238 for (li = l->lv_first; li != NULL; li = li->li_next)
6239 set_ref_in_item(&li->li_tv, copyID);
6240}
6241
6242/*
6243 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6244 */
6245 static void
6246set_ref_in_item(tv, copyID)
6247 typval_T *tv;
6248 int copyID;
6249{
6250 dict_T *dd;
6251 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006252
6253 switch (tv->v_type)
6254 {
6255 case VAR_DICT:
6256 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006257 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006258 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006259 /* Didn't see this dict yet. */
6260 dd->dv_copyID = copyID;
6261 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006262 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006263 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006264
6265 case VAR_LIST:
6266 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006267 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006268 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006269 /* Didn't see this list yet. */
6270 ll->lv_copyID = copyID;
6271 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006272 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006273 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006274 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006275 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006276}
6277
6278/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006279 * Allocate an empty header for a dictionary.
6280 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006281 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282dict_alloc()
6283{
Bram Moolenaar33570922005-01-25 22:26:29 +00006284 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006285
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006288 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006289 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006290 if (first_dict != NULL)
6291 first_dict->dv_used_prev = d;
6292 d->dv_used_next = first_dict;
6293 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006294 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006295
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006297 d->dv_lock = 0;
6298 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006299 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006300 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006301 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006302}
6303
6304/*
6305 * Unreference a Dictionary: decrement the reference count and free it when it
6306 * becomes zero.
6307 */
6308 static void
6309dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006311{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006312 if (d != NULL && --d->dv_refcount <= 0)
6313 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314}
6315
6316/*
6317 * Free a Dictionary, including all items it contains.
6318 * Ignores the reference count.
6319 */
6320 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006321dict_free(d, recurse)
6322 dict_T *d;
6323 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006324{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006325 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006326 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006327 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006328
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006329 /* Remove the dict from the list of dicts for garbage collection. */
6330 if (d->dv_used_prev == NULL)
6331 first_dict = d->dv_used_next;
6332 else
6333 d->dv_used_prev->dv_used_next = d->dv_used_next;
6334 if (d->dv_used_next != NULL)
6335 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6336
6337 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006338 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006339 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006341 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006342 if (!HASHITEM_EMPTY(hi))
6343 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006344 /* Remove the item before deleting it, just in case there is
6345 * something recursive causing trouble. */
6346 di = HI2DI(hi);
6347 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006348 if (recurse || (di->di_tv.v_type != VAR_LIST
6349 && di->di_tv.v_type != VAR_DICT))
6350 clear_tv(&di->di_tv);
6351 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006352 --todo;
6353 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006355 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356 vim_free(d);
6357}
6358
6359/*
6360 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006361 * The "key" is copied to the new item.
6362 * Note that the value of the item "di_tv" still needs to be initialized!
6363 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006364 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006365 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006366dictitem_alloc(key)
6367 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006368{
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006370
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006371 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006372 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006374 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006375 di->di_flags = 0;
6376 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006377 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006378}
6379
6380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006381 * Make a copy of a Dictionary item.
6382 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006383 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006384dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006385 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006386{
Bram Moolenaar33570922005-01-25 22:26:29 +00006387 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006388
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006389 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6390 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 if (di != NULL)
6392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006393 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006394 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006395 copy_tv(&org->di_tv, &di->di_tv);
6396 }
6397 return di;
6398}
6399
6400/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006401 * Remove item "item" from Dictionary "dict" and free it.
6402 */
6403 static void
6404dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006405 dict_T *dict;
6406 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006407{
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006409
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006411 if (HASHITEM_EMPTY(hi))
6412 EMSG2(_(e_intern2), "dictitem_remove()");
6413 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006415 dictitem_free(item);
6416}
6417
6418/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419 * Free a dict item. Also clears the value.
6420 */
6421 static void
6422dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006424{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425 clear_tv(&item->di_tv);
6426 vim_free(item);
6427}
6428
6429/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006430 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6431 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006432 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006433 * Returns NULL when out of memory.
6434 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006436dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006437 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006439 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006440{
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 dict_T *copy;
6442 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006443 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006445
6446 if (orig == NULL)
6447 return NULL;
6448
6449 copy = dict_alloc();
6450 if (copy != NULL)
6451 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006452 if (copyID != 0)
6453 {
6454 orig->dv_copyID = copyID;
6455 orig->dv_copydict = copy;
6456 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006457 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006458 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006459 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006460 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006462 --todo;
6463
6464 di = dictitem_alloc(hi->hi_key);
6465 if (di == NULL)
6466 break;
6467 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006468 {
6469 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6470 copyID) == FAIL)
6471 {
6472 vim_free(di);
6473 break;
6474 }
6475 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006476 else
6477 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6478 if (dict_add(copy, di) == FAIL)
6479 {
6480 dictitem_free(di);
6481 break;
6482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006485
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006487 if (todo > 0)
6488 {
6489 dict_unref(copy);
6490 copy = NULL;
6491 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006492 }
6493
6494 return copy;
6495}
6496
6497/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006498 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006499 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006501 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006502dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 dict_T *d;
6504 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006505{
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507}
6508
Bram Moolenaar8c711452005-01-14 21:53:12 +00006509/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006510 * Add a number or string entry to dictionary "d".
6511 * When "str" is NULL use number "nr", otherwise use "str".
6512 * Returns FAIL when out of memory and when key already exists.
6513 */
6514 int
6515dict_add_nr_str(d, key, nr, str)
6516 dict_T *d;
6517 char *key;
6518 long nr;
6519 char_u *str;
6520{
6521 dictitem_T *item;
6522
6523 item = dictitem_alloc((char_u *)key);
6524 if (item == NULL)
6525 return FAIL;
6526 item->di_tv.v_lock = 0;
6527 if (str == NULL)
6528 {
6529 item->di_tv.v_type = VAR_NUMBER;
6530 item->di_tv.vval.v_number = nr;
6531 }
6532 else
6533 {
6534 item->di_tv.v_type = VAR_STRING;
6535 item->di_tv.vval.v_string = vim_strsave(str);
6536 }
6537 if (dict_add(d, item) == FAIL)
6538 {
6539 dictitem_free(item);
6540 return FAIL;
6541 }
6542 return OK;
6543}
6544
6545/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006546 * Get the number of items in a Dictionary.
6547 */
6548 static long
6549dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006550 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 if (d == NULL)
6553 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006554 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555}
6556
6557/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006558 * Find item "key[len]" in Dictionary "d".
6559 * If "len" is negative use strlen(key).
6560 * Returns NULL when not found.
6561 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006563dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006565 char_u *key;
6566 int len;
6567{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006568#define AKEYLEN 200
6569 char_u buf[AKEYLEN];
6570 char_u *akey;
6571 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006573
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006574 if (len < 0)
6575 akey = key;
6576 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006577 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006578 tofree = akey = vim_strnsave(key, len);
6579 if (akey == NULL)
6580 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006581 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006582 else
6583 {
6584 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006585 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006586 akey = buf;
6587 }
6588
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006590 vim_free(tofree);
6591 if (HASHITEM_EMPTY(hi))
6592 return NULL;
6593 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594}
6595
6596/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006597 * Get a string item from a dictionary.
6598 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00006599 * Returns NULL if the entry doesn't exist or out of memory.
6600 */
6601 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006602get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00006603 dict_T *d;
6604 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006605 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006606{
6607 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006608 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006609
6610 di = dict_find(d, key, -1);
6611 if (di == NULL)
6612 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00006613 s = get_tv_string(&di->di_tv);
6614 if (save && s != NULL)
6615 s = vim_strsave(s);
6616 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00006617}
6618
6619/*
6620 * Get a number item from a dictionary.
6621 * Returns 0 if the entry doesn't exist or out of memory.
6622 */
6623 long
6624get_dict_number(d, key)
6625 dict_T *d;
6626 char_u *key;
6627{
6628 dictitem_T *di;
6629
6630 di = dict_find(d, key, -1);
6631 if (di == NULL)
6632 return 0;
6633 return get_tv_number(&di->di_tv);
6634}
6635
6636/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006637 * Return an allocated string with the string representation of a Dictionary.
6638 * May return NULL.
6639 */
6640 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006641dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006643 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644{
6645 garray_T ga;
6646 int first = TRUE;
6647 char_u *tofree;
6648 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006650 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006652 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006653
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006654 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006655 return NULL;
6656 ga_init2(&ga, (int)sizeof(char), 80);
6657 ga_append(&ga, '{');
6658
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006659 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006660 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006662 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006664 --todo;
6665
6666 if (first)
6667 first = FALSE;
6668 else
6669 ga_concat(&ga, (char_u *)", ");
6670
6671 tofree = string_quote(hi->hi_key, FALSE);
6672 if (tofree != NULL)
6673 {
6674 ga_concat(&ga, tofree);
6675 vim_free(tofree);
6676 }
6677 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006678 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006679 if (s != NULL)
6680 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006681 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 if (s == NULL)
6683 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006685 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006686 if (todo > 0)
6687 {
6688 vim_free(ga.ga_data);
6689 return NULL;
6690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006691
6692 ga_append(&ga, '}');
6693 ga_append(&ga, NUL);
6694 return (char_u *)ga.ga_data;
6695}
6696
6697/*
6698 * Allocate a variable for a Dictionary and fill it from "*arg".
6699 * Return OK or FAIL. Returns NOTDONE for {expr}.
6700 */
6701 static int
6702get_dict_tv(arg, rettv, evaluate)
6703 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006704 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006705 int evaluate;
6706{
Bram Moolenaar33570922005-01-25 22:26:29 +00006707 dict_T *d = NULL;
6708 typval_T tvkey;
6709 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00006710 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006711 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006712 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006713 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006714
6715 /*
6716 * First check if it's not a curly-braces thing: {expr}.
6717 * Must do this without evaluating, otherwise a function may be called
6718 * twice. Unfortunately this means we need to call eval1() twice for the
6719 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006720 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006721 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006722 if (*start != '}')
6723 {
6724 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6725 return FAIL;
6726 if (*start == '}')
6727 return NOTDONE;
6728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006729
6730 if (evaluate)
6731 {
6732 d = dict_alloc();
6733 if (d == NULL)
6734 return FAIL;
6735 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006736 tvkey.v_type = VAR_UNKNOWN;
6737 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006738
6739 *arg = skipwhite(*arg + 1);
6740 while (**arg != '}' && **arg != NUL)
6741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006742 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006743 goto failret;
6744 if (**arg != ':')
6745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006746 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006747 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006748 goto failret;
6749 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00006750 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006751 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006752 key = get_tv_string_buf_chk(&tvkey, buf);
6753 if (key == NULL || *key == NUL)
6754 {
6755 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6756 if (key != NULL)
6757 EMSG(_(e_emptykey));
6758 clear_tv(&tvkey);
6759 goto failret;
6760 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006762
6763 *arg = skipwhite(*arg + 1);
6764 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6765 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00006766 if (evaluate)
6767 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006768 goto failret;
6769 }
6770 if (evaluate)
6771 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006772 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006773 if (item != NULL)
6774 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006775 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006776 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006777 clear_tv(&tv);
6778 goto failret;
6779 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006780 item = dictitem_alloc(key);
6781 clear_tv(&tvkey);
6782 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006784 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006785 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006786 if (dict_add(d, item) == FAIL)
6787 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006788 }
6789 }
6790
6791 if (**arg == '}')
6792 break;
6793 if (**arg != ',')
6794 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006795 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006796 goto failret;
6797 }
6798 *arg = skipwhite(*arg + 1);
6799 }
6800
6801 if (**arg != '}')
6802 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006803 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006804failret:
6805 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00006806 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006807 return FAIL;
6808 }
6809
6810 *arg = skipwhite(*arg + 1);
6811 if (evaluate)
6812 {
6813 rettv->v_type = VAR_DICT;
6814 rettv->vval.v_dict = d;
6815 ++d->dv_refcount;
6816 }
6817
6818 return OK;
6819}
6820
Bram Moolenaar8c711452005-01-14 21:53:12 +00006821/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006822 * Return a string with the string representation of a variable.
6823 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006824 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006825 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006826 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006827 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006828 */
6829 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006830echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006831 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006832 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006833 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006834 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006835{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006836 static int recurse = 0;
6837 char_u *r = NULL;
6838
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006841 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006842 *tofree = NULL;
6843 return NULL;
6844 }
6845 ++recurse;
6846
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006847 switch (tv->v_type)
6848 {
6849 case VAR_FUNC:
6850 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006851 r = tv->vval.v_string;
6852 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006855 if (tv->vval.v_list == NULL)
6856 {
6857 *tofree = NULL;
6858 r = NULL;
6859 }
6860 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
6861 {
6862 *tofree = NULL;
6863 r = (char_u *)"[...]";
6864 }
6865 else
6866 {
6867 tv->vval.v_list->lv_copyID = copyID;
6868 *tofree = list2string(tv, copyID);
6869 r = *tofree;
6870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006871 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006872
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006874 if (tv->vval.v_dict == NULL)
6875 {
6876 *tofree = NULL;
6877 r = NULL;
6878 }
6879 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
6880 {
6881 *tofree = NULL;
6882 r = (char_u *)"{...}";
6883 }
6884 else
6885 {
6886 tv->vval.v_dict->dv_copyID = copyID;
6887 *tofree = dict2string(tv, copyID);
6888 r = *tofree;
6889 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006890 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006892 case VAR_STRING:
6893 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006894 *tofree = NULL;
6895 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006896 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006897
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006898 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902
6903 --recurse;
6904 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006905}
6906
6907/*
6908 * Return a string with the string representation of a variable.
6909 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6910 * "numbuf" is used for a number.
6911 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006912 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006913 */
6914 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006915tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006917 char_u **tofree;
6918 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006919 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006920{
6921 switch (tv->v_type)
6922 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006923 case VAR_FUNC:
6924 *tofree = string_quote(tv->vval.v_string, TRUE);
6925 return *tofree;
6926 case VAR_STRING:
6927 *tofree = string_quote(tv->vval.v_string, FALSE);
6928 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006929 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006930 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006931 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006932 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006933 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006934 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006935 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006936 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006937}
6938
6939/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 * Return string "str" in ' quotes, doubling ' characters.
6941 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006942 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006943 */
6944 static char_u *
6945string_quote(str, function)
6946 char_u *str;
6947 int function;
6948{
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006950 char_u *p, *r, *s;
6951
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 len = (function ? 13 : 3);
6953 if (str != NULL)
6954 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006955 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00006956 for (p = str; *p != NUL; mb_ptr_adv(p))
6957 if (*p == '\'')
6958 ++len;
6959 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006960 s = r = alloc(len);
6961 if (r != NULL)
6962 {
6963 if (function)
6964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006965 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006966 r += 10;
6967 }
6968 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 if (str != NULL)
6971 for (p = str; *p != NUL; )
6972 {
6973 if (*p == '\'')
6974 *r++ = '\'';
6975 MB_COPY_CHAR(p, r);
6976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006978 if (function)
6979 *r++ = ')';
6980 *r++ = NUL;
6981 }
6982 return s;
6983}
6984
6985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 * Get the value of an environment variable.
6987 * "arg" is pointing to the '$'. It is advanced to after the name.
6988 * If the environment variable was not set, silently assume it is empty.
6989 * Always return OK.
6990 */
6991 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006992get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 int evaluate;
6996{
6997 char_u *string = NULL;
6998 int len;
6999 int cc;
7000 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007001 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
7003 ++*arg;
7004 name = *arg;
7005 len = get_env_len(arg);
7006 if (evaluate)
7007 {
7008 if (len != 0)
7009 {
7010 cc = name[len];
7011 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007012 /* first try vim_getenv(), fast for normal environment vars */
7013 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007015 {
7016 if (!mustfree)
7017 string = vim_strsave(string);
7018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 else
7020 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007021 if (mustfree)
7022 vim_free(string);
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 /* next try expanding things like $VIM and ${HOME} */
7025 string = expand_env_save(name - 1);
7026 if (string != NULL && *string == '$')
7027 {
7028 vim_free(string);
7029 string = NULL;
7030 }
7031 }
7032 name[len] = cc;
7033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007034 rettv->v_type = VAR_STRING;
7035 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037
7038 return OK;
7039}
7040
7041/*
7042 * Array with names and number of arguments of all internal functions
7043 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7044 */
7045static struct fst
7046{
7047 char *f_name; /* function name */
7048 char f_min_argc; /* minimal number of arguments */
7049 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007051 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052} functions[] =
7053{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007054 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {"append", 2, 2, f_append},
7056 {"argc", 0, 0, f_argc},
7057 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007058 {"argv", 0, 1, f_argv},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007060 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {"bufexists", 1, 1, f_bufexists},
7062 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7063 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7064 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7065 {"buflisted", 1, 1, f_buflisted},
7066 {"bufloaded", 1, 1, f_bufloaded},
7067 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007068 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 {"bufwinnr", 1, 1, f_bufwinnr},
7070 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007071 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007072 {"call", 2, 3, f_call},
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007073 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {"char2nr", 1, 1, f_char2nr},
7075 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007076 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007078#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007079 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007080 {"complete_add", 1, 1, f_complete_add},
7081 {"complete_check", 0, 0, f_complete_check},
7082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007084 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007085 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007087 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007088 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {"delete", 1, 1, f_delete},
7090 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007091 {"diff_filler", 1, 1, f_diff_filler},
7092 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007093 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007095 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {"eventhandler", 0, 0, f_eventhandler},
7097 {"executable", 1, 1, f_executable},
7098 {"exists", 1, 1, f_exists},
7099 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007100 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007101 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7103 {"filereadable", 1, 1, f_filereadable},
7104 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007105 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007106 {"finddir", 1, 3, f_finddir},
7107 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {"fnamemodify", 2, 2, f_fnamemodify},
7109 {"foldclosed", 1, 1, f_foldclosed},
7110 {"foldclosedend", 1, 1, f_foldclosedend},
7111 {"foldlevel", 1, 1, f_foldlevel},
7112 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007113 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007115 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007116 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007117 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007118 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {"getbufvar", 2, 2, f_getbufvar},
7120 {"getchar", 0, 1, f_getchar},
7121 {"getcharmod", 0, 0, f_getcharmod},
7122 {"getcmdline", 0, 0, f_getcmdline},
7123 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007124 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007126 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007127 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 {"getfsize", 1, 1, f_getfsize},
7129 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007130 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007131 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007132 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007133 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaara5525202006-03-02 22:52:09 +00007134 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007135 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007136 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007138 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {"getwinposx", 0, 0, f_getwinposx},
7140 {"getwinposy", 0, 0, f_getwinposy},
7141 {"getwinvar", 2, 2, f_getwinvar},
7142 {"glob", 1, 1, f_glob},
7143 {"globpath", 2, 2, f_globpath},
7144 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007145 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007146 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007147 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7149 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7150 {"histadd", 2, 2, f_histadd},
7151 {"histdel", 1, 2, f_histdel},
7152 {"histget", 1, 2, f_histget},
7153 {"histnr", 1, 1, f_histnr},
7154 {"hlID", 1, 1, f_hlID},
7155 {"hlexists", 1, 1, f_hlexists},
7156 {"hostname", 0, 0, f_hostname},
7157 {"iconv", 3, 3, f_iconv},
7158 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007159 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007160 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007162 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {"inputrestore", 0, 0, f_inputrestore},
7164 {"inputsave", 0, 0, f_inputsave},
7165 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007166 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007168 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007170 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007173 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {"libcall", 3, 3, f_libcall},
7175 {"libcallnr", 3, 3, f_libcallnr},
7176 {"line", 1, 1, f_line},
7177 {"line2byte", 1, 1, f_line2byte},
7178 {"lispindent", 1, 1, f_lispindent},
7179 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007180 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007181 {"maparg", 1, 3, f_maparg},
7182 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007183 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007184 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007185 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007186 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007187 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007188 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007189 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007190 {"max", 1, 1, f_max},
7191 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007192#ifdef vim_mkdir
7193 {"mkdir", 1, 3, f_mkdir},
7194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {"mode", 0, 0, f_mode},
7196 {"nextnonblank", 1, 1, f_nextnonblank},
7197 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007198 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007200 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007201 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007203 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007204 {"reltime", 0, 2, f_reltime},
7205 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {"remote_expr", 2, 3, f_remote_expr},
7207 {"remote_foreground", 1, 1, f_remote_foreground},
7208 {"remote_peek", 1, 2, f_remote_peek},
7209 {"remote_read", 1, 1, f_remote_read},
7210 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007211 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007213 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007215 {"reverse", 1, 1, f_reverse},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007216 {"search", 1, 3, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007217 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaareddf53b2006-02-27 00:11:10 +00007218 {"searchpair", 3, 6, f_searchpair},
7219 {"searchpairpos", 3, 6, f_searchpairpos},
7220 {"searchpos", 1, 3, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {"server2client", 2, 2, f_server2client},
7222 {"serverlist", 0, 0, f_serverlist},
7223 {"setbufvar", 3, 3, f_setbufvar},
7224 {"setcmdpos", 1, 1, f_setcmdpos},
7225 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007226 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007227 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007228 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007229 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007231 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar60a495f2006-10-03 12:44:42 +00007233 {"shellescape", 1, 1, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007235 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007236 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007237 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007238 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007239 {"split", 1, 3, f_split},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007240 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241#ifdef HAVE_STRFTIME
7242 {"strftime", 1, 2, f_strftime},
7243#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007245 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {"strlen", 1, 1, f_strlen},
7247 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007248 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {"strtrans", 1, 1, f_strtrans},
7250 {"submatch", 1, 1, f_submatch},
7251 {"substitute", 4, 4, f_substitute},
7252 {"synID", 3, 3, f_synID},
7253 {"synIDattr", 2, 3, f_synIDattr},
7254 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007255 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007256 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007257 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007259 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007260 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007262 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {"tolower", 1, 1, f_tolower},
7264 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007265 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {"virtcol", 1, 1, f_virtcol},
7269 {"visualmode", 0, 1, f_visualmode},
7270 {"winbufnr", 1, 1, f_winbufnr},
7271 {"wincol", 0, 0, f_wincol},
7272 {"winheight", 1, 1, f_winheight},
7273 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007274 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007276 {"winrestview", 1, 1, f_winrestview},
7277 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007279 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280};
7281
7282#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7283
7284/*
7285 * Function given to ExpandGeneric() to obtain the list of internal
7286 * or user defined function names.
7287 */
7288 char_u *
7289get_function_name(xp, idx)
7290 expand_T *xp;
7291 int idx;
7292{
7293 static int intidx = -1;
7294 char_u *name;
7295
7296 if (idx == 0)
7297 intidx = -1;
7298 if (intidx < 0)
7299 {
7300 name = get_user_func_name(xp, idx);
7301 if (name != NULL)
7302 return name;
7303 }
7304 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7305 {
7306 STRCPY(IObuff, functions[intidx].f_name);
7307 STRCAT(IObuff, "(");
7308 if (functions[intidx].f_max_argc == 0)
7309 STRCAT(IObuff, ")");
7310 return IObuff;
7311 }
7312
7313 return NULL;
7314}
7315
7316/*
7317 * Function given to ExpandGeneric() to obtain the list of internal or
7318 * user defined variable or function names.
7319 */
7320/*ARGSUSED*/
7321 char_u *
7322get_expr_name(xp, idx)
7323 expand_T *xp;
7324 int idx;
7325{
7326 static int intidx = -1;
7327 char_u *name;
7328
7329 if (idx == 0)
7330 intidx = -1;
7331 if (intidx < 0)
7332 {
7333 name = get_function_name(xp, idx);
7334 if (name != NULL)
7335 return name;
7336 }
7337 return get_user_var_name(xp, ++intidx);
7338}
7339
7340#endif /* FEAT_CMDL_COMPL */
7341
7342/*
7343 * Find internal function in table above.
7344 * Return index, or -1 if not found
7345 */
7346 static int
7347find_internal_func(name)
7348 char_u *name; /* name of the function */
7349{
7350 int first = 0;
7351 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7352 int cmp;
7353 int x;
7354
7355 /*
7356 * Find the function name in the table. Binary search.
7357 */
7358 while (first <= last)
7359 {
7360 x = first + ((unsigned)(last - first) >> 1);
7361 cmp = STRCMP(name, functions[x].f_name);
7362 if (cmp < 0)
7363 last = x - 1;
7364 else if (cmp > 0)
7365 first = x + 1;
7366 else
7367 return x;
7368 }
7369 return -1;
7370}
7371
7372/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007373 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7374 * name it contains, otherwise return "name".
7375 */
7376 static char_u *
7377deref_func_name(name, lenp)
7378 char_u *name;
7379 int *lenp;
7380{
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 int cc;
7383
7384 cc = name[*lenp];
7385 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007386 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007387 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007389 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 {
7392 *lenp = 0;
7393 return (char_u *)""; /* just in case */
7394 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007395 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007397 }
7398
7399 return name;
7400}
7401
7402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 * Allocate a variable for the result of a function.
7404 * Return OK or FAIL.
7405 */
7406 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7408 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 char_u *name; /* name of the function */
7410 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 char_u **arg; /* argument, pointing to the '(' */
7413 linenr_T firstline; /* first line of range */
7414 linenr_T lastline; /* last line of range */
7415 int *doesrange; /* return: function handled range */
7416 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
7419 char_u *argp;
7420 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007421 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 int argcount = 0; /* number of arguments found */
7423
7424 /*
7425 * Get the arguments.
7426 */
7427 argp = *arg;
7428 while (argcount < MAX_FUNC_ARGS)
7429 {
7430 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7431 if (*argp == ')' || *argp == ',' || *argp == NUL)
7432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7434 {
7435 ret = FAIL;
7436 break;
7437 }
7438 ++argcount;
7439 if (*argp != ',')
7440 break;
7441 }
7442 if (*argp == ')')
7443 ++argp;
7444 else
7445 ret = FAIL;
7446
7447 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007448 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 {
7452 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007455 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007459 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 *arg = skipwhite(argp);
7462 return ret;
7463}
7464
7465
7466/*
7467 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007468 * Return OK when the function can't be called, FAIL otherwise.
7469 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 */
7471 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007473 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 char_u *name; /* name of the function */
7475 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007476 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007478 typval_T *argvars; /* vars for arguments, must have "argcount"
7479 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 linenr_T firstline; /* first line of range */
7481 linenr_T lastline; /* last line of range */
7482 int *doesrange; /* return: function handled range */
7483 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487#define ERROR_UNKNOWN 0
7488#define ERROR_TOOMANY 1
7489#define ERROR_TOOFEW 2
7490#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491#define ERROR_DICT 4
7492#define ERROR_NONE 5
7493#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 int error = ERROR_NONE;
7495 int i;
7496 int llen;
7497 ufunc_T *fp;
7498 int cc;
7499#define FLEN_FIXED 40
7500 char_u fname_buf[FLEN_FIXED + 1];
7501 char_u *fname;
7502
7503 /*
7504 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7505 * Change <SNR>123_name() to K_SNR 123_name().
7506 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7507 */
7508 cc = name[len];
7509 name[len] = NUL;
7510 llen = eval_fname_script(name);
7511 if (llen > 0)
7512 {
7513 fname_buf[0] = K_SPECIAL;
7514 fname_buf[1] = KS_EXTRA;
7515 fname_buf[2] = (int)KE_SNR;
7516 i = 3;
7517 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7518 {
7519 if (current_SID <= 0)
7520 error = ERROR_SCRIPT;
7521 else
7522 {
7523 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7524 i = (int)STRLEN(fname_buf);
7525 }
7526 }
7527 if (i + STRLEN(name + llen) < FLEN_FIXED)
7528 {
7529 STRCPY(fname_buf + i, name + llen);
7530 fname = fname_buf;
7531 }
7532 else
7533 {
7534 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7535 if (fname == NULL)
7536 error = ERROR_OTHER;
7537 else
7538 {
7539 mch_memmove(fname, fname_buf, (size_t)i);
7540 STRCPY(fname + i, name + llen);
7541 }
7542 }
7543 }
7544 else
7545 fname = name;
7546
7547 *doesrange = FALSE;
7548
7549
7550 /* execute the function if no errors detected and executing */
7551 if (evaluate && error == ERROR_NONE)
7552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 error = ERROR_UNKNOWN;
7555
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007556 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {
7558 /*
7559 * User defined function.
7560 */
7561 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007562
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007564 /* Trigger FuncUndefined event, may load the function. */
7565 if (fp == NULL
7566 && apply_autocmds(EVENT_FUNCUNDEFINED,
7567 fname, fname, TRUE, NULL)
7568 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007570 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 fp = find_func(fname);
7572 }
7573#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007574 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007575 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007576 {
7577 /* loaded a package, search for the function again */
7578 fp = find_func(fname);
7579 }
7580
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (fp != NULL)
7582 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007583 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007585 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007587 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007589 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007590 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 else
7592 {
7593 /*
7594 * Call the user function.
7595 * Save and restore search patterns, script variables and
7596 * redo buffer.
7597 */
7598 save_search_patterns();
7599 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007600 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007601 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007602 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007603 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7604 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7605 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007606 /* Function was unreferenced while being used, free it
7607 * now. */
7608 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 restoreRedobuff();
7610 restore_search_patterns();
7611 error = ERROR_NONE;
7612 }
7613 }
7614 }
7615 else
7616 {
7617 /*
7618 * Find the function name in the table, call its implementation.
7619 */
7620 i = find_internal_func(fname);
7621 if (i >= 0)
7622 {
7623 if (argcount < functions[i].f_min_argc)
7624 error = ERROR_TOOFEW;
7625 else if (argcount > functions[i].f_max_argc)
7626 error = ERROR_TOOMANY;
7627 else
7628 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007629 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 error = ERROR_NONE;
7632 }
7633 }
7634 }
7635 /*
7636 * The function call (or "FuncUndefined" autocommand sequence) might
7637 * have been aborted by an error, an interrupt, or an explicitly thrown
7638 * exception that has not been caught so far. This situation can be
7639 * tested for by calling aborting(). For an error in an internal
7640 * function or for the "E132" error in call_user_func(), however, the
7641 * throw point at which the "force_abort" flag (temporarily reset by
7642 * emsg()) is normally updated has not been reached yet. We need to
7643 * update that flag first to make aborting() reliable.
7644 */
7645 update_force_abort();
7646 }
7647 if (error == ERROR_NONE)
7648 ret = OK;
7649
7650 /*
7651 * Report an error unless the argument evaluation or function call has been
7652 * cancelled due to an aborting error, an interrupt, or an exception.
7653 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 if (!aborting())
7655 {
7656 switch (error)
7657 {
7658 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007659 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 break;
7661 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007662 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 break;
7664 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007665 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 name);
7667 break;
7668 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007669 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 name);
7671 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007672 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007673 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674 name);
7675 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 }
7677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
7679 name[len] = cc;
7680 if (fname != name && fname != fname_buf)
7681 vim_free(fname);
7682
7683 return ret;
7684}
7685
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007686/*
7687 * Give an error message with a function name. Handle <SNR> things.
7688 */
7689 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00007690emsg_funcname(ermsg, name)
7691 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007692 char_u *name;
7693{
7694 char_u *p;
7695
7696 if (*name == K_SPECIAL)
7697 p = concat_str((char_u *)"<SNR>", name + 3);
7698 else
7699 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007700 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007701 if (p != name)
7702 vim_free(p);
7703}
7704
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705/*********************************************
7706 * Implementation of the built-in functions
7707 */
7708
7709/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007710 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 */
7712 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007713f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 typval_T *argvars;
7715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007719 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007720 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007722 if ((l = argvars[0].vval.v_list) != NULL
7723 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7724 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007725 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726 }
7727 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007728 EMSG(_(e_listreq));
7729}
7730
7731/*
7732 * "append(lnum, string/list)" function
7733 */
7734 static void
7735f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 typval_T *argvars;
7737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007738{
7739 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007740 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 list_T *l = NULL;
7742 listitem_T *li = NULL;
7743 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007744 long added = 0;
7745
Bram Moolenaar0d660222005-01-07 21:51:51 +00007746 lnum = get_tv_lnum(argvars);
7747 if (lnum >= 0
7748 && lnum <= curbuf->b_ml.ml_line_count
7749 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007750 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007751 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007753 l = argvars[1].vval.v_list;
7754 if (l == NULL)
7755 return;
7756 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007757 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007758 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007759 for (;;)
7760 {
7761 if (l == NULL)
7762 tv = &argvars[1]; /* append a string */
7763 else if (li == NULL)
7764 break; /* end of list */
7765 else
7766 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007767 line = get_tv_string_chk(tv);
7768 if (line == NULL) /* type error */
7769 {
7770 rettv->vval.v_number = 1; /* Failed */
7771 break;
7772 }
7773 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007774 ++added;
7775 if (l == NULL)
7776 break;
7777 li = li->li_next;
7778 }
7779
7780 appended_lines_mark(lnum, added);
7781 if (curwin->w_cursor.lnum > lnum)
7782 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007784 else
7785 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786}
7787
7788/*
7789 * "argc()" function
7790 */
7791/* ARGSUSED */
7792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007793f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007794 typval_T *argvars;
7795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007797 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
7800/*
7801 * "argidx()" function
7802 */
7803/* ARGSUSED */
7804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007806 typval_T *argvars;
7807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810}
7811
7812/*
7813 * "argv(nr)" function
7814 */
7815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007817 typval_T *argvars;
7818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 int idx;
7821
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007822 if (argvars[0].v_type != VAR_UNKNOWN)
7823 {
7824 idx = get_tv_number_chk(&argvars[0], NULL);
7825 if (idx >= 0 && idx < ARGCOUNT)
7826 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
7827 else
7828 rettv->vval.v_string = NULL;
7829 rettv->v_type = VAR_STRING;
7830 }
7831 else if (rettv_list_alloc(rettv) == OK)
7832 for (idx = 0; idx < ARGCOUNT; ++idx)
7833 list_append_string(rettv->vval.v_list,
7834 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835}
7836
7837/*
7838 * "browse(save, title, initdir, default)" function
7839 */
7840/* ARGSUSED */
7841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 typval_T *argvars;
7844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846#ifdef FEAT_BROWSE
7847 int save;
7848 char_u *title;
7849 char_u *initdir;
7850 char_u *defname;
7851 char_u buf[NUMBUFLEN];
7852 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007853 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 save = get_tv_number_chk(&argvars[0], &error);
7856 title = get_tv_string_chk(&argvars[1]);
7857 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7858 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007860 if (error || title == NULL || initdir == NULL || defname == NULL)
7861 rettv->vval.v_string = NULL;
7862 else
7863 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007864 do_browse(save ? BROWSE_SAVE : 0,
7865 title, defname, NULL, initdir, NULL, curbuf);
7866#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007870}
7871
7872/*
7873 * "browsedir(title, initdir)" function
7874 */
7875/* ARGSUSED */
7876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 typval_T *argvars;
7879 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007880{
7881#ifdef FEAT_BROWSE
7882 char_u *title;
7883 char_u *initdir;
7884 char_u buf[NUMBUFLEN];
7885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007886 title = get_tv_string_chk(&argvars[0]);
7887 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007889 if (title == NULL || initdir == NULL)
7890 rettv->vval.v_string = NULL;
7891 else
7892 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007893 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898}
7899
Bram Moolenaar33570922005-01-25 22:26:29 +00007900static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007901
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902/*
7903 * Find a buffer by number or exact name.
7904 */
7905 static buf_T *
7906find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
7909 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 if (avar->v_type == VAR_NUMBER)
7912 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007913 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007916 if (buf == NULL)
7917 {
7918 /* No full path name match, try a match with a URL or a "nofile"
7919 * buffer, these don't use the full path. */
7920 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7921 if (buf->b_fname != NULL
7922 && (path_with_url(buf->b_fname)
7923#ifdef FEAT_QUICKFIX
7924 || bt_nofile(buf)
7925#endif
7926 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007928 break;
7929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 }
7931 return buf;
7932}
7933
7934/*
7935 * "bufexists(expr)" function
7936 */
7937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007938f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 typval_T *argvars;
7940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007942 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943}
7944
7945/*
7946 * "buflisted(expr)" function
7947 */
7948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007950 typval_T *argvars;
7951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 buf_T *buf;
7954
7955 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957}
7958
7959/*
7960 * "bufloaded(expr)" function
7961 */
7962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007963f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 typval_T *argvars;
7965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 buf_T *buf;
7968
7969 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971}
7972
Bram Moolenaar33570922005-01-25 22:26:29 +00007973static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007974
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975/*
7976 * Get buffer by number or pattern.
7977 */
7978 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007982 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 int save_magic;
7984 char_u *save_cpo;
7985 buf_T *buf;
7986
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007987 if (tv->v_type == VAR_NUMBER)
7988 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007989 if (tv->v_type != VAR_STRING)
7990 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 if (name == NULL || *name == NUL)
7992 return curbuf;
7993 if (name[0] == '$' && name[1] == NUL)
7994 return lastbuf;
7995
7996 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7997 save_magic = p_magic;
7998 p_magic = TRUE;
7999 save_cpo = p_cpo;
8000 p_cpo = (char_u *)"";
8001
8002 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8003 TRUE, FALSE));
8004
8005 p_magic = save_magic;
8006 p_cpo = save_cpo;
8007
8008 /* If not found, try expanding the name, like done for bufexists(). */
8009 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
8012 return buf;
8013}
8014
8015/*
8016 * "bufname(expr)" function
8017 */
8018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 typval_T *argvars;
8021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022{
8023 buf_T *buf;
8024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008025 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008027 buf = get_buf_tv(&argvars[0]);
8028 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008030 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008032 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 --emsg_off;
8034}
8035
8036/*
8037 * "bufnr(expr)" function
8038 */
8039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008040f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 typval_T *argvars;
8042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008045 int error = FALSE;
8046 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008050 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008051 --emsg_off;
8052
8053 /* If the buffer isn't found and the second argument is not zero create a
8054 * new buffer. */
8055 if (buf == NULL
8056 && argvars[1].v_type != VAR_UNKNOWN
8057 && get_tv_number_chk(&argvars[1], &error) != 0
8058 && !error
8059 && (name = get_tv_string_chk(&argvars[0])) != NULL
8060 && !error)
8061 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8062
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008064 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008066 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
8069/*
8070 * "bufwinnr(nr)" function
8071 */
8072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 typval_T *argvars;
8075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077#ifdef FEAT_WINDOWS
8078 win_T *wp;
8079 int winnr = 0;
8080#endif
8081 buf_T *buf;
8082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008083 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008085 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086#ifdef FEAT_WINDOWS
8087 for (wp = firstwin; wp; wp = wp->w_next)
8088 {
8089 ++winnr;
8090 if (wp->w_buffer == buf)
8091 break;
8092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008093 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008095 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#endif
8097 --emsg_off;
8098}
8099
8100/*
8101 * "byte2line(byte)" function
8102 */
8103/*ARGSUSED*/
8104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008106 typval_T *argvars;
8107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111#else
8112 long boff = 0;
8113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008114 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008118 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 (linenr_T)0, &boff);
8120#endif
8121}
8122
8123/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008124 * "byteidx()" function
8125 */
8126/*ARGSUSED*/
8127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008128f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008129 typval_T *argvars;
8130 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008131{
8132#ifdef FEAT_MBYTE
8133 char_u *t;
8134#endif
8135 char_u *str;
8136 long idx;
8137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008138 str = get_tv_string_chk(&argvars[0]);
8139 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008140 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008141 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008142 return;
8143
8144#ifdef FEAT_MBYTE
8145 t = str;
8146 for ( ; idx > 0; idx--)
8147 {
8148 if (*t == NUL) /* EOL reached */
8149 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008150 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008151 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008152 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008153#else
8154 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008155 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008156#endif
8157}
8158
8159/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008160 * "call(func, arglist)" function
8161 */
8162 static void
8163f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008164 typval_T *argvars;
8165 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008166{
8167 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008168 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008169 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008171 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008173
8174 rettv->vval.v_number = 0;
8175 if (argvars[1].v_type != VAR_LIST)
8176 {
8177 EMSG(_(e_listreq));
8178 return;
8179 }
8180 if (argvars[1].vval.v_list == NULL)
8181 return;
8182
8183 if (argvars[0].v_type == VAR_FUNC)
8184 func = argvars[0].vval.v_string;
8185 else
8186 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008187 if (*func == NUL)
8188 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008189
Bram Moolenaare9a41262005-01-15 22:18:47 +00008190 if (argvars[2].v_type != VAR_UNKNOWN)
8191 {
8192 if (argvars[2].v_type != VAR_DICT)
8193 {
8194 EMSG(_(e_dictreq));
8195 return;
8196 }
8197 selfdict = argvars[2].vval.v_dict;
8198 }
8199
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008200 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8201 item = item->li_next)
8202 {
8203 if (argc == MAX_FUNC_ARGS)
8204 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008205 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008206 break;
8207 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008208 /* Make a copy of each argument. This is needed to be able to set
8209 * v_lock to VAR_FIXED in the copy without changing the original list.
8210 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008211 copy_tv(&item->li_tv, &argv[argc++]);
8212 }
8213
8214 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008215 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008216 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8217 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008218
8219 /* Free the arguments. */
8220 while (argc > 0)
8221 clear_tv(&argv[--argc]);
8222}
8223
8224/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008225 * "changenr()" function
8226 */
8227/*ARGSUSED*/
8228 static void
8229f_changenr(argvars, rettv)
8230 typval_T *argvars;
8231 typval_T *rettv;
8232{
8233 rettv->vval.v_number = curbuf->b_u_seq_cur;
8234}
8235
8236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 * "char2nr(string)" function
8238 */
8239 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008240f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008241 typval_T *argvars;
8242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243{
8244#ifdef FEAT_MBYTE
8245 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008246 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 else
8248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008249 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250}
8251
8252/*
8253 * "cindent(lnum)" function
8254 */
8255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008256f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008257 typval_T *argvars;
8258 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259{
8260#ifdef FEAT_CINDENT
8261 pos_T pos;
8262 linenr_T lnum;
8263
8264 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008265 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8267 {
8268 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 curwin->w_cursor = pos;
8271 }
8272 else
8273#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008274 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275}
8276
8277/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008278 * "clearmatches()" function
8279 */
8280/*ARGSUSED*/
8281 static void
8282f_clearmatches(argvars, rettv)
8283 typval_T *argvars;
8284 typval_T *rettv;
8285{
8286#ifdef FEAT_SEARCH_EXTRA
8287 clear_matches(curwin);
8288#endif
8289}
8290
8291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 * "col(string)" function
8293 */
8294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008295f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 typval_T *argvars;
8297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298{
8299 colnr_T col = 0;
8300 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008301 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008303 fp = var2fpos(&argvars[0], FALSE, &fnum);
8304 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {
8306 if (fp->col == MAXCOL)
8307 {
8308 /* '> can be MAXCOL, get the length of the line then */
8309 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008310 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 else
8312 col = MAXCOL;
8313 }
8314 else
8315 {
8316 col = fp->col + 1;
8317#ifdef FEAT_VIRTUALEDIT
8318 /* col(".") when the cursor is on the NUL at the end of the line
8319 * because of "coladd" can be seen as an extra column. */
8320 if (virtual_active() && fp == &curwin->w_cursor)
8321 {
8322 char_u *p = ml_get_cursor();
8323
8324 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8325 curwin->w_virtcol - curwin->w_cursor.coladd))
8326 {
8327# ifdef FEAT_MBYTE
8328 int l;
8329
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008330 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 col += l;
8332# else
8333 if (*p != NUL && p[1] == NUL)
8334 ++col;
8335# endif
8336 }
8337 }
8338#endif
8339 }
8340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342}
8343
Bram Moolenaar572cb562005-08-05 21:35:02 +00008344#if defined(FEAT_INS_EXPAND)
8345/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008346 * "complete()" function
8347 */
8348/*ARGSUSED*/
8349 static void
8350f_complete(argvars, rettv)
8351 typval_T *argvars;
8352 typval_T *rettv;
8353{
8354 int startcol;
8355
8356 if ((State & INSERT) == 0)
8357 {
8358 EMSG(_("E785: complete() can only be used in Insert mode"));
8359 return;
8360 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008361
8362 /* Check for undo allowed here, because if something was already inserted
8363 * the line was already saved for undo and this check isn't done. */
8364 if (!undo_allowed())
8365 return;
8366
Bram Moolenaarade00832006-03-10 21:46:58 +00008367 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8368 {
8369 EMSG(_(e_invarg));
8370 return;
8371 }
8372
8373 startcol = get_tv_number_chk(&argvars[0], NULL);
8374 if (startcol <= 0)
8375 return;
8376
8377 set_completion(startcol - 1, argvars[1].vval.v_list);
8378}
8379
8380/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00008381 * "complete_add()" function
8382 */
8383/*ARGSUSED*/
8384 static void
8385f_complete_add(argvars, rettv)
8386 typval_T *argvars;
8387 typval_T *rettv;
8388{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00008389 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00008390}
8391
8392/*
8393 * "complete_check()" function
8394 */
8395/*ARGSUSED*/
8396 static void
8397f_complete_check(argvars, rettv)
8398 typval_T *argvars;
8399 typval_T *rettv;
8400{
8401 int saved = RedrawingDisabled;
8402
8403 RedrawingDisabled = 0;
8404 ins_compl_check_keys(0);
8405 rettv->vval.v_number = compl_interrupted;
8406 RedrawingDisabled = saved;
8407}
8408#endif
8409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410/*
8411 * "confirm(message, buttons[, default [, type]])" function
8412 */
8413/*ARGSUSED*/
8414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008416 typval_T *argvars;
8417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8420 char_u *message;
8421 char_u *buttons = NULL;
8422 char_u buf[NUMBUFLEN];
8423 char_u buf2[NUMBUFLEN];
8424 int def = 1;
8425 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008426 char_u *typestr;
8427 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008429 message = get_tv_string_chk(&argvars[0]);
8430 if (message == NULL)
8431 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008432 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008434 buttons = get_tv_string_buf_chk(&argvars[1], buf);
8435 if (buttons == NULL)
8436 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008439 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008440 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
8443 if (typestr == NULL)
8444 error = TRUE;
8445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008447 switch (TOUPPER_ASC(*typestr))
8448 {
8449 case 'E': type = VIM_ERROR; break;
8450 case 'Q': type = VIM_QUESTION; break;
8451 case 'I': type = VIM_INFO; break;
8452 case 'W': type = VIM_WARNING; break;
8453 case 'G': type = VIM_GENERIC; break;
8454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 }
8456 }
8457 }
8458 }
8459
8460 if (buttons == NULL || *buttons == NUL)
8461 buttons = (char_u *)_("&Ok");
8462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008463 if (error)
8464 rettv->vval.v_number = 0;
8465 else
8466 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 def, NULL);
8468#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008469 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#endif
8471}
8472
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008473/*
8474 * "copy()" function
8475 */
8476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008477f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008478 typval_T *argvars;
8479 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008480{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008481 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008485 * "count()" function
8486 */
8487 static void
8488f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008489 typval_T *argvars;
8490 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008491{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008492 long n = 0;
8493 int ic = FALSE;
8494
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008496 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008497 listitem_T *li;
8498 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008500
Bram Moolenaare9a41262005-01-15 22:18:47 +00008501 if ((l = argvars[0].vval.v_list) != NULL)
8502 {
8503 li = l->lv_first;
8504 if (argvars[2].v_type != VAR_UNKNOWN)
8505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008506 int error = FALSE;
8507
8508 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008509 if (argvars[3].v_type != VAR_UNKNOWN)
8510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008511 idx = get_tv_number_chk(&argvars[3], &error);
8512 if (!error)
8513 {
8514 li = list_find(l, idx);
8515 if (li == NULL)
8516 EMSGN(_(e_listidx), idx);
8517 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008518 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008519 if (error)
8520 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008521 }
8522
8523 for ( ; li != NULL; li = li->li_next)
8524 if (tv_equal(&li->li_tv, &argvars[1], ic))
8525 ++n;
8526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008528 else if (argvars[0].v_type == VAR_DICT)
8529 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 int todo;
8531 dict_T *d;
8532 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008534 if ((d = argvars[0].vval.v_dict) != NULL)
8535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008536 int error = FALSE;
8537
Bram Moolenaare9a41262005-01-15 22:18:47 +00008538 if (argvars[2].v_type != VAR_UNKNOWN)
8539 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008540 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008541 if (argvars[3].v_type != VAR_UNKNOWN)
8542 EMSG(_(e_invarg));
8543 }
8544
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008545 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008547 {
8548 if (!HASHITEM_EMPTY(hi))
8549 {
8550 --todo;
8551 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8552 ++n;
8553 }
8554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008555 }
8556 }
8557 else
8558 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008559 rettv->vval.v_number = n;
8560}
8561
8562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8564 *
8565 * Checks the existence of a cscope connection.
8566 */
8567/*ARGSUSED*/
8568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008570 typval_T *argvars;
8571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572{
8573#ifdef FEAT_CSCOPE
8574 int num = 0;
8575 char_u *dbpath = NULL;
8576 char_u *prepend = NULL;
8577 char_u buf[NUMBUFLEN];
8578
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008579 if (argvars[0].v_type != VAR_UNKNOWN
8580 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 num = (int)get_tv_number(&argvars[0]);
8583 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591#endif
8592}
8593
8594/*
8595 * "cursor(lnum, col)" function
8596 *
8597 * Moves the cursor to the specified line and column
8598 */
8599/*ARGSUSED*/
8600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008601f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008602 typval_T *argvars;
8603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008606#ifdef FEAT_VIRTUALEDIT
8607 long coladd = 0;
8608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaara5525202006-03-02 22:52:09 +00008610 if (argvars[1].v_type == VAR_UNKNOWN)
8611 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008612 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00008613
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008614 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00008615 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 line = pos.lnum;
8617 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00008618#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008619 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00008620#endif
8621 }
8622 else
8623 {
8624 line = get_tv_lnum(argvars);
8625 col = get_tv_number_chk(&argvars[1], NULL);
8626#ifdef FEAT_VIRTUALEDIT
8627 if (argvars[2].v_type != VAR_UNKNOWN)
8628 coladd = get_tv_number_chk(&argvars[2], NULL);
8629#endif
8630 }
8631 if (line < 0 || col < 0
8632#ifdef FEAT_VIRTUALEDIT
8633 || coladd < 0
8634#endif
8635 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 if (line > 0)
8638 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 if (col > 0)
8640 curwin->w_cursor.col = col - 1;
8641#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00008642 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#endif
8644
8645 /* Make sure the cursor is in a valid position. */
8646 check_cursor();
8647#ifdef FEAT_MBYTE
8648 /* Correct cursor for multi-byte character. */
8649 if (has_mbyte)
8650 mb_adjust_cursor();
8651#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008652
8653 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654}
8655
8656/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008657 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 */
8659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008660f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 typval_T *argvars;
8662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008664 int noref = 0;
8665
8666 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008668 if (noref < 0 || noref > 1)
8669 EMSG(_(e_invarg));
8670 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008671 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672}
8673
8674/*
8675 * "delete()" function
8676 */
8677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008678f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 typval_T *argvars;
8680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008685 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686}
8687
8688/*
8689 * "did_filetype()" function
8690 */
8691/*ARGSUSED*/
8692 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *argvars;
8695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#endif
8702}
8703
8704/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008705 * "diff_filler()" function
8706 */
8707/*ARGSUSED*/
8708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008709f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008710 typval_T *argvars;
8711 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008712{
8713#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008714 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008715#endif
8716}
8717
8718/*
8719 * "diff_hlID()" function
8720 */
8721/*ARGSUSED*/
8722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008726{
8727#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008729 static linenr_T prev_lnum = 0;
8730 static int changedtick = 0;
8731 static int fnum = 0;
8732 static int change_start = 0;
8733 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00008734 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008735 int filler_lines;
8736 int col;
8737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008738 if (lnum < 0) /* ignore type error in {lnum} arg */
8739 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008740 if (lnum != prev_lnum
8741 || changedtick != curbuf->b_changedtick
8742 || fnum != curbuf->b_fnum)
8743 {
8744 /* New line, buffer, change: need to get the values. */
8745 filler_lines = diff_check(curwin, lnum);
8746 if (filler_lines < 0)
8747 {
8748 if (filler_lines == -1)
8749 {
8750 change_start = MAXCOL;
8751 change_end = -1;
8752 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8753 hlID = HLF_ADD; /* added line */
8754 else
8755 hlID = HLF_CHD; /* changed line */
8756 }
8757 else
8758 hlID = HLF_ADD; /* added line */
8759 }
8760 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008761 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008762 prev_lnum = lnum;
8763 changedtick = curbuf->b_changedtick;
8764 fnum = curbuf->b_fnum;
8765 }
8766
8767 if (hlID == HLF_CHD || hlID == HLF_TXD)
8768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008770 if (col >= change_start && col <= change_end)
8771 hlID = HLF_TXD; /* changed text */
8772 else
8773 hlID = HLF_CHD; /* changed line */
8774 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008775 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008776#endif
8777}
8778
8779/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008780 * "empty({expr})" function
8781 */
8782 static void
8783f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008784 typval_T *argvars;
8785 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008786{
8787 int n;
8788
8789 switch (argvars[0].v_type)
8790 {
8791 case VAR_STRING:
8792 case VAR_FUNC:
8793 n = argvars[0].vval.v_string == NULL
8794 || *argvars[0].vval.v_string == NUL;
8795 break;
8796 case VAR_NUMBER:
8797 n = argvars[0].vval.v_number == 0;
8798 break;
8799 case VAR_LIST:
8800 n = argvars[0].vval.v_list == NULL
8801 || argvars[0].vval.v_list->lv_first == NULL;
8802 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008803 case VAR_DICT:
8804 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008805 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008806 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008807 default:
8808 EMSG2(_(e_intern2), "f_empty()");
8809 n = 0;
8810 }
8811
8812 rettv->vval.v_number = n;
8813}
8814
8815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 * "escape({string}, {chars})" function
8817 */
8818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008820 typval_T *argvars;
8821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
8823 char_u buf[NUMBUFLEN];
8824
Bram Moolenaar758711c2005-02-02 23:11:38 +00008825 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8826 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828}
8829
8830/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008831 * "eval()" function
8832 */
8833/*ARGSUSED*/
8834 static void
8835f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *argvars;
8837 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008838{
8839 char_u *s;
8840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008841 s = get_tv_string_chk(&argvars[0]);
8842 if (s != NULL)
8843 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8846 {
8847 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008848 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008849 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008850 else if (*s != NUL)
8851 EMSG(_(e_trailing));
8852}
8853
8854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 * "eventhandler()" function
8856 */
8857/*ARGSUSED*/
8858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008860 typval_T *argvars;
8861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864}
8865
8866/*
8867 * "executable()" function
8868 */
8869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008871 typval_T *argvars;
8872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875}
8876
8877/*
8878 * "exists()" function
8879 */
8880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008881f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008882 typval_T *argvars;
8883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885 char_u *p;
8886 char_u *name;
8887 int n = FALSE;
8888 int len = 0;
8889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 if (*p == '$') /* environment variable */
8892 {
8893 /* first try "normal" environment variables (fast) */
8894 if (mch_getenv(p + 1) != NULL)
8895 n = TRUE;
8896 else
8897 {
8898 /* try expanding things like $VIM and ${HOME} */
8899 p = expand_env_save(p);
8900 if (p != NULL && *p != '$')
8901 n = TRUE;
8902 vim_free(p);
8903 }
8904 }
8905 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00008906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00008908 if (*skipwhite(p) != NUL)
8909 n = FALSE; /* trailing garbage */
8910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 else if (*p == '*') /* internal or user defined function */
8912 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008913 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915 else if (*p == ':')
8916 {
8917 n = cmd_exists(p + 1);
8918 }
8919 else if (*p == '#')
8920 {
8921#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00008922 if (p[1] == '#')
8923 n = autocmd_supported(p + 2);
8924 else
8925 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#endif
8927 }
8928 else /* internal variable */
8929 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008930 char_u *tofree;
8931 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008933 /* get_name_len() takes care of expanding curly braces */
8934 name = p;
8935 len = get_name_len(&p, &tofree, TRUE, FALSE);
8936 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008938 if (tofree != NULL)
8939 name = tofree;
8940 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8941 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008943 /* handle d.key, l[idx], f(expr) */
8944 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8945 if (n)
8946 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 }
8948 }
Bram Moolenaar79783442006-05-05 21:18:03 +00008949 if (*p != NUL)
8950 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008952 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 }
8954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956}
8957
8958/*
8959 * "expand()" function
8960 */
8961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *argvars;
8964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
8966 char_u *s;
8967 int len;
8968 char_u *errormsg;
8969 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8970 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008971 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->v_type = VAR_STRING;
8974 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (*s == '%' || *s == '#' || *s == '<')
8976 {
8977 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008978 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 --emsg_off;
8980 }
8981 else
8982 {
8983 /* When the optional second argument is non-zero, don't remove matches
8984 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008985 if (argvars[1].v_type != VAR_UNKNOWN
8986 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008988 if (!error)
8989 {
8990 ExpandInit(&xpc);
8991 xpc.xp_context = EXPAND_FILES;
8992 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008993 }
8994 else
8995 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 }
8997}
8998
8999/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009000 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009001 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009002 */
9003 static void
9004f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009007{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009008 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009009 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 list_T *l1, *l2;
9012 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009015
Bram Moolenaare9a41262005-01-15 22:18:47 +00009016 l1 = argvars[0].vval.v_list;
9017 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009018 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9019 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009020 {
9021 if (argvars[2].v_type != VAR_UNKNOWN)
9022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009023 before = get_tv_number_chk(&argvars[2], &error);
9024 if (error)
9025 return; /* type error; errmsg already given */
9026
Bram Moolenaar758711c2005-02-02 23:11:38 +00009027 if (before == l1->lv_len)
9028 item = NULL;
9029 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009030 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009031 item = list_find(l1, before);
9032 if (item == NULL)
9033 {
9034 EMSGN(_(e_listidx), before);
9035 return;
9036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 }
9038 }
9039 else
9040 item = NULL;
9041 list_extend(l1, l2, item);
9042
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 copy_tv(&argvars[0], rettv);
9044 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009046 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9047 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009048 dict_T *d1, *d2;
9049 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009050 char_u *action;
9051 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009053 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054
9055 d1 = argvars[0].vval.v_dict;
9056 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009057 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9058 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009059 {
9060 /* Check the third argument. */
9061 if (argvars[2].v_type != VAR_UNKNOWN)
9062 {
9063 static char *(av[]) = {"keep", "force", "error"};
9064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 action = get_tv_string_chk(&argvars[2]);
9066 if (action == NULL)
9067 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009068 for (i = 0; i < 3; ++i)
9069 if (STRCMP(action, av[i]) == 0)
9070 break;
9071 if (i == 3)
9072 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009073 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009074 return;
9075 }
9076 }
9077 else
9078 action = (char_u *)"force";
9079
9080 /* Go over all entries in the second dict and add them to the
9081 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009082 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009085 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009087 --todo;
9088 di1 = dict_find(d1, hi2->hi_key, -1);
9089 if (di1 == NULL)
9090 {
9091 di1 = dictitem_copy(HI2DI(hi2));
9092 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9093 dictitem_free(di1);
9094 }
9095 else if (*action == 'e')
9096 {
9097 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9098 break;
9099 }
9100 else if (*action == 'f')
9101 {
9102 clear_tv(&di1->di_tv);
9103 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9104 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009105 }
9106 }
9107
Bram Moolenaare9a41262005-01-15 22:18:47 +00009108 copy_tv(&argvars[0], rettv);
9109 }
9110 }
9111 else
9112 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009113}
9114
9115/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009116 * "feedkeys()" function
9117 */
9118/*ARGSUSED*/
9119 static void
9120f_feedkeys(argvars, rettv)
9121 typval_T *argvars;
9122 typval_T *rettv;
9123{
9124 int remap = TRUE;
9125 char_u *keys, *flags;
9126 char_u nbuf[NUMBUFLEN];
9127 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009128 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009129
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009130 /* This is not allowed in the sandbox. If the commands would still be
9131 * executed in the sandbox it would be OK, but it probably happens later,
9132 * when "sandbox" is no longer set. */
9133 if (check_secure())
9134 return;
9135
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009136 rettv->vval.v_number = 0;
9137 keys = get_tv_string(&argvars[0]);
9138 if (*keys != NUL)
9139 {
9140 if (argvars[1].v_type != VAR_UNKNOWN)
9141 {
9142 flags = get_tv_string_buf(&argvars[1], nbuf);
9143 for ( ; *flags != NUL; ++flags)
9144 {
9145 switch (*flags)
9146 {
9147 case 'n': remap = FALSE; break;
9148 case 'm': remap = TRUE; break;
9149 case 't': typed = TRUE; break;
9150 }
9151 }
9152 }
9153
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009154 /* Need to escape K_SPECIAL and CSI before putting the string in the
9155 * typeahead buffer. */
9156 keys_esc = vim_strsave_escape_csi(keys);
9157 if (keys_esc != NULL)
9158 {
9159 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009160 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009161 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009162 if (vgetc_busy)
9163 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009164 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009165 }
9166}
9167
9168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 * "filereadable()" function
9170 */
9171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009173 typval_T *argvars;
9174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176 FILE *fd;
9177 char_u *p;
9178 int n;
9179
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
9182 {
9183 n = TRUE;
9184 fclose(fd);
9185 }
9186 else
9187 n = FALSE;
9188
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009193 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 * rights to write into.
9195 */
9196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009198 typval_T *argvars;
9199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009201 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
Bram Moolenaar33570922005-01-25 22:26:29 +00009204static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009205
9206 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *argvars;
9209 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009210 int dir;
9211{
9212#ifdef FEAT_SEARCHPATH
9213 char_u *fname;
9214 char_u *fresult = NULL;
9215 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9216 char_u *p;
9217 char_u pathbuf[NUMBUFLEN];
9218 int count = 1;
9219 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009220 int error = FALSE;
9221#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009222
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009223 rettv->vval.v_string = NULL;
9224 rettv->v_type = VAR_STRING;
9225
9226#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009227 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009228
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009229 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9232 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009233 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 else
9235 {
9236 if (*p != NUL)
9237 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009240 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009242 }
9243
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009244 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9245 error = TRUE;
9246
9247 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009249 do
9250 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009251 if (rettv->v_type == VAR_STRING)
9252 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 fresult = find_file_in_path_option(first ? fname : NULL,
9254 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar5b6b1ca2007-03-27 08:19:43 +00009255 0, first, path, dir, curbuf->b_ffname,
Bram Moolenaare580b0c2006-03-21 21:33:03 +00009256 dir ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009257 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009258
9259 if (fresult != NULL && rettv->v_type == VAR_LIST)
9260 list_append_string(rettv->vval.v_list, fresult, -1);
9261
9262 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009264
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009265 if (rettv->v_type == VAR_STRING)
9266 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009267#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009268}
9269
Bram Moolenaar33570922005-01-25 22:26:29 +00009270static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9271static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009272
9273/*
9274 * Implementation of map() and filter().
9275 */
9276 static void
9277filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009278 typval_T *argvars;
9279 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009280 int map;
9281{
9282 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009283 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009284 listitem_T *li, *nli;
9285 list_T *l = NULL;
9286 dictitem_T *di;
9287 hashtab_T *ht;
9288 hashitem_T *hi;
9289 dict_T *d = NULL;
9290 typval_T save_val;
9291 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009292 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009293 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009294 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009295 int save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009296
9297 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009298 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009299 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009300 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009301 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009302 return;
9303 }
9304 else if (argvars[0].v_type == VAR_DICT)
9305 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009306 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009307 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 return;
9309 }
9310 else
9311 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009312 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009313 return;
9314 }
9315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009316 expr = get_tv_string_buf_chk(&argvars[1], buf);
9317 /* On type errors, the preceding call has already displayed an error
9318 * message. Avoid a misleading error message for an empty string that
9319 * was not passed as argument. */
9320 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009321 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322 prepare_vimvar(VV_VAL, &save_val);
9323 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009324
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009325 /* We reset "did_emsg" to be able to detect whether an error
9326 * occurred during evaluation of the expression. */
9327 save_did_emsg = did_emsg;
9328 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009330 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 prepare_vimvar(VV_KEY, &save_key);
9333 vimvars[VV_KEY].vv_type = VAR_STRING;
9334
9335 ht = &d->dv_hashtab;
9336 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009337 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009338 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009340 if (!HASHITEM_EMPTY(hi))
9341 {
9342 --todo;
9343 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009344 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 break;
9346 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009347 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009348 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009349 break;
9350 if (!map && rem)
9351 dictitem_remove(d, di);
9352 clear_tv(&vimvars[VV_KEY].vv_tv);
9353 }
9354 }
9355 hash_unlock(ht);
9356
9357 restore_vimvar(VV_KEY, &save_key);
9358 }
9359 else
9360 {
9361 for (li = l->lv_first; li != NULL; li = nli)
9362 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009363 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009364 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365 nli = li->li_next;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009366 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009367 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009368 break;
9369 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009370 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009371 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009372 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009375
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009376 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378
9379 copy_tv(&argvars[0], rettv);
9380}
9381
9382 static int
9383filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385 char_u *expr;
9386 int map;
9387 int *remp;
9388{
Bram Moolenaar33570922005-01-25 22:26:29 +00009389 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009390 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009391 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392
Bram Moolenaar33570922005-01-25 22:26:29 +00009393 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009394 s = expr;
9395 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009396 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009397 if (*s != NUL) /* check for trailing chars after expr */
9398 {
9399 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009400 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401 }
9402 if (map)
9403 {
9404 /* map(): replace the list item value */
9405 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +00009406 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009407 *tv = rettv;
9408 }
9409 else
9410 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 int error = FALSE;
9412
Bram Moolenaare9a41262005-01-15 22:18:47 +00009413 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009415 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009416 /* On type error, nothing has been removed; return FAIL to stop the
9417 * loop. The error message was given by get_tv_number_chk(). */
9418 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009419 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009420 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009421 retval = OK;
9422theend:
Bram Moolenaar33570922005-01-25 22:26:29 +00009423 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +00009424 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009425}
9426
9427/*
9428 * "filter()" function
9429 */
9430 static void
9431f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 typval_T *argvars;
9433 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009434{
9435 filter_map(argvars, rettv, FALSE);
9436}
9437
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009438/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009439 * "finddir({fname}[, {path}[, {count}]])" function
9440 */
9441 static void
9442f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 typval_T *argvars;
9444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009445{
9446 findfilendir(argvars, rettv, TRUE);
9447}
9448
9449/*
9450 * "findfile({fname}[, {path}[, {count}]])" function
9451 */
9452 static void
9453f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 typval_T *argvars;
9455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009456{
9457 findfilendir(argvars, rettv, FALSE);
9458}
9459
9460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 * "fnamemodify({fname}, {mods})" function
9462 */
9463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009464f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009465 typval_T *argvars;
9466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468 char_u *fname;
9469 char_u *mods;
9470 int usedlen = 0;
9471 int len;
9472 char_u *fbuf = NULL;
9473 char_u buf[NUMBUFLEN];
9474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009475 fname = get_tv_string_chk(&argvars[0]);
9476 mods = get_tv_string_buf_chk(&argvars[1], buf);
9477 if (fname == NULL || mods == NULL)
9478 fname = NULL;
9479 else
9480 {
9481 len = (int)STRLEN(fname);
9482 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
9483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009487 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 vim_free(fbuf);
9491}
9492
Bram Moolenaar33570922005-01-25 22:26:29 +00009493static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494
9495/*
9496 * "foldclosed()" function
9497 */
9498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 typval_T *argvars;
9501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 int end;
9503{
9504#ifdef FEAT_FOLDING
9505 linenr_T lnum;
9506 linenr_T first, last;
9507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9510 {
9511 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
9512 {
9513 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 return;
9518 }
9519 }
9520#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522}
9523
9524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009525 * "foldclosed()" function
9526 */
9527 static void
9528f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009531{
9532 foldclosed_both(argvars, rettv, FALSE);
9533}
9534
9535/*
9536 * "foldclosedend()" function
9537 */
9538 static void
9539f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009540 typval_T *argvars;
9541 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009542{
9543 foldclosed_both(argvars, rettv, TRUE);
9544}
9545
9546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 * "foldlevel()" function
9548 */
9549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 typval_T *argvars;
9552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554#ifdef FEAT_FOLDING
9555 linenr_T lnum;
9556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 else
9561#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563}
9564
9565/*
9566 * "foldtext()" function
9567 */
9568/*ARGSUSED*/
9569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009571 typval_T *argvars;
9572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
9574#ifdef FEAT_FOLDING
9575 linenr_T lnum;
9576 char_u *s;
9577 char_u *r;
9578 int len;
9579 char *txt;
9580#endif
9581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->v_type = VAR_STRING;
9583 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009585 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9586 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9587 <= curbuf->b_ml.ml_line_count
9588 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 {
9590 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009591 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9592 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 {
9594 if (!linewhite(lnum))
9595 break;
9596 ++lnum;
9597 }
9598
9599 /* Find interesting text in this line. */
9600 s = skipwhite(ml_get(lnum));
9601 /* skip C comment-start */
9602 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009605 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009606 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009607 {
9608 s = skipwhite(ml_get(lnum + 1));
9609 if (*s == '*')
9610 s = skipwhite(s + 1);
9611 }
9612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 txt = _("+-%s%3ld lines: ");
9614 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009615 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 + 20 /* for %3ld */
9617 + STRLEN(s))); /* concatenated */
9618 if (r != NULL)
9619 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9621 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9622 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 len = (int)STRLEN(r);
9624 STRCAT(r, s);
9625 /* remove 'foldmarker' and 'commentstring' */
9626 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 }
9629 }
9630#endif
9631}
9632
9633/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009634 * "foldtextresult(lnum)" function
9635 */
9636/*ARGSUSED*/
9637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009639 typval_T *argvars;
9640 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009641{
9642#ifdef FEAT_FOLDING
9643 linenr_T lnum;
9644 char_u *text;
9645 char_u buf[51];
9646 foldinfo_T foldinfo;
9647 int fold_count;
9648#endif
9649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->v_type = VAR_STRING;
9651 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009652#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009654 /* treat illegal types and illegal string values for {lnum} the same */
9655 if (lnum < 0)
9656 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009657 fold_count = foldedCount(curwin, lnum, &foldinfo);
9658 if (fold_count > 0)
9659 {
9660 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9661 &foldinfo, buf);
9662 if (text == buf)
9663 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009665 }
9666#endif
9667}
9668
9669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 * "foreground()" function
9671 */
9672/*ARGSUSED*/
9673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009675 typval_T *argvars;
9676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679#ifdef FEAT_GUI
9680 if (gui.in_use)
9681 gui_mch_set_foreground();
9682#else
9683# ifdef WIN32
9684 win32_set_foreground();
9685# endif
9686#endif
9687}
9688
9689/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009690 * "function()" function
9691 */
9692/*ARGSUSED*/
9693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009695 typval_T *argvars;
9696 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009697{
9698 char_u *s;
9699
Bram Moolenaara7043832005-01-21 11:56:39 +00009700 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009702 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009703 EMSG2(_(e_invarg2), s);
9704 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009705 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009706 else
9707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_string = vim_strsave(s);
9709 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009710 }
9711}
9712
9713/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009714 * "garbagecollect()" function
9715 */
9716/*ARGSUSED*/
9717 static void
9718f_garbagecollect(argvars, rettv)
9719 typval_T *argvars;
9720 typval_T *rettv;
9721{
Bram Moolenaar9fecb462006-09-05 10:59:47 +00009722 /* This is postponed until we are back at the toplevel, because we may be
9723 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9724 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00009725
9726 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
9727 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009728}
9729
9730/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009731 * "get()" function
9732 */
9733 static void
9734f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009735 typval_T *argvars;
9736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009737{
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 listitem_T *li;
9739 list_T *l;
9740 dictitem_T *di;
9741 dict_T *d;
9742 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009743
Bram Moolenaare9a41262005-01-15 22:18:47 +00009744 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009745 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009748 int error = FALSE;
9749
9750 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9751 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009752 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009753 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009755 else if (argvars[0].v_type == VAR_DICT)
9756 {
9757 if ((d = argvars[0].vval.v_dict) != NULL)
9758 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009759 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009760 if (di != NULL)
9761 tv = &di->di_tv;
9762 }
9763 }
9764 else
9765 EMSG2(_(e_listdictarg), "get()");
9766
9767 if (tv == NULL)
9768 {
9769 if (argvars[2].v_type == VAR_UNKNOWN)
9770 rettv->vval.v_number = 0;
9771 else
9772 copy_tv(&argvars[2], rettv);
9773 }
9774 else
9775 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009776}
9777
Bram Moolenaar342337a2005-07-21 21:11:17 +00009778static 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 +00009779
9780/*
9781 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009782 * Return a range (from start to end) of lines in rettv from the specified
9783 * buffer.
9784 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009785 */
9786 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009787get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009788 buf_T *buf;
9789 linenr_T start;
9790 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009791 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009792 typval_T *rettv;
9793{
9794 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009795
Bram Moolenaar342337a2005-07-21 21:11:17 +00009796 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009797 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009798 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009799 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009800 }
9801 else
9802 rettv->vval.v_number = 0;
9803
9804 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9805 return;
9806
9807 if (!retlist)
9808 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009809 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9810 p = ml_get_buf(buf, start, FALSE);
9811 else
9812 p = (char_u *)"";
9813
9814 rettv->v_type = VAR_STRING;
9815 rettv->vval.v_string = vim_strsave(p);
9816 }
9817 else
9818 {
9819 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009820 return;
9821
9822 if (start < 1)
9823 start = 1;
9824 if (end > buf->b_ml.ml_line_count)
9825 end = buf->b_ml.ml_line_count;
9826 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00009827 if (list_append_string(rettv->vval.v_list,
9828 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009829 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009830 }
9831}
9832
9833/*
9834 * "getbufline()" function
9835 */
9836 static void
9837f_getbufline(argvars, rettv)
9838 typval_T *argvars;
9839 typval_T *rettv;
9840{
9841 linenr_T lnum;
9842 linenr_T end;
9843 buf_T *buf;
9844
9845 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9846 ++emsg_off;
9847 buf = get_buf_tv(&argvars[0]);
9848 --emsg_off;
9849
Bram Moolenaar661b1822005-07-28 22:36:45 +00009850 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009851 if (argvars[2].v_type == VAR_UNKNOWN)
9852 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009853 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009854 end = get_tv_lnum_buf(&argvars[2], buf);
9855
Bram Moolenaar342337a2005-07-21 21:11:17 +00009856 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009857}
9858
Bram Moolenaar0d660222005-01-07 21:51:51 +00009859/*
9860 * "getbufvar()" function
9861 */
9862 static void
9863f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009864 typval_T *argvars;
9865 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009866{
9867 buf_T *buf;
9868 buf_T *save_curbuf;
9869 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009870 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9873 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009874 ++emsg_off;
9875 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009876
9877 rettv->v_type = VAR_STRING;
9878 rettv->vval.v_string = NULL;
9879
9880 if (buf != NULL && varname != NULL)
9881 {
9882 if (*varname == '&') /* buffer-local-option */
9883 {
9884 /* set curbuf to be our buf, temporarily */
9885 save_curbuf = curbuf;
9886 curbuf = buf;
9887
9888 get_option_tv(&varname, rettv, TRUE);
9889
9890 /* restore previous notion of curbuf */
9891 curbuf = save_curbuf;
9892 }
9893 else
9894 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895 if (*varname == NUL)
9896 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9897 * scope prefix before the NUL byte is required by
9898 * find_var_in_ht(). */
9899 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009900 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009901 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009902 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009903 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009904 }
9905 }
9906
9907 --emsg_off;
9908}
9909
9910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 * "getchar()" function
9912 */
9913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 typval_T *argvars;
9916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917{
9918 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009919 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920
Bram Moolenaar4015b2c2006-06-22 19:01:34 +00009921 /* Position the cursor. Needed after a message that ends in a space. */
9922 windgoto(msg_row, msg_col);
9923
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 ++no_mapping;
9925 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00009926 for (;;)
9927 {
9928 if (argvars[0].v_type == VAR_UNKNOWN)
9929 /* getchar(): blocking wait. */
9930 n = safe_vgetc();
9931 else if (get_tv_number_chk(&argvars[0], &error) == 1)
9932 /* getchar(1): only check if char avail */
9933 n = vpeekc();
9934 else if (error || vpeekc() == NUL)
9935 /* illegal argument or getchar(0) and no char avail: return zero */
9936 n = 0;
9937 else
9938 /* getchar(0) and char avail: return char */
9939 n = safe_vgetc();
9940 if (n == K_IGNORE)
9941 continue;
9942 break;
9943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 --no_mapping;
9945 --allow_keys;
9946
Bram Moolenaar219b8702006-11-01 14:32:36 +00009947 vimvars[VV_MOUSE_WIN].vv_nr = 0;
9948 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
9949 vimvars[VV_MOUSE_COL].vv_nr = 0;
9950
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 if (IS_SPECIAL(n) || mod_mask != 0)
9953 {
9954 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9955 int i = 0;
9956
9957 /* Turn a special key into three bytes, plus modifier. */
9958 if (mod_mask != 0)
9959 {
9960 temp[i++] = K_SPECIAL;
9961 temp[i++] = KS_MODIFIER;
9962 temp[i++] = mod_mask;
9963 }
9964 if (IS_SPECIAL(n))
9965 {
9966 temp[i++] = K_SPECIAL;
9967 temp[i++] = K_SECOND(n);
9968 temp[i++] = K_THIRD(n);
9969 }
9970#ifdef FEAT_MBYTE
9971 else if (has_mbyte)
9972 i += (*mb_char2bytes)(n, temp + i);
9973#endif
9974 else
9975 temp[i++] = n;
9976 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->v_type = VAR_STRING;
9978 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +00009979
9980#ifdef FEAT_MOUSE
9981 if (n == K_LEFTMOUSE
9982 || n == K_LEFTMOUSE_NM
9983 || n == K_LEFTDRAG
9984 || n == K_LEFTRELEASE
9985 || n == K_LEFTRELEASE_NM
9986 || n == K_MIDDLEMOUSE
9987 || n == K_MIDDLEDRAG
9988 || n == K_MIDDLERELEASE
9989 || n == K_RIGHTMOUSE
9990 || n == K_RIGHTDRAG
9991 || n == K_RIGHTRELEASE
9992 || n == K_X1MOUSE
9993 || n == K_X1DRAG
9994 || n == K_X1RELEASE
9995 || n == K_X2MOUSE
9996 || n == K_X2DRAG
9997 || n == K_X2RELEASE
9998 || n == K_MOUSEDOWN
9999 || n == K_MOUSEUP)
10000 {
10001 int row = mouse_row;
10002 int col = mouse_col;
10003 win_T *win;
10004 linenr_T lnum;
10005# ifdef FEAT_WINDOWS
10006 win_T *wp;
10007# endif
10008 int n = 1;
10009
10010 if (row >= 0 && col >= 0)
10011 {
10012 /* Find the window at the mouse coordinates and compute the
10013 * text position. */
10014 win = mouse_find_win(&row, &col);
10015 (void)mouse_comp_pos(win, &row, &col, &lnum);
10016# ifdef FEAT_WINDOWS
10017 for (wp = firstwin; wp != win; wp = wp->w_next)
10018 ++n;
10019# endif
10020 vimvars[VV_MOUSE_WIN].vv_nr = n;
10021 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10022 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10023 }
10024 }
10025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 }
10027}
10028
10029/*
10030 * "getcharmod()" function
10031 */
10032/*ARGSUSED*/
10033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010034f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010035 typval_T *argvars;
10036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039}
10040
10041/*
10042 * "getcmdline()" function
10043 */
10044/*ARGSUSED*/
10045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010047 typval_T *argvars;
10048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->v_type = VAR_STRING;
10051 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
10054/*
10055 * "getcmdpos()" function
10056 */
10057/*ARGSUSED*/
10058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010060 typval_T *argvars;
10061 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064}
10065
10066/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010067 * "getcmdtype()" function
10068 */
10069/*ARGSUSED*/
10070 static void
10071f_getcmdtype(argvars, rettv)
10072 typval_T *argvars;
10073 typval_T *rettv;
10074{
10075 rettv->v_type = VAR_STRING;
10076 rettv->vval.v_string = alloc(2);
10077 if (rettv->vval.v_string != NULL)
10078 {
10079 rettv->vval.v_string[0] = get_cmdline_type();
10080 rettv->vval.v_string[1] = NUL;
10081 }
10082}
10083
10084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 * "getcwd()" function
10086 */
10087/*ARGSUSED*/
10088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
10093 char_u cwd[MAXPATHL];
10094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 else
10099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010102 if (rettv->vval.v_string != NULL)
10103 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104#endif
10105 }
10106}
10107
10108/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010109 * "getfontname()" function
10110 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010111/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010114 typval_T *argvars;
10115 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010116{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->v_type = VAR_STRING;
10118 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010119#ifdef FEAT_GUI
10120 if (gui.in_use)
10121 {
10122 GuiFont font;
10123 char_u *name = NULL;
10124
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010125 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010126 {
10127 /* Get the "Normal" font. Either the name saved by
10128 * hl_set_font_name() or from the font ID. */
10129 font = gui.norm_font;
10130 name = hl_get_font_name();
10131 }
10132 else
10133 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010135 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10136 return;
10137 font = gui_mch_get_font(name, FALSE);
10138 if (font == NOFONT)
10139 return; /* Invalid font name, return empty string. */
10140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010142 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010143 gui_mch_free_font(font);
10144 }
10145#endif
10146}
10147
10148/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010149 * "getfperm({fname})" function
10150 */
10151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 typval_T *argvars;
10154 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010155{
10156 char_u *fname;
10157 struct stat st;
10158 char_u *perm = NULL;
10159 char_u flags[] = "rwx";
10160 int i;
10161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010165 if (mch_stat((char *)fname, &st) >= 0)
10166 {
10167 perm = vim_strsave((char_u *)"---------");
10168 if (perm != NULL)
10169 {
10170 for (i = 0; i < 9; i++)
10171 {
10172 if (st.st_mode & (1 << (8 - i)))
10173 perm[i] = flags[i % 3];
10174 }
10175 }
10176 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010178}
10179
10180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 * "getfsize({fname})" function
10182 */
10183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010185 typval_T *argvars;
10186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187{
10188 char_u *fname;
10189 struct stat st;
10190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010193 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
10195 if (mch_stat((char *)fname, &st) >= 0)
10196 {
10197 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010198 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010201 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010202
10203 /* non-perfect check for overflow */
10204 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10205 rettv->vval.v_number = -2;
10206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 }
10208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210}
10211
10212/*
10213 * "getftime({fname})" function
10214 */
10215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010217 typval_T *argvars;
10218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219{
10220 char_u *fname;
10221 struct stat st;
10222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224
10225 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229}
10230
10231/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010232 * "getftype({fname})" function
10233 */
10234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010236 typval_T *argvars;
10237 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010238{
10239 char_u *fname;
10240 struct stat st;
10241 char_u *type = NULL;
10242 char *t;
10243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010244 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010245
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010246 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010247 if (mch_lstat((char *)fname, &st) >= 0)
10248 {
10249#ifdef S_ISREG
10250 if (S_ISREG(st.st_mode))
10251 t = "file";
10252 else if (S_ISDIR(st.st_mode))
10253 t = "dir";
10254# ifdef S_ISLNK
10255 else if (S_ISLNK(st.st_mode))
10256 t = "link";
10257# endif
10258# ifdef S_ISBLK
10259 else if (S_ISBLK(st.st_mode))
10260 t = "bdev";
10261# endif
10262# ifdef S_ISCHR
10263 else if (S_ISCHR(st.st_mode))
10264 t = "cdev";
10265# endif
10266# ifdef S_ISFIFO
10267 else if (S_ISFIFO(st.st_mode))
10268 t = "fifo";
10269# endif
10270# ifdef S_ISSOCK
10271 else if (S_ISSOCK(st.st_mode))
10272 t = "fifo";
10273# endif
10274 else
10275 t = "other";
10276#else
10277# ifdef S_IFMT
10278 switch (st.st_mode & S_IFMT)
10279 {
10280 case S_IFREG: t = "file"; break;
10281 case S_IFDIR: t = "dir"; break;
10282# ifdef S_IFLNK
10283 case S_IFLNK: t = "link"; break;
10284# endif
10285# ifdef S_IFBLK
10286 case S_IFBLK: t = "bdev"; break;
10287# endif
10288# ifdef S_IFCHR
10289 case S_IFCHR: t = "cdev"; break;
10290# endif
10291# ifdef S_IFIFO
10292 case S_IFIFO: t = "fifo"; break;
10293# endif
10294# ifdef S_IFSOCK
10295 case S_IFSOCK: t = "socket"; break;
10296# endif
10297 default: t = "other";
10298 }
10299# else
10300 if (mch_isdir(fname))
10301 t = "dir";
10302 else
10303 t = "file";
10304# endif
10305#endif
10306 type = vim_strsave((char_u *)t);
10307 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010309}
10310
10311/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010312 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010313 */
10314 static void
10315f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010316 typval_T *argvars;
10317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010318{
10319 linenr_T lnum;
10320 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010321 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010322
10323 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010324 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010325 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010326 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010327 retlist = FALSE;
10328 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010330 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010331 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010332 retlist = TRUE;
10333 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010334
Bram Moolenaar342337a2005-07-21 21:11:17 +000010335 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010336}
10337
10338/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010339 * "getmatches()" function
10340 */
10341/*ARGSUSED*/
10342 static void
10343f_getmatches(argvars, rettv)
10344 typval_T *argvars;
10345 typval_T *rettv;
10346{
10347#ifdef FEAT_SEARCH_EXTRA
10348 dict_T *dict;
10349 matchitem_T *cur = curwin->w_match_head;
10350
10351 rettv->vval.v_number = 0;
10352
10353 if (rettv_list_alloc(rettv) == OK)
10354 {
10355 while (cur != NULL)
10356 {
10357 dict = dict_alloc();
10358 if (dict == NULL)
10359 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010360 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
10361 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
10362 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
10363 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
10364 list_append_dict(rettv->vval.v_list, dict);
10365 cur = cur->next;
10366 }
10367 }
10368#endif
10369}
10370
10371/*
Bram Moolenaara5525202006-03-02 22:52:09 +000010372 * "getpos(string)" function
10373 */
10374 static void
10375f_getpos(argvars, rettv)
10376 typval_T *argvars;
10377 typval_T *rettv;
10378{
10379 pos_T *fp;
10380 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010381 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010382
10383 if (rettv_list_alloc(rettv) == OK)
10384 {
10385 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010386 fp = var2fpos(&argvars[0], TRUE, &fnum);
10387 if (fnum != -1)
10388 list_append_number(l, (varnumber_T)fnum);
10389 else
10390 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000010391 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
10392 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000010393 list_append_number(l, (fp != NULL)
10394 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000010395 : (varnumber_T)0);
10396 list_append_number(l,
10397#ifdef FEAT_VIRTUALEDIT
10398 (fp != NULL) ? (varnumber_T)fp->coladd :
10399#endif
10400 (varnumber_T)0);
10401 }
10402 else
10403 rettv->vval.v_number = FALSE;
10404}
10405
10406/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000010407 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000010408 */
Bram Moolenaar280f1262006-01-30 00:14:18 +000010409/*ARGSUSED*/
Bram Moolenaar2641f772005-03-25 21:58:17 +000010410 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000010411f_getqflist(argvars, rettv)
10412 typval_T *argvars;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010413 typval_T *rettv;
10414{
10415#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000010416 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010417#endif
10418
Bram Moolenaar77f66d62007-02-20 02:16:18 +000010419 rettv->vval.v_number = 0;
Bram Moolenaar2641f772005-03-25 21:58:17 +000010420#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010421 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000010422 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000010423 wp = NULL;
10424 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
10425 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010426 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010427 if (wp == NULL)
10428 return;
10429 }
10430
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010431 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000010432 }
10433#endif
10434}
10435
10436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 * "getreg()" function
10438 */
10439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010441 typval_T *argvars;
10442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443{
10444 char_u *strregname;
10445 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010446 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010449 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 strregname = get_tv_string_chk(&argvars[0]);
10452 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010453 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010454 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000010455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010457 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 regname = (strregname == NULL ? '"' : *strregname);
10459 if (regname == 0)
10460 regname = '"';
10461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 rettv->vval.v_string = error ? NULL :
10464 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465}
10466
10467/*
10468 * "getregtype()" function
10469 */
10470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010471f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010472 typval_T *argvars;
10473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474{
10475 char_u *strregname;
10476 int regname;
10477 char_u buf[NUMBUFLEN + 2];
10478 long reglen = 0;
10479
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010480 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 {
10482 strregname = get_tv_string_chk(&argvars[0]);
10483 if (strregname == NULL) /* type error; errmsg already given */
10484 {
10485 rettv->v_type = VAR_STRING;
10486 rettv->vval.v_string = NULL;
10487 return;
10488 }
10489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 else
10491 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010492 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493
10494 regname = (strregname == NULL ? '"' : *strregname);
10495 if (regname == 0)
10496 regname = '"';
10497
10498 buf[0] = NUL;
10499 buf[1] = NUL;
10500 switch (get_reg_type(regname, &reglen))
10501 {
10502 case MLINE: buf[0] = 'V'; break;
10503 case MCHAR: buf[0] = 'v'; break;
10504#ifdef FEAT_VISUAL
10505 case MBLOCK:
10506 buf[0] = Ctrl_V;
10507 sprintf((char *)buf + 1, "%ld", reglen + 1);
10508 break;
10509#endif
10510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511 rettv->v_type = VAR_STRING;
10512 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513}
10514
10515/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010516 * "gettabwinvar()" function
10517 */
10518 static void
10519f_gettabwinvar(argvars, rettv)
10520 typval_T *argvars;
10521 typval_T *rettv;
10522{
10523 getwinvar(argvars, rettv, 1);
10524}
10525
10526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 * "getwinposx()" function
10528 */
10529/*ARGSUSED*/
10530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010532 typval_T *argvars;
10533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010535 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536#ifdef FEAT_GUI
10537 if (gui.in_use)
10538 {
10539 int x, y;
10540
10541 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 }
10544#endif
10545}
10546
10547/*
10548 * "getwinposy()" function
10549 */
10550/*ARGSUSED*/
10551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010553 typval_T *argvars;
10554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557#ifdef FEAT_GUI
10558 if (gui.in_use)
10559 {
10560 int x, y;
10561
10562 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 }
10565#endif
10566}
10567
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010568/*
10569 * Find window specifed by "vp" in tabpage "tp".
10570 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010571 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010572find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010573 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010574 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000010575{
10576#ifdef FEAT_WINDOWS
10577 win_T *wp;
10578#endif
10579 int nr;
10580
10581 nr = get_tv_number_chk(vp, NULL);
10582
10583#ifdef FEAT_WINDOWS
10584 if (nr < 0)
10585 return NULL;
10586 if (nr == 0)
10587 return curwin;
10588
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010589 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
10590 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000010591 if (--nr <= 0)
10592 break;
10593 return wp;
10594#else
10595 if (nr == 0 || nr == 1)
10596 return curwin;
10597 return NULL;
10598#endif
10599}
10600
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601/*
10602 * "getwinvar()" function
10603 */
10604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 typval_T *argvars;
10607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010609 getwinvar(argvars, rettv, 0);
10610}
10611
10612/*
10613 * getwinvar() and gettabwinvar()
10614 */
10615 static void
10616getwinvar(argvars, rettv, off)
10617 typval_T *argvars;
10618 typval_T *rettv;
10619 int off; /* 1 for gettabwinvar() */
10620{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 win_T *win, *oldcurwin;
10622 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010623 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010624 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625
Bram Moolenaar99ebf042006-04-15 20:28:54 +000010626#ifdef FEAT_WINDOWS
10627 if (off == 1)
10628 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10629 else
10630 tp = curtab;
10631#endif
10632 win = find_win_by_nr(&argvars[off], tp);
10633 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636 rettv->v_type = VAR_STRING;
10637 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638
10639 if (win != NULL && varname != NULL)
10640 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010641 /* Set curwin to be our win, temporarily. Also set curbuf, so
10642 * that we can get buffer-local options. */
10643 oldcurwin = curwin;
10644 curwin = win;
10645 curbuf = win->w_buffer;
10646
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 else
10650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010651 if (*varname == NUL)
10652 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10653 * scope prefix before the NUL byte is required by
10654 * find_var_in_ht(). */
10655 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010657 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010659 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000010661
10662 /* restore previous notion of curwin */
10663 curwin = oldcurwin;
10664 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 }
10666
10667 --emsg_off;
10668}
10669
10670/*
10671 * "glob()" function
10672 */
10673 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010674f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010675 typval_T *argvars;
10676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677{
10678 expand_T xpc;
10679
10680 ExpandInit(&xpc);
10681 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682 rettv->v_type = VAR_STRING;
10683 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685}
10686
10687/*
10688 * "globpath()" function
10689 */
10690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010692 typval_T *argvars;
10693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694{
10695 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 if (file == NULL)
10700 rettv->vval.v_string = NULL;
10701 else
10702 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703}
10704
10705/*
10706 * "has()" function
10707 */
10708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010710 typval_T *argvars;
10711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
10713 int i;
10714 char_u *name;
10715 int n = FALSE;
10716 static char *(has_list[]) =
10717 {
10718#ifdef AMIGA
10719 "amiga",
10720# ifdef FEAT_ARP
10721 "arp",
10722# endif
10723#endif
10724#ifdef __BEOS__
10725 "beos",
10726#endif
10727#ifdef MSDOS
10728# ifdef DJGPP
10729 "dos32",
10730# else
10731 "dos16",
10732# endif
10733#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000010734#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 "mac",
10736#endif
10737#if defined(MACOS_X_UNIX)
10738 "macunix",
10739#endif
10740#ifdef OS2
10741 "os2",
10742#endif
10743#ifdef __QNX__
10744 "qnx",
10745#endif
10746#ifdef RISCOS
10747 "riscos",
10748#endif
10749#ifdef UNIX
10750 "unix",
10751#endif
10752#ifdef VMS
10753 "vms",
10754#endif
10755#ifdef WIN16
10756 "win16",
10757#endif
10758#ifdef WIN32
10759 "win32",
10760#endif
10761#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10762 "win32unix",
10763#endif
10764#ifdef WIN64
10765 "win64",
10766#endif
10767#ifdef EBCDIC
10768 "ebcdic",
10769#endif
10770#ifndef CASE_INSENSITIVE_FILENAME
10771 "fname_case",
10772#endif
10773#ifdef FEAT_ARABIC
10774 "arabic",
10775#endif
10776#ifdef FEAT_AUTOCMD
10777 "autocmd",
10778#endif
10779#ifdef FEAT_BEVAL
10780 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010781# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10782 "balloon_multiline",
10783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784#endif
10785#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10786 "builtin_terms",
10787# ifdef ALL_BUILTIN_TCAPS
10788 "all_builtin_terms",
10789# endif
10790#endif
10791#ifdef FEAT_BYTEOFF
10792 "byte_offset",
10793#endif
10794#ifdef FEAT_CINDENT
10795 "cindent",
10796#endif
10797#ifdef FEAT_CLIENTSERVER
10798 "clientserver",
10799#endif
10800#ifdef FEAT_CLIPBOARD
10801 "clipboard",
10802#endif
10803#ifdef FEAT_CMDL_COMPL
10804 "cmdline_compl",
10805#endif
10806#ifdef FEAT_CMDHIST
10807 "cmdline_hist",
10808#endif
10809#ifdef FEAT_COMMENTS
10810 "comments",
10811#endif
10812#ifdef FEAT_CRYPT
10813 "cryptv",
10814#endif
10815#ifdef FEAT_CSCOPE
10816 "cscope",
10817#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010818#ifdef CURSOR_SHAPE
10819 "cursorshape",
10820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821#ifdef DEBUG
10822 "debug",
10823#endif
10824#ifdef FEAT_CON_DIALOG
10825 "dialog_con",
10826#endif
10827#ifdef FEAT_GUI_DIALOG
10828 "dialog_gui",
10829#endif
10830#ifdef FEAT_DIFF
10831 "diff",
10832#endif
10833#ifdef FEAT_DIGRAPHS
10834 "digraphs",
10835#endif
10836#ifdef FEAT_DND
10837 "dnd",
10838#endif
10839#ifdef FEAT_EMACS_TAGS
10840 "emacs_tags",
10841#endif
10842 "eval", /* always present, of course! */
10843#ifdef FEAT_EX_EXTRA
10844 "ex_extra",
10845#endif
10846#ifdef FEAT_SEARCH_EXTRA
10847 "extra_search",
10848#endif
10849#ifdef FEAT_FKMAP
10850 "farsi",
10851#endif
10852#ifdef FEAT_SEARCHPATH
10853 "file_in_path",
10854#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010855#if defined(UNIX) && !defined(USE_SYSTEM)
10856 "filterpipe",
10857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858#ifdef FEAT_FIND_ID
10859 "find_in_path",
10860#endif
10861#ifdef FEAT_FOLDING
10862 "folding",
10863#endif
10864#ifdef FEAT_FOOTER
10865 "footer",
10866#endif
10867#if !defined(USE_SYSTEM) && defined(UNIX)
10868 "fork",
10869#endif
10870#ifdef FEAT_GETTEXT
10871 "gettext",
10872#endif
10873#ifdef FEAT_GUI
10874 "gui",
10875#endif
10876#ifdef FEAT_GUI_ATHENA
10877# ifdef FEAT_GUI_NEXTAW
10878 "gui_neXtaw",
10879# else
10880 "gui_athena",
10881# endif
10882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883#ifdef FEAT_GUI_GTK
10884 "gui_gtk",
10885# ifdef HAVE_GTK2
10886 "gui_gtk2",
10887# endif
10888#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000010889#ifdef FEAT_GUI_GNOME
10890 "gui_gnome",
10891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892#ifdef FEAT_GUI_MAC
10893 "gui_mac",
10894#endif
10895#ifdef FEAT_GUI_MOTIF
10896 "gui_motif",
10897#endif
10898#ifdef FEAT_GUI_PHOTON
10899 "gui_photon",
10900#endif
10901#ifdef FEAT_GUI_W16
10902 "gui_win16",
10903#endif
10904#ifdef FEAT_GUI_W32
10905 "gui_win32",
10906#endif
10907#ifdef FEAT_HANGULIN
10908 "hangul_input",
10909#endif
10910#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10911 "iconv",
10912#endif
10913#ifdef FEAT_INS_EXPAND
10914 "insert_expand",
10915#endif
10916#ifdef FEAT_JUMPLIST
10917 "jumplist",
10918#endif
10919#ifdef FEAT_KEYMAP
10920 "keymap",
10921#endif
10922#ifdef FEAT_LANGMAP
10923 "langmap",
10924#endif
10925#ifdef FEAT_LIBCALL
10926 "libcall",
10927#endif
10928#ifdef FEAT_LINEBREAK
10929 "linebreak",
10930#endif
10931#ifdef FEAT_LISP
10932 "lispindent",
10933#endif
10934#ifdef FEAT_LISTCMDS
10935 "listcmds",
10936#endif
10937#ifdef FEAT_LOCALMAP
10938 "localmap",
10939#endif
10940#ifdef FEAT_MENU
10941 "menu",
10942#endif
10943#ifdef FEAT_SESSION
10944 "mksession",
10945#endif
10946#ifdef FEAT_MODIFY_FNAME
10947 "modify_fname",
10948#endif
10949#ifdef FEAT_MOUSE
10950 "mouse",
10951#endif
10952#ifdef FEAT_MOUSESHAPE
10953 "mouseshape",
10954#endif
10955#if defined(UNIX) || defined(VMS)
10956# ifdef FEAT_MOUSE_DEC
10957 "mouse_dec",
10958# endif
10959# ifdef FEAT_MOUSE_GPM
10960 "mouse_gpm",
10961# endif
10962# ifdef FEAT_MOUSE_JSB
10963 "mouse_jsbterm",
10964# endif
10965# ifdef FEAT_MOUSE_NET
10966 "mouse_netterm",
10967# endif
10968# ifdef FEAT_MOUSE_PTERM
10969 "mouse_pterm",
10970# endif
10971# ifdef FEAT_MOUSE_XTERM
10972 "mouse_xterm",
10973# endif
10974#endif
10975#ifdef FEAT_MBYTE
10976 "multi_byte",
10977#endif
10978#ifdef FEAT_MBYTE_IME
10979 "multi_byte_ime",
10980#endif
10981#ifdef FEAT_MULTI_LANG
10982 "multi_lang",
10983#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010984#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010985#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010986 "mzscheme",
10987#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989#ifdef FEAT_OLE
10990 "ole",
10991#endif
10992#ifdef FEAT_OSFILETYPE
10993 "osfiletype",
10994#endif
10995#ifdef FEAT_PATH_EXTRA
10996 "path_extra",
10997#endif
10998#ifdef FEAT_PERL
10999#ifndef DYNAMIC_PERL
11000 "perl",
11001#endif
11002#endif
11003#ifdef FEAT_PYTHON
11004#ifndef DYNAMIC_PYTHON
11005 "python",
11006#endif
11007#endif
11008#ifdef FEAT_POSTSCRIPT
11009 "postscript",
11010#endif
11011#ifdef FEAT_PRINTER
11012 "printer",
11013#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011014#ifdef FEAT_PROFILE
11015 "profile",
11016#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011017#ifdef FEAT_RELTIME
11018 "reltime",
11019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020#ifdef FEAT_QUICKFIX
11021 "quickfix",
11022#endif
11023#ifdef FEAT_RIGHTLEFT
11024 "rightleft",
11025#endif
11026#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11027 "ruby",
11028#endif
11029#ifdef FEAT_SCROLLBIND
11030 "scrollbind",
11031#endif
11032#ifdef FEAT_CMDL_INFO
11033 "showcmd",
11034 "cmdline_info",
11035#endif
11036#ifdef FEAT_SIGNS
11037 "signs",
11038#endif
11039#ifdef FEAT_SMARTINDENT
11040 "smartindent",
11041#endif
11042#ifdef FEAT_SNIFF
11043 "sniff",
11044#endif
11045#ifdef FEAT_STL_OPT
11046 "statusline",
11047#endif
11048#ifdef FEAT_SUN_WORKSHOP
11049 "sun_workshop",
11050#endif
11051#ifdef FEAT_NETBEANS_INTG
11052 "netbeans_intg",
11053#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011054#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011055 "spell",
11056#endif
11057#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 "syntax",
11059#endif
11060#if defined(USE_SYSTEM) || !defined(UNIX)
11061 "system",
11062#endif
11063#ifdef FEAT_TAG_BINS
11064 "tag_binary",
11065#endif
11066#ifdef FEAT_TAG_OLDSTATIC
11067 "tag_old_static",
11068#endif
11069#ifdef FEAT_TAG_ANYWHITE
11070 "tag_any_white",
11071#endif
11072#ifdef FEAT_TCL
11073# ifndef DYNAMIC_TCL
11074 "tcl",
11075# endif
11076#endif
11077#ifdef TERMINFO
11078 "terminfo",
11079#endif
11080#ifdef FEAT_TERMRESPONSE
11081 "termresponse",
11082#endif
11083#ifdef FEAT_TEXTOBJ
11084 "textobjects",
11085#endif
11086#ifdef HAVE_TGETENT
11087 "tgetent",
11088#endif
11089#ifdef FEAT_TITLE
11090 "title",
11091#endif
11092#ifdef FEAT_TOOLBAR
11093 "toolbar",
11094#endif
11095#ifdef FEAT_USR_CMDS
11096 "user-commands", /* was accidentally included in 5.4 */
11097 "user_commands",
11098#endif
11099#ifdef FEAT_VIMINFO
11100 "viminfo",
11101#endif
11102#ifdef FEAT_VERTSPLIT
11103 "vertsplit",
11104#endif
11105#ifdef FEAT_VIRTUALEDIT
11106 "virtualedit",
11107#endif
11108#ifdef FEAT_VISUAL
11109 "visual",
11110#endif
11111#ifdef FEAT_VISUALEXTRA
11112 "visualextra",
11113#endif
11114#ifdef FEAT_VREPLACE
11115 "vreplace",
11116#endif
11117#ifdef FEAT_WILDIGN
11118 "wildignore",
11119#endif
11120#ifdef FEAT_WILDMENU
11121 "wildmenu",
11122#endif
11123#ifdef FEAT_WINDOWS
11124 "windows",
11125#endif
11126#ifdef FEAT_WAK
11127 "winaltkeys",
11128#endif
11129#ifdef FEAT_WRITEBACKUP
11130 "writebackup",
11131#endif
11132#ifdef FEAT_XIM
11133 "xim",
11134#endif
11135#ifdef FEAT_XFONTSET
11136 "xfontset",
11137#endif
11138#ifdef USE_XSMP
11139 "xsmp",
11140#endif
11141#ifdef USE_XSMP_INTERACT
11142 "xsmp_interact",
11143#endif
11144#ifdef FEAT_XCLIPBOARD
11145 "xterm_clipboard",
11146#endif
11147#ifdef FEAT_XTERM_SAVE
11148 "xterm_save",
11149#endif
11150#if defined(UNIX) && defined(FEAT_X11)
11151 "X11",
11152#endif
11153 NULL
11154 };
11155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 for (i = 0; has_list[i] != NULL; ++i)
11158 if (STRICMP(name, has_list[i]) == 0)
11159 {
11160 n = TRUE;
11161 break;
11162 }
11163
11164 if (n == FALSE)
11165 {
11166 if (STRNICMP(name, "patch", 5) == 0)
11167 n = has_patch(atoi((char *)name + 5));
11168 else if (STRICMP(name, "vim_starting") == 0)
11169 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011170#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11171 else if (STRICMP(name, "balloon_multiline") == 0)
11172 n = multiline_balloon_available();
11173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174#ifdef DYNAMIC_TCL
11175 else if (STRICMP(name, "tcl") == 0)
11176 n = tcl_enabled(FALSE);
11177#endif
11178#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11179 else if (STRICMP(name, "iconv") == 0)
11180 n = iconv_enabled(FALSE);
11181#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011182#ifdef DYNAMIC_MZSCHEME
11183 else if (STRICMP(name, "mzscheme") == 0)
11184 n = mzscheme_enabled(FALSE);
11185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186#ifdef DYNAMIC_RUBY
11187 else if (STRICMP(name, "ruby") == 0)
11188 n = ruby_enabled(FALSE);
11189#endif
11190#ifdef DYNAMIC_PYTHON
11191 else if (STRICMP(name, "python") == 0)
11192 n = python_enabled(FALSE);
11193#endif
11194#ifdef DYNAMIC_PERL
11195 else if (STRICMP(name, "perl") == 0)
11196 n = perl_enabled(FALSE);
11197#endif
11198#ifdef FEAT_GUI
11199 else if (STRICMP(name, "gui_running") == 0)
11200 n = (gui.in_use || gui.starting);
11201# ifdef FEAT_GUI_W32
11202 else if (STRICMP(name, "gui_win32s") == 0)
11203 n = gui_is_win32s();
11204# endif
11205# ifdef FEAT_BROWSE
11206 else if (STRICMP(name, "browse") == 0)
11207 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11208# endif
11209#endif
11210#ifdef FEAT_SYN_HL
11211 else if (STRICMP(name, "syntax_items") == 0)
11212 n = syntax_present(curbuf);
11213#endif
11214#if defined(WIN3264)
11215 else if (STRICMP(name, "win95") == 0)
11216 n = mch_windows95();
11217#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011218#ifdef FEAT_NETBEANS_INTG
11219 else if (STRICMP(name, "netbeans_enabled") == 0)
11220 n = usingNetbeans;
11221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 }
11223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225}
11226
11227/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 * "has_key()" function
11229 */
11230 static void
11231f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011232 typval_T *argvars;
11233 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234{
11235 rettv->vval.v_number = 0;
11236 if (argvars[0].v_type != VAR_DICT)
11237 {
11238 EMSG(_(e_dictreq));
11239 return;
11240 }
11241 if (argvars[0].vval.v_dict == NULL)
11242 return;
11243
11244 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011245 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246}
11247
11248/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011249 * "haslocaldir()" function
11250 */
11251/*ARGSUSED*/
11252 static void
11253f_haslocaldir(argvars, rettv)
11254 typval_T *argvars;
11255 typval_T *rettv;
11256{
11257 rettv->vval.v_number = (curwin->w_localdir != NULL);
11258}
11259
11260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 * "hasmapto()" function
11262 */
11263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 typval_T *argvars;
11266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
11268 char_u *name;
11269 char_u *mode;
11270 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011271 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011274 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 mode = (char_u *)"nvo";
11276 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011279 if (argvars[2].v_type != VAR_UNKNOWN)
11280 abbr = get_tv_number(&argvars[2]);
11281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282
Bram Moolenaar2c932302006-03-18 21:42:09 +000011283 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287}
11288
11289/*
11290 * "histadd()" function
11291 */
11292/*ARGSUSED*/
11293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011295 typval_T *argvars;
11296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297{
11298#ifdef FEAT_CMDHIST
11299 int histype;
11300 char_u *str;
11301 char_u buf[NUMBUFLEN];
11302#endif
11303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011304 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 if (check_restricted() || check_secure())
11306 return;
11307#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11309 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 if (histype >= 0)
11311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 if (*str != NUL)
11314 {
11315 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 return;
11318 }
11319 }
11320#endif
11321}
11322
11323/*
11324 * "histdel()" function
11325 */
11326/*ARGSUSED*/
11327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011329 typval_T *argvars;
11330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
11332#ifdef FEAT_CMDHIST
11333 int n;
11334 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11338 if (str == NULL)
11339 n = 0;
11340 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011343 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 else
11348 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011349 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 get_tv_string_buf(&argvars[1], buf));
11351 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354#endif
11355}
11356
11357/*
11358 * "histget()" function
11359 */
11360/*ARGSUSED*/
11361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *argvars;
11364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
11366#ifdef FEAT_CMDHIST
11367 int type;
11368 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
11372 if (str == NULL)
11373 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 {
11376 type = get_histtype(str);
11377 if (argvars[1].v_type == VAR_UNKNOWN)
11378 idx = get_history_idx(type);
11379 else
11380 idx = (int)get_tv_number_chk(&argvars[1], NULL);
11381 /* -1 on type error */
11382 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
11383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388}
11389
11390/*
11391 * "histnr()" function
11392 */
11393/*ARGSUSED*/
11394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011396 typval_T *argvars;
11397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
11399 int i;
11400
11401#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 char_u *history = get_tv_string_chk(&argvars[0]);
11403
11404 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 if (i >= HIST_CMD && i < HIST_COUNT)
11406 i = get_history_idx(i);
11407 else
11408#endif
11409 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411}
11412
11413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 * "highlightID(name)" function
11415 */
11416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011418 typval_T *argvars;
11419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011421 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422}
11423
11424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011425 * "highlight_exists()" function
11426 */
11427 static void
11428f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011429 typval_T *argvars;
11430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011431{
11432 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
11433}
11434
11435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 * "hostname()" function
11437 */
11438/*ARGSUSED*/
11439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 typval_T *argvars;
11442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 char_u hostname[256];
11445
11446 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 rettv->v_type = VAR_STRING;
11448 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449}
11450
11451/*
11452 * iconv() function
11453 */
11454/*ARGSUSED*/
11455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011457 typval_T *argvars;
11458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
11460#ifdef FEAT_MBYTE
11461 char_u buf1[NUMBUFLEN];
11462 char_u buf2[NUMBUFLEN];
11463 char_u *from, *to, *str;
11464 vimconv_T vimconv;
11465#endif
11466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->v_type = VAR_STRING;
11468 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469
11470#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 str = get_tv_string(&argvars[0]);
11472 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
11473 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 vimconv.vc_type = CONV_NONE;
11475 convert_setup(&vimconv, from, to);
11476
11477 /* If the encodings are equal, no conversion needed. */
11478 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482
11483 convert_setup(&vimconv, NULL, NULL);
11484 vim_free(from);
11485 vim_free(to);
11486#endif
11487}
11488
11489/*
11490 * "indent()" function
11491 */
11492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011494 typval_T *argvars;
11495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496{
11497 linenr_T lnum;
11498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504}
11505
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011506/*
11507 * "index()" function
11508 */
11509 static void
11510f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *argvars;
11512 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011513{
Bram Moolenaar33570922005-01-25 22:26:29 +000011514 list_T *l;
11515 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011516 long idx = 0;
11517 int ic = FALSE;
11518
11519 rettv->vval.v_number = -1;
11520 if (argvars[0].v_type != VAR_LIST)
11521 {
11522 EMSG(_(e_listreq));
11523 return;
11524 }
11525 l = argvars[0].vval.v_list;
11526 if (l != NULL)
11527 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011528 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 int error = FALSE;
11532
Bram Moolenaar758711c2005-02-02 23:11:38 +000011533 /* Start at specified item. Use the cached index that list_find()
11534 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000011536 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011537 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011538 ic = get_tv_number_chk(&argvars[3], &error);
11539 if (error)
11540 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011541 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011542
Bram Moolenaar758711c2005-02-02 23:11:38 +000011543 for ( ; item != NULL; item = item->li_next, ++idx)
11544 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011545 {
11546 rettv->vval.v_number = idx;
11547 break;
11548 }
11549 }
11550}
11551
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552static int inputsecret_flag = 0;
11553
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011554static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
11555
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011557 * This function is used by f_input() and f_inputdialog() functions. The third
11558 * argument to f_input() specifies the type of completion to use at the
11559 * prompt. The third argument to f_inputdialog() specifies the value to return
11560 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 */
11562 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011563get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000011564 typval_T *argvars;
11565 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011566 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011568 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 char_u *p = NULL;
11570 int c;
11571 char_u buf[NUMBUFLEN];
11572 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011574 int xp_type = EXPAND_NOTHING;
11575 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000011578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579
11580#ifdef NO_CONSOLE_INPUT
11581 /* While starting up, there is no place to enter text. */
11582 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584#endif
11585
11586 cmd_silent = FALSE; /* Want to see the prompt. */
11587 if (prompt != NULL)
11588 {
11589 /* Only the part of the message after the last NL is considered as
11590 * prompt for the command line */
11591 p = vim_strrchr(prompt, '\n');
11592 if (p == NULL)
11593 p = prompt;
11594 else
11595 {
11596 ++p;
11597 c = *p;
11598 *p = NUL;
11599 msg_start();
11600 msg_clr_eos();
11601 msg_puts_attr(prompt, echo_attr);
11602 msg_didout = FALSE;
11603 msg_starthere();
11604 *p = c;
11605 }
11606 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 if (argvars[1].v_type != VAR_UNKNOWN)
11609 {
11610 defstr = get_tv_string_buf_chk(&argvars[1], buf);
11611 if (defstr != NULL)
11612 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011614 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000011615 {
11616 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000011617 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000011618 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011619
Bram Moolenaar4463f292005-09-25 22:20:24 +000011620 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011621
Bram Moolenaar4463f292005-09-25 22:20:24 +000011622 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
11623 if (xp_name == NULL)
11624 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011625
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011626 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011627
Bram Moolenaar4463f292005-09-25 22:20:24 +000011628 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
11629 &xp_arg) == FAIL)
11630 return;
11631 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011632 }
11633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011634 if (defstr != NULL)
11635 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011636 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
11637 xp_type, xp_arg);
11638
11639 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 /* since the user typed this, no need to wait for return */
11642 need_wait_return = FALSE;
11643 msg_didout = FALSE;
11644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 cmd_silent = cmd_silent_save;
11646}
11647
11648/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011649 * "input()" function
11650 * Also handles inputsecret() when inputsecret is set.
11651 */
11652 static void
11653f_input(argvars, rettv)
11654 typval_T *argvars;
11655 typval_T *rettv;
11656{
11657 get_user_input(argvars, rettv, FALSE);
11658}
11659
11660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 * "inputdialog()" function
11662 */
11663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011664f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667{
11668#if defined(FEAT_GUI_TEXTDIALOG)
11669 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11670 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
11671 {
11672 char_u *message;
11673 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011674 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 message = get_tv_string_chk(&argvars[0]);
11677 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000011678 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000011679 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 else
11681 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 if (message != NULL && defstr != NULL
11683 && do_dialog(VIM_QUESTION, NULL, message,
11684 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 else
11687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688 if (message != NULL && defstr != NULL
11689 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011690 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 rettv->vval.v_string = vim_strsave(
11692 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 }
11698 else
11699#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000011700 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701}
11702
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011703/*
11704 * "inputlist()" function
11705 */
11706 static void
11707f_inputlist(argvars, rettv)
11708 typval_T *argvars;
11709 typval_T *rettv;
11710{
11711 listitem_T *li;
11712 int selected;
11713 int mouse_used;
11714
11715 rettv->vval.v_number = 0;
11716#ifdef NO_CONSOLE_INPUT
11717 /* While starting up, there is no place to enter text. */
11718 if (no_console_input())
11719 return;
11720#endif
11721 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
11722 {
11723 EMSG2(_(e_listarg), "inputlist()");
11724 return;
11725 }
11726
11727 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000011728 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000011729 lines_left = Rows; /* avoid more prompt */
11730 msg_scroll = TRUE;
11731 msg_clr_eos();
11732
11733 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
11734 {
11735 msg_puts(get_tv_string(&li->li_tv));
11736 msg_putchar('\n');
11737 }
11738
11739 /* Ask for choice. */
11740 selected = prompt_for_number(&mouse_used);
11741 if (mouse_used)
11742 selected -= lines_left;
11743
11744 rettv->vval.v_number = selected;
11745}
11746
11747
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
11749
11750/*
11751 * "inputrestore()" function
11752 */
11753/*ARGSUSED*/
11754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011756 typval_T *argvars;
11757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758{
11759 if (ga_userinput.ga_len > 0)
11760 {
11761 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
11763 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 }
11766 else if (p_verbose > 1)
11767 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000011768 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770 }
11771}
11772
11773/*
11774 * "inputsave()" function
11775 */
11776/*ARGSUSED*/
11777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011779 typval_T *argvars;
11780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781{
11782 /* Add an entry to the stack of typehead storage. */
11783 if (ga_grow(&ga_userinput, 1) == OK)
11784 {
11785 save_typeahead((tasave_T *)(ga_userinput.ga_data)
11786 + ga_userinput.ga_len);
11787 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 }
11790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011791 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792}
11793
11794/*
11795 * "inputsecret()" function
11796 */
11797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011798f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011799 typval_T *argvars;
11800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801{
11802 ++cmdline_star;
11803 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 --cmdline_star;
11806 --inputsecret_flag;
11807}
11808
11809/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011810 * "insert()" function
11811 */
11812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011813f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011814 typval_T *argvars;
11815 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011816{
11817 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011818 listitem_T *item;
11819 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011821
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011822 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011823 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011824 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011825 else if ((l = argvars[0].vval.v_list) != NULL
11826 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011827 {
11828 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 before = get_tv_number_chk(&argvars[2], &error);
11830 if (error)
11831 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011832
Bram Moolenaar758711c2005-02-02 23:11:38 +000011833 if (before == l->lv_len)
11834 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011835 else
11836 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011837 item = list_find(l, before);
11838 if (item == NULL)
11839 {
11840 EMSGN(_(e_listidx), before);
11841 l = NULL;
11842 }
11843 }
11844 if (l != NULL)
11845 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011846 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011847 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011848 }
11849 }
11850}
11851
11852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 * "isdirectory()" function
11854 */
11855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011857 typval_T *argvars;
11858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011860 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861}
11862
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011863/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011864 * "islocked()" function
11865 */
11866 static void
11867f_islocked(argvars, rettv)
11868 typval_T *argvars;
11869 typval_T *rettv;
11870{
11871 lval_T lv;
11872 char_u *end;
11873 dictitem_T *di;
11874
11875 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011876 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11877 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011878 if (end != NULL && lv.ll_name != NULL)
11879 {
11880 if (*end != NUL)
11881 EMSG(_(e_trailing));
11882 else
11883 {
11884 if (lv.ll_tv == NULL)
11885 {
11886 if (check_changedtick(lv.ll_name))
11887 rettv->vval.v_number = 1; /* always locked */
11888 else
11889 {
11890 di = find_var(lv.ll_name, NULL);
11891 if (di != NULL)
11892 {
11893 /* Consider a variable locked when:
11894 * 1. the variable itself is locked
11895 * 2. the value of the variable is locked.
11896 * 3. the List or Dict value is locked.
11897 */
11898 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11899 || tv_islocked(&di->di_tv));
11900 }
11901 }
11902 }
11903 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011904 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011905 else if (lv.ll_newkey != NULL)
11906 EMSG2(_(e_dictkey), lv.ll_newkey);
11907 else if (lv.ll_list != NULL)
11908 /* List item. */
11909 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11910 else
11911 /* Dictionary item. */
11912 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11913 }
11914 }
11915
11916 clear_lval(&lv);
11917}
11918
Bram Moolenaar33570922005-01-25 22:26:29 +000011919static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011920
11921/*
11922 * Turn a dict into a list:
11923 * "what" == 0: list of keys
11924 * "what" == 1: list of values
11925 * "what" == 2: list of items
11926 */
11927 static void
11928dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011929 typval_T *argvars;
11930 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011931 int what;
11932{
Bram Moolenaar33570922005-01-25 22:26:29 +000011933 list_T *l2;
11934 dictitem_T *di;
11935 hashitem_T *hi;
11936 listitem_T *li;
11937 listitem_T *li2;
11938 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011939 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011940
11941 rettv->vval.v_number = 0;
11942 if (argvars[0].v_type != VAR_DICT)
11943 {
11944 EMSG(_(e_dictreq));
11945 return;
11946 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011947 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011948 return;
11949
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011950 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011951 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011952
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011954 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011955 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011956 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011958 --todo;
11959 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011960
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011961 li = listitem_alloc();
11962 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011963 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011964 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011965
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011966 if (what == 0)
11967 {
11968 /* keys() */
11969 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011970 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011971 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11972 }
11973 else if (what == 1)
11974 {
11975 /* values() */
11976 copy_tv(&di->di_tv, &li->li_tv);
11977 }
11978 else
11979 {
11980 /* items() */
11981 l2 = list_alloc();
11982 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011983 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011984 li->li_tv.vval.v_list = l2;
11985 if (l2 == NULL)
11986 break;
11987 ++l2->lv_refcount;
11988
11989 li2 = listitem_alloc();
11990 if (li2 == NULL)
11991 break;
11992 list_append(l2, li2);
11993 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011994 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011995 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11996
11997 li2 = listitem_alloc();
11998 if (li2 == NULL)
11999 break;
12000 list_append(l2, li2);
12001 copy_tv(&di->di_tv, &li2->li_tv);
12002 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012003 }
12004 }
12005}
12006
12007/*
12008 * "items(dict)" function
12009 */
12010 static void
12011f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012012 typval_T *argvars;
12013 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012014{
12015 dict_list(argvars, rettv, 2);
12016}
12017
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012019 * "join()" function
12020 */
12021 static void
12022f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012023 typval_T *argvars;
12024 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012025{
12026 garray_T ga;
12027 char_u *sep;
12028
12029 rettv->vval.v_number = 0;
12030 if (argvars[0].v_type != VAR_LIST)
12031 {
12032 EMSG(_(e_listreq));
12033 return;
12034 }
12035 if (argvars[0].vval.v_list == NULL)
12036 return;
12037 if (argvars[1].v_type == VAR_UNKNOWN)
12038 sep = (char_u *)" ";
12039 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012041
12042 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012043
12044 if (sep != NULL)
12045 {
12046 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012047 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 ga_append(&ga, NUL);
12049 rettv->vval.v_string = (char_u *)ga.ga_data;
12050 }
12051 else
12052 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012053}
12054
12055/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012056 * "keys()" function
12057 */
12058 static void
12059f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012060 typval_T *argvars;
12061 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012062{
12063 dict_list(argvars, rettv, 0);
12064}
12065
12066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 * "last_buffer_nr()" function.
12068 */
12069/*ARGSUSED*/
12070 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012071f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012072 typval_T *argvars;
12073 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074{
12075 int n = 0;
12076 buf_T *buf;
12077
12078 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12079 if (n < buf->b_fnum)
12080 n = buf->b_fnum;
12081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012083}
12084
12085/*
12086 * "len()" function
12087 */
12088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012090 typval_T *argvars;
12091 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012092{
12093 switch (argvars[0].v_type)
12094 {
12095 case VAR_STRING:
12096 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 rettv->vval.v_number = (varnumber_T)STRLEN(
12098 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012099 break;
12100 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012102 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012103 case VAR_DICT:
12104 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12105 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012106 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012107 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012108 break;
12109 }
12110}
12111
Bram Moolenaar33570922005-01-25 22:26:29 +000012112static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012113
12114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012116 typval_T *argvars;
12117 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012118 int type;
12119{
12120#ifdef FEAT_LIBCALL
12121 char_u *string_in;
12122 char_u **string_result;
12123 int nr_result;
12124#endif
12125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012126 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012127 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012131
12132 if (check_restricted() || check_secure())
12133 return;
12134
12135#ifdef FEAT_LIBCALL
12136 /* The first two args must be strings, otherwise its meaningless */
12137 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12138 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012139 string_in = NULL;
12140 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012141 string_in = argvars[2].vval.v_string;
12142 if (type == VAR_NUMBER)
12143 string_result = NULL;
12144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012146 if (mch_libcall(argvars[0].vval.v_string,
12147 argvars[1].vval.v_string,
12148 string_in,
12149 argvars[2].vval.v_number,
12150 string_result,
12151 &nr_result) == OK
12152 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012154 }
12155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156}
12157
12158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159 * "libcall()" function
12160 */
12161 static void
12162f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012163 typval_T *argvars;
12164 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012165{
12166 libcall_common(argvars, rettv, VAR_STRING);
12167}
12168
12169/*
12170 * "libcallnr()" function
12171 */
12172 static void
12173f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012174 typval_T *argvars;
12175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176{
12177 libcall_common(argvars, rettv, VAR_NUMBER);
12178}
12179
12180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 * "line(string)" function
12182 */
12183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012185 typval_T *argvars;
12186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187{
12188 linenr_T lnum = 0;
12189 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012190 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012192 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 if (fp != NULL)
12194 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012195 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196}
12197
12198/*
12199 * "line2byte(lnum)" function
12200 */
12201/*ARGSUSED*/
12202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012203f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012204 typval_T *argvars;
12205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206{
12207#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012208 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209#else
12210 linenr_T lnum;
12211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12217 if (rettv->vval.v_number >= 0)
12218 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219#endif
12220}
12221
12222/*
12223 * "lispindent(lnum)" function
12224 */
12225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012226f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012227 typval_T *argvars;
12228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229{
12230#ifdef FEAT_LISP
12231 pos_T pos;
12232 linenr_T lnum;
12233
12234 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12237 {
12238 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012239 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 curwin->w_cursor = pos;
12241 }
12242 else
12243#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245}
12246
12247/*
12248 * "localtime()" function
12249 */
12250/*ARGSUSED*/
12251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012253 typval_T *argvars;
12254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257}
12258
Bram Moolenaar33570922005-01-25 22:26:29 +000012259static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260
12261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012263 typval_T *argvars;
12264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 int exact;
12266{
12267 char_u *keys;
12268 char_u *which;
12269 char_u buf[NUMBUFLEN];
12270 char_u *keys_buf = NULL;
12271 char_u *rhs;
12272 int mode;
12273 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012274 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275
12276 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012277 rettv->v_type = VAR_STRING;
12278 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012280 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 if (*keys == NUL)
12282 return;
12283
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012284 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012286 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012287 if (argvars[2].v_type != VAR_UNKNOWN)
12288 abbr = get_tv_number(&argvars[2]);
12289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 else
12291 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012292 if (which == NULL)
12293 return;
12294
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 mode = get_map_mode(&which, 0);
12296
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012297 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012298 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 vim_free(keys_buf);
12300 if (rhs != NULL)
12301 {
12302 ga_init(&ga);
12303 ga.ga_itemsize = 1;
12304 ga.ga_growsize = 40;
12305
12306 while (*rhs != NUL)
12307 ga_concat(&ga, str2special(&rhs, FALSE));
12308
12309 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012310 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 }
12312}
12313
12314/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012315 * "map()" function
12316 */
12317 static void
12318f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012319 typval_T *argvars;
12320 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012321{
12322 filter_map(argvars, rettv, TRUE);
12323}
12324
12325/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012326 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 */
12328 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012329f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012330 typval_T *argvars;
12331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012333 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334}
12335
12336/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012337 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338 */
12339 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012340f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012341 typval_T *argvars;
12342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012344 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345}
12346
Bram Moolenaar33570922005-01-25 22:26:29 +000012347static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348
12349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012351 typval_T *argvars;
12352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 int type;
12354{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012355 char_u *str = NULL;
12356 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 char_u *pat;
12358 regmatch_T regmatch;
12359 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012360 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 char_u *save_cpo;
12362 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012363 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012364 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012365 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012366 list_T *l = NULL;
12367 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012368 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012369 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370
12371 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12372 save_cpo = p_cpo;
12373 p_cpo = (char_u *)"";
12374
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012375 rettv->vval.v_number = -1;
12376 if (type == 3)
12377 {
12378 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012379 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012380 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012381 }
12382 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->v_type = VAR_STRING;
12385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012388 if (argvars[0].v_type == VAR_LIST)
12389 {
12390 if ((l = argvars[0].vval.v_list) == NULL)
12391 goto theend;
12392 li = l->lv_first;
12393 }
12394 else
12395 expr = str = get_tv_string(&argvars[0]);
12396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012397 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
12398 if (pat == NULL)
12399 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012400
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 int error = FALSE;
12404
12405 start = get_tv_number_chk(&argvars[2], &error);
12406 if (error)
12407 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012408 if (l != NULL)
12409 {
12410 li = list_find(l, start);
12411 if (li == NULL)
12412 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012413 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012414 }
12415 else
12416 {
12417 if (start < 0)
12418 start = 0;
12419 if (start > (long)STRLEN(str))
12420 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012421 /* When "count" argument is there ignore matches before "start",
12422 * otherwise skip part of the string. Differs when pattern is "^"
12423 * or "\<". */
12424 if (argvars[3].v_type != VAR_UNKNOWN)
12425 startcol = start;
12426 else
12427 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012428 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012429
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012430 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 nth = get_tv_number_chk(&argvars[3], &error);
12432 if (error)
12433 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 }
12435
12436 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12437 if (regmatch.regprog != NULL)
12438 {
12439 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012440
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012441 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012442 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012443 if (l != NULL)
12444 {
12445 if (li == NULL)
12446 {
12447 match = FALSE;
12448 break;
12449 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012450 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012451 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012452 if (str == NULL)
12453 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012454 }
12455
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012456 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012457
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012458 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012459 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012460 if (l == NULL && !match)
12461 break;
12462
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012463 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012464 if (l != NULL)
12465 {
12466 li = li->li_next;
12467 ++idx;
12468 }
12469 else
12470 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012471#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012472 startcol = (colnr_T)(regmatch.startp[0]
12473 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012474#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000012475 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012476#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012477 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012478 }
12479
12480 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012482 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012483 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012484 int i;
12485
12486 /* return list with matched string and submatches */
12487 for (i = 0; i < NSUBEXP; ++i)
12488 {
12489 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000012490 {
12491 if (list_append_string(rettv->vval.v_list,
12492 (char_u *)"", 0) == FAIL)
12493 break;
12494 }
12495 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000012496 regmatch.startp[i],
12497 (int)(regmatch.endp[i] - regmatch.startp[i]))
12498 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012499 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012500 }
12501 }
12502 else if (type == 2)
12503 {
12504 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012505 if (l != NULL)
12506 copy_tv(&li->li_tv, rettv);
12507 else
12508 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012510 }
12511 else if (l != NULL)
12512 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 else
12514 {
12515 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517 (varnumber_T)(regmatch.startp[0] - str);
12518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000012520 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012521 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522 }
12523 }
12524 vim_free(regmatch.regprog);
12525 }
12526
12527theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012528 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 p_cpo = save_cpo;
12530}
12531
12532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533 * "match()" function
12534 */
12535 static void
12536f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012537 typval_T *argvars;
12538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012539{
12540 find_some_match(argvars, rettv, 1);
12541}
12542
12543/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012544 * "matchadd()" function
12545 */
12546 static void
12547f_matchadd(argvars, rettv)
12548 typval_T *argvars;
12549 typval_T *rettv;
12550{
12551#ifdef FEAT_SEARCH_EXTRA
12552 char_u buf[NUMBUFLEN];
12553 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
12554 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
12555 int prio = 10; /* default priority */
12556 int id = -1;
12557 int error = FALSE;
12558
12559 rettv->vval.v_number = -1;
12560
12561 if (grp == NULL || pat == NULL)
12562 return;
12563 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012564 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012565 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000012566 if (argvars[3].v_type != VAR_UNKNOWN)
12567 id = get_tv_number_chk(&argvars[3], &error);
12568 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012569 if (error == TRUE)
12570 return;
12571 if (id >= 1 && id <= 3)
12572 {
12573 EMSGN("E798: ID is reserved for \":match\": %ld", id);
12574 return;
12575 }
12576
12577 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
12578#endif
12579}
12580
12581/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012582 * "matcharg()" function
12583 */
12584 static void
12585f_matcharg(argvars, rettv)
12586 typval_T *argvars;
12587 typval_T *rettv;
12588{
12589 if (rettv_list_alloc(rettv) == OK)
12590 {
12591#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012592 int id = get_tv_number(&argvars[0]);
12593 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012594
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012595 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012596 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012597 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
12598 {
12599 list_append_string(rettv->vval.v_list,
12600 syn_id2name(m->hlg_id), -1);
12601 list_append_string(rettv->vval.v_list, m->pattern, -1);
12602 }
12603 else
12604 {
12605 list_append_string(rettv->vval.v_list, NUL, -1);
12606 list_append_string(rettv->vval.v_list, NUL, -1);
12607 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012608 }
12609#endif
12610 }
12611}
12612
12613/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012614 * "matchdelete()" function
12615 */
12616 static void
12617f_matchdelete(argvars, rettv)
12618 typval_T *argvars;
12619 typval_T *rettv;
12620{
12621#ifdef FEAT_SEARCH_EXTRA
12622 rettv->vval.v_number = match_delete(curwin,
12623 (int)get_tv_number(&argvars[0]), TRUE);
12624#endif
12625}
12626
12627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012628 * "matchend()" function
12629 */
12630 static void
12631f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012632 typval_T *argvars;
12633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012634{
12635 find_some_match(argvars, rettv, 0);
12636}
12637
12638/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012639 * "matchlist()" function
12640 */
12641 static void
12642f_matchlist(argvars, rettv)
12643 typval_T *argvars;
12644 typval_T *rettv;
12645{
12646 find_some_match(argvars, rettv, 3);
12647}
12648
12649/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012650 * "matchstr()" function
12651 */
12652 static void
12653f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012654 typval_T *argvars;
12655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012656{
12657 find_some_match(argvars, rettv, 2);
12658}
12659
Bram Moolenaar33570922005-01-25 22:26:29 +000012660static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012661
12662 static void
12663max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000012664 typval_T *argvars;
12665 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012666 int domax;
12667{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012668 long n = 0;
12669 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012670 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012671
12672 if (argvars[0].v_type == VAR_LIST)
12673 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012674 list_T *l;
12675 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012676
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012677 l = argvars[0].vval.v_list;
12678 if (l != NULL)
12679 {
12680 li = l->lv_first;
12681 if (li != NULL)
12682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000012684 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012685 {
12686 li = li->li_next;
12687 if (li == NULL)
12688 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012690 if (domax ? i > n : i < n)
12691 n = i;
12692 }
12693 }
12694 }
12695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012696 else if (argvars[0].v_type == VAR_DICT)
12697 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012698 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012699 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000012700 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012701 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012702
12703 d = argvars[0].vval.v_dict;
12704 if (d != NULL)
12705 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012706 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012707 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012709 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012711 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012712 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012713 if (first)
12714 {
12715 n = i;
12716 first = FALSE;
12717 }
12718 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012719 n = i;
12720 }
12721 }
12722 }
12723 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012724 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000012725 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012727}
12728
12729/*
12730 * "max()" function
12731 */
12732 static void
12733f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012734 typval_T *argvars;
12735 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012736{
12737 max_min(argvars, rettv, TRUE);
12738}
12739
12740/*
12741 * "min()" function
12742 */
12743 static void
12744f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012745 typval_T *argvars;
12746 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012747{
12748 max_min(argvars, rettv, FALSE);
12749}
12750
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012751static int mkdir_recurse __ARGS((char_u *dir, int prot));
12752
12753/*
12754 * Create the directory in which "dir" is located, and higher levels when
12755 * needed.
12756 */
12757 static int
12758mkdir_recurse(dir, prot)
12759 char_u *dir;
12760 int prot;
12761{
12762 char_u *p;
12763 char_u *updir;
12764 int r = FAIL;
12765
12766 /* Get end of directory name in "dir".
12767 * We're done when it's "/" or "c:/". */
12768 p = gettail_sep(dir);
12769 if (p <= get_past_head(dir))
12770 return OK;
12771
12772 /* If the directory exists we're done. Otherwise: create it.*/
12773 updir = vim_strnsave(dir, (int)(p - dir));
12774 if (updir == NULL)
12775 return FAIL;
12776 if (mch_isdir(updir))
12777 r = OK;
12778 else if (mkdir_recurse(updir, prot) == OK)
12779 r = vim_mkdir_emsg(updir, prot);
12780 vim_free(updir);
12781 return r;
12782}
12783
12784#ifdef vim_mkdir
12785/*
12786 * "mkdir()" function
12787 */
12788 static void
12789f_mkdir(argvars, rettv)
12790 typval_T *argvars;
12791 typval_T *rettv;
12792{
12793 char_u *dir;
12794 char_u buf[NUMBUFLEN];
12795 int prot = 0755;
12796
12797 rettv->vval.v_number = FAIL;
12798 if (check_restricted() || check_secure())
12799 return;
12800
12801 dir = get_tv_string_buf(&argvars[0], buf);
12802 if (argvars[1].v_type != VAR_UNKNOWN)
12803 {
12804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012805 prot = get_tv_number_chk(&argvars[2], NULL);
12806 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012807 mkdir_recurse(dir, prot);
12808 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012810}
12811#endif
12812
Bram Moolenaar0d660222005-01-07 21:51:51 +000012813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 * "mode()" function
12815 */
12816/*ARGSUSED*/
12817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *argvars;
12820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821{
12822 char_u buf[2];
12823
12824#ifdef FEAT_VISUAL
12825 if (VIsual_active)
12826 {
12827 if (VIsual_select)
12828 buf[0] = VIsual_mode + 's' - 'v';
12829 else
12830 buf[0] = VIsual_mode;
12831 }
12832 else
12833#endif
12834 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
12835 buf[0] = 'r';
12836 else if (State & INSERT)
12837 {
12838 if (State & REPLACE_FLAG)
12839 buf[0] = 'R';
12840 else
12841 buf[0] = 'i';
12842 }
12843 else if (State & CMDLINE)
12844 buf[0] = 'c';
12845 else
12846 buf[0] = 'n';
12847
12848 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->vval.v_string = vim_strsave(buf);
12850 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851}
12852
12853/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012854 * "nextnonblank()" function
12855 */
12856 static void
12857f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012858 typval_T *argvars;
12859 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860{
12861 linenr_T lnum;
12862
12863 for (lnum = get_tv_lnum(argvars); ; ++lnum)
12864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012866 {
12867 lnum = 0;
12868 break;
12869 }
12870 if (*skipwhite(ml_get(lnum)) != NUL)
12871 break;
12872 }
12873 rettv->vval.v_number = lnum;
12874}
12875
12876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 * "nr2char()" function
12878 */
12879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012881 typval_T *argvars;
12882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883{
12884 char_u buf[NUMBUFLEN];
12885
12886#ifdef FEAT_MBYTE
12887 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 else
12890#endif
12891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 buf[1] = NUL;
12894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 rettv->v_type = VAR_STRING;
12896 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012897}
12898
12899/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012900 * "pathshorten()" function
12901 */
12902 static void
12903f_pathshorten(argvars, rettv)
12904 typval_T *argvars;
12905 typval_T *rettv;
12906{
12907 char_u *p;
12908
12909 rettv->v_type = VAR_STRING;
12910 p = get_tv_string_chk(&argvars[0]);
12911 if (p == NULL)
12912 rettv->vval.v_string = NULL;
12913 else
12914 {
12915 p = vim_strsave(p);
12916 rettv->vval.v_string = p;
12917 if (p != NULL)
12918 shorten_dir(p);
12919 }
12920}
12921
12922/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012923 * "prevnonblank()" function
12924 */
12925 static void
12926f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012927 typval_T *argvars;
12928 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012929{
12930 linenr_T lnum;
12931
12932 lnum = get_tv_lnum(argvars);
12933 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12934 lnum = 0;
12935 else
12936 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12937 --lnum;
12938 rettv->vval.v_number = lnum;
12939}
12940
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012941#ifdef HAVE_STDARG_H
12942/* This dummy va_list is here because:
12943 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12944 * - locally in the function results in a "used before set" warning
12945 * - using va_start() to initialize it gives "function with fixed args" error */
12946static va_list ap;
12947#endif
12948
Bram Moolenaar8c711452005-01-14 21:53:12 +000012949/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012950 * "printf()" function
12951 */
12952 static void
12953f_printf(argvars, rettv)
12954 typval_T *argvars;
12955 typval_T *rettv;
12956{
12957 rettv->v_type = VAR_STRING;
12958 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012959#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012960 {
12961 char_u buf[NUMBUFLEN];
12962 int len;
12963 char_u *s;
12964 int saved_did_emsg = did_emsg;
12965 char *fmt;
12966
12967 /* Get the required length, allocate the buffer and do it for real. */
12968 did_emsg = FALSE;
12969 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012970 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012971 if (!did_emsg)
12972 {
12973 s = alloc(len + 1);
12974 if (s != NULL)
12975 {
12976 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012977 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012978 }
12979 }
12980 did_emsg |= saved_did_emsg;
12981 }
12982#endif
12983}
12984
12985/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000012986 * "pumvisible()" function
12987 */
12988/*ARGSUSED*/
12989 static void
12990f_pumvisible(argvars, rettv)
12991 typval_T *argvars;
12992 typval_T *rettv;
12993{
12994 rettv->vval.v_number = 0;
12995#ifdef FEAT_INS_EXPAND
12996 if (pum_visible())
12997 rettv->vval.v_number = 1;
12998#endif
12999}
13000
13001/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013002 * "range()" function
13003 */
13004 static void
13005f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013006 typval_T *argvars;
13007 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013008{
13009 long start;
13010 long end;
13011 long stride = 1;
13012 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013016 if (argvars[1].v_type == VAR_UNKNOWN)
13017 {
13018 end = start - 1;
13019 start = 0;
13020 }
13021 else
13022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013023 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013025 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013026 }
13027
13028 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 if (error)
13030 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013031 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013032 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013033 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013034 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013035 else
13036 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013037 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013038 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013039 if (list_append_number(rettv->vval.v_list,
13040 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013041 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013042 }
13043}
13044
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013045/*
13046 * "readfile()" function
13047 */
13048 static void
13049f_readfile(argvars, rettv)
13050 typval_T *argvars;
13051 typval_T *rettv;
13052{
13053 int binary = FALSE;
13054 char_u *fname;
13055 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013056 listitem_T *li;
13057#define FREAD_SIZE 200 /* optimized for text lines */
13058 char_u buf[FREAD_SIZE];
13059 int readlen; /* size of last fread() */
13060 int buflen; /* nr of valid chars in buf[] */
13061 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13062 int tolist; /* first byte in buf[] still to be put in list */
13063 int chop; /* how many CR to chop off */
13064 char_u *prev = NULL; /* previously read bytes, if any */
13065 int prevlen = 0; /* length of "prev" if not NULL */
13066 char_u *s;
13067 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013068 long maxline = MAXLNUM;
13069 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013070
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013071 if (argvars[1].v_type != VAR_UNKNOWN)
13072 {
13073 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13074 binary = TRUE;
13075 if (argvars[2].v_type != VAR_UNKNOWN)
13076 maxline = get_tv_number(&argvars[2]);
13077 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013078
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013079 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013080 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013081
13082 /* Always open the file in binary mode, library functions have a mind of
13083 * their own about CR-LF conversion. */
13084 fname = get_tv_string(&argvars[0]);
13085 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13086 {
13087 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13088 return;
13089 }
13090
13091 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013092 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013093 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013094 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013095 buflen = filtd + readlen;
13096 tolist = 0;
13097 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13098 {
13099 if (buf[filtd] == '\n' || readlen <= 0)
13100 {
13101 /* Only when in binary mode add an empty list item when the
13102 * last line ends in a '\n'. */
13103 if (!binary && readlen == 0 && filtd == 0)
13104 break;
13105
13106 /* Found end-of-line or end-of-file: add a text line to the
13107 * list. */
13108 chop = 0;
13109 if (!binary)
13110 while (filtd - chop - 1 >= tolist
13111 && buf[filtd - chop - 1] == '\r')
13112 ++chop;
13113 len = filtd - tolist - chop;
13114 if (prev == NULL)
13115 s = vim_strnsave(buf + tolist, len);
13116 else
13117 {
13118 s = alloc((unsigned)(prevlen + len + 1));
13119 if (s != NULL)
13120 {
13121 mch_memmove(s, prev, prevlen);
13122 vim_free(prev);
13123 prev = NULL;
13124 mch_memmove(s + prevlen, buf + tolist, len);
13125 s[prevlen + len] = NUL;
13126 }
13127 }
13128 tolist = filtd + 1;
13129
13130 li = listitem_alloc();
13131 if (li == NULL)
13132 {
13133 vim_free(s);
13134 break;
13135 }
13136 li->li_tv.v_type = VAR_STRING;
13137 li->li_tv.v_lock = 0;
13138 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013139 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013140
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013141 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013142 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013143 if (readlen <= 0)
13144 break;
13145 }
13146 else if (buf[filtd] == NUL)
13147 buf[filtd] = '\n';
13148 }
13149 if (readlen <= 0)
13150 break;
13151
13152 if (tolist == 0)
13153 {
13154 /* "buf" is full, need to move text to an allocated buffer */
13155 if (prev == NULL)
13156 {
13157 prev = vim_strnsave(buf, buflen);
13158 prevlen = buflen;
13159 }
13160 else
13161 {
13162 s = alloc((unsigned)(prevlen + buflen));
13163 if (s != NULL)
13164 {
13165 mch_memmove(s, prev, prevlen);
13166 mch_memmove(s + prevlen, buf, buflen);
13167 vim_free(prev);
13168 prev = s;
13169 prevlen += buflen;
13170 }
13171 }
13172 filtd = 0;
13173 }
13174 else
13175 {
13176 mch_memmove(buf, buf + tolist, buflen - tolist);
13177 filtd -= tolist;
13178 }
13179 }
13180
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013181 /*
13182 * For a negative line count use only the lines at the end of the file,
13183 * free the rest.
13184 */
13185 if (maxline < 0)
13186 while (cnt > -maxline)
13187 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013188 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013189 --cnt;
13190 }
13191
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013192 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013193 fclose(fd);
13194}
13195
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013196#if defined(FEAT_RELTIME)
13197static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13198
13199/*
13200 * Convert a List to proftime_T.
13201 * Return FAIL when there is something wrong.
13202 */
13203 static int
13204list2proftime(arg, tm)
13205 typval_T *arg;
13206 proftime_T *tm;
13207{
13208 long n1, n2;
13209 int error = FALSE;
13210
13211 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13212 || arg->vval.v_list->lv_len != 2)
13213 return FAIL;
13214 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13215 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13216# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013217 tm->HighPart = n1;
13218 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013219# else
13220 tm->tv_sec = n1;
13221 tm->tv_usec = n2;
13222# endif
13223 return error ? FAIL : OK;
13224}
13225#endif /* FEAT_RELTIME */
13226
13227/*
13228 * "reltime()" function
13229 */
13230 static void
13231f_reltime(argvars, rettv)
13232 typval_T *argvars;
13233 typval_T *rettv;
13234{
13235#ifdef FEAT_RELTIME
13236 proftime_T res;
13237 proftime_T start;
13238
13239 if (argvars[0].v_type == VAR_UNKNOWN)
13240 {
13241 /* No arguments: get current time. */
13242 profile_start(&res);
13243 }
13244 else if (argvars[1].v_type == VAR_UNKNOWN)
13245 {
13246 if (list2proftime(&argvars[0], &res) == FAIL)
13247 return;
13248 profile_end(&res);
13249 }
13250 else
13251 {
13252 /* Two arguments: compute the difference. */
13253 if (list2proftime(&argvars[0], &start) == FAIL
13254 || list2proftime(&argvars[1], &res) == FAIL)
13255 return;
13256 profile_sub(&res, &start);
13257 }
13258
13259 if (rettv_list_alloc(rettv) == OK)
13260 {
13261 long n1, n2;
13262
13263# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013264 n1 = res.HighPart;
13265 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013266# else
13267 n1 = res.tv_sec;
13268 n2 = res.tv_usec;
13269# endif
13270 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
13271 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
13272 }
13273#endif
13274}
13275
13276/*
13277 * "reltimestr()" function
13278 */
13279 static void
13280f_reltimestr(argvars, rettv)
13281 typval_T *argvars;
13282 typval_T *rettv;
13283{
13284#ifdef FEAT_RELTIME
13285 proftime_T tm;
13286#endif
13287
13288 rettv->v_type = VAR_STRING;
13289 rettv->vval.v_string = NULL;
13290#ifdef FEAT_RELTIME
13291 if (list2proftime(&argvars[0], &tm) == OK)
13292 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
13293#endif
13294}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013295
Bram Moolenaar0d660222005-01-07 21:51:51 +000013296#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13297static void make_connection __ARGS((void));
13298static int check_connection __ARGS((void));
13299
13300 static void
13301make_connection()
13302{
13303 if (X_DISPLAY == NULL
13304# ifdef FEAT_GUI
13305 && !gui.in_use
13306# endif
13307 )
13308 {
13309 x_force_connect = TRUE;
13310 setup_term_clip();
13311 x_force_connect = FALSE;
13312 }
13313}
13314
13315 static int
13316check_connection()
13317{
13318 make_connection();
13319 if (X_DISPLAY == NULL)
13320 {
13321 EMSG(_("E240: No connection to Vim server"));
13322 return FAIL;
13323 }
13324 return OK;
13325}
13326#endif
13327
13328#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013329static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330
13331 static void
13332remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000013333 typval_T *argvars;
13334 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013335 int expr;
13336{
13337 char_u *server_name;
13338 char_u *keys;
13339 char_u *r = NULL;
13340 char_u buf[NUMBUFLEN];
13341# ifdef WIN32
13342 HWND w;
13343# else
13344 Window w;
13345# endif
13346
13347 if (check_restricted() || check_secure())
13348 return;
13349
13350# ifdef FEAT_X11
13351 if (check_connection() == FAIL)
13352 return;
13353# endif
13354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013355 server_name = get_tv_string_chk(&argvars[0]);
13356 if (server_name == NULL)
13357 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013358 keys = get_tv_string_buf(&argvars[1], buf);
13359# ifdef WIN32
13360 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
13361# else
13362 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
13363 < 0)
13364# endif
13365 {
13366 if (r != NULL)
13367 EMSG(r); /* sending worked but evaluation failed */
13368 else
13369 EMSG2(_("E241: Unable to send to %s"), server_name);
13370 return;
13371 }
13372
13373 rettv->vval.v_string = r;
13374
13375 if (argvars[2].v_type != VAR_UNKNOWN)
13376 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000013378 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013380
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013381 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 v.di_tv.v_type = VAR_STRING;
13383 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 idvar = get_tv_string_chk(&argvars[2]);
13385 if (idvar != NULL)
13386 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013388 }
13389}
13390#endif
13391
13392/*
13393 * "remote_expr()" function
13394 */
13395/*ARGSUSED*/
13396 static void
13397f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013400{
13401 rettv->v_type = VAR_STRING;
13402 rettv->vval.v_string = NULL;
13403#ifdef FEAT_CLIENTSERVER
13404 remote_common(argvars, rettv, TRUE);
13405#endif
13406}
13407
13408/*
13409 * "remote_foreground()" function
13410 */
13411/*ARGSUSED*/
13412 static void
13413f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013414 typval_T *argvars;
13415 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013416{
13417 rettv->vval.v_number = 0;
13418#ifdef FEAT_CLIENTSERVER
13419# ifdef WIN32
13420 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421 {
13422 char_u *server_name = get_tv_string_chk(&argvars[0]);
13423
13424 if (server_name != NULL)
13425 serverForeground(server_name);
13426 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013427# else
13428 /* Send a foreground() expression to the server. */
13429 argvars[1].v_type = VAR_STRING;
13430 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
13431 argvars[2].v_type = VAR_UNKNOWN;
13432 remote_common(argvars, rettv, TRUE);
13433 vim_free(argvars[1].vval.v_string);
13434# endif
13435#endif
13436}
13437
13438/*ARGSUSED*/
13439 static void
13440f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013441 typval_T *argvars;
13442 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443{
13444#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013446 char_u *s = NULL;
13447# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013448 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013449# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013450 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013451
13452 if (check_restricted() || check_secure())
13453 {
13454 rettv->vval.v_number = -1;
13455 return;
13456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013457 serverid = get_tv_string_chk(&argvars[0]);
13458 if (serverid == NULL)
13459 {
13460 rettv->vval.v_number = -1;
13461 return; /* type error; errmsg already given */
13462 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013463# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013464 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013465 if (n == 0)
13466 rettv->vval.v_number = -1;
13467 else
13468 {
13469 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
13470 rettv->vval.v_number = (s != NULL);
13471 }
13472# else
13473 rettv->vval.v_number = 0;
13474 if (check_connection() == FAIL)
13475 return;
13476
13477 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013479# endif
13480
13481 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
13482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 char_u *retvar;
13484
Bram Moolenaar33570922005-01-25 22:26:29 +000013485 v.di_tv.v_type = VAR_STRING;
13486 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 retvar = get_tv_string_chk(&argvars[1]);
13488 if (retvar != NULL)
13489 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000013490 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491 }
13492#else
13493 rettv->vval.v_number = -1;
13494#endif
13495}
13496
13497/*ARGSUSED*/
13498 static void
13499f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013502{
13503 char_u *r = NULL;
13504
13505#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 char_u *serverid = get_tv_string_chk(&argvars[0]);
13507
13508 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000013509 {
13510# ifdef WIN32
13511 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013512 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013513
Bram Moolenaareb3593b2006-04-22 22:33:57 +000013514 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013515 if (n != 0)
13516 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
13517 if (r == NULL)
13518# else
13519 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013520 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013521# endif
13522 EMSG(_("E277: Unable to read a server reply"));
13523 }
13524#endif
13525 rettv->v_type = VAR_STRING;
13526 rettv->vval.v_string = r;
13527}
13528
13529/*
13530 * "remote_send()" function
13531 */
13532/*ARGSUSED*/
13533 static void
13534f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *argvars;
13536 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013537{
13538 rettv->v_type = VAR_STRING;
13539 rettv->vval.v_string = NULL;
13540#ifdef FEAT_CLIENTSERVER
13541 remote_common(argvars, rettv, FALSE);
13542#endif
13543}
13544
13545/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013546 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013547 */
13548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013550 typval_T *argvars;
13551 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013552{
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 list_T *l;
13554 listitem_T *item, *item2;
13555 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013556 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013557 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013558 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 dict_T *d;
13560 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013561
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013562 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013563 if (argvars[0].v_type == VAR_DICT)
13564 {
13565 if (argvars[2].v_type != VAR_UNKNOWN)
13566 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013567 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013568 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013570 key = get_tv_string_chk(&argvars[1]);
13571 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013573 di = dict_find(d, key, -1);
13574 if (di == NULL)
13575 EMSG2(_(e_dictkey), key);
13576 else
13577 {
13578 *rettv = di->di_tv;
13579 init_tv(&di->di_tv);
13580 dictitem_remove(d, di);
13581 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013582 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013583 }
13584 }
13585 else if (argvars[0].v_type != VAR_LIST)
13586 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013587 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000013588 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013590 int error = FALSE;
13591
13592 idx = get_tv_number_chk(&argvars[1], &error);
13593 if (error)
13594 ; /* type error: do nothing, errmsg already given */
13595 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013596 EMSGN(_(e_listidx), idx);
13597 else
13598 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013599 if (argvars[2].v_type == VAR_UNKNOWN)
13600 {
13601 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013602 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013603 *rettv = item->li_tv;
13604 vim_free(item);
13605 }
13606 else
13607 {
13608 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013609 end = get_tv_number_chk(&argvars[2], &error);
13610 if (error)
13611 ; /* type error: do nothing */
13612 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013613 EMSGN(_(e_listidx), end);
13614 else
13615 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013616 int cnt = 0;
13617
13618 for (li = item; li != NULL; li = li->li_next)
13619 {
13620 ++cnt;
13621 if (li == item2)
13622 break;
13623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013624 if (li == NULL) /* didn't find "item2" after "item" */
13625 EMSG(_(e_invrange));
13626 else
13627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000013628 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013629 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013630 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013631 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013632 l->lv_first = item;
13633 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013634 item->li_prev = NULL;
13635 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013636 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013637 }
13638 }
13639 }
13640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013641 }
13642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643}
13644
13645/*
13646 * "rename({from}, {to})" function
13647 */
13648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013650 typval_T *argvars;
13651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652{
13653 char_u buf[NUMBUFLEN];
13654
13655 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013656 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
13659 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
13662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013663 * "repeat()" function
13664 */
13665/*ARGSUSED*/
13666 static void
13667f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013668 typval_T *argvars;
13669 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013670{
13671 char_u *p;
13672 int n;
13673 int slen;
13674 int len;
13675 char_u *r;
13676 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013677
13678 n = get_tv_number(&argvars[1]);
13679 if (argvars[0].v_type == VAR_LIST)
13680 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013681 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013682 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013683 if (list_extend(rettv->vval.v_list,
13684 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013685 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013686 }
13687 else
13688 {
13689 p = get_tv_string(&argvars[0]);
13690 rettv->v_type = VAR_STRING;
13691 rettv->vval.v_string = NULL;
13692
13693 slen = (int)STRLEN(p);
13694 len = slen * n;
13695 if (len <= 0)
13696 return;
13697
13698 r = alloc(len + 1);
13699 if (r != NULL)
13700 {
13701 for (i = 0; i < n; i++)
13702 mch_memmove(r + i * slen, p, (size_t)slen);
13703 r[len] = NUL;
13704 }
13705
13706 rettv->vval.v_string = r;
13707 }
13708}
13709
13710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 * "resolve()" function
13712 */
13713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013715 typval_T *argvars;
13716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717{
13718 char_u *p;
13719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721#ifdef FEAT_SHORTCUT
13722 {
13723 char_u *v = NULL;
13724
13725 v = mch_resolve_shortcut(p);
13726 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013727 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 }
13731#else
13732# ifdef HAVE_READLINK
13733 {
13734 char_u buf[MAXPATHL + 1];
13735 char_u *cpy;
13736 int len;
13737 char_u *remain = NULL;
13738 char_u *q;
13739 int is_relative_to_current = FALSE;
13740 int has_trailing_pathsep = FALSE;
13741 int limit = 100;
13742
13743 p = vim_strsave(p);
13744
13745 if (p[0] == '.' && (vim_ispathsep(p[1])
13746 || (p[1] == '.' && (vim_ispathsep(p[2])))))
13747 is_relative_to_current = TRUE;
13748
13749 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013750 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 has_trailing_pathsep = TRUE;
13752
13753 q = getnextcomp(p);
13754 if (*q != NUL)
13755 {
13756 /* Separate the first path component in "p", and keep the
13757 * remainder (beginning with the path separator). */
13758 remain = vim_strsave(q - 1);
13759 q[-1] = NUL;
13760 }
13761
13762 for (;;)
13763 {
13764 for (;;)
13765 {
13766 len = readlink((char *)p, (char *)buf, MAXPATHL);
13767 if (len <= 0)
13768 break;
13769 buf[len] = NUL;
13770
13771 if (limit-- == 0)
13772 {
13773 vim_free(p);
13774 vim_free(remain);
13775 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 goto fail;
13778 }
13779
13780 /* Ensure that the result will have a trailing path separator
13781 * if the argument has one. */
13782 if (remain == NULL && has_trailing_pathsep)
13783 add_pathsep(buf);
13784
13785 /* Separate the first path component in the link value and
13786 * concatenate the remainders. */
13787 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
13788 if (*q != NUL)
13789 {
13790 if (remain == NULL)
13791 remain = vim_strsave(q - 1);
13792 else
13793 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000013794 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 if (cpy != NULL)
13796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 vim_free(remain);
13798 remain = cpy;
13799 }
13800 }
13801 q[-1] = NUL;
13802 }
13803
13804 q = gettail(p);
13805 if (q > p && *q == NUL)
13806 {
13807 /* Ignore trailing path separator. */
13808 q[-1] = NUL;
13809 q = gettail(p);
13810 }
13811 if (q > p && !mch_isFullName(buf))
13812 {
13813 /* symlink is relative to directory of argument */
13814 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
13815 if (cpy != NULL)
13816 {
13817 STRCPY(cpy, p);
13818 STRCPY(gettail(cpy), buf);
13819 vim_free(p);
13820 p = cpy;
13821 }
13822 }
13823 else
13824 {
13825 vim_free(p);
13826 p = vim_strsave(buf);
13827 }
13828 }
13829
13830 if (remain == NULL)
13831 break;
13832
13833 /* Append the first path component of "remain" to "p". */
13834 q = getnextcomp(remain + 1);
13835 len = q - remain - (*q != NUL);
13836 cpy = vim_strnsave(p, STRLEN(p) + len);
13837 if (cpy != NULL)
13838 {
13839 STRNCAT(cpy, remain, len);
13840 vim_free(p);
13841 p = cpy;
13842 }
13843 /* Shorten "remain". */
13844 if (*q != NUL)
Bram Moolenaar452a81b2007-08-06 20:28:43 +000013845 mch_memmove(remain, q - 1, STRLEN(q - 1) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 else
13847 {
13848 vim_free(remain);
13849 remain = NULL;
13850 }
13851 }
13852
13853 /* If the result is a relative path name, make it explicitly relative to
13854 * the current directory if and only if the argument had this form. */
13855 if (!vim_ispathsep(*p))
13856 {
13857 if (is_relative_to_current
13858 && *p != NUL
13859 && !(p[0] == '.'
13860 && (p[1] == NUL
13861 || vim_ispathsep(p[1])
13862 || (p[1] == '.'
13863 && (p[2] == NUL
13864 || vim_ispathsep(p[2]))))))
13865 {
13866 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013867 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 if (cpy != NULL)
13869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 vim_free(p);
13871 p = cpy;
13872 }
13873 }
13874 else if (!is_relative_to_current)
13875 {
13876 /* Strip leading "./". */
13877 q = p;
13878 while (q[0] == '.' && vim_ispathsep(q[1]))
13879 q += 2;
13880 if (q > p)
13881 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
13882 }
13883 }
13884
13885 /* Ensure that the result will have no trailing path separator
13886 * if the argument had none. But keep "/" or "//". */
13887 if (!has_trailing_pathsep)
13888 {
13889 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013890 if (after_pathsep(p, q))
13891 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 }
13893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013894 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 }
13896# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013897 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898# endif
13899#endif
13900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013901 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902
13903#ifdef HAVE_READLINK
13904fail:
13905#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907}
13908
13909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013910 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 */
13912 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013914 typval_T *argvars;
13915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916{
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 list_T *l;
13918 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919
Bram Moolenaar0d660222005-01-07 21:51:51 +000013920 rettv->vval.v_number = 0;
13921 if (argvars[0].v_type != VAR_LIST)
13922 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013923 else if ((l = argvars[0].vval.v_list) != NULL
13924 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013925 {
13926 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013927 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013928 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013929 while (li != NULL)
13930 {
13931 ni = li->li_prev;
13932 list_append(l, li);
13933 li = ni;
13934 }
13935 rettv->vval.v_list = l;
13936 rettv->v_type = VAR_LIST;
13937 ++l->lv_refcount;
13938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939}
13940
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013941#define SP_NOMOVE 0x01 /* don't move cursor */
13942#define SP_REPEAT 0x02 /* repeat to find outer pair */
13943#define SP_RETCOUNT 0x04 /* return matchcount */
13944#define SP_SETPCMARK 0x08 /* set previous context mark */
13945#define SP_START 0x10 /* accept match at start position */
13946#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13947#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013948
Bram Moolenaar33570922005-01-25 22:26:29 +000013949static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950
13951/*
13952 * Get flags for a search function.
13953 * Possibly sets "p_ws".
13954 * Returns BACKWARD, FORWARD or zero (for an error).
13955 */
13956 static int
13957get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013958 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 int *flagsp;
13960{
13961 int dir = FORWARD;
13962 char_u *flags;
13963 char_u nbuf[NUMBUFLEN];
13964 int mask;
13965
13966 if (varp->v_type != VAR_UNKNOWN)
13967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 flags = get_tv_string_buf_chk(varp, nbuf);
13969 if (flags == NULL)
13970 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971 while (*flags != NUL)
13972 {
13973 switch (*flags)
13974 {
13975 case 'b': dir = BACKWARD; break;
13976 case 'w': p_ws = TRUE; break;
13977 case 'W': p_ws = FALSE; break;
13978 default: mask = 0;
13979 if (flagsp != NULL)
13980 switch (*flags)
13981 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013982 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000013983 case 'e': mask = SP_END; break;
13984 case 'm': mask = SP_RETCOUNT; break;
13985 case 'n': mask = SP_NOMOVE; break;
13986 case 'p': mask = SP_SUBPAT; break;
13987 case 'r': mask = SP_REPEAT; break;
13988 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013989 }
13990 if (mask == 0)
13991 {
13992 EMSG2(_(e_invarg2), flags);
13993 dir = 0;
13994 }
13995 else
13996 *flagsp |= mask;
13997 }
13998 if (dir == 0)
13999 break;
14000 ++flags;
14001 }
14002 }
14003 return dir;
14004}
14005
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014007 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014009 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014010search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014011 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014012 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014013 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014015 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 char_u *pat;
14017 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014018 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 int save_p_ws = p_ws;
14020 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014021 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014022 long lnum_stop = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014023 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014024 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014026 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014027 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014028 if (dir == 0)
14029 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014030 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014031 if (flags & SP_START)
14032 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014033 if (flags & SP_END)
14034 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014035
14036 /* Optional extra argument: line number to stop searching. */
14037 if (argvars[1].v_type != VAR_UNKNOWN
14038 && argvars[2].v_type != VAR_UNKNOWN)
14039 {
14040 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14041 if (lnum_stop < 0)
14042 goto theend;
14043 }
14044
Bram Moolenaar231334e2005-07-25 20:46:57 +000014045 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014046 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014047 * Check to make sure only those flags are set.
14048 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14049 * flags cannot be set. Check for that condition also.
14050 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014051 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014052 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014055 goto theend;
14056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014058 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014059 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
14060 options, RE_SEARCH, (linenr_T)lnum_stop);
14061 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014063 if (flags & SP_SUBPAT)
14064 retval = subpatnum;
14065 else
14066 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014067 if (flags & SP_SETPCMARK)
14068 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014070 if (match_pos != NULL)
14071 {
14072 /* Store the match cursor position */
14073 match_pos->lnum = pos.lnum;
14074 match_pos->col = pos.col + 1;
14075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 /* "/$" will put the cursor after the end of the line, may need to
14077 * correct that here */
14078 check_cursor();
14079 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014080
14081 /* If 'n' flag is used: restore cursor position. */
14082 if (flags & SP_NOMOVE)
14083 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014084 else
14085 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014086theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014088
14089 return retval;
14090}
14091
14092/*
14093 * "search()" function
14094 */
14095 static void
14096f_search(argvars, rettv)
14097 typval_T *argvars;
14098 typval_T *rettv;
14099{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014100 int flags = 0;
14101
14102 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103}
14104
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014106 * "searchdecl()" function
14107 */
14108 static void
14109f_searchdecl(argvars, rettv)
14110 typval_T *argvars;
14111 typval_T *rettv;
14112{
14113 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014114 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014115 int error = FALSE;
14116 char_u *name;
14117
14118 rettv->vval.v_number = 1; /* default: FAIL */
14119
14120 name = get_tv_string_chk(&argvars[0]);
14121 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014122 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014123 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014124 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14125 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14126 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014127 if (!error && name != NULL)
14128 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014129 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014130}
14131
14132/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014133 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014135 static int
14136searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014137 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014138 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139{
14140 char_u *spat, *mpat, *epat;
14141 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 int dir;
14144 int flags = 0;
14145 char_u nbuf1[NUMBUFLEN];
14146 char_u nbuf2[NUMBUFLEN];
14147 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014148 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014149 long lnum_stop = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 spat = get_tv_string_chk(&argvars[0]);
14153 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14154 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14155 if (spat == NULL || mpat == NULL || epat == NULL)
14156 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 /* Handle the optional fourth argument: flags */
14159 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014160 if (dir == 0)
14161 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014162
14163 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014164 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14165 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014166 if ((flags & (SP_END | SP_SUBPAT)) != 0
14167 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014168 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014169 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014170 goto theend;
14171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014173 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014174 if (argvars[3].v_type == VAR_UNKNOWN
14175 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 skip = (char_u *)"";
14177 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014180 if (argvars[5].v_type != VAR_UNKNOWN)
14181 {
14182 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14183 if (lnum_stop < 0)
14184 goto theend;
14185 }
14186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 if (skip == NULL)
14188 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014190 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
14191 match_pos, lnum_stop);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014192
14193theend:
14194 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014195
14196 return retval;
14197}
14198
14199/*
14200 * "searchpair()" function
14201 */
14202 static void
14203f_searchpair(argvars, rettv)
14204 typval_T *argvars;
14205 typval_T *rettv;
14206{
14207 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
14208}
14209
14210/*
14211 * "searchpairpos()" function
14212 */
14213 static void
14214f_searchpairpos(argvars, rettv)
14215 typval_T *argvars;
14216 typval_T *rettv;
14217{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014218 pos_T match_pos;
14219 int lnum = 0;
14220 int col = 0;
14221
14222 rettv->vval.v_number = 0;
14223
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014224 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014225 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014226
14227 if (searchpair_cmn(argvars, &match_pos) > 0)
14228 {
14229 lnum = match_pos.lnum;
14230 col = match_pos.col;
14231 }
14232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014233 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14234 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014235}
14236
14237/*
14238 * Search for a start/middle/end thing.
14239 * Used by searchpair(), see its documentation for the details.
14240 * Returns 0 or -1 for no match,
14241 */
14242 long
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014243do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos, lnum_stop)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014244 char_u *spat; /* start pattern */
14245 char_u *mpat; /* middle pattern */
14246 char_u *epat; /* end pattern */
14247 int dir; /* BACKWARD or FORWARD */
14248 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014249 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014250 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014251 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014252{
14253 char_u *save_cpo;
14254 char_u *pat, *pat2 = NULL, *pat3 = NULL;
14255 long retval = 0;
14256 pos_T pos;
14257 pos_T firstpos;
14258 pos_T foundpos;
14259 pos_T save_cursor;
14260 pos_T save_pos;
14261 int n;
14262 int r;
14263 int nest = 1;
14264 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014265 int options = SEARCH_KEEP;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014266
14267 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14268 save_cpo = p_cpo;
14269 p_cpo = (char_u *)"";
14270
14271 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14272 * start/middle/end (pat3, for the top pair). */
14273 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
14274 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
14275 if (pat2 == NULL || pat3 == NULL)
14276 goto theend;
14277 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
14278 if (*mpat == NUL)
14279 STRCPY(pat3, pat2);
14280 else
14281 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14282 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014283 if (flags & SP_START)
14284 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014285
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 save_cursor = curwin->w_cursor;
14287 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000014288 clearpos(&firstpos);
14289 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 pat = pat3;
14291 for (;;)
14292 {
14293 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014294 options, RE_SEARCH, lnum_stop);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
14296 /* didn't find it or found the first match again: FAIL */
14297 break;
14298
14299 if (firstpos.lnum == 0)
14300 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000014301 if (equalpos(pos, foundpos))
14302 {
14303 /* Found the same position again. Can happen with a pattern that
14304 * has "\zs" at the end and searching backwards. Advance one
14305 * character and try again. */
14306 if (dir == BACKWARD)
14307 decl(&pos);
14308 else
14309 incl(&pos);
14310 }
14311 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312
14313 /* If the skip pattern matches, ignore this match. */
14314 if (*skip != NUL)
14315 {
14316 save_pos = curwin->w_cursor;
14317 curwin->w_cursor = pos;
14318 r = eval_to_bool(skip, &err, NULL, FALSE);
14319 curwin->w_cursor = save_pos;
14320 if (err)
14321 {
14322 /* Evaluating {skip} caused an error, break here. */
14323 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014324 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 break;
14326 }
14327 if (r)
14328 continue;
14329 }
14330
14331 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
14332 {
14333 /* Found end when searching backwards or start when searching
14334 * forward: nested pair. */
14335 ++nest;
14336 pat = pat2; /* nested, don't search for middle */
14337 }
14338 else
14339 {
14340 /* Found end when searching forward or start when searching
14341 * backward: end of (nested) pair; or found middle in outer pair. */
14342 if (--nest == 1)
14343 pat = pat3; /* outer level, search for middle */
14344 }
14345
14346 if (nest == 0)
14347 {
14348 /* Found the match: return matchcount or line number. */
14349 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014350 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014352 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014353 if (flags & SP_SETPCMARK)
14354 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 curwin->w_cursor = pos;
14356 if (!(flags & SP_REPEAT))
14357 break;
14358 nest = 1; /* search for next unmatched */
14359 }
14360 }
14361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014362 if (match_pos != NULL)
14363 {
14364 /* Store the match cursor position */
14365 match_pos->lnum = curwin->w_cursor.lnum;
14366 match_pos->col = curwin->w_cursor.col + 1;
14367 }
14368
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014370 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 curwin->w_cursor = save_cursor;
14372
14373theend:
14374 vim_free(pat2);
14375 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014377
14378 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379}
14380
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014381/*
14382 * "searchpos()" function
14383 */
14384 static void
14385f_searchpos(argvars, rettv)
14386 typval_T *argvars;
14387 typval_T *rettv;
14388{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014389 pos_T match_pos;
14390 int lnum = 0;
14391 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014392 int n;
14393 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014394
14395 rettv->vval.v_number = 0;
14396
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014397 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014398 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014399
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014400 n = search_cmn(argvars, &match_pos, &flags);
14401 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014402 {
14403 lnum = match_pos.lnum;
14404 col = match_pos.col;
14405 }
14406
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014407 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
14408 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014409 if (flags & SP_SUBPAT)
14410 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014411}
14412
14413
Bram Moolenaar0d660222005-01-07 21:51:51 +000014414/*ARGSUSED*/
14415 static void
14416f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014417 typval_T *argvars;
14418 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014420#ifdef FEAT_CLIENTSERVER
14421 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 char_u *server = get_tv_string_chk(&argvars[0]);
14423 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 if (server == NULL || reply == NULL)
14427 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428 if (check_restricted() || check_secure())
14429 return;
14430# ifdef FEAT_X11
14431 if (check_connection() == FAIL)
14432 return;
14433# endif
14434
14435 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437 EMSG(_("E258: Unable to send to client"));
14438 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440 rettv->vval.v_number = 0;
14441#else
14442 rettv->vval.v_number = -1;
14443#endif
14444}
14445
14446/*ARGSUSED*/
14447 static void
14448f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 typval_T *argvars;
14450 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014451{
14452 char_u *r = NULL;
14453
14454#ifdef FEAT_CLIENTSERVER
14455# ifdef WIN32
14456 r = serverGetVimNames();
14457# else
14458 make_connection();
14459 if (X_DISPLAY != NULL)
14460 r = serverGetVimNames(X_DISPLAY);
14461# endif
14462#endif
14463 rettv->v_type = VAR_STRING;
14464 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
14467/*
14468 * "setbufvar()" function
14469 */
14470/*ARGSUSED*/
14471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014472f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014473 typval_T *argvars;
14474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
14476 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014479 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 char_u nbuf[NUMBUFLEN];
14481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014482 rettv->vval.v_number = 0;
14483
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 if (check_restricted() || check_secure())
14485 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014486 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
14487 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014488 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 varp = &argvars[2];
14490
14491 if (buf != NULL && varname != NULL && varp != NULL)
14492 {
14493 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495
14496 if (*varname == '&')
14497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014498 long numval;
14499 char_u *strval;
14500 int error = FALSE;
14501
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014503 numval = get_tv_number_chk(varp, &error);
14504 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 if (!error && strval != NULL)
14506 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 }
14508 else
14509 {
14510 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
14511 if (bufvarname != NULL)
14512 {
14513 STRCPY(bufvarname, "b:");
14514 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014515 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 vim_free(bufvarname);
14517 }
14518 }
14519
14520 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523}
14524
14525/*
14526 * "setcmdpos()" function
14527 */
14528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 typval_T *argvars;
14531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 int pos = (int)get_tv_number(&argvars[0]) - 1;
14534
14535 if (pos >= 0)
14536 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537}
14538
14539/*
14540 * "setline()" function
14541 */
14542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014543f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 typval_T *argvars;
14545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
14547 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000014548 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014549 list_T *l = NULL;
14550 listitem_T *li = NULL;
14551 long added = 0;
14552 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014554 lnum = get_tv_lnum(&argvars[0]);
14555 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014557 l = argvars[1].vval.v_list;
14558 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014560 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014561 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014562
14563 rettv->vval.v_number = 0; /* OK */
14564 for (;;)
14565 {
14566 if (l != NULL)
14567 {
14568 /* list argument, get next string */
14569 if (li == NULL)
14570 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014571 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014572 li = li->li_next;
14573 }
14574
14575 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014576 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014577 break;
14578 if (lnum <= curbuf->b_ml.ml_line_count)
14579 {
14580 /* existing line, replace it */
14581 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
14582 {
14583 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000014584 if (lnum == curwin->w_cursor.lnum)
14585 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000014586 rettv->vval.v_number = 0; /* OK */
14587 }
14588 }
14589 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
14590 {
14591 /* lnum is one past the last line, append the line */
14592 ++added;
14593 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
14594 rettv->vval.v_number = 0; /* OK */
14595 }
14596
14597 if (l == NULL) /* only one string argument */
14598 break;
14599 ++lnum;
14600 }
14601
14602 if (added > 0)
14603 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604}
14605
14606/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014607 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000014608 */
14609/*ARGSUSED*/
14610 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014611set_qf_ll_list(wp, list_arg, action_arg, rettv)
14612 win_T *wp;
14613 typval_T *list_arg;
14614 typval_T *action_arg;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014615 typval_T *rettv;
14616{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014617#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014618 char_u *act;
14619 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000014620#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014621
Bram Moolenaar2641f772005-03-25 21:58:17 +000014622 rettv->vval.v_number = -1;
14623
14624#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014625 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014626 EMSG(_(e_listreq));
14627 else
14628 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014629 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000014630
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014631 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014632 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014633 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014634 if (act == NULL)
14635 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000014636 if (*act == 'a' || *act == 'r')
14637 action = *act;
14638 }
14639
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014640 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000014641 rettv->vval.v_number = 0;
14642 }
14643#endif
14644}
14645
14646/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014647 * "setloclist()" function
14648 */
14649/*ARGSUSED*/
14650 static void
14651f_setloclist(argvars, rettv)
14652 typval_T *argvars;
14653 typval_T *rettv;
14654{
14655 win_T *win;
14656
14657 rettv->vval.v_number = -1;
14658
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014659 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014660 if (win != NULL)
14661 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
14662}
14663
14664/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014665 * "setmatches()" function
14666 */
14667 static void
14668f_setmatches(argvars, rettv)
14669 typval_T *argvars;
14670 typval_T *rettv;
14671{
14672#ifdef FEAT_SEARCH_EXTRA
14673 list_T *l;
14674 listitem_T *li;
14675 dict_T *d;
14676
14677 rettv->vval.v_number = -1;
14678 if (argvars[0].v_type != VAR_LIST)
14679 {
14680 EMSG(_(e_listreq));
14681 return;
14682 }
14683 if ((l = argvars[0].vval.v_list) != NULL)
14684 {
14685
14686 /* To some extent make sure that we are dealing with a list from
14687 * "getmatches()". */
14688 li = l->lv_first;
14689 while (li != NULL)
14690 {
14691 if (li->li_tv.v_type != VAR_DICT
14692 || (d = li->li_tv.vval.v_dict) == NULL)
14693 {
14694 EMSG(_(e_invarg));
14695 return;
14696 }
14697 if (!(dict_find(d, (char_u *)"group", -1) != NULL
14698 && dict_find(d, (char_u *)"pattern", -1) != NULL
14699 && dict_find(d, (char_u *)"priority", -1) != NULL
14700 && dict_find(d, (char_u *)"id", -1) != NULL))
14701 {
14702 EMSG(_(e_invarg));
14703 return;
14704 }
14705 li = li->li_next;
14706 }
14707
14708 clear_matches(curwin);
14709 li = l->lv_first;
14710 while (li != NULL)
14711 {
14712 d = li->li_tv.vval.v_dict;
14713 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
14714 get_dict_string(d, (char_u *)"pattern", FALSE),
14715 (int)get_dict_number(d, (char_u *)"priority"),
14716 (int)get_dict_number(d, (char_u *)"id"));
14717 li = li->li_next;
14718 }
14719 rettv->vval.v_number = 0;
14720 }
14721#endif
14722}
14723
14724/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014725 * "setpos()" function
14726 */
14727/*ARGSUSED*/
14728 static void
14729f_setpos(argvars, rettv)
14730 typval_T *argvars;
14731 typval_T *rettv;
14732{
14733 pos_T pos;
14734 int fnum;
14735 char_u *name;
14736
14737 name = get_tv_string_chk(argvars);
14738 if (name != NULL)
14739 {
14740 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
14741 {
14742 --pos.col;
14743 if (name[0] == '.') /* cursor */
14744 {
14745 if (fnum == curbuf->b_fnum)
14746 {
14747 curwin->w_cursor = pos;
14748 check_cursor();
14749 }
14750 else
14751 EMSG(_(e_invarg));
14752 }
14753 else if (name[0] == '\'') /* mark */
14754 (void)setmark_pos(name[1], &pos, fnum);
14755 else
14756 EMSG(_(e_invarg));
14757 }
14758 }
14759}
14760
14761/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000014762 * "setqflist()" function
14763 */
14764/*ARGSUSED*/
14765 static void
14766f_setqflist(argvars, rettv)
14767 typval_T *argvars;
14768 typval_T *rettv;
14769{
14770 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
14771}
14772
14773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774 * "setreg()" function
14775 */
14776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014777f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014778 typval_T *argvars;
14779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014780{
14781 int regname;
14782 char_u *strregname;
14783 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 int append;
14786 char_u yank_type;
14787 long block_len;
14788
14789 block_len = -1;
14790 yank_type = MAUTO;
14791 append = FALSE;
14792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014794 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 if (strregname == NULL)
14797 return; /* type error; errmsg already given */
14798 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 if (regname == 0 || regname == '@')
14800 regname = '"';
14801 else if (regname == '=')
14802 return;
14803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014806 stropt = get_tv_string_chk(&argvars[2]);
14807 if (stropt == NULL)
14808 return; /* type error */
14809 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 switch (*stropt)
14811 {
14812 case 'a': case 'A': /* append */
14813 append = TRUE;
14814 break;
14815 case 'v': case 'c': /* character-wise selection */
14816 yank_type = MCHAR;
14817 break;
14818 case 'V': case 'l': /* line-wise selection */
14819 yank_type = MLINE;
14820 break;
14821#ifdef FEAT_VISUAL
14822 case 'b': case Ctrl_V: /* block-wise selection */
14823 yank_type = MBLOCK;
14824 if (VIM_ISDIGIT(stropt[1]))
14825 {
14826 ++stropt;
14827 block_len = getdigits(&stropt) - 1;
14828 --stropt;
14829 }
14830 break;
14831#endif
14832 }
14833 }
14834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 strval = get_tv_string_chk(&argvars[1]);
14836 if (strval != NULL)
14837 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014839 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840}
14841
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014842/*
14843 * "settabwinvar()" function
14844 */
14845 static void
14846f_settabwinvar(argvars, rettv)
14847 typval_T *argvars;
14848 typval_T *rettv;
14849{
14850 setwinvar(argvars, rettv, 1);
14851}
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852
14853/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014854 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014857f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014858 typval_T *argvars;
14859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014861 setwinvar(argvars, rettv, 0);
14862}
14863
14864/*
14865 * "setwinvar()" and "settabwinvar()" functions
14866 */
14867 static void
14868setwinvar(argvars, rettv, off)
14869 typval_T *argvars;
14870 typval_T *rettv;
14871 int off;
14872{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 win_T *win;
14874#ifdef FEAT_WINDOWS
14875 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014876 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877#endif
14878 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014881 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014883 rettv->vval.v_number = 0;
14884
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 if (check_restricted() || check_secure())
14886 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014887
14888#ifdef FEAT_WINDOWS
14889 if (off == 1)
14890 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
14891 else
14892 tp = curtab;
14893#endif
14894 win = find_win_by_nr(&argvars[off], tp);
14895 varname = get_tv_string_chk(&argvars[off + 1]);
14896 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897
14898 if (win != NULL && varname != NULL && varp != NULL)
14899 {
14900#ifdef FEAT_WINDOWS
14901 /* set curwin to be our win, temporarily */
14902 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014903 save_curtab = curtab;
14904 goto_tabpage_tp(tp);
14905 if (!win_valid(win))
14906 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907 curwin = win;
14908 curbuf = curwin->w_buffer;
14909#endif
14910
14911 if (*varname == '&')
14912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014913 long numval;
14914 char_u *strval;
14915 int error = FALSE;
14916
Bram Moolenaar071d4272004-06-13 20:20:40 +000014917 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 numval = get_tv_number_chk(varp, &error);
14919 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014920 if (!error && strval != NULL)
14921 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 }
14923 else
14924 {
14925 winvarname = alloc((unsigned)STRLEN(varname) + 3);
14926 if (winvarname != NULL)
14927 {
14928 STRCPY(winvarname, "w:");
14929 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014930 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 vim_free(winvarname);
14932 }
14933 }
14934
14935#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000014936 /* Restore current tabpage and window, if still valid (autocomands can
14937 * make them invalid). */
14938 if (valid_tabpage(save_curtab))
14939 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 if (win_valid(save_curwin))
14941 {
14942 curwin = save_curwin;
14943 curbuf = curwin->w_buffer;
14944 }
14945#endif
14946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947}
14948
14949/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000014950 * "shellescape({string})" function
14951 */
14952 static void
14953f_shellescape(argvars, rettv)
14954 typval_T *argvars;
14955 typval_T *rettv;
14956{
14957 rettv->vval.v_string = vim_strsave_shellescape(get_tv_string(&argvars[0]));
14958 rettv->v_type = VAR_STRING;
14959}
14960
14961/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014962 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 */
14964 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014965f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014966 typval_T *argvars;
14967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971 p = get_tv_string(&argvars[0]);
14972 rettv->vval.v_string = vim_strsave(p);
14973 simplify_filename(rettv->vval.v_string); /* simplify in place */
14974 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975}
14976
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977static int
14978#ifdef __BORLANDC__
14979 _RTLENTRYF
14980#endif
14981 item_compare __ARGS((const void *s1, const void *s2));
14982static int
14983#ifdef __BORLANDC__
14984 _RTLENTRYF
14985#endif
14986 item_compare2 __ARGS((const void *s1, const void *s2));
14987
14988static int item_compare_ic;
14989static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014990static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014991#define ITEM_COMPARE_FAIL 999
14992
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 static int
14997#ifdef __BORLANDC__
14998_RTLENTRYF
14999#endif
15000item_compare(s1, s2)
15001 const void *s1;
15002 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015004 char_u *p1, *p2;
15005 char_u *tofree1, *tofree2;
15006 int res;
15007 char_u numbuf1[NUMBUFLEN];
15008 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015010 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15011 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015012 if (p1 == NULL)
15013 p1 = (char_u *)"";
15014 if (p2 == NULL)
15015 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015016 if (item_compare_ic)
15017 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019 res = STRCMP(p1, p2);
15020 vim_free(tofree1);
15021 vim_free(tofree2);
15022 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023}
15024
15025 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015026#ifdef __BORLANDC__
15027_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029item_compare2(s1, s2)
15030 const void *s1;
15031 const void *s2;
15032{
15033 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015035 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015038 /* shortcut after failure in previous call; compare all items equal */
15039 if (item_compare_func_err)
15040 return 0;
15041
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015042 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15043 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015044 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15045 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046
15047 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015048 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015049 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050 clear_tv(&argv[0]);
15051 clear_tv(&argv[1]);
15052
15053 if (res == FAIL)
15054 res = ITEM_COMPARE_FAIL;
15055 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015056 /* return value has wrong type */
15057 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15058 if (item_compare_func_err)
15059 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060 clear_tv(&rettv);
15061 return res;
15062}
15063
15064/*
15065 * "sort({list})" function
15066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 typval_T *argvars;
15070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071{
Bram Moolenaar33570922005-01-25 22:26:29 +000015072 list_T *l;
15073 listitem_T *li;
15074 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015075 long len;
15076 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078 rettv->vval.v_number = 0;
15079 if (argvars[0].v_type != VAR_LIST)
15080 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081 else
15082 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015084 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 return;
15086 rettv->vval.v_list = l;
15087 rettv->v_type = VAR_LIST;
15088 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090 len = list_len(l);
15091 if (len <= 1)
15092 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094 item_compare_ic = FALSE;
15095 item_compare_func = NULL;
15096 if (argvars[1].v_type != VAR_UNKNOWN)
15097 {
15098 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100 else
15101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015102 int error = FALSE;
15103
15104 i = get_tv_number_chk(&argvars[1], &error);
15105 if (error)
15106 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107 if (i == 1)
15108 item_compare_ic = TRUE;
15109 else
15110 item_compare_func = get_tv_string(&argvars[1]);
15111 }
15112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113
Bram Moolenaar0d660222005-01-07 21:51:51 +000015114 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116 if (ptrs == NULL)
15117 return;
15118 i = 0;
15119 for (li = l->lv_first; li != NULL; li = li->li_next)
15120 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015122 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123 /* test the compare function */
15124 if (item_compare_func != NULL
15125 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15126 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015127 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 {
15130 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015131 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132 item_compare_func == NULL ? item_compare : item_compare2);
15133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 if (!item_compare_func_err)
15135 {
15136 /* Clear the List and append the items in the sorted order. */
15137 l->lv_first = l->lv_last = NULL;
15138 l->lv_len = 0;
15139 for (i = 0; i < len; ++i)
15140 list_append(l, ptrs[i]);
15141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 }
15143
15144 vim_free(ptrs);
15145 }
15146}
15147
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015148/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015149 * "soundfold({word})" function
15150 */
15151 static void
15152f_soundfold(argvars, rettv)
15153 typval_T *argvars;
15154 typval_T *rettv;
15155{
15156 char_u *s;
15157
15158 rettv->v_type = VAR_STRING;
15159 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015160#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015161 rettv->vval.v_string = eval_soundfold(s);
15162#else
15163 rettv->vval.v_string = vim_strsave(s);
15164#endif
15165}
15166
15167/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015168 * "spellbadword()" function
15169 */
15170/* ARGSUSED */
15171 static void
15172f_spellbadword(argvars, rettv)
15173 typval_T *argvars;
15174 typval_T *rettv;
15175{
Bram Moolenaar4463f292005-09-25 22:20:24 +000015176 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015177 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015178 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015179
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015180 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015181 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015182
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015183#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000015184 if (argvars[0].v_type == VAR_UNKNOWN)
15185 {
15186 /* Find the start and length of the badly spelled word. */
15187 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
15188 if (len != 0)
15189 word = ml_get_cursor();
15190 }
15191 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15192 {
15193 char_u *str = get_tv_string_chk(&argvars[0]);
15194 int capcol = -1;
15195
15196 if (str != NULL)
15197 {
15198 /* Check the argument for spelling. */
15199 while (*str != NUL)
15200 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015201 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015202 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015203 {
15204 word = str;
15205 break;
15206 }
15207 str += len;
15208 }
15209 }
15210 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015211#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000015212
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015213 list_append_string(rettv->vval.v_list, word, len);
15214 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000015215 attr == HLF_SPB ? "bad" :
15216 attr == HLF_SPR ? "rare" :
15217 attr == HLF_SPL ? "local" :
15218 attr == HLF_SPC ? "caps" :
15219 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015220}
15221
15222/*
15223 * "spellsuggest()" function
15224 */
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015225/*ARGSUSED*/
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015226 static void
15227f_spellsuggest(argvars, rettv)
15228 typval_T *argvars;
15229 typval_T *rettv;
15230{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015231#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015232 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015233 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015234 int maxcount;
15235 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015236 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015237 listitem_T *li;
15238 int need_capital = FALSE;
15239#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015240
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015241 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015242 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015243
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015244#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015245 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
15246 {
15247 str = get_tv_string(&argvars[0]);
15248 if (argvars[1].v_type != VAR_UNKNOWN)
15249 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015250 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015251 if (maxcount <= 0)
15252 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000015253 if (argvars[2].v_type != VAR_UNKNOWN)
15254 {
15255 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
15256 if (typeerr)
15257 return;
15258 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015259 }
15260 else
15261 maxcount = 25;
15262
Bram Moolenaar4770d092006-01-12 23:22:24 +000015263 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015264
15265 for (i = 0; i < ga.ga_len; ++i)
15266 {
15267 str = ((char_u **)ga.ga_data)[i];
15268
15269 li = listitem_alloc();
15270 if (li == NULL)
15271 vim_free(str);
15272 else
15273 {
15274 li->li_tv.v_type = VAR_STRING;
15275 li->li_tv.v_lock = 0;
15276 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015277 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015278 }
15279 }
15280 ga_clear(&ga);
15281 }
15282#endif
15283}
15284
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015286f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015287 typval_T *argvars;
15288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015289{
15290 char_u *str;
15291 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015292 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293 regmatch_T regmatch;
15294 char_u patbuf[NUMBUFLEN];
15295 char_u *save_cpo;
15296 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015297 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015298 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015299 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015300
15301 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15302 save_cpo = p_cpo;
15303 p_cpo = (char_u *)"";
15304
15305 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015306 if (argvars[1].v_type != VAR_UNKNOWN)
15307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015308 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15309 if (pat == NULL)
15310 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015311 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015312 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015313 }
15314 if (pat == NULL || *pat == NUL)
15315 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015316
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015317 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319 if (typeerr)
15320 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15323 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015326 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015327 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015328 if (*str == NUL)
15329 match = FALSE; /* empty item at the end */
15330 else
15331 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015332 if (match)
15333 end = regmatch.startp[0];
15334 else
15335 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015336 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
15337 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015338 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015339 if (list_append_string(rettv->vval.v_list, str,
15340 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015342 }
15343 if (!match)
15344 break;
15345 /* Advance to just after the match. */
15346 if (regmatch.endp[0] > str)
15347 col = 0;
15348 else
15349 {
15350 /* Don't get stuck at the same match. */
15351#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015352 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015353#else
15354 col = 1;
15355#endif
15356 }
15357 str = regmatch.endp[0];
15358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359
Bram Moolenaar0d660222005-01-07 21:51:51 +000015360 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362
Bram Moolenaar0d660222005-01-07 21:51:51 +000015363 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364}
15365
Bram Moolenaar2c932302006-03-18 21:42:09 +000015366/*
15367 * "str2nr()" function
15368 */
15369 static void
15370f_str2nr(argvars, rettv)
15371 typval_T *argvars;
15372 typval_T *rettv;
15373{
15374 int base = 10;
15375 char_u *p;
15376 long n;
15377
15378 if (argvars[1].v_type != VAR_UNKNOWN)
15379 {
15380 base = get_tv_number(&argvars[1]);
15381 if (base != 8 && base != 10 && base != 16)
15382 {
15383 EMSG(_(e_invarg));
15384 return;
15385 }
15386 }
15387
15388 p = skipwhite(get_tv_string(&argvars[0]));
15389 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
15390 rettv->vval.v_number = n;
15391}
15392
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393#ifdef HAVE_STRFTIME
15394/*
15395 * "strftime({format}[, {time}])" function
15396 */
15397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 typval_T *argvars;
15400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401{
15402 char_u result_buf[256];
15403 struct tm *curtime;
15404 time_t seconds;
15405 char_u *p;
15406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015410 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 seconds = time(NULL);
15412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 curtime = localtime(&seconds);
15415 /* MSVC returns NULL for an invalid value of seconds. */
15416 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 else
15419 {
15420# ifdef FEAT_MBYTE
15421 vimconv_T conv;
15422 char_u *enc;
15423
15424 conv.vc_type = CONV_NONE;
15425 enc = enc_locale();
15426 convert_setup(&conv, p_enc, enc);
15427 if (conv.vc_type != CONV_NONE)
15428 p = string_convert(&conv, p, NULL);
15429# endif
15430 if (p != NULL)
15431 (void)strftime((char *)result_buf, sizeof(result_buf),
15432 (char *)p, curtime);
15433 else
15434 result_buf[0] = NUL;
15435
15436# ifdef FEAT_MBYTE
15437 if (conv.vc_type != CONV_NONE)
15438 vim_free(p);
15439 convert_setup(&conv, enc, p_enc);
15440 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 else
15443# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445
15446# ifdef FEAT_MBYTE
15447 /* Release conversion descriptors */
15448 convert_setup(&conv, NULL, NULL);
15449 vim_free(enc);
15450# endif
15451 }
15452}
15453#endif
15454
15455/*
15456 * "stridx()" function
15457 */
15458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015459f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015460 typval_T *argvars;
15461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462{
15463 char_u buf[NUMBUFLEN];
15464 char_u *needle;
15465 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000015466 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000015468 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 needle = get_tv_string_chk(&argvars[1]);
15471 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000015472 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015473 if (needle == NULL || haystack == NULL)
15474 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475
Bram Moolenaar33570922005-01-25 22:26:29 +000015476 if (argvars[2].v_type != VAR_UNKNOWN)
15477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015478 int error = FALSE;
15479
15480 start_idx = get_tv_number_chk(&argvars[2], &error);
15481 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000015482 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015483 if (start_idx >= 0)
15484 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000015485 }
15486
15487 pos = (char_u *)strstr((char *)haystack, (char *)needle);
15488 if (pos != NULL)
15489 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490}
15491
15492/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015493 * "string()" function
15494 */
15495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015497 typval_T *argvars;
15498 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015499{
15500 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015501 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015503 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015504 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015505 /* Make a copy if we have a value but it's not in allocate memory. */
15506 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015507 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508}
15509
15510/*
15511 * "strlen()" function
15512 */
15513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015515 typval_T *argvars;
15516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 rettv->vval.v_number = (varnumber_T)(STRLEN(
15519 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520}
15521
15522/*
15523 * "strpart()" function
15524 */
15525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015526f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015527 typval_T *argvars;
15528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529{
15530 char_u *p;
15531 int n;
15532 int len;
15533 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015534 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 slen = (int)STRLEN(p);
15538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015539 n = get_tv_number_chk(&argvars[1], &error);
15540 if (error)
15541 len = 0;
15542 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015543 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 else
15545 len = slen - n; /* default len: all bytes that are available. */
15546
15547 /*
15548 * Only return the overlap between the specified part and the actual
15549 * string.
15550 */
15551 if (n < 0)
15552 {
15553 len += n;
15554 n = 0;
15555 }
15556 else if (n > slen)
15557 n = slen;
15558 if (len < 0)
15559 len = 0;
15560 else if (n + len > slen)
15561 len = slen - n;
15562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015563 rettv->v_type = VAR_STRING;
15564 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565}
15566
15567/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015568 * "strridx()" function
15569 */
15570 static void
15571f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015572 typval_T *argvars;
15573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574{
15575 char_u buf[NUMBUFLEN];
15576 char_u *needle;
15577 char_u *haystack;
15578 char_u *rest;
15579 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015580 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015582 needle = get_tv_string_chk(&argvars[1]);
15583 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015584
15585 rettv->vval.v_number = -1;
15586 if (needle == NULL || haystack == NULL)
15587 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015588
15589 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015590 if (argvars[2].v_type != VAR_UNKNOWN)
15591 {
15592 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015593 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000015594 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015595 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015596 }
15597 else
15598 end_idx = haystack_len;
15599
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000015601 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000015603 lastmatch = haystack + end_idx;
15604 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015605 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000015606 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015607 for (rest = haystack; *rest != '\0'; ++rest)
15608 {
15609 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000015610 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 break;
15612 lastmatch = rest;
15613 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000015614 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015615
15616 if (lastmatch == NULL)
15617 rettv->vval.v_number = -1;
15618 else
15619 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
15620}
15621
15622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 * "strtrans()" function
15624 */
15625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015627 typval_T *argvars;
15628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015630 rettv->v_type = VAR_STRING;
15631 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632}
15633
15634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015635 * "submatch()" function
15636 */
15637 static void
15638f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015639 typval_T *argvars;
15640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641{
15642 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015643 rettv->vval.v_string =
15644 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645}
15646
15647/*
15648 * "substitute()" function
15649 */
15650 static void
15651f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015652 typval_T *argvars;
15653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654{
15655 char_u patbuf[NUMBUFLEN];
15656 char_u subbuf[NUMBUFLEN];
15657 char_u flagsbuf[NUMBUFLEN];
15658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015659 char_u *str = get_tv_string_chk(&argvars[0]);
15660 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15661 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
15662 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
15663
Bram Moolenaar0d660222005-01-07 21:51:51 +000015664 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
15666 rettv->vval.v_string = NULL;
15667 else
15668 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015669}
15670
15671/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015672 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 */
15674/*ARGSUSED*/
15675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015676f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015677 typval_T *argvars;
15678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679{
15680 int id = 0;
15681#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015682 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 long col;
15684 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015685 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015687 lnum = get_tv_lnum(argvars); /* -1 on type error */
15688 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
15689 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015691 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000015692 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000015693 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694#endif
15695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015696 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697}
15698
15699/*
15700 * "synIDattr(id, what [, mode])" function
15701 */
15702/*ARGSUSED*/
15703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015704f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015705 typval_T *argvars;
15706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707{
15708 char_u *p = NULL;
15709#ifdef FEAT_SYN_HL
15710 int id;
15711 char_u *what;
15712 char_u *mode;
15713 char_u modebuf[NUMBUFLEN];
15714 int modec;
15715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716 id = get_tv_number(&argvars[0]);
15717 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015718 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015720 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 modec = TOLOWER_ASC(mode[0]);
15722 if (modec != 't' && modec != 'c'
15723#ifdef FEAT_GUI
15724 && modec != 'g'
15725#endif
15726 )
15727 modec = 0; /* replace invalid with current */
15728 }
15729 else
15730 {
15731#ifdef FEAT_GUI
15732 if (gui.in_use)
15733 modec = 'g';
15734 else
15735#endif
15736 if (t_colors > 1)
15737 modec = 'c';
15738 else
15739 modec = 't';
15740 }
15741
15742
15743 switch (TOLOWER_ASC(what[0]))
15744 {
15745 case 'b':
15746 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
15747 p = highlight_color(id, what, modec);
15748 else /* bold */
15749 p = highlight_has_attr(id, HL_BOLD, modec);
15750 break;
15751
15752 case 'f': /* fg[#] */
15753 p = highlight_color(id, what, modec);
15754 break;
15755
15756 case 'i':
15757 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
15758 p = highlight_has_attr(id, HL_INVERSE, modec);
15759 else /* italic */
15760 p = highlight_has_attr(id, HL_ITALIC, modec);
15761 break;
15762
15763 case 'n': /* name */
15764 p = get_highlight_name(NULL, id - 1);
15765 break;
15766
15767 case 'r': /* reverse */
15768 p = highlight_has_attr(id, HL_INVERSE, modec);
15769 break;
15770
15771 case 's': /* standout */
15772 p = highlight_has_attr(id, HL_STANDOUT, modec);
15773 break;
15774
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000015775 case 'u':
15776 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
15777 /* underline */
15778 p = highlight_has_attr(id, HL_UNDERLINE, modec);
15779 else
15780 /* undercurl */
15781 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 break;
15783 }
15784
15785 if (p != NULL)
15786 p = vim_strsave(p);
15787#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788 rettv->v_type = VAR_STRING;
15789 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790}
15791
15792/*
15793 * "synIDtrans(id)" function
15794 */
15795/*ARGSUSED*/
15796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015797f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015798 typval_T *argvars;
15799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800{
15801 int id;
15802
15803#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015804 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805
15806 if (id > 0)
15807 id = syn_get_final_id(id);
15808 else
15809#endif
15810 id = 0;
15811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015812 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813}
15814
15815/*
15816 * "system()" function
15817 */
15818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015819f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 typval_T *argvars;
15821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015823 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015825 char_u *infile = NULL;
15826 char_u buf[NUMBUFLEN];
15827 int err = FALSE;
15828 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015830 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015831 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000015832
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015834 {
15835 /*
15836 * Write the string to a temp file, to be used for input of the shell
15837 * command.
15838 */
15839 if ((infile = vim_tempname('i')) == NULL)
15840 {
15841 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000015842 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015843 }
15844
15845 fd = mch_fopen((char *)infile, WRITEBIN);
15846 if (fd == NULL)
15847 {
15848 EMSG2(_(e_notopen), infile);
15849 goto done;
15850 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015851 p = get_tv_string_buf_chk(&argvars[1], buf);
15852 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015853 {
15854 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015855 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015856 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015857 if (fwrite(p, STRLEN(p), 1, fd) != 1)
15858 err = TRUE;
15859 if (fclose(fd) != 0)
15860 err = TRUE;
15861 if (err)
15862 {
15863 EMSG(_("E677: Error writing temp file"));
15864 goto done;
15865 }
15866 }
15867
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015868 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
15869 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015870
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871#ifdef USE_CR
15872 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015873 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 {
15875 char_u *s;
15876
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015877 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 {
15879 if (*s == CAR)
15880 *s = NL;
15881 }
15882 }
15883#else
15884# ifdef USE_CRNL
15885 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015886 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 {
15888 char_u *s, *d;
15889
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015890 d = res;
15891 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 {
15893 if (s[0] == CAR && s[1] == NL)
15894 ++s;
15895 *d++ = *s;
15896 }
15897 *d = NUL;
15898 }
15899# endif
15900#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000015901
15902done:
15903 if (infile != NULL)
15904 {
15905 mch_remove(infile);
15906 vim_free(infile);
15907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015908 rettv->v_type = VAR_STRING;
15909 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910}
15911
15912/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015913 * "tabpagebuflist()" function
15914 */
15915/* ARGSUSED */
15916 static void
15917f_tabpagebuflist(argvars, rettv)
15918 typval_T *argvars;
15919 typval_T *rettv;
15920{
15921#ifndef FEAT_WINDOWS
15922 rettv->vval.v_number = 0;
15923#else
15924 tabpage_T *tp;
15925 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015926
15927 if (argvars[0].v_type == VAR_UNKNOWN)
15928 wp = firstwin;
15929 else
15930 {
15931 tp = find_tabpage((int)get_tv_number(&argvars[0]));
15932 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000015933 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015934 }
15935 if (wp == NULL)
15936 rettv->vval.v_number = 0;
15937 else
15938 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015939 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015940 rettv->vval.v_number = 0;
15941 else
15942 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015943 for (; wp != NULL; wp = wp->w_next)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015944 if (list_append_number(rettv->vval.v_list,
15945 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015946 break;
15947 }
15948 }
15949#endif
15950}
15951
15952
15953/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015954 * "tabpagenr()" function
15955 */
15956/* ARGSUSED */
15957 static void
15958f_tabpagenr(argvars, rettv)
15959 typval_T *argvars;
15960 typval_T *rettv;
15961{
15962 int nr = 1;
15963#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015964 char_u *arg;
15965
15966 if (argvars[0].v_type != VAR_UNKNOWN)
15967 {
15968 arg = get_tv_string_chk(&argvars[0]);
15969 nr = 0;
15970 if (arg != NULL)
15971 {
15972 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000015973 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015974 else
15975 EMSG2(_(e_invexpr2), arg);
15976 }
15977 }
15978 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000015979 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000015980#endif
15981 rettv->vval.v_number = nr;
15982}
15983
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000015984
15985#ifdef FEAT_WINDOWS
15986static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
15987
15988/*
15989 * Common code for tabpagewinnr() and winnr().
15990 */
15991 static int
15992get_winnr(tp, argvar)
15993 tabpage_T *tp;
15994 typval_T *argvar;
15995{
15996 win_T *twin;
15997 int nr = 1;
15998 win_T *wp;
15999 char_u *arg;
16000
16001 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16002 if (argvar->v_type != VAR_UNKNOWN)
16003 {
16004 arg = get_tv_string_chk(argvar);
16005 if (arg == NULL)
16006 nr = 0; /* type error; errmsg already given */
16007 else if (STRCMP(arg, "$") == 0)
16008 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16009 else if (STRCMP(arg, "#") == 0)
16010 {
16011 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16012 if (twin == NULL)
16013 nr = 0;
16014 }
16015 else
16016 {
16017 EMSG2(_(e_invexpr2), arg);
16018 nr = 0;
16019 }
16020 }
16021
16022 if (nr > 0)
16023 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16024 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016025 {
16026 if (wp == NULL)
16027 {
16028 /* didn't find it in this tabpage */
16029 nr = 0;
16030 break;
16031 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016032 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016033 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016034 return nr;
16035}
16036#endif
16037
16038/*
16039 * "tabpagewinnr()" function
16040 */
16041/* ARGSUSED */
16042 static void
16043f_tabpagewinnr(argvars, rettv)
16044 typval_T *argvars;
16045 typval_T *rettv;
16046{
16047 int nr = 1;
16048#ifdef FEAT_WINDOWS
16049 tabpage_T *tp;
16050
16051 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16052 if (tp == NULL)
16053 nr = 0;
16054 else
16055 nr = get_winnr(tp, &argvars[1]);
16056#endif
16057 rettv->vval.v_number = nr;
16058}
16059
16060
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016061/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016062 * "tagfiles()" function
16063 */
16064/*ARGSUSED*/
16065 static void
16066f_tagfiles(argvars, rettv)
16067 typval_T *argvars;
16068 typval_T *rettv;
16069{
16070 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016071 tagname_T tn;
16072 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016074 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016075 {
16076 rettv->vval.v_number = 0;
16077 return;
16078 }
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016079
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016080 for (first = TRUE; ; first = FALSE)
16081 if (get_tagfname(&tn, first, fname) == FAIL
16082 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016083 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016084 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016085}
16086
16087/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016088 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016089 */
16090 static void
16091f_taglist(argvars, rettv)
16092 typval_T *argvars;
16093 typval_T *rettv;
16094{
16095 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016096
16097 tag_pattern = get_tv_string(&argvars[0]);
16098
16099 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016100 if (*tag_pattern == NUL)
16101 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016102
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016103 if (rettv_list_alloc(rettv) == OK)
16104 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016105}
16106
16107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 * "tempname()" function
16109 */
16110/*ARGSUSED*/
16111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016112f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 typval_T *argvars;
16114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115{
16116 static int x = 'A';
16117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016118 rettv->v_type = VAR_STRING;
16119 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120
16121 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16122 * names. Skip 'I' and 'O', they are used for shell redirection. */
16123 do
16124 {
16125 if (x == 'Z')
16126 x = '0';
16127 else if (x == '9')
16128 x = 'A';
16129 else
16130 {
16131#ifdef EBCDIC
16132 if (x == 'I')
16133 x = 'J';
16134 else if (x == 'R')
16135 x = 'S';
16136 else
16137#endif
16138 ++x;
16139 }
16140 } while (x == 'I' || x == 'O');
16141}
16142
16143/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000016144 * "test(list)" function: Just checking the walls...
16145 */
16146/*ARGSUSED*/
16147 static void
16148f_test(argvars, rettv)
16149 typval_T *argvars;
16150 typval_T *rettv;
16151{
16152 /* Used for unit testing. Change the code below to your liking. */
16153#if 0
16154 listitem_T *li;
16155 list_T *l;
16156 char_u *bad, *good;
16157
16158 if (argvars[0].v_type != VAR_LIST)
16159 return;
16160 l = argvars[0].vval.v_list;
16161 if (l == NULL)
16162 return;
16163 li = l->lv_first;
16164 if (li == NULL)
16165 return;
16166 bad = get_tv_string(&li->li_tv);
16167 li = li->li_next;
16168 if (li == NULL)
16169 return;
16170 good = get_tv_string(&li->li_tv);
16171 rettv->vval.v_number = test_edit_score(bad, good);
16172#endif
16173}
16174
16175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 * "tolower(string)" function
16177 */
16178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016179f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016180 typval_T *argvars;
16181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182{
16183 char_u *p;
16184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016185 p = vim_strsave(get_tv_string(&argvars[0]));
16186 rettv->v_type = VAR_STRING;
16187 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188
16189 if (p != NULL)
16190 while (*p != NUL)
16191 {
16192#ifdef FEAT_MBYTE
16193 int l;
16194
16195 if (enc_utf8)
16196 {
16197 int c, lc;
16198
16199 c = utf_ptr2char(p);
16200 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016201 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202 /* TODO: reallocate string when byte count changes. */
16203 if (utf_char2len(lc) == l)
16204 utf_char2bytes(lc, p);
16205 p += l;
16206 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016207 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 p += l; /* skip multi-byte character */
16209 else
16210#endif
16211 {
16212 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
16213 ++p;
16214 }
16215 }
16216}
16217
16218/*
16219 * "toupper(string)" function
16220 */
16221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016223 typval_T *argvars;
16224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016226 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228}
16229
16230/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000016231 * "tr(string, fromstr, tostr)" function
16232 */
16233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016234f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016235 typval_T *argvars;
16236 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016237{
16238 char_u *instr;
16239 char_u *fromstr;
16240 char_u *tostr;
16241 char_u *p;
16242#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000016243 int inlen;
16244 int fromlen;
16245 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016246 int idx;
16247 char_u *cpstr;
16248 int cplen;
16249 int first = TRUE;
16250#endif
16251 char_u buf[NUMBUFLEN];
16252 char_u buf2[NUMBUFLEN];
16253 garray_T ga;
16254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016255 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016256 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
16257 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016258
16259 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 rettv->v_type = VAR_STRING;
16261 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 if (fromstr == NULL || tostr == NULL)
16263 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000016264 ga_init2(&ga, (int)sizeof(char), 80);
16265
16266#ifdef FEAT_MBYTE
16267 if (!has_mbyte)
16268#endif
16269 /* not multi-byte: fromstr and tostr must be the same length */
16270 if (STRLEN(fromstr) != STRLEN(tostr))
16271 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016272#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000016273error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016274#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000016275 EMSG2(_(e_invarg2), fromstr);
16276 ga_clear(&ga);
16277 return;
16278 }
16279
16280 /* fromstr and tostr have to contain the same number of chars */
16281 while (*instr != NUL)
16282 {
16283#ifdef FEAT_MBYTE
16284 if (has_mbyte)
16285 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016286 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016287 cpstr = instr;
16288 cplen = inlen;
16289 idx = 0;
16290 for (p = fromstr; *p != NUL; p += fromlen)
16291 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016292 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016293 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
16294 {
16295 for (p = tostr; *p != NUL; p += tolen)
16296 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016297 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016298 if (idx-- == 0)
16299 {
16300 cplen = tolen;
16301 cpstr = p;
16302 break;
16303 }
16304 }
16305 if (*p == NUL) /* tostr is shorter than fromstr */
16306 goto error;
16307 break;
16308 }
16309 ++idx;
16310 }
16311
16312 if (first && cpstr == instr)
16313 {
16314 /* Check that fromstr and tostr have the same number of
16315 * (multi-byte) characters. Done only once when a character
16316 * of instr doesn't appear in fromstr. */
16317 first = FALSE;
16318 for (p = tostr; *p != NUL; p += tolen)
16319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016320 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016321 --idx;
16322 }
16323 if (idx != 0)
16324 goto error;
16325 }
16326
16327 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000016328 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000016329 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016330
16331 instr += inlen;
16332 }
16333 else
16334#endif
16335 {
16336 /* When not using multi-byte chars we can do it faster. */
16337 p = vim_strchr(fromstr, *instr);
16338 if (p != NULL)
16339 ga_append(&ga, tostr[p - fromstr]);
16340 else
16341 ga_append(&ga, *instr);
16342 ++instr;
16343 }
16344 }
16345
Bram Moolenaar61b974b2006-12-05 09:32:29 +000016346 /* add a terminating NUL */
16347 ga_grow(&ga, 1);
16348 ga_append(&ga, NUL);
16349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000016351}
16352
16353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 * "type(expr)" function
16355 */
16356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016358 typval_T *argvars;
16359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016361 int n;
16362
16363 switch (argvars[0].v_type)
16364 {
16365 case VAR_NUMBER: n = 0; break;
16366 case VAR_STRING: n = 1; break;
16367 case VAR_FUNC: n = 2; break;
16368 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016369 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016370 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
16371 }
16372 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373}
16374
16375/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016376 * "values(dict)" function
16377 */
16378 static void
16379f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 typval_T *argvars;
16381 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016382{
16383 dict_list(argvars, rettv, 1);
16384}
16385
16386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 * "virtcol(string)" function
16388 */
16389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016390f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 typval_T *argvars;
16392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393{
16394 colnr_T vcol = 0;
16395 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016396 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016398 fp = var2fpos(&argvars[0], FALSE, &fnum);
16399 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
16400 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 {
16402 getvvcol(curwin, fp, NULL, NULL, &vcol);
16403 ++vcol;
16404 }
16405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016406 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407}
16408
16409/*
16410 * "visualmode()" function
16411 */
16412/*ARGSUSED*/
16413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016414f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016415 typval_T *argvars;
16416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417{
16418#ifdef FEAT_VISUAL
16419 char_u str[2];
16420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016421 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 str[0] = curbuf->b_visual_mode_eval;
16423 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016424 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425
16426 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016427 if ((argvars[0].v_type == VAR_NUMBER
16428 && argvars[0].vval.v_number != 0)
16429 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016430 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 curbuf->b_visual_mode_eval = NUL;
16432#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016433 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434#endif
16435}
16436
16437/*
16438 * "winbufnr(nr)" function
16439 */
16440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016442 typval_T *argvars;
16443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444{
16445 win_T *wp;
16446
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016447 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016448 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452}
16453
16454/*
16455 * "wincol()" function
16456 */
16457/*ARGSUSED*/
16458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016459f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016460 typval_T *argvars;
16461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462{
16463 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465}
16466
16467/*
16468 * "winheight(nr)" function
16469 */
16470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016471f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016472 typval_T *argvars;
16473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474{
16475 win_T *wp;
16476
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016477 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016479 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016481 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482}
16483
16484/*
16485 * "winline()" function
16486 */
16487/*ARGSUSED*/
16488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 typval_T *argvars;
16491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492{
16493 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016494 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016495}
16496
16497/*
16498 * "winnr()" function
16499 */
16500/* ARGSUSED */
16501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016502f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016503 typval_T *argvars;
16504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505{
16506 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016507
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016509 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016511 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512}
16513
16514/*
16515 * "winrestcmd()" function
16516 */
16517/* ARGSUSED */
16518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016519f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016520 typval_T *argvars;
16521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522{
16523#ifdef FEAT_WINDOWS
16524 win_T *wp;
16525 int winnr = 1;
16526 garray_T ga;
16527 char_u buf[50];
16528
16529 ga_init2(&ga, (int)sizeof(char), 70);
16530 for (wp = firstwin; wp != NULL; wp = wp->w_next)
16531 {
16532 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
16533 ga_concat(&ga, buf);
16534# ifdef FEAT_VERTSPLIT
16535 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
16536 ga_concat(&ga, buf);
16537# endif
16538 ++winnr;
16539 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000016540 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016544 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016546 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547}
16548
16549/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016550 * "winrestview()" function
16551 */
16552/* ARGSUSED */
16553 static void
16554f_winrestview(argvars, rettv)
16555 typval_T *argvars;
16556 typval_T *rettv;
16557{
16558 dict_T *dict;
16559
16560 if (argvars[0].v_type != VAR_DICT
16561 || (dict = argvars[0].vval.v_dict) == NULL)
16562 EMSG(_(e_invarg));
16563 else
16564 {
16565 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
16566 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
16567#ifdef FEAT_VIRTUALEDIT
16568 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
16569#endif
16570 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016571 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016572
Bram Moolenaar6f11a412006-09-06 20:16:42 +000016573 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016574#ifdef FEAT_DIFF
16575 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
16576#endif
16577 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
16578 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
16579
16580 check_cursor();
16581 changed_cline_bef_curs();
16582 invalidate_botline();
16583 redraw_later(VALID);
16584
16585 if (curwin->w_topline == 0)
16586 curwin->w_topline = 1;
16587 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
16588 curwin->w_topline = curbuf->b_ml.ml_line_count;
16589#ifdef FEAT_DIFF
16590 check_topfill(curwin, TRUE);
16591#endif
16592 }
16593}
16594
16595/*
16596 * "winsaveview()" function
16597 */
16598/* ARGSUSED */
16599 static void
16600f_winsaveview(argvars, rettv)
16601 typval_T *argvars;
16602 typval_T *rettv;
16603{
16604 dict_T *dict;
16605
16606 dict = dict_alloc();
16607 if (dict == NULL)
16608 return;
16609 rettv->v_type = VAR_DICT;
16610 rettv->vval.v_dict = dict;
16611 ++dict->dv_refcount;
16612
16613 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
16614 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
16615#ifdef FEAT_VIRTUALEDIT
16616 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
16617#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000016618 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016619 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
16620
16621 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
16622#ifdef FEAT_DIFF
16623 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
16624#endif
16625 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
16626 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
16627}
16628
16629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 * "winwidth(nr)" function
16631 */
16632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016634 typval_T *argvars;
16635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636{
16637 win_T *wp;
16638
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016639 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016641 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 else
16643#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016644 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016646 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647#endif
16648}
16649
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016651 * "writefile()" function
16652 */
16653 static void
16654f_writefile(argvars, rettv)
16655 typval_T *argvars;
16656 typval_T *rettv;
16657{
16658 int binary = FALSE;
16659 char_u *fname;
16660 FILE *fd;
16661 listitem_T *li;
16662 char_u *s;
16663 int ret = 0;
16664 int c;
16665
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016666 if (check_restricted() || check_secure())
16667 return;
16668
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016669 if (argvars[0].v_type != VAR_LIST)
16670 {
16671 EMSG2(_(e_listarg), "writefile()");
16672 return;
16673 }
16674 if (argvars[0].vval.v_list == NULL)
16675 return;
16676
16677 if (argvars[2].v_type != VAR_UNKNOWN
16678 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
16679 binary = TRUE;
16680
16681 /* Always open the file in binary mode, library functions have a mind of
16682 * their own about CR-LF conversion. */
16683 fname = get_tv_string(&argvars[1]);
16684 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
16685 {
16686 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
16687 ret = -1;
16688 }
16689 else
16690 {
16691 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
16692 li = li->li_next)
16693 {
16694 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
16695 {
16696 if (*s == '\n')
16697 c = putc(NUL, fd);
16698 else
16699 c = putc(*s, fd);
16700 if (c == EOF)
16701 {
16702 ret = -1;
16703 break;
16704 }
16705 }
16706 if (!binary || li->li_next != NULL)
16707 if (putc('\n', fd) == EOF)
16708 {
16709 ret = -1;
16710 break;
16711 }
16712 if (ret < 0)
16713 {
16714 EMSG(_(e_write));
16715 break;
16716 }
16717 }
16718 fclose(fd);
16719 }
16720
16721 rettv->vval.v_number = ret;
16722}
16723
16724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016726 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 */
16728 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000016729var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016731 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016732 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016734 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016736 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737
Bram Moolenaara5525202006-03-02 22:52:09 +000016738 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016739 if (varp->v_type == VAR_LIST)
16740 {
16741 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016742 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000016743 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000016744 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016745
16746 l = varp->vval.v_list;
16747 if (l == NULL)
16748 return NULL;
16749
16750 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016751 pos.lnum = list_find_nr(l, 0L, &error);
16752 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016753 return NULL; /* invalid line number */
16754
16755 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016756 pos.col = list_find_nr(l, 1L, &error);
16757 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016758 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016759 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000016760
16761 /* We accept "$" for the column number: last column. */
16762 li = list_find(l, 1L);
16763 if (li != NULL && li->li_tv.v_type == VAR_STRING
16764 && li->li_tv.vval.v_string != NULL
16765 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
16766 pos.col = len + 1;
16767
Bram Moolenaara5525202006-03-02 22:52:09 +000016768 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000016769 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016770 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000016771 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016772
Bram Moolenaara5525202006-03-02 22:52:09 +000016773#ifdef FEAT_VIRTUALEDIT
16774 /* Get the virtual offset. Defaults to zero. */
16775 pos.coladd = list_find_nr(l, 2L, &error);
16776 if (error)
16777 pos.coladd = 0;
16778#endif
16779
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016780 return &pos;
16781 }
16782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016783 name = get_tv_string_chk(varp);
16784 if (name == NULL)
16785 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 if (name[0] == '.') /* cursor */
16787 return &curwin->w_cursor;
16788 if (name[0] == '\'') /* mark */
16789 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016790 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
16792 return NULL;
16793 return pp;
16794 }
Bram Moolenaara5525202006-03-02 22:52:09 +000016795
16796#ifdef FEAT_VIRTUALEDIT
16797 pos.coladd = 0;
16798#endif
16799
Bram Moolenaar477933c2007-07-17 14:32:23 +000016800 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016801 {
16802 pos.col = 0;
16803 if (name[1] == '0') /* "w0": first visible line */
16804 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016805 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016806 pos.lnum = curwin->w_topline;
16807 return &pos;
16808 }
16809 else if (name[1] == '$') /* "w$": last visible line */
16810 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000016811 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000016812 pos.lnum = curwin->w_botline - 1;
16813 return &pos;
16814 }
16815 }
16816 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000016818 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 {
16820 pos.lnum = curbuf->b_ml.ml_line_count;
16821 pos.col = 0;
16822 }
16823 else
16824 {
16825 pos.lnum = curwin->w_cursor.lnum;
16826 pos.col = (colnr_T)STRLEN(ml_get_curline());
16827 }
16828 return &pos;
16829 }
16830 return NULL;
16831}
16832
16833/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016834 * Convert list in "arg" into a position and optional file number.
16835 * When "fnump" is NULL there is no file number, only 3 items.
16836 * Note that the column is passed on as-is, the caller may want to decrement
16837 * it to use 1 for the first column.
16838 * Return FAIL when conversion is not possible, doesn't check the position for
16839 * validity.
16840 */
16841 static int
16842list2fpos(arg, posp, fnump)
16843 typval_T *arg;
16844 pos_T *posp;
16845 int *fnump;
16846{
16847 list_T *l = arg->vval.v_list;
16848 long i = 0;
16849 long n;
16850
Bram Moolenaarbde35262006-07-23 20:12:24 +000016851 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16852 * when "fnump" isn't NULL and "coladd" is optional. */
16853 if (arg->v_type != VAR_LIST
16854 || l == NULL
16855 || l->lv_len < (fnump == NULL ? 2 : 3)
16856 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016857 return FAIL;
16858
16859 if (fnump != NULL)
16860 {
16861 n = list_find_nr(l, i++, NULL); /* fnum */
16862 if (n < 0)
16863 return FAIL;
16864 if (n == 0)
16865 n = curbuf->b_fnum; /* current buffer */
16866 *fnump = n;
16867 }
16868
16869 n = list_find_nr(l, i++, NULL); /* lnum */
16870 if (n < 0)
16871 return FAIL;
16872 posp->lnum = n;
16873
16874 n = list_find_nr(l, i++, NULL); /* col */
16875 if (n < 0)
16876 return FAIL;
16877 posp->col = n;
16878
16879#ifdef FEAT_VIRTUALEDIT
16880 n = list_find_nr(l, i, NULL);
16881 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000016882 posp->coladd = 0;
16883 else
16884 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016885#endif
16886
16887 return OK;
16888}
16889
16890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891 * Get the length of an environment variable name.
16892 * Advance "arg" to the first character after the name.
16893 * Return 0 for error.
16894 */
16895 static int
16896get_env_len(arg)
16897 char_u **arg;
16898{
16899 char_u *p;
16900 int len;
16901
16902 for (p = *arg; vim_isIDc(*p); ++p)
16903 ;
16904 if (p == *arg) /* no name found */
16905 return 0;
16906
16907 len = (int)(p - *arg);
16908 *arg = p;
16909 return len;
16910}
16911
16912/*
16913 * Get the length of the name of a function or internal variable.
16914 * "arg" is advanced to the first non-white character after the name.
16915 * Return 0 if something is wrong.
16916 */
16917 static int
16918get_id_len(arg)
16919 char_u **arg;
16920{
16921 char_u *p;
16922 int len;
16923
16924 /* Find the end of the name. */
16925 for (p = *arg; eval_isnamec(*p); ++p)
16926 ;
16927 if (p == *arg) /* no name found */
16928 return 0;
16929
16930 len = (int)(p - *arg);
16931 *arg = skipwhite(p);
16932
16933 return len;
16934}
16935
16936/*
Bram Moolenaara7043832005-01-21 11:56:39 +000016937 * Get the length of the name of a variable or function.
16938 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016940 * Return -1 if curly braces expansion failed.
16941 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 * If the name contains 'magic' {}'s, expand them and return the
16943 * expanded name in an allocated string via 'alias' - caller must free.
16944 */
16945 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016946get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 char_u **arg;
16948 char_u **alias;
16949 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016950 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951{
16952 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 char_u *p;
16954 char_u *expr_start;
16955 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956
16957 *alias = NULL; /* default to no alias */
16958
16959 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
16960 && (*arg)[2] == (int)KE_SNR)
16961 {
16962 /* hard coded <SNR>, already translated */
16963 *arg += 3;
16964 return get_id_len(arg) + 3;
16965 }
16966 len = eval_fname_script(*arg);
16967 if (len > 0)
16968 {
16969 /* literal "<SID>", "s:" or "<SNR>" */
16970 *arg += len;
16971 }
16972
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016974 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016976 p = find_name_end(*arg, &expr_start, &expr_end,
16977 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016978 if (expr_start != NULL)
16979 {
16980 char_u *temp_string;
16981
16982 if (!evaluate)
16983 {
16984 len += (int)(p - *arg);
16985 *arg = skipwhite(p);
16986 return len;
16987 }
16988
16989 /*
16990 * Include any <SID> etc in the expanded string:
16991 * Thus the -len here.
16992 */
16993 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
16994 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016995 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996 *alias = temp_string;
16997 *arg = skipwhite(p);
16998 return (int)STRLEN(temp_string);
16999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000
17001 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017002 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 EMSG2(_(e_invexpr2), *arg);
17004
17005 return len;
17006}
17007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017008/*
17009 * Find the end of a variable or function name, taking care of magic braces.
17010 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17011 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017012 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013 * Return a pointer to just after the name. Equal to "arg" if there is no
17014 * valid name.
17015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017017find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 char_u *arg;
17019 char_u **expr_start;
17020 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017021 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017023 int mb_nest = 0;
17024 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 char_u *p;
17026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017027 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029 *expr_start = NULL;
17030 *expr_end = NULL;
17031 }
17032
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017033 /* Quick check for valid starting character. */
17034 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17035 return arg;
17036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017037 for (p = arg; *p != NUL
17038 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017039 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017040 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017041 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017042 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017043 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017044 if (*p == '\'')
17045 {
17046 /* skip over 'string' to avoid counting [ and ] inside it. */
17047 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17048 ;
17049 if (*p == NUL)
17050 break;
17051 }
17052 else if (*p == '"')
17053 {
17054 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17055 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17056 if (*p == '\\' && p[1] != NUL)
17057 ++p;
17058 if (*p == NUL)
17059 break;
17060 }
17061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017062 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017064 if (*p == '[')
17065 ++br_nest;
17066 else if (*p == ']')
17067 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017070 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017072 if (*p == '{')
17073 {
17074 mb_nest++;
17075 if (expr_start != NULL && *expr_start == NULL)
17076 *expr_start = p;
17077 }
17078 else if (*p == '}')
17079 {
17080 mb_nest--;
17081 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17082 *expr_end = p;
17083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 }
17086
17087 return p;
17088}
17089
17090/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017091 * Expands out the 'magic' {}'s in a variable/function name.
17092 * Note that this can call itself recursively, to deal with
17093 * constructs like foo{bar}{baz}{bam}
17094 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17095 * "in_start" ^
17096 * "expr_start" ^
17097 * "expr_end" ^
17098 * "in_end" ^
17099 *
17100 * Returns a new allocated string, which the caller must free.
17101 * Returns NULL for failure.
17102 */
17103 static char_u *
17104make_expanded_name(in_start, expr_start, expr_end, in_end)
17105 char_u *in_start;
17106 char_u *expr_start;
17107 char_u *expr_end;
17108 char_u *in_end;
17109{
17110 char_u c1;
17111 char_u *retval = NULL;
17112 char_u *temp_result;
17113 char_u *nextcmd = NULL;
17114
17115 if (expr_end == NULL || in_end == NULL)
17116 return NULL;
17117 *expr_start = NUL;
17118 *expr_end = NUL;
17119 c1 = *in_end;
17120 *in_end = NUL;
17121
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017122 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017123 if (temp_result != NULL && nextcmd == NULL)
17124 {
17125 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
17126 + (in_end - expr_end) + 1));
17127 if (retval != NULL)
17128 {
17129 STRCPY(retval, in_start);
17130 STRCAT(retval, temp_result);
17131 STRCAT(retval, expr_end + 1);
17132 }
17133 }
17134 vim_free(temp_result);
17135
17136 *in_end = c1; /* put char back for error messages */
17137 *expr_start = '{';
17138 *expr_end = '}';
17139
17140 if (retval != NULL)
17141 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017142 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017143 if (expr_start != NULL)
17144 {
17145 /* Further expansion! */
17146 temp_result = make_expanded_name(retval, expr_start,
17147 expr_end, temp_result);
17148 vim_free(retval);
17149 retval = temp_result;
17150 }
17151 }
17152
17153 return retval;
17154}
17155
17156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000017158 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 */
17160 static int
17161eval_isnamec(c)
17162 int c;
17163{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017164 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
17165}
17166
17167/*
17168 * Return TRUE if character "c" can be used as the first character in a
17169 * variable or function name (excluding '{' and '}').
17170 */
17171 static int
17172eval_isnamec1(c)
17173 int c;
17174{
17175 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176}
17177
17178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 * Set number v: variable to "val".
17180 */
17181 void
17182set_vim_var_nr(idx, val)
17183 int idx;
17184 long val;
17185{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017186 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187}
17188
17189/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017190 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 */
17192 long
17193get_vim_var_nr(idx)
17194 int idx;
17195{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017196 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197}
17198
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017199#if defined(FEAT_AUTOCMD) || defined(PROTO)
17200/*
17201 * Get string v: variable value. Uses a static buffer, can only be used once.
17202 */
17203 char_u *
17204get_vim_var_str(idx)
17205 int idx;
17206{
17207 return get_tv_string(&vimvars[idx].vv_tv);
17208}
17209#endif
17210
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211/*
17212 * Set v:count, v:count1 and v:prevcount.
17213 */
17214 void
17215set_vcount(count, count1)
17216 long count;
17217 long count1;
17218{
Bram Moolenaare9a41262005-01-15 22:18:47 +000017219 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
17220 vimvars[VV_COUNT].vv_nr = count;
17221 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222}
17223
17224/*
17225 * Set string v: variable to a copy of "val".
17226 */
17227 void
17228set_vim_var_string(idx, val, len)
17229 int idx;
17230 char_u *val;
17231 int len; /* length of "val" to use or -1 (whole string) */
17232{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017233 /* Need to do this (at least) once, since we can't initialize a union.
17234 * Will always be invoked when "v:progname" is set. */
17235 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
17236
Bram Moolenaare9a41262005-01-15 22:18:47 +000017237 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017239 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017241 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017243 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244}
17245
17246/*
17247 * Set v:register if needed.
17248 */
17249 void
17250set_reg_var(c)
17251 int c;
17252{
17253 char_u regname;
17254
17255 if (c == 0 || c == ' ')
17256 regname = '"';
17257 else
17258 regname = c;
17259 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000017260 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 set_vim_var_string(VV_REG, &regname, 1);
17262}
17263
17264/*
17265 * Get or set v:exception. If "oldval" == NULL, return the current value.
17266 * Otherwise, restore the value to "oldval" and return NULL.
17267 * Must always be called in pairs to save and restore v:exception! Does not
17268 * take care of memory allocations.
17269 */
17270 char_u *
17271v_exception(oldval)
17272 char_u *oldval;
17273{
17274 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017275 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276
Bram Moolenaare9a41262005-01-15 22:18:47 +000017277 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 return NULL;
17279}
17280
17281/*
17282 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17283 * Otherwise, restore the value to "oldval" and return NULL.
17284 * Must always be called in pairs to save and restore v:throwpoint! Does not
17285 * take care of memory allocations.
17286 */
17287 char_u *
17288v_throwpoint(oldval)
17289 char_u *oldval;
17290{
17291 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017292 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
Bram Moolenaare9a41262005-01-15 22:18:47 +000017294 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 return NULL;
17296}
17297
17298#if defined(FEAT_AUTOCMD) || defined(PROTO)
17299/*
17300 * Set v:cmdarg.
17301 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17302 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17303 * Must always be called in pairs!
17304 */
17305 char_u *
17306set_cmdarg(eap, oldarg)
17307 exarg_T *eap;
17308 char_u *oldarg;
17309{
17310 char_u *oldval;
17311 char_u *newval;
17312 unsigned len;
17313
Bram Moolenaare9a41262005-01-15 22:18:47 +000017314 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017315 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017317 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000017318 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017319 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320 }
17321
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017322 if (eap->force_bin == FORCE_BIN)
17323 len = 6;
17324 else if (eap->force_bin == FORCE_NOBIN)
17325 len = 8;
17326 else
17327 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017328
17329 if (eap->read_edit)
17330 len += 7;
17331
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017332 if (eap->force_ff != 0)
17333 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
17334# ifdef FEAT_MBYTE
17335 if (eap->force_enc != 0)
17336 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017337 if (eap->bad_char != 0)
17338 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017339# endif
17340
17341 newval = alloc(len + 1);
17342 if (newval == NULL)
17343 return NULL;
17344
17345 if (eap->force_bin == FORCE_BIN)
17346 sprintf((char *)newval, " ++bin");
17347 else if (eap->force_bin == FORCE_NOBIN)
17348 sprintf((char *)newval, " ++nobin");
17349 else
17350 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017351
17352 if (eap->read_edit)
17353 STRCAT(newval, " ++edit");
17354
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017355 if (eap->force_ff != 0)
17356 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
17357 eap->cmd + eap->force_ff);
17358# ifdef FEAT_MBYTE
17359 if (eap->force_enc != 0)
17360 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
17361 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000017362 if (eap->bad_char != 0)
17363 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
17364 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017365# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000017366 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000017367 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368}
17369#endif
17370
17371/*
17372 * Get the value of internal variable "name".
17373 * Return OK or FAIL.
17374 */
17375 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017376get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 char_u *name;
17378 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017380 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381{
17382 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000017383 typval_T *tv = NULL;
17384 typval_T atv;
17385 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387
17388 /* truncate the name, so that we can use strcmp() */
17389 cc = name[len];
17390 name[len] = NUL;
17391
17392 /*
17393 * Check for "b:changedtick".
17394 */
17395 if (STRCMP(name, "b:changedtick") == 0)
17396 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000017397 atv.v_type = VAR_NUMBER;
17398 atv.vval.v_number = curbuf->b_changedtick;
17399 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 }
17401
17402 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 * Check for user-defined variables.
17404 */
17405 else
17406 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017407 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 }
17411
Bram Moolenaare9a41262005-01-15 22:18:47 +000017412 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017414 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416 ret = FAIL;
17417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017418 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017419 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420
17421 name[len] = cc;
17422
17423 return ret;
17424}
17425
17426/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017427 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17428 * Also handle function call with Funcref variable: func(expr)
17429 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17430 */
17431 static int
17432handle_subscript(arg, rettv, evaluate, verbose)
17433 char_u **arg;
17434 typval_T *rettv;
17435 int evaluate; /* do more than finding the end */
17436 int verbose; /* give error messages */
17437{
17438 int ret = OK;
17439 dict_T *selfdict = NULL;
17440 char_u *s;
17441 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000017442 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017443
17444 while (ret == OK
17445 && (**arg == '['
17446 || (**arg == '.' && rettv->v_type == VAR_DICT)
17447 || (**arg == '(' && rettv->v_type == VAR_FUNC))
17448 && !vim_iswhite(*(*arg - 1)))
17449 {
17450 if (**arg == '(')
17451 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000017452 /* need to copy the funcref so that we can clear rettv */
17453 functv = *rettv;
17454 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017455
17456 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000017457 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017458 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000017459 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
17460 &len, evaluate, selfdict);
17461
17462 /* Clear the funcref afterwards, so that deleting it while
17463 * evaluating the arguments is possible (see test55). */
17464 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017465
17466 /* Stop the expression evaluation when immediately aborting on
17467 * error, or when an interrupt occurred or an exception was thrown
17468 * but not caught. */
17469 if (aborting())
17470 {
17471 if (ret == OK)
17472 clear_tv(rettv);
17473 ret = FAIL;
17474 }
17475 dict_unref(selfdict);
17476 selfdict = NULL;
17477 }
17478 else /* **arg == '[' || **arg == '.' */
17479 {
17480 dict_unref(selfdict);
17481 if (rettv->v_type == VAR_DICT)
17482 {
17483 selfdict = rettv->vval.v_dict;
17484 if (selfdict != NULL)
17485 ++selfdict->dv_refcount;
17486 }
17487 else
17488 selfdict = NULL;
17489 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
17490 {
17491 clear_tv(rettv);
17492 ret = FAIL;
17493 }
17494 }
17495 }
17496 dict_unref(selfdict);
17497 return ret;
17498}
17499
17500/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017501 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17502 * value).
17503 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017504 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017506{
Bram Moolenaar33570922005-01-25 22:26:29 +000017507 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017508}
17509
17510/*
17511 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 * The string "s" must have been allocated, it is consumed.
17513 * Return NULL for out of memory, the variable otherwise.
17514 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017515 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017516alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517 char_u *s;
17518{
Bram Moolenaar33570922005-01-25 22:26:29 +000017519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 rettv = alloc_tv();
17522 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524 rettv->v_type = VAR_STRING;
17525 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 }
17527 else
17528 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017529 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530}
17531
17532/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017533 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000017535 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017536free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538{
17539 if (varp != NULL)
17540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017541 switch (varp->v_type)
17542 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017543 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017544 func_unref(varp->vval.v_string);
17545 /*FALLTHROUGH*/
17546 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017547 vim_free(varp->vval.v_string);
17548 break;
17549 case VAR_LIST:
17550 list_unref(varp->vval.v_list);
17551 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017552 case VAR_DICT:
17553 dict_unref(varp->vval.v_dict);
17554 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017555 case VAR_NUMBER:
17556 case VAR_UNKNOWN:
17557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017558 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017559 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017560 break;
17561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 vim_free(varp);
17563 }
17564}
17565
17566/*
17567 * Free the memory for a variable value and set the value to NULL or 0.
17568 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017569 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017570clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017571 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572{
17573 if (varp != NULL)
17574 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017575 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017577 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017578 func_unref(varp->vval.v_string);
17579 /*FALLTHROUGH*/
17580 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017581 vim_free(varp->vval.v_string);
17582 varp->vval.v_string = NULL;
17583 break;
17584 case VAR_LIST:
17585 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017586 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017587 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017588 case VAR_DICT:
17589 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017590 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017591 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017592 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017593 varp->vval.v_number = 0;
17594 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017595 case VAR_UNKNOWN:
17596 break;
17597 default:
17598 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017600 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 }
17602}
17603
17604/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017605 * Set the value of a variable to NULL without freeing items.
17606 */
17607 static void
17608init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017609 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017610{
17611 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017612 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613}
17614
17615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 * Get the number value of a variable.
17617 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017618 * For incompatible types, return 0.
17619 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17620 * caller of incompatible types: it sets *denote to TRUE if "denote"
17621 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 */
17623 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017624get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017625 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 int error = FALSE;
17628
17629 return get_tv_number_chk(varp, &error); /* return 0L on error */
17630}
17631
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017632 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017633get_tv_number_chk(varp, denote)
17634 typval_T *varp;
17635 int *denote;
17636{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017637 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017639 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017641 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017642 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017643 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017644 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017645 break;
17646 case VAR_STRING:
17647 if (varp->vval.v_string != NULL)
17648 vim_str2nr(varp->vval.v_string, NULL, NULL,
17649 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017650 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017651 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000017652 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017653 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017654 case VAR_DICT:
17655 EMSG(_("E728: Using a Dictionary as a number"));
17656 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017657 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017658 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017661 if (denote == NULL) /* useful for values that must be unsigned */
17662 n = -1;
17663 else
17664 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017665 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666}
17667
17668/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017669 * Get the lnum from the first argument.
17670 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017671 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 */
17673 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000017675 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676{
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 linenr_T lnum;
17679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017680 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 if (lnum == 0) /* no valid number, try using line() */
17682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017683 rettv.v_type = VAR_NUMBER;
17684 f_line(argvars, &rettv);
17685 lnum = rettv.vval.v_number;
17686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 }
17688 return lnum;
17689}
17690
17691/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000017692 * Get the lnum from the first argument.
17693 * Also accepts "$", then "buf" is used.
17694 * Returns 0 on error.
17695 */
17696 static linenr_T
17697get_tv_lnum_buf(argvars, buf)
17698 typval_T *argvars;
17699 buf_T *buf;
17700{
17701 if (argvars[0].v_type == VAR_STRING
17702 && argvars[0].vval.v_string != NULL
17703 && argvars[0].vval.v_string[0] == '$'
17704 && buf != NULL)
17705 return buf->b_ml.ml_line_count;
17706 return get_tv_number_chk(&argvars[0], NULL);
17707}
17708
17709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 * Get the string value of a variable.
17711 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000017712 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17713 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 * If the String variable has never been set, return an empty string.
17715 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017716 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17717 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 */
17719 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017720get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000017721 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722{
17723 static char_u mybuf[NUMBUFLEN];
17724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017725 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726}
17727
17728 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017729get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000017730 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017731 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 char_u *res = get_tv_string_buf_chk(varp, buf);
17734
17735 return res != NULL ? res : (char_u *)"";
17736}
17737
Bram Moolenaar4be06f92005-07-29 22:36:03 +000017738 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739get_tv_string_chk(varp)
17740 typval_T *varp;
17741{
17742 static char_u mybuf[NUMBUFLEN];
17743
17744 return get_tv_string_buf_chk(varp, mybuf);
17745}
17746
17747 static char_u *
17748get_tv_string_buf_chk(varp, buf)
17749 typval_T *varp;
17750 char_u *buf;
17751{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017752 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017754 case VAR_NUMBER:
17755 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
17756 return buf;
17757 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017758 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017759 break;
17760 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017761 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000017762 break;
17763 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017764 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765 break;
17766 case VAR_STRING:
17767 if (varp->vval.v_string != NULL)
17768 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017769 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017770 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017771 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017774 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775}
17776
17777/*
17778 * Find variable "name" in the list of variables.
17779 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017780 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017781 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000017782 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017784 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017785find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017787 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791
Bram Moolenaara7043832005-01-21 11:56:39 +000017792 ht = find_var_ht(name, &varname);
17793 if (htp != NULL)
17794 *htp = ht;
17795 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017797 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798}
17799
17800/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017801 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000017802 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017804 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017805find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000017806 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000017807 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017808 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000017809{
Bram Moolenaar33570922005-01-25 22:26:29 +000017810 hashitem_T *hi;
17811
17812 if (*varname == NUL)
17813 {
17814 /* Must be something like "s:", otherwise "ht" would be NULL. */
17815 switch (varname[-2])
17816 {
17817 case 's': return &SCRIPT_SV(current_SID).sv_var;
17818 case 'g': return &globvars_var;
17819 case 'v': return &vimvars_var;
17820 case 'b': return &curbuf->b_bufvar;
17821 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017822#ifdef FEAT_WINDOWS
17823 case 't': return &curtab->tp_winvar;
17824#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017825 case 'l': return current_funccal == NULL
17826 ? NULL : &current_funccal->l_vars_var;
17827 case 'a': return current_funccal == NULL
17828 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 }
17830 return NULL;
17831 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017832
17833 hi = hash_find(ht, varname);
17834 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017835 {
17836 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017837 * worked find the variable again. Don't auto-load a script if it was
17838 * loaded already, otherwise it would be loaded every time when
17839 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017840 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017841 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017842 hi = hash_find(ht, varname);
17843 if (HASHITEM_EMPTY(hi))
17844 return NULL;
17845 }
Bram Moolenaar33570922005-01-25 22:26:29 +000017846 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000017847}
17848
17849/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017850 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000017851 * Set "varname" to the start of name without ':'.
17852 */
Bram Moolenaar33570922005-01-25 22:26:29 +000017853 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000017854find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 char_u *name;
17856 char_u **varname;
17857{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017858 hashitem_T *hi;
17859
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 if (name[1] != ':')
17861 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017862 /* The name must not start with a colon or #. */
17863 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 return NULL;
17865 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017866
17867 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000017868 hi = hash_find(&compat_hashtab, name);
17869 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000017870 return &compat_hashtab;
17871
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017873 return &globvarht; /* global variable */
17874 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 }
17876 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017877 if (*name == 'g') /* global variable */
17878 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017879 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17880 */
17881 if (vim_strchr(name + 2, ':') != NULL
17882 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017883 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017885 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000017887 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000017888#ifdef FEAT_WINDOWS
17889 if (*name == 't') /* tab page variable */
17890 return &curtab->tp_vars.dv_hashtab;
17891#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000017892 if (*name == 'v') /* v: variable */
17893 return &vimvarht;
17894 if (*name == 'a' && current_funccal != NULL) /* function argument */
17895 return &current_funccal->l_avars.dv_hashtab;
17896 if (*name == 'l' && current_funccal != NULL) /* local function variable */
17897 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 if (*name == 's' /* script variable */
17899 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
17900 return &SCRIPT_VARS(current_SID);
17901 return NULL;
17902}
17903
17904/*
17905 * Get the string value of a (global/local) variable.
17906 * Returns NULL when it doesn't exist.
17907 */
17908 char_u *
17909get_var_value(name)
17910 char_u *name;
17911{
Bram Moolenaar33570922005-01-25 22:26:29 +000017912 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913
Bram Moolenaara7043832005-01-21 11:56:39 +000017914 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 if (v == NULL)
17916 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000017917 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918}
17919
17920/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017921 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 * sourcing this script and when executing functions defined in the script.
17923 */
17924 void
17925new_script_vars(id)
17926 scid_T id;
17927{
Bram Moolenaara7043832005-01-21 11:56:39 +000017928 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000017929 hashtab_T *ht;
17930 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000017931
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
17933 {
Bram Moolenaara7043832005-01-21 11:56:39 +000017934 /* Re-allocating ga_data means that an ht_array pointing to
17935 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000017936 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000017937 for (i = 1; i <= ga_scripts.ga_len; ++i)
17938 {
17939 ht = &SCRIPT_VARS(i);
17940 if (ht->ht_mask == HT_INIT_SIZE - 1)
17941 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000017942 sv = &SCRIPT_SV(i);
17943 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000017944 }
17945
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946 while (ga_scripts.ga_len < id)
17947 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017948 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
17949 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 }
17952 }
17953}
17954
17955/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017956 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17957 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 */
17959 void
Bram Moolenaar33570922005-01-25 22:26:29 +000017960init_var_dict(dict, dict_var)
17961 dict_T *dict;
17962 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963{
Bram Moolenaar33570922005-01-25 22:26:29 +000017964 hash_init(&dict->dv_hashtab);
17965 dict->dv_refcount = 99999;
17966 dict_var->di_tv.vval.v_dict = dict;
17967 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017968 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017969 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17970 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971}
17972
17973/*
17974 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000017975 * Frees all allocated variables and the value they contain.
17976 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 */
17978 void
Bram Moolenaara7043832005-01-21 11:56:39 +000017979vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000017980 hashtab_T *ht;
17981{
17982 vars_clear_ext(ht, TRUE);
17983}
17984
17985/*
17986 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17987 */
17988 static void
17989vars_clear_ext(ht, free_val)
17990 hashtab_T *ht;
17991 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992{
Bram Moolenaara7043832005-01-21 11:56:39 +000017993 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 hashitem_T *hi;
17995 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996
Bram Moolenaar33570922005-01-25 22:26:29 +000017997 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017998 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000017999 for (hi = ht->ht_array; todo > 0; ++hi)
18000 {
18001 if (!HASHITEM_EMPTY(hi))
18002 {
18003 --todo;
18004
Bram Moolenaar33570922005-01-25 22:26:29 +000018005 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018006 * ht_array might change then. hash_clear() takes care of it
18007 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018008 v = HI2DI(hi);
18009 if (free_val)
18010 clear_tv(&v->di_tv);
18011 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18012 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018013 }
18014 }
18015 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018016 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017}
18018
Bram Moolenaara7043832005-01-21 11:56:39 +000018019/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018020 * Delete a variable from hashtab "ht" at item "hi".
18021 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018024delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 hashtab_T *ht;
18026 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027{
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018029
18030 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000018031 clear_tv(&di->di_tv);
18032 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033}
18034
18035/*
18036 * List the value of one internal variable.
18037 */
18038 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018039list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000018040 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018042 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018044 char_u *tofree;
18045 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018046 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018047
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018048 s = echo_string(&v->di_tv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000018049 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018050 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018051 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052}
18053
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018055list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056 char_u *prefix;
18057 char_u *name;
18058 int type;
18059 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018060 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061{
Bram Moolenaar31859182007-08-14 20:41:13 +000018062 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18063 msg_start();
18064 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 if (name != NULL) /* "a:" vars don't have a name stored */
18066 msg_puts(name);
18067 msg_putchar(' ');
18068 msg_advance(22);
18069 if (type == VAR_NUMBER)
18070 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018071 else if (type == VAR_FUNC)
18072 msg_putchar('*');
18073 else if (type == VAR_LIST)
18074 {
18075 msg_putchar('[');
18076 if (*string == '[')
18077 ++string;
18078 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000018079 else if (type == VAR_DICT)
18080 {
18081 msg_putchar('{');
18082 if (*string == '{')
18083 ++string;
18084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 else
18086 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018087
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018089
18090 if (type == VAR_FUNC)
18091 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000018092 if (*first)
18093 {
18094 msg_clr_eos();
18095 *first = FALSE;
18096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097}
18098
18099/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 * If the variable already exists, the value is updated.
18102 * Otherwise the variable is created.
18103 */
18104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018105set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018107 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018108 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109{
Bram Moolenaar33570922005-01-25 22:26:29 +000018110 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018112 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018113 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018115 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018116 {
18117 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
18118 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
18119 ? name[2] : name[0]))
18120 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018121 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018122 return;
18123 }
18124 if (function_exists(name))
18125 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018126 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018127 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018128 return;
18129 }
18130 }
18131
Bram Moolenaara7043832005-01-21 11:56:39 +000018132 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000018134 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000018135 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000018136 return;
18137 }
18138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018139 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000018140 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018142 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018143 if (var_check_ro(v->di_flags, name)
18144 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000018145 return;
18146 if (v->di_tv.v_type != tv->v_type
18147 && !((v->di_tv.v_type == VAR_STRING
18148 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018149 && (tv->v_type == VAR_STRING
18150 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018151 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000018152 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018153 return;
18154 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018155
18156 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000018157 * Handle setting internal v: variables separately: we don't change
18158 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000018159 */
18160 if (ht == &vimvarht)
18161 {
18162 if (v->di_tv.v_type == VAR_STRING)
18163 {
18164 vim_free(v->di_tv.vval.v_string);
18165 if (copy || tv->v_type != VAR_STRING)
18166 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
18167 else
18168 {
18169 /* Take over the string to avoid an extra alloc/free. */
18170 v->di_tv.vval.v_string = tv->vval.v_string;
18171 tv->vval.v_string = NULL;
18172 }
18173 }
18174 else if (v->di_tv.v_type != VAR_NUMBER)
18175 EMSG2(_(e_intern2), "set_var()");
18176 else
18177 v->di_tv.vval.v_number = get_tv_number(tv);
18178 return;
18179 }
18180
18181 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 }
18183 else /* add a new variable */
18184 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000018185 /* Can't add "v:" variable. */
18186 if (ht == &vimvarht)
18187 {
18188 EMSG2(_(e_illvar), name);
18189 return;
18190 }
18191
Bram Moolenaar92124a32005-06-17 22:03:40 +000018192 /* Make sure the variable name is valid. */
18193 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000018194 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
18195 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000018196 {
18197 EMSG2(_(e_illvar), varname);
18198 return;
18199 }
18200
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018201 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18202 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000018203 if (v == NULL)
18204 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000018205 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000018206 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018208 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 return;
18210 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018211 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018214 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000018215 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018216 else
18217 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018218 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018219 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222}
18223
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018224/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018225 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000018226 * Also give an error message.
18227 */
18228 static int
18229var_check_ro(flags, name)
18230 int flags;
18231 char_u *name;
18232{
18233 if (flags & DI_FLAGS_RO)
18234 {
18235 EMSG2(_(e_readonlyvar), name);
18236 return TRUE;
18237 }
18238 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
18239 {
18240 EMSG2(_(e_readonlysbx), name);
18241 return TRUE;
18242 }
18243 return FALSE;
18244}
18245
18246/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000018247 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18248 * Also give an error message.
18249 */
18250 static int
18251var_check_fixed(flags, name)
18252 int flags;
18253 char_u *name;
18254{
18255 if (flags & DI_FLAGS_FIX)
18256 {
18257 EMSG2(_("E795: Cannot delete variable %s"), name);
18258 return TRUE;
18259 }
18260 return FALSE;
18261}
18262
18263/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018264 * Return TRUE if typeval "tv" is set to be locked (immutable).
18265 * Also give an error message, using "name".
18266 */
18267 static int
18268tv_check_lock(lock, name)
18269 int lock;
18270 char_u *name;
18271{
18272 if (lock & VAR_LOCKED)
18273 {
18274 EMSG2(_("E741: Value is locked: %s"),
18275 name == NULL ? (char_u *)_("Unknown") : name);
18276 return TRUE;
18277 }
18278 if (lock & VAR_FIXED)
18279 {
18280 EMSG2(_("E742: Cannot change value of %s"),
18281 name == NULL ? (char_u *)_("Unknown") : name);
18282 return TRUE;
18283 }
18284 return FALSE;
18285}
18286
18287/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018288 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018289 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018290 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018293copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000018294 typval_T *from;
18295 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018297 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018298 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018299 switch (from->v_type)
18300 {
18301 case VAR_NUMBER:
18302 to->vval.v_number = from->vval.v_number;
18303 break;
18304 case VAR_STRING:
18305 case VAR_FUNC:
18306 if (from->vval.v_string == NULL)
18307 to->vval.v_string = NULL;
18308 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018309 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018310 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018311 if (from->v_type == VAR_FUNC)
18312 func_ref(to->vval.v_string);
18313 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018314 break;
18315 case VAR_LIST:
18316 if (from->vval.v_list == NULL)
18317 to->vval.v_list = NULL;
18318 else
18319 {
18320 to->vval.v_list = from->vval.v_list;
18321 ++to->vval.v_list->lv_refcount;
18322 }
18323 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018324 case VAR_DICT:
18325 if (from->vval.v_dict == NULL)
18326 to->vval.v_dict = NULL;
18327 else
18328 {
18329 to->vval.v_dict = from->vval.v_dict;
18330 ++to->vval.v_dict->dv_refcount;
18331 }
18332 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018333 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018335 break;
18336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337}
18338
18339/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000018340 * Make a copy of an item.
18341 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018342 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18343 * reference to an already copied list/dict can be used.
18344 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018345 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018346 static int
18347item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000018348 typval_T *from;
18349 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018350 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018351 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018352{
18353 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018354 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018355
Bram Moolenaar33570922005-01-25 22:26:29 +000018356 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018357 {
18358 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018359 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018360 }
18361 ++recurse;
18362
18363 switch (from->v_type)
18364 {
18365 case VAR_NUMBER:
18366 case VAR_STRING:
18367 case VAR_FUNC:
18368 copy_tv(from, to);
18369 break;
18370 case VAR_LIST:
18371 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018372 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018373 if (from->vval.v_list == NULL)
18374 to->vval.v_list = NULL;
18375 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
18376 {
18377 /* use the copy made earlier */
18378 to->vval.v_list = from->vval.v_list->lv_copylist;
18379 ++to->vval.v_list->lv_refcount;
18380 }
18381 else
18382 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
18383 if (to->vval.v_list == NULL)
18384 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018385 break;
18386 case VAR_DICT:
18387 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018388 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018389 if (from->vval.v_dict == NULL)
18390 to->vval.v_dict = NULL;
18391 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
18392 {
18393 /* use the copy made earlier */
18394 to->vval.v_dict = from->vval.v_dict->dv_copydict;
18395 ++to->vval.v_dict->dv_refcount;
18396 }
18397 else
18398 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
18399 if (to->vval.v_dict == NULL)
18400 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018401 break;
18402 default:
18403 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018404 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018405 }
18406 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018407 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018408}
18409
18410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 * ":echo expr1 ..." print each argument separated with a space, add a
18412 * newline at the end.
18413 * ":echon expr1 ..." print each argument plain.
18414 */
18415 void
18416ex_echo(eap)
18417 exarg_T *eap;
18418{
18419 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018420 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018421 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422 char_u *p;
18423 int needclr = TRUE;
18424 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018425 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426
18427 if (eap->skip)
18428 ++emsg_skip;
18429 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
18430 {
18431 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433 {
18434 /*
18435 * Report the invalid expression unless the expression evaluation
18436 * has been cancelled due to an aborting error, an interrupt, or an
18437 * exception.
18438 */
18439 if (!aborting())
18440 EMSG2(_(e_invexpr2), p);
18441 break;
18442 }
18443 if (!eap->skip)
18444 {
18445 if (atstart)
18446 {
18447 atstart = FALSE;
18448 /* Call msg_start() after eval1(), evaluating the expression
18449 * may cause a message to appear. */
18450 if (eap->cmdidx == CMD_echo)
18451 msg_start();
18452 }
18453 else if (eap->cmdidx == CMD_echo)
18454 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018455 p = echo_string(&rettv, &tofree, numbuf, ++current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018456 if (p != NULL)
18457 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018459 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018461 if (*p != TAB && needclr)
18462 {
18463 /* remove any text still there from the command */
18464 msg_clr_eos();
18465 needclr = FALSE;
18466 }
18467 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 }
18469 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018470 {
18471#ifdef FEAT_MBYTE
18472 if (has_mbyte)
18473 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018474 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018475
18476 (void)msg_outtrans_len_attr(p, i, echo_attr);
18477 p += i - 1;
18478 }
18479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018481 (void)msg_outtrans_len_attr(p, 1, echo_attr);
18482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018484 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 arg = skipwhite(arg);
18488 }
18489 eap->nextcmd = check_nextcmd(arg);
18490
18491 if (eap->skip)
18492 --emsg_skip;
18493 else
18494 {
18495 /* remove text that may still be there from the command */
18496 if (needclr)
18497 msg_clr_eos();
18498 if (eap->cmdidx == CMD_echo)
18499 msg_end();
18500 }
18501}
18502
18503/*
18504 * ":echohl {name}".
18505 */
18506 void
18507ex_echohl(eap)
18508 exarg_T *eap;
18509{
18510 int id;
18511
18512 id = syn_name2id(eap->arg);
18513 if (id == 0)
18514 echo_attr = 0;
18515 else
18516 echo_attr = syn_id2attr(id);
18517}
18518
18519/*
18520 * ":execute expr1 ..." execute the result of an expression.
18521 * ":echomsg expr1 ..." Print a message
18522 * ":echoerr expr1 ..." Print an error
18523 * Each gets spaces around each argument and a newline at the end for
18524 * echo commands
18525 */
18526 void
18527ex_execute(eap)
18528 exarg_T *eap;
18529{
18530 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018531 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 int ret = OK;
18533 char_u *p;
18534 garray_T ga;
18535 int len;
18536 int save_did_emsg;
18537
18538 ga_init2(&ga, 1, 80);
18539
18540 if (eap->skip)
18541 ++emsg_skip;
18542 while (*arg != NUL && *arg != '|' && *arg != '\n')
18543 {
18544 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018545 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 {
18547 /*
18548 * Report the invalid expression unless the expression evaluation
18549 * has been cancelled due to an aborting error, an interrupt, or an
18550 * exception.
18551 */
18552 if (!aborting())
18553 EMSG2(_(e_invexpr2), p);
18554 ret = FAIL;
18555 break;
18556 }
18557
18558 if (!eap->skip)
18559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018560 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 len = (int)STRLEN(p);
18562 if (ga_grow(&ga, len + 2) == FAIL)
18563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018564 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 ret = FAIL;
18566 break;
18567 }
18568 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 ga.ga_len += len;
18572 }
18573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 arg = skipwhite(arg);
18576 }
18577
18578 if (ret != FAIL && ga.ga_data != NULL)
18579 {
18580 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000018581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000018583 out_flush();
18584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 else if (eap->cmdidx == CMD_echoerr)
18586 {
18587 /* We don't want to abort following commands, restore did_emsg. */
18588 save_did_emsg = did_emsg;
18589 EMSG((char_u *)ga.ga_data);
18590 if (!force_abort)
18591 did_emsg = save_did_emsg;
18592 }
18593 else if (eap->cmdidx == CMD_execute)
18594 do_cmdline((char_u *)ga.ga_data,
18595 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
18596 }
18597
18598 ga_clear(&ga);
18599
18600 if (eap->skip)
18601 --emsg_skip;
18602
18603 eap->nextcmd = check_nextcmd(arg);
18604}
18605
18606/*
18607 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18608 * "arg" points to the "&" or '+' when called, to "option" when returning.
18609 * Returns NULL when no option name found. Otherwise pointer to the char
18610 * after the option name.
18611 */
18612 static char_u *
18613find_option_end(arg, opt_flags)
18614 char_u **arg;
18615 int *opt_flags;
18616{
18617 char_u *p = *arg;
18618
18619 ++p;
18620 if (*p == 'g' && p[1] == ':')
18621 {
18622 *opt_flags = OPT_GLOBAL;
18623 p += 2;
18624 }
18625 else if (*p == 'l' && p[1] == ':')
18626 {
18627 *opt_flags = OPT_LOCAL;
18628 p += 2;
18629 }
18630 else
18631 *opt_flags = 0;
18632
18633 if (!ASCII_ISALPHA(*p))
18634 return NULL;
18635 *arg = p;
18636
18637 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
18638 p += 4; /* termcap option */
18639 else
18640 while (ASCII_ISALPHA(*p))
18641 ++p;
18642 return p;
18643}
18644
18645/*
18646 * ":function"
18647 */
18648 void
18649ex_function(eap)
18650 exarg_T *eap;
18651{
18652 char_u *theline;
18653 int j;
18654 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 char_u *name = NULL;
18657 char_u *p;
18658 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018659 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 garray_T newargs;
18661 garray_T newlines;
18662 int varargs = FALSE;
18663 int mustend = FALSE;
18664 int flags = 0;
18665 ufunc_T *fp;
18666 int indent;
18667 int nesting;
18668 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018669 dictitem_T *v;
18670 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018671 static int func_nr = 0; /* number for nameless function */
18672 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018673 hashtab_T *ht;
18674 int todo;
18675 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018676 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677
18678 /*
18679 * ":function" without argument: list functions.
18680 */
18681 if (ends_excmd(*eap->arg))
18682 {
18683 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018684 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018685 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000018686 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018687 {
18688 if (!HASHITEM_EMPTY(hi))
18689 {
18690 --todo;
18691 fp = HI2UF(hi);
18692 if (!isdigit(*fp->uf_name))
18693 list_func_head(fp, FALSE);
18694 }
18695 }
18696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 eap->nextcmd = check_nextcmd(eap->arg);
18698 return;
18699 }
18700
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018701 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018702 * ":function /pat": list functions matching pattern.
18703 */
18704 if (*eap->arg == '/')
18705 {
18706 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
18707 if (!eap->skip)
18708 {
18709 regmatch_T regmatch;
18710
18711 c = *p;
18712 *p = NUL;
18713 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
18714 *p = c;
18715 if (regmatch.regprog != NULL)
18716 {
18717 regmatch.rm_ic = p_ic;
18718
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018719 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018720 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
18721 {
18722 if (!HASHITEM_EMPTY(hi))
18723 {
18724 --todo;
18725 fp = HI2UF(hi);
18726 if (!isdigit(*fp->uf_name)
18727 && vim_regexec(&regmatch, fp->uf_name, 0))
18728 list_func_head(fp, FALSE);
18729 }
18730 }
18731 }
18732 }
18733 if (*p == '/')
18734 ++p;
18735 eap->nextcmd = check_nextcmd(p);
18736 return;
18737 }
18738
18739 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018740 * Get the function name. There are these situations:
18741 * func normal function name
18742 * "name" == func, "fudi.fd_dict" == NULL
18743 * dict.func new dictionary entry
18744 * "name" == NULL, "fudi.fd_dict" set,
18745 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18746 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018747 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018748 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18749 * dict.func existing dict entry that's not a Funcref
18750 * "name" == NULL, "fudi.fd_dict" set,
18751 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018754 name = trans_function_name(&p, eap->skip, 0, &fudi);
18755 paren = (vim_strchr(p, '(') != NULL);
18756 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 {
18758 /*
18759 * Return on an invalid expression in braces, unless the expression
18760 * evaluation has been cancelled due to an aborting error, an
18761 * interrupt, or an exception.
18762 */
18763 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018764 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018765 if (!eap->skip && fudi.fd_newkey != NULL)
18766 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018767 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770 else
18771 eap->skip = TRUE;
18772 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000018773
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774 /* An error in a function call during evaluation of an expression in magic
18775 * braces should not cause the function not to be defined. */
18776 saved_did_emsg = did_emsg;
18777 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778
18779 /*
18780 * ":function func" with only function name: list function.
18781 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018782 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 {
18784 if (!ends_excmd(*skipwhite(p)))
18785 {
18786 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018787 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 }
18789 eap->nextcmd = check_nextcmd(p);
18790 if (eap->nextcmd != NULL)
18791 *p = NUL;
18792 if (!eap->skip && !got_int)
18793 {
18794 fp = find_func(name);
18795 if (fp != NULL)
18796 {
18797 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018798 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018800 if (FUNCLINE(fp, j) == NULL)
18801 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 msg_putchar('\n');
18803 msg_outnum((long)(j + 1));
18804 if (j < 9)
18805 msg_putchar(' ');
18806 if (j < 99)
18807 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018808 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 out_flush(); /* show a line at a time */
18810 ui_breakcheck();
18811 }
18812 if (!got_int)
18813 {
18814 msg_putchar('\n');
18815 msg_puts((char_u *)" endfunction");
18816 }
18817 }
18818 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018819 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018821 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 }
18823
18824 /*
18825 * ":function name(arg1, arg2)" Define function.
18826 */
18827 p = skipwhite(p);
18828 if (*p != '(')
18829 {
18830 if (!eap->skip)
18831 {
18832 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018833 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 }
18835 /* attempt to continue by skipping some text */
18836 if (vim_strchr(p, '(') != NULL)
18837 p = vim_strchr(p, '(');
18838 }
18839 p = skipwhite(p + 1);
18840
18841 ga_init2(&newargs, (int)sizeof(char_u *), 3);
18842 ga_init2(&newlines, (int)sizeof(char_u *), 3);
18843
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018844 if (!eap->skip)
18845 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018846 /* Check the name of the function. Unless it's a dictionary function
18847 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018848 if (name != NULL)
18849 arg = name;
18850 else
18851 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000018852 if (arg != NULL && (fudi.fd_di == NULL
18853 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018854 {
18855 if (*arg == K_SPECIAL)
18856 j = 3;
18857 else
18858 j = 0;
18859 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
18860 : eval_isnamec(arg[j])))
18861 ++j;
18862 if (arg[j] != NUL)
18863 emsg_funcname(_(e_invarg2), arg);
18864 }
18865 }
18866
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867 /*
18868 * Isolate the arguments: "arg1, arg2, ...)"
18869 */
18870 while (*p != ')')
18871 {
18872 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
18873 {
18874 varargs = TRUE;
18875 p += 3;
18876 mustend = TRUE;
18877 }
18878 else
18879 {
18880 arg = p;
18881 while (ASCII_ISALNUM(*p) || *p == '_')
18882 ++p;
18883 if (arg == p || isdigit(*arg)
18884 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
18885 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
18886 {
18887 if (!eap->skip)
18888 EMSG2(_("E125: Illegal argument: %s"), arg);
18889 break;
18890 }
18891 if (ga_grow(&newargs, 1) == FAIL)
18892 goto erret;
18893 c = *p;
18894 *p = NUL;
18895 arg = vim_strsave(arg);
18896 if (arg == NULL)
18897 goto erret;
18898 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
18899 *p = c;
18900 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 if (*p == ',')
18902 ++p;
18903 else
18904 mustend = TRUE;
18905 }
18906 p = skipwhite(p);
18907 if (mustend && *p != ')')
18908 {
18909 if (!eap->skip)
18910 EMSG2(_(e_invarg2), eap->arg);
18911 break;
18912 }
18913 }
18914 ++p; /* skip the ')' */
18915
Bram Moolenaare9a41262005-01-15 22:18:47 +000018916 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 for (;;)
18918 {
18919 p = skipwhite(p);
18920 if (STRNCMP(p, "range", 5) == 0)
18921 {
18922 flags |= FC_RANGE;
18923 p += 5;
18924 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018925 else if (STRNCMP(p, "dict", 4) == 0)
18926 {
18927 flags |= FC_DICT;
18928 p += 4;
18929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 else if (STRNCMP(p, "abort", 5) == 0)
18931 {
18932 flags |= FC_ABORT;
18933 p += 5;
18934 }
18935 else
18936 break;
18937 }
18938
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018939 /* When there is a line break use what follows for the function body.
18940 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18941 if (*p == '\n')
18942 line_arg = p + 1;
18943 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944 EMSG(_(e_trailing));
18945
18946 /*
18947 * Read the body of the function, until ":endfunction" is found.
18948 */
18949 if (KeyTyped)
18950 {
18951 /* Check if the function already exists, don't let the user type the
18952 * whole function before telling him it doesn't work! For a script we
18953 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018954 if (!eap->skip && !eap->forceit)
18955 {
18956 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
18957 EMSG(_(e_funcdict));
18958 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018959 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018962 if (!eap->skip && did_emsg)
18963 goto erret;
18964
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 msg_putchar('\n'); /* don't overwrite the function name */
18966 cmdline_row = msg_row;
18967 }
18968
18969 indent = 2;
18970 nesting = 0;
18971 for (;;)
18972 {
18973 msg_scroll = TRUE;
18974 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000018975 sourcing_lnum_off = sourcing_lnum;
18976
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000018977 if (line_arg != NULL)
18978 {
18979 /* Use eap->arg, split up in parts by line breaks. */
18980 theline = line_arg;
18981 p = vim_strchr(theline, '\n');
18982 if (p == NULL)
18983 line_arg += STRLEN(line_arg);
18984 else
18985 {
18986 *p = NUL;
18987 line_arg = p + 1;
18988 }
18989 }
18990 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 theline = getcmdline(':', 0L, indent);
18992 else
18993 theline = eap->getline(':', eap->cookie, indent);
18994 if (KeyTyped)
18995 lines_left = Rows - 1;
18996 if (theline == NULL)
18997 {
18998 EMSG(_("E126: Missing :endfunction"));
18999 goto erret;
19000 }
19001
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019002 /* Detect line continuation: sourcing_lnum increased more than one. */
19003 if (sourcing_lnum > sourcing_lnum_off + 1)
19004 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
19005 else
19006 sourcing_lnum_off = 0;
19007
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 if (skip_until != NULL)
19009 {
19010 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19011 * don't check for ":endfunc". */
19012 if (STRCMP(theline, skip_until) == 0)
19013 {
19014 vim_free(skip_until);
19015 skip_until = NULL;
19016 }
19017 }
19018 else
19019 {
19020 /* skip ':' and blanks*/
19021 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
19022 ;
19023
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019024 /* Check for "endfunction". */
19025 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019027 if (line_arg == NULL)
19028 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 break;
19030 }
19031
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019032 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 * at "end". */
19034 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
19035 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019036 else if (STRNCMP(p, "if", 2) == 0
19037 || STRNCMP(p, "wh", 2) == 0
19038 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 || STRNCMP(p, "try", 3) == 0)
19040 indent += 2;
19041
19042 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019043 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019045 if (*p == '!')
19046 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 p += eval_fname_script(p);
19048 if (ASCII_ISALPHA(*p))
19049 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019050 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 if (*skipwhite(p) == '(')
19052 {
19053 ++nesting;
19054 indent += 2;
19055 }
19056 }
19057 }
19058
19059 /* Check for ":append" or ":insert". */
19060 p = skip_range(p, NULL);
19061 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
19062 || (p[0] == 'i'
19063 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
19064 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
19065 skip_until = vim_strsave((char_u *)".");
19066
19067 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19068 arg = skipwhite(skiptowhite(p));
19069 if (arg[0] == '<' && arg[1] =='<'
19070 && ((p[0] == 'p' && p[1] == 'y'
19071 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
19072 || (p[0] == 'p' && p[1] == 'e'
19073 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
19074 || (p[0] == 't' && p[1] == 'c'
19075 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
19076 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
19077 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000019078 || (p[0] == 'm' && p[1] == 'z'
19079 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 ))
19081 {
19082 /* ":python <<" continues until a dot, like ":append" */
19083 p = skipwhite(arg + 2);
19084 if (*p == NUL)
19085 skip_until = vim_strsave((char_u *)".");
19086 else
19087 skip_until = vim_strsave(p);
19088 }
19089 }
19090
19091 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019092 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019093 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019094 if (line_arg == NULL)
19095 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019097 }
19098
19099 /* Copy the line to newly allocated memory. get_one_sourceline()
19100 * allocates 250 bytes per line, this saves 80% on average. The cost
19101 * is an extra alloc/free. */
19102 p = vim_strsave(theline);
19103 if (p != NULL)
19104 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019105 if (line_arg == NULL)
19106 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019107 theline = p;
19108 }
19109
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019110 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
19111
19112 /* Add NULL lines for continuation lines, so that the line count is
19113 * equal to the index in the growarray. */
19114 while (sourcing_lnum_off-- > 0)
19115 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019116
19117 /* Check for end of eap->arg. */
19118 if (line_arg != NULL && *line_arg == NUL)
19119 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 }
19121
19122 /* Don't define the function when skipping commands or when an error was
19123 * detected. */
19124 if (eap->skip || did_emsg)
19125 goto erret;
19126
19127 /*
19128 * If there are no errors, add the function
19129 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019130 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019131 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019132 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000019133 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019134 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019135 emsg_funcname("E707: Function name conflicts with variable: %s",
19136 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019137 goto erret;
19138 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019139
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019140 fp = find_func(name);
19141 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019143 if (!eap->forceit)
19144 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019145 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019146 goto erret;
19147 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019148 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019149 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019150 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019151 name);
19152 goto erret;
19153 }
19154 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019155 ga_clear_strings(&(fp->uf_args));
19156 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019157 vim_free(name);
19158 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 }
19161 else
19162 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019163 char numbuf[20];
19164
19165 fp = NULL;
19166 if (fudi.fd_newkey == NULL && !eap->forceit)
19167 {
19168 EMSG(_(e_funcdict));
19169 goto erret;
19170 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000019171 if (fudi.fd_di == NULL)
19172 {
19173 /* Can't add a function to a locked dictionary */
19174 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
19175 goto erret;
19176 }
19177 /* Can't change an existing function if it is locked */
19178 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
19179 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019180
19181 /* Give the function a sequential number. Can only be used with a
19182 * Funcref! */
19183 vim_free(name);
19184 sprintf(numbuf, "%d", ++func_nr);
19185 name = vim_strsave((char_u *)numbuf);
19186 if (name == NULL)
19187 goto erret;
19188 }
19189
19190 if (fp == NULL)
19191 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019192 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019193 {
19194 int slen, plen;
19195 char_u *scriptname;
19196
19197 /* Check that the autoload name matches the script name. */
19198 j = FAIL;
19199 if (sourcing_name != NULL)
19200 {
19201 scriptname = autoload_name(name);
19202 if (scriptname != NULL)
19203 {
19204 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019205 plen = (int)STRLEN(p);
19206 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019207 if (slen > plen && fnamecmp(p,
19208 sourcing_name + slen - plen) == 0)
19209 j = OK;
19210 vim_free(scriptname);
19211 }
19212 }
19213 if (j == FAIL)
19214 {
19215 EMSG2(_("E746: Function name does not match script file name: %s"), name);
19216 goto erret;
19217 }
19218 }
19219
19220 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 if (fp == NULL)
19222 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019223
19224 if (fudi.fd_dict != NULL)
19225 {
19226 if (fudi.fd_di == NULL)
19227 {
19228 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019229 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019230 if (fudi.fd_di == NULL)
19231 {
19232 vim_free(fp);
19233 goto erret;
19234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019235 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
19236 {
19237 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000019238 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019239 goto erret;
19240 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019241 }
19242 else
19243 /* overwrite existing dict entry */
19244 clear_tv(&fudi.fd_di->di_tv);
19245 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019246 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019247 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019248 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019249
19250 /* behave like "dict" was used */
19251 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019252 }
19253
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019255 STRCPY(fp->uf_name, name);
19256 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019258 fp->uf_args = newargs;
19259 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019260#ifdef FEAT_PROFILE
19261 fp->uf_tml_count = NULL;
19262 fp->uf_tml_total = NULL;
19263 fp->uf_tml_self = NULL;
19264 fp->uf_profiling = FALSE;
19265 if (prof_def_func())
19266 func_do_profile(fp);
19267#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019268 fp->uf_varargs = varargs;
19269 fp->uf_flags = flags;
19270 fp->uf_calls = 0;
19271 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019272 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273
19274erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 ga_clear_strings(&newargs);
19276 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019277ret_free:
19278 vim_free(skip_until);
19279 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282}
19283
19284/*
19285 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000019286 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019288 * flags:
19289 * TFN_INT: internal function name OK
19290 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 * Advances "pp" to just after the function name (if no error).
19292 */
19293 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019294trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 char_u **pp;
19296 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019297 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000019298 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019300 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 char_u *start;
19302 char_u *end;
19303 int lead;
19304 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000019306 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019307
19308 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019309 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000019311
19312 /* Check for hard coded <SNR>: already translated function ID (from a user
19313 * command). */
19314 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
19315 && (*pp)[2] == (int)KE_SNR)
19316 {
19317 *pp += 3;
19318 len = get_id_len(pp) + 3;
19319 return vim_strnsave(start, len);
19320 }
19321
19322 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19323 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000019325 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019327
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019328 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
19329 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019330 if (end == start)
19331 {
19332 if (!skip)
19333 EMSG(_("E129: Function name required"));
19334 goto theend;
19335 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019336 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019337 {
19338 /*
19339 * Report an invalid expression in braces, unless the expression
19340 * evaluation has been cancelled due to an aborting error, an
19341 * interrupt, or an exception.
19342 */
19343 if (!aborting())
19344 {
19345 if (end != NULL)
19346 EMSG2(_(e_invarg2), start);
19347 }
19348 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019349 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019350 goto theend;
19351 }
19352
19353 if (lv.ll_tv != NULL)
19354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019355 if (fdp != NULL)
19356 {
19357 fdp->fd_dict = lv.ll_dict;
19358 fdp->fd_newkey = lv.ll_newkey;
19359 lv.ll_newkey = NULL;
19360 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019361 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019362 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
19363 {
19364 name = vim_strsave(lv.ll_tv->vval.v_string);
19365 *pp = end;
19366 }
19367 else
19368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019369 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
19370 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019371 EMSG(_(e_funcref));
19372 else
19373 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019374 name = NULL;
19375 }
19376 goto theend;
19377 }
19378
19379 if (lv.ll_name == NULL)
19380 {
19381 /* Error found, but continue after the function name. */
19382 *pp = end;
19383 goto theend;
19384 }
19385
Bram Moolenaar33e1a802007-09-06 12:26:44 +000019386 /* Check if the name is a Funcref. If so, use the value. */
19387 if (lv.ll_exp_name != NULL)
19388 {
19389 len = (int)STRLEN(lv.ll_exp_name);
19390 name = deref_func_name(lv.ll_exp_name, &len);
19391 if (name == lv.ll_exp_name)
19392 name = NULL;
19393 }
19394 else
19395 {
19396 len = (int)(end - *pp);
19397 name = deref_func_name(*pp, &len);
19398 if (name == *pp)
19399 name = NULL;
19400 }
19401 if (name != NULL)
19402 {
19403 name = vim_strsave(name);
19404 *pp = end;
19405 goto theend;
19406 }
19407
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019408 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019409 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019410 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000019411 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
19412 && STRNCMP(lv.ll_name, "s:", 2) == 0)
19413 {
19414 /* When there was "s:" already or the name expanded to get a
19415 * leading "s:" then remove it. */
19416 lv.ll_name += 2;
19417 len -= 2;
19418 lead = 2;
19419 }
19420 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019421 else
Bram Moolenaara7043832005-01-21 11:56:39 +000019422 {
19423 if (lead == 2) /* skip over "s:" */
19424 lv.ll_name += 2;
19425 len = (int)(end - lv.ll_name);
19426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019427
19428 /*
19429 * Copy the function name to allocated memory.
19430 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19431 * Accept <SNR>123_name() outside a script.
19432 */
19433 if (skip)
19434 lead = 0; /* do nothing */
19435 else if (lead > 0)
19436 {
19437 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000019438 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
19439 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019440 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000019441 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019442 if (current_SID <= 0)
19443 {
19444 EMSG(_(e_usingsid));
19445 goto theend;
19446 }
19447 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
19448 lead += (int)STRLEN(sid_buf);
19449 }
19450 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019451 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019452 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019453 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019454 goto theend;
19455 }
19456 name = alloc((unsigned)(len + lead + 1));
19457 if (name != NULL)
19458 {
19459 if (lead > 0)
19460 {
19461 name[0] = K_SPECIAL;
19462 name[1] = KS_EXTRA;
19463 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000019464 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000019465 STRCPY(name + 3, sid_buf);
19466 }
19467 mch_memmove(name + lead, lv.ll_name, (size_t)len);
19468 name[len + lead] = NUL;
19469 }
19470 *pp = end;
19471
19472theend:
19473 clear_lval(&lv);
19474 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475}
19476
19477/*
19478 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19479 * Return 2 if "p" starts with "s:".
19480 * Return 0 otherwise.
19481 */
19482 static int
19483eval_fname_script(p)
19484 char_u *p;
19485{
19486 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
19487 || STRNICMP(p + 1, "SNR>", 4) == 0))
19488 return 5;
19489 if (p[0] == 's' && p[1] == ':')
19490 return 2;
19491 return 0;
19492}
19493
19494/*
19495 * Return TRUE if "p" starts with "<SID>" or "s:".
19496 * Only works if eval_fname_script() returned non-zero for "p"!
19497 */
19498 static int
19499eval_fname_sid(p)
19500 char_u *p;
19501{
19502 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
19503}
19504
19505/*
19506 * List the head of the function: "name(arg1, arg2)".
19507 */
19508 static void
19509list_func_head(fp, indent)
19510 ufunc_T *fp;
19511 int indent;
19512{
19513 int j;
19514
19515 msg_start();
19516 if (indent)
19517 MSG_PUTS(" ");
19518 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019519 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 {
19521 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019522 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 }
19524 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019525 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019527 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 {
19529 if (j)
19530 MSG_PUTS(", ");
19531 msg_puts(FUNCARG(fp, j));
19532 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019533 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 {
19535 if (j)
19536 MSG_PUTS(", ");
19537 MSG_PUTS("...");
19538 }
19539 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019540 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000019541 if (p_verbose > 0)
19542 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543}
19544
19545/*
19546 * Find a function by name, return pointer to it in ufuncs.
19547 * Return NULL for unknown function.
19548 */
19549 static ufunc_T *
19550find_func(name)
19551 char_u *name;
19552{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019553 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019555 hi = hash_find(&func_hashtab, name);
19556 if (!HASHITEM_EMPTY(hi))
19557 return HI2UF(hi);
19558 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559}
19560
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000019561#if defined(EXITFREE) || defined(PROTO)
19562 void
19563free_all_functions()
19564{
19565 hashitem_T *hi;
19566
19567 /* Need to start all over every time, because func_free() may change the
19568 * hash table. */
19569 while (func_hashtab.ht_used > 0)
19570 for (hi = func_hashtab.ht_array; ; ++hi)
19571 if (!HASHITEM_EMPTY(hi))
19572 {
19573 func_free(HI2UF(hi));
19574 break;
19575 }
19576}
19577#endif
19578
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019579/*
19580 * Return TRUE if a function "name" exists.
19581 */
19582 static int
19583function_exists(name)
19584 char_u *name;
19585{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019586 char_u *nm = name;
19587 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019588 int n = FALSE;
19589
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000019590 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000019591 nm = skipwhite(nm);
19592
19593 /* Only accept "funcname", "funcname ", "funcname (..." and
19594 * "funcname(...", not "funcname!...". */
19595 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019596 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019597 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019598 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019599 else
19600 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019601 }
Bram Moolenaar79783442006-05-05 21:18:03 +000019602 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019603 return n;
19604}
19605
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019606/*
19607 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019608 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019609 */
19610 static int
19611builtin_function(name)
19612 char_u *name;
19613{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019614 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
19615 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019616}
19617
Bram Moolenaar05159a02005-02-26 23:04:13 +000019618#if defined(FEAT_PROFILE) || defined(PROTO)
19619/*
19620 * Start profiling function "fp".
19621 */
19622 static void
19623func_do_profile(fp)
19624 ufunc_T *fp;
19625{
19626 fp->uf_tm_count = 0;
19627 profile_zero(&fp->uf_tm_self);
19628 profile_zero(&fp->uf_tm_total);
19629 if (fp->uf_tml_count == NULL)
19630 fp->uf_tml_count = (int *)alloc_clear((unsigned)
19631 (sizeof(int) * fp->uf_lines.ga_len));
19632 if (fp->uf_tml_total == NULL)
19633 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
19634 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19635 if (fp->uf_tml_self == NULL)
19636 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
19637 (sizeof(proftime_T) * fp->uf_lines.ga_len));
19638 fp->uf_tml_idx = -1;
19639 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
19640 || fp->uf_tml_self == NULL)
19641 return; /* out of memory */
19642
19643 fp->uf_profiling = TRUE;
19644}
19645
19646/*
19647 * Dump the profiling results for all functions in file "fd".
19648 */
19649 void
19650func_dump_profile(fd)
19651 FILE *fd;
19652{
19653 hashitem_T *hi;
19654 int todo;
19655 ufunc_T *fp;
19656 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000019657 ufunc_T **sorttab;
19658 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000019659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019660 todo = (int)func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000019661 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
19662
Bram Moolenaar05159a02005-02-26 23:04:13 +000019663 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
19664 {
19665 if (!HASHITEM_EMPTY(hi))
19666 {
19667 --todo;
19668 fp = HI2UF(hi);
19669 if (fp->uf_profiling)
19670 {
Bram Moolenaar73830342005-02-28 22:48:19 +000019671 if (sorttab != NULL)
19672 sorttab[st_len++] = fp;
19673
Bram Moolenaar05159a02005-02-26 23:04:13 +000019674 if (fp->uf_name[0] == K_SPECIAL)
19675 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
19676 else
19677 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
19678 if (fp->uf_tm_count == 1)
19679 fprintf(fd, "Called 1 time\n");
19680 else
19681 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
19682 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
19683 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
19684 fprintf(fd, "\n");
19685 fprintf(fd, "count total (s) self (s)\n");
19686
19687 for (i = 0; i < fp->uf_lines.ga_len; ++i)
19688 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019689 if (FUNCLINE(fp, i) == NULL)
19690 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000019691 prof_func_line(fd, fp->uf_tml_count[i],
19692 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019693 fprintf(fd, "%s\n", FUNCLINE(fp, i));
19694 }
19695 fprintf(fd, "\n");
19696 }
19697 }
19698 }
Bram Moolenaar73830342005-02-28 22:48:19 +000019699
19700 if (sorttab != NULL && st_len > 0)
19701 {
19702 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19703 prof_total_cmp);
19704 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
19705 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
19706 prof_self_cmp);
19707 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
19708 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000019709}
Bram Moolenaar73830342005-02-28 22:48:19 +000019710
19711 static void
19712prof_sort_list(fd, sorttab, st_len, title, prefer_self)
19713 FILE *fd;
19714 ufunc_T **sorttab;
19715 int st_len;
19716 char *title;
19717 int prefer_self; /* when equal print only self time */
19718{
19719 int i;
19720 ufunc_T *fp;
19721
19722 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
19723 fprintf(fd, "count total (s) self (s) function\n");
19724 for (i = 0; i < 20 && i < st_len; ++i)
19725 {
19726 fp = sorttab[i];
19727 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
19728 prefer_self);
19729 if (fp->uf_name[0] == K_SPECIAL)
19730 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
19731 else
19732 fprintf(fd, " %s()\n", fp->uf_name);
19733 }
19734 fprintf(fd, "\n");
19735}
19736
19737/*
19738 * Print the count and times for one function or function line.
19739 */
19740 static void
19741prof_func_line(fd, count, total, self, prefer_self)
19742 FILE *fd;
19743 int count;
19744 proftime_T *total;
19745 proftime_T *self;
19746 int prefer_self; /* when equal print only self time */
19747{
19748 if (count > 0)
19749 {
19750 fprintf(fd, "%5d ", count);
19751 if (prefer_self && profile_equal(total, self))
19752 fprintf(fd, " ");
19753 else
19754 fprintf(fd, "%s ", profile_msg(total));
19755 if (!prefer_self && profile_equal(total, self))
19756 fprintf(fd, " ");
19757 else
19758 fprintf(fd, "%s ", profile_msg(self));
19759 }
19760 else
19761 fprintf(fd, " ");
19762}
19763
19764/*
19765 * Compare function for total time sorting.
19766 */
19767 static int
19768#ifdef __BORLANDC__
19769_RTLENTRYF
19770#endif
19771prof_total_cmp(s1, s2)
19772 const void *s1;
19773 const void *s2;
19774{
19775 ufunc_T *p1, *p2;
19776
19777 p1 = *(ufunc_T **)s1;
19778 p2 = *(ufunc_T **)s2;
19779 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
19780}
19781
19782/*
19783 * Compare function for self time sorting.
19784 */
19785 static int
19786#ifdef __BORLANDC__
19787_RTLENTRYF
19788#endif
19789prof_self_cmp(s1, s2)
19790 const void *s1;
19791 const void *s2;
19792{
19793 ufunc_T *p1, *p2;
19794
19795 p1 = *(ufunc_T **)s1;
19796 p2 = *(ufunc_T **)s2;
19797 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
19798}
19799
Bram Moolenaar05159a02005-02-26 23:04:13 +000019800#endif
19801
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019802/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019803 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019804 * Return TRUE if a package was loaded.
19805 */
19806 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019807script_autoload(name, reload)
19808 char_u *name;
19809 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019810{
19811 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019812 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019813 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019814 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019815
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019816 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019817 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019818 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019819 return FALSE;
19820
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019821 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019822
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019823 /* Find the name in the list of previously loaded package names. Skip
19824 * "autoload/", it's always the same. */
19825 for (i = 0; i < ga_loaded.ga_len; ++i)
19826 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
19827 break;
19828 if (!reload && i < ga_loaded.ga_len)
19829 ret = FALSE; /* was loaded already */
19830 else
19831 {
19832 /* Remember the name if it wasn't loaded already. */
19833 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
19834 {
19835 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
19836 tofree = NULL;
19837 }
19838
19839 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000019840 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019841 ret = TRUE;
19842 }
19843
19844 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019845 return ret;
19846}
19847
19848/*
19849 * Return the autoload script name for a function or variable name.
19850 * Returns NULL when out of memory.
19851 */
19852 static char_u *
19853autoload_name(name)
19854 char_u *name;
19855{
19856 char_u *p;
19857 char_u *scriptname;
19858
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019859 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019860 scriptname = alloc((unsigned)(STRLEN(name) + 14));
19861 if (scriptname == NULL)
19862 return FALSE;
19863 STRCPY(scriptname, "autoload/");
19864 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019865 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019866 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019867 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019868 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019869 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000019870}
19871
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19873
19874/*
19875 * Function given to ExpandGeneric() to obtain the list of user defined
19876 * function names.
19877 */
19878 char_u *
19879get_user_func_name(xp, idx)
19880 expand_T *xp;
19881 int idx;
19882{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019883 static long_u done;
19884 static hashitem_T *hi;
19885 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886
19887 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019889 done = 0;
19890 hi = func_hashtab.ht_array;
19891 }
19892 if (done < func_hashtab.ht_used)
19893 {
19894 if (done++ > 0)
19895 ++hi;
19896 while (HASHITEM_EMPTY(hi))
19897 ++hi;
19898 fp = HI2UF(hi);
19899
19900 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
19901 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902
19903 cat_func_name(IObuff, fp);
19904 if (xp->xp_context != EXPAND_USER_FUNC)
19905 {
19906 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019907 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 STRCAT(IObuff, ")");
19909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 return IObuff;
19911 }
19912 return NULL;
19913}
19914
19915#endif /* FEAT_CMDL_COMPL */
19916
19917/*
19918 * Copy the function name of "fp" to buffer "buf".
19919 * "buf" must be able to hold the function name plus three bytes.
19920 * Takes care of script-local function names.
19921 */
19922 static void
19923cat_func_name(buf, fp)
19924 char_u *buf;
19925 ufunc_T *fp;
19926{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019927 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 {
19929 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019930 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 }
19932 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019933 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934}
19935
19936/*
19937 * ":delfunction {name}"
19938 */
19939 void
19940ex_delfunction(eap)
19941 exarg_T *eap;
19942{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019943 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 char_u *p;
19945 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947
19948 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019949 name = trans_function_name(&p, eap->skip, 0, &fudi);
19950 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019952 {
19953 if (fudi.fd_dict != NULL && !eap->skip)
19954 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 if (!ends_excmd(*skipwhite(p)))
19958 {
19959 vim_free(name);
19960 EMSG(_(e_trailing));
19961 return;
19962 }
19963 eap->nextcmd = check_nextcmd(p);
19964 if (eap->nextcmd != NULL)
19965 *p = NUL;
19966
19967 if (!eap->skip)
19968 fp = find_func(name);
19969 vim_free(name);
19970
19971 if (!eap->skip)
19972 {
19973 if (fp == NULL)
19974 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000019975 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 return;
19977 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019978 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 {
19980 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
19981 return;
19982 }
19983
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019984 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019986 /* Delete the dict item that refers to the function, it will
19987 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019988 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019990 else
19991 func_free(fp);
19992 }
19993}
19994
19995/*
19996 * Free a function and remove it from the list of functions.
19997 */
19998 static void
19999func_free(fp)
20000 ufunc_T *fp;
20001{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020002 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020003
20004 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020005 ga_clear_strings(&(fp->uf_args));
20006 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000020007#ifdef FEAT_PROFILE
20008 vim_free(fp->uf_tml_count);
20009 vim_free(fp->uf_tml_total);
20010 vim_free(fp->uf_tml_self);
20011#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020012
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020013 /* remove the function from the function hashtable */
20014 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
20015 if (HASHITEM_EMPTY(hi))
20016 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020017 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020018 hash_remove(&func_hashtab, hi);
20019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020020 vim_free(fp);
20021}
20022
20023/*
20024 * Unreference a Function: decrement the reference count and free it when it
20025 * becomes zero. Only for numbered functions.
20026 */
20027 static void
20028func_unref(name)
20029 char_u *name;
20030{
20031 ufunc_T *fp;
20032
20033 if (name != NULL && isdigit(*name))
20034 {
20035 fp = find_func(name);
20036 if (fp == NULL)
20037 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020038 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020039 {
20040 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020041 * when "uf_calls" becomes zero. */
20042 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020043 func_free(fp);
20044 }
20045 }
20046}
20047
20048/*
20049 * Count a reference to a Function.
20050 */
20051 static void
20052func_ref(name)
20053 char_u *name;
20054{
20055 ufunc_T *fp;
20056
20057 if (name != NULL && isdigit(*name))
20058 {
20059 fp = find_func(name);
20060 if (fp == NULL)
20061 EMSG2(_(e_intern2), "func_ref()");
20062 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020063 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 }
20065}
20066
20067/*
20068 * Call a user function.
20069 */
20070 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000020071call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 ufunc_T *fp; /* pointer to function */
20073 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000020074 typval_T *argvars; /* arguments */
20075 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 linenr_T firstline; /* first line of range */
20077 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079{
Bram Moolenaar33570922005-01-25 22:26:29 +000020080 char_u *save_sourcing_name;
20081 linenr_T save_sourcing_lnum;
20082 scid_T save_current_SID;
20083 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000020084 int save_did_emsg;
20085 static int depth = 0;
20086 dictitem_T *v;
20087 int fixvar_idx = 0; /* index in fixvar[] */
20088 int i;
20089 int ai;
20090 char_u numbuf[NUMBUFLEN];
20091 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020092#ifdef FEAT_PROFILE
20093 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020094 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096
20097 /* If depth of calling is getting too high, don't execute the function */
20098 if (depth >= p_mfd)
20099 {
20100 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 rettv->v_type = VAR_NUMBER;
20102 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 return;
20104 }
20105 ++depth;
20106
20107 line_breakcheck(); /* check for CTRL-C hit */
20108
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020109 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 fc.rettv = rettv;
20113 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 fc.linenr = 0;
20115 fc.returned = FALSE;
20116 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020118 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 fc.dbg_tick = debug_tick;
20120
Bram Moolenaar33570922005-01-25 22:26:29 +000020121 /*
20122 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20123 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20124 * each argument variable and saves a lot of time.
20125 */
20126 /*
20127 * Init l: variables.
20128 */
20129 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000020130 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020131 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020132 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20133 * some compiler that checks the destination size. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 v = &fc.fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000020135 name = v->di_key;
20136 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000020137 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
20138 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
20139 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020140 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020141 v->di_tv.vval.v_dict = selfdict;
20142 ++selfdict->dv_refcount;
20143 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020144
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 /*
20146 * Init a: variables.
20147 * Set a:0 to "argcount".
20148 * Set a:000 to a list with room for the "..." arguments.
20149 */
20150 init_var_dict(&fc.l_avars, &fc.l_avars_var);
20151 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020152 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 v = &fc.fixvar[fixvar_idx++].var;
20154 STRCPY(v->di_key, "000");
20155 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20156 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20157 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020158 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020159 v->di_tv.vval.v_list = &fc.l_varlist;
20160 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
20161 fc.l_varlist.lv_refcount = 99999;
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000020162 fc.l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020163
20164 /*
20165 * Set a:firstline to "firstline" and a:lastline to "lastline".
20166 * Set a:name to named arguments.
20167 * Set a:N to the "..." arguments.
20168 */
20169 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
20170 (varnumber_T)firstline);
20171 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
20172 (varnumber_T)lastline);
20173 for (i = 0; i < argcount; ++i)
20174 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020175 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020176 if (ai < 0)
20177 /* named argument a:name */
20178 name = FUNCARG(fp, i);
20179 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020180 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 /* "..." argument a:1, a:2, etc. */
20182 sprintf((char *)numbuf, "%d", ai + 1);
20183 name = numbuf;
20184 }
20185 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
20186 {
20187 v = &fc.fixvar[fixvar_idx++].var;
20188 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20189 }
20190 else
20191 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020192 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20193 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000020194 if (v == NULL)
20195 break;
20196 v->di_flags = DI_FLAGS_RO;
20197 }
20198 STRCPY(v->di_key, name);
20199 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
20200
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020201 /* Note: the values are copied directly to avoid alloc/free.
20202 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020203 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020204 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020205
20206 if (ai >= 0 && ai < MAX_FUNC_ARGS)
20207 {
20208 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
20209 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020210 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020211 }
20212 }
20213
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 /* Don't redraw while executing the function. */
20215 ++RedrawingDisabled;
20216 save_sourcing_name = sourcing_name;
20217 save_sourcing_lnum = sourcing_lnum;
20218 sourcing_lnum = 1;
20219 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020220 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 if (sourcing_name != NULL)
20222 {
20223 if (save_sourcing_name != NULL
20224 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
20225 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
20226 else
20227 STRCPY(sourcing_name, "function ");
20228 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
20229
20230 if (p_verbose >= 12)
20231 {
20232 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020233 verbose_enter_scroll();
20234
Bram Moolenaar555b2802005-05-19 21:08:39 +000020235 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 if (p_verbose >= 14)
20237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020239 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020240 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020241 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242
20243 msg_puts((char_u *)"(");
20244 for (i = 0; i < argcount; ++i)
20245 {
20246 if (i > 0)
20247 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020248 if (argvars[i].v_type == VAR_NUMBER)
20249 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 else
20251 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020252 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
20253 if (s != NULL)
20254 {
20255 trunc_string(s, buf, MSG_BUF_CLEN);
20256 msg_puts(buf);
20257 vim_free(tofree);
20258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 }
20260 }
20261 msg_puts((char_u *)")");
20262 }
20263 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020264
20265 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 --no_wait_return;
20267 }
20268 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020269#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020270 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020271 {
20272 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
20273 func_do_profile(fp);
20274 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020275 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020276 {
20277 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020278 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020279 profile_zero(&fp->uf_tm_children);
20280 }
20281 script_prof_save(&wait_start);
20282 }
20283#endif
20284
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020286 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 save_did_emsg = did_emsg;
20288 did_emsg = FALSE;
20289
20290 /* call do_cmdline() to execute the lines */
20291 do_cmdline(NULL, get_func_line, (void *)&fc,
20292 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
20293
20294 --RedrawingDisabled;
20295
20296 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020297 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020299 clear_tv(rettv);
20300 rettv->v_type = VAR_NUMBER;
20301 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 }
20303
Bram Moolenaar05159a02005-02-26 23:04:13 +000020304#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020305 if (do_profiling == PROF_YES && (fp->uf_profiling
20306 || (fc.caller != NULL && &fc.caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000020307 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020308 profile_end(&call_start);
20309 profile_sub_wait(&wait_start, &call_start);
20310 profile_add(&fp->uf_tm_total, &call_start);
20311 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020312 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020313 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000020314 profile_add(&fc.caller->func->uf_tm_children, &call_start);
20315 profile_add(&fc.caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020316 }
20317 }
20318#endif
20319
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 /* when being verbose, mention the return value */
20321 if (p_verbose >= 12)
20322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020324 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000020327 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020328 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000020329 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
20330 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000020331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000020333 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000020334 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000020335 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020336 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020337
Bram Moolenaar555b2802005-05-19 21:08:39 +000020338 /* The value may be very long. Skip the middle part, so that we
20339 * have some idea how it starts and ends. smsg() would always
20340 * truncate it at the end. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020341 s = tv2string(fc.rettv, &tofree, numbuf2, 0);
20342 if (s != NULL)
20343 {
20344 trunc_string(s, buf, MSG_BUF_CLEN);
20345 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
20346 vim_free(tofree);
20347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 }
20349 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020350
20351 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 --no_wait_return;
20353 }
20354
20355 vim_free(sourcing_name);
20356 sourcing_name = save_sourcing_name;
20357 sourcing_lnum = save_sourcing_lnum;
20358 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020359#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020360 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020361 script_prof_restore(&wait_start);
20362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
20364 if (p_verbose >= 12 && sourcing_name != NULL)
20365 {
20366 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020367 verbose_enter_scroll();
20368
Bram Moolenaar555b2802005-05-19 21:08:39 +000020369 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000020371
20372 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 --no_wait_return;
20374 }
20375
20376 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020377 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378
Bram Moolenaar33570922005-01-25 22:26:29 +000020379 /* The a: variables typevals were not alloced, only free the allocated
20380 * variables. */
20381 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
20382
20383 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 --depth;
20385}
20386
20387/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020388 * Add a number variable "name" to dict "dp" with value "nr".
20389 */
20390 static void
20391add_nr_var(dp, v, name, nr)
20392 dict_T *dp;
20393 dictitem_T *v;
20394 char *name;
20395 varnumber_T nr;
20396{
20397 STRCPY(v->di_key, name);
20398 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20399 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
20400 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020401 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020402 v->di_tv.vval.v_number = nr;
20403}
20404
20405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 * ":return [expr]"
20407 */
20408 void
20409ex_return(eap)
20410 exarg_T *eap;
20411{
20412 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 int returning = FALSE;
20415
20416 if (current_funccal == NULL)
20417 {
20418 EMSG(_("E133: :return not inside a function"));
20419 return;
20420 }
20421
20422 if (eap->skip)
20423 ++emsg_skip;
20424
20425 eap->nextcmd = NULL;
20426 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020427 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 {
20429 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020430 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 }
20434 /* It's safer to return also on error. */
20435 else if (!eap->skip)
20436 {
20437 /*
20438 * Return unless the expression evaluation has been cancelled due to an
20439 * aborting error, an interrupt, or an exception.
20440 */
20441 if (!aborting())
20442 returning = do_return(eap, FALSE, TRUE, NULL);
20443 }
20444
20445 /* When skipping or the return gets pending, advance to the next command
20446 * in this line (!returning). Otherwise, ignore the rest of the line.
20447 * Following lines will be ignored by get_func_line(). */
20448 if (returning)
20449 eap->nextcmd = NULL;
20450 else if (eap->nextcmd == NULL) /* no argument */
20451 eap->nextcmd = check_nextcmd(arg);
20452
20453 if (eap->skip)
20454 --emsg_skip;
20455}
20456
20457/*
20458 * Return from a function. Possibly makes the return pending. Also called
20459 * for a pending return at the ":endtry" or after returning from an extra
20460 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000020461 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020462 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 * FALSE when the return gets pending.
20464 */
20465 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020466do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 exarg_T *eap;
20468 int reanimate;
20469 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020470 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471{
20472 int idx;
20473 struct condstack *cstack = eap->cstack;
20474
20475 if (reanimate)
20476 /* Undo the return. */
20477 current_funccal->returned = FALSE;
20478
20479 /*
20480 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20481 * not in its finally clause (which then is to be executed next) is found.
20482 * In this case, make the ":return" pending for execution at the ":endtry".
20483 * Otherwise, return normally.
20484 */
20485 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
20486 if (idx >= 0)
20487 {
20488 cstack->cs_pending[idx] = CSTP_RETURN;
20489
20490 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 /* A pending return again gets pending. "rettv" points to an
20492 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020494 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 else
20496 {
20497 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020498 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020500 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020502 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 {
20504 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020505 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020506 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 else
20508 EMSG(_(e_outofmem));
20509 }
20510 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020511 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512
20513 if (reanimate)
20514 {
20515 /* The pending return value could be overwritten by a ":return"
20516 * without argument in a finally clause; reset the default
20517 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020518 current_funccal->rettv->v_type = VAR_NUMBER;
20519 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 }
20521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020522 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 }
20524 else
20525 {
20526 current_funccal->returned = TRUE;
20527
20528 /* If the return is carried out now, store the return value. For
20529 * a return immediately after reanimation, the value is already
20530 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020531 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020533 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000020534 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020536 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537 }
20538 }
20539
20540 return idx < 0;
20541}
20542
20543/*
20544 * Free the variable with a pending return value.
20545 */
20546 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020547discard_pending_return(rettv)
20548 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549{
Bram Moolenaar33570922005-01-25 22:26:29 +000020550 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551}
20552
20553/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020554 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 * is an allocated string. Used by report_pending() for verbose messages.
20556 */
20557 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020558get_return_cmd(rettv)
20559 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020561 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020562 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020563 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020565 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020566 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020567 if (s == NULL)
20568 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020569
20570 STRCPY(IObuff, ":return ");
20571 STRNCPY(IObuff + 8, s, IOSIZE - 8);
20572 if (STRLEN(s) + 8 >= IOSIZE)
20573 STRCPY(IObuff + IOSIZE - 4, "...");
20574 vim_free(tofree);
20575 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576}
20577
20578/*
20579 * Get next function line.
20580 * Called by do_cmdline() to get the next line.
20581 * Returns allocated string, or NULL for end of function.
20582 */
20583/* ARGSUSED */
20584 char_u *
20585get_func_line(c, cookie, indent)
20586 int c; /* not used */
20587 void *cookie;
20588 int indent; /* not used */
20589{
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020591 ufunc_T *fp = fcp->func;
20592 char_u *retval;
20593 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594
20595 /* If breakpoints have been added/deleted need to check for it. */
20596 if (fcp->dbg_tick != debug_tick)
20597 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020598 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 sourcing_lnum);
20600 fcp->dbg_tick = debug_tick;
20601 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000020602#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020603 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020604 func_line_end(cookie);
20605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606
Bram Moolenaar05159a02005-02-26 23:04:13 +000020607 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020608 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
20609 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 retval = NULL;
20611 else
20612 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020613 /* Skip NULL lines (continuation lines). */
20614 while (fcp->linenr < gap->ga_len
20615 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
20616 ++fcp->linenr;
20617 if (fcp->linenr >= gap->ga_len)
20618 retval = NULL;
20619 else
20620 {
20621 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
20622 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020623#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000020624 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020625 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020626#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 }
20629
20630 /* Did we encounter a breakpoint? */
20631 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
20632 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020633 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020635 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 sourcing_lnum);
20637 fcp->dbg_tick = debug_tick;
20638 }
20639
20640 return retval;
20641}
20642
Bram Moolenaar05159a02005-02-26 23:04:13 +000020643#if defined(FEAT_PROFILE) || defined(PROTO)
20644/*
20645 * Called when starting to read a function line.
20646 * "sourcing_lnum" must be correct!
20647 * When skipping lines it may not actually be executed, but we won't find out
20648 * until later and we need to store the time now.
20649 */
20650 void
20651func_line_start(cookie)
20652 void *cookie;
20653{
20654 funccall_T *fcp = (funccall_T *)cookie;
20655 ufunc_T *fp = fcp->func;
20656
20657 if (fp->uf_profiling && sourcing_lnum >= 1
20658 && sourcing_lnum <= fp->uf_lines.ga_len)
20659 {
20660 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020661 /* Skip continuation lines. */
20662 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
20663 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020664 fp->uf_tml_execed = FALSE;
20665 profile_start(&fp->uf_tml_start);
20666 profile_zero(&fp->uf_tml_children);
20667 profile_get_wait(&fp->uf_tml_wait);
20668 }
20669}
20670
20671/*
20672 * Called when actually executing a function line.
20673 */
20674 void
20675func_line_exec(cookie)
20676 void *cookie;
20677{
20678 funccall_T *fcp = (funccall_T *)cookie;
20679 ufunc_T *fp = fcp->func;
20680
20681 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20682 fp->uf_tml_execed = TRUE;
20683}
20684
20685/*
20686 * Called when done with a function line.
20687 */
20688 void
20689func_line_end(cookie)
20690 void *cookie;
20691{
20692 funccall_T *fcp = (funccall_T *)cookie;
20693 ufunc_T *fp = fcp->func;
20694
20695 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
20696 {
20697 if (fp->uf_tml_execed)
20698 {
20699 ++fp->uf_tml_count[fp->uf_tml_idx];
20700 profile_end(&fp->uf_tml_start);
20701 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020702 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000020703 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
20704 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020705 }
20706 fp->uf_tml_idx = -1;
20707 }
20708}
20709#endif
20710
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711/*
20712 * Return TRUE if the currently active function should be ended, because a
20713 * return was encountered or an error occured. Used inside a ":while".
20714 */
20715 int
20716func_has_ended(cookie)
20717 void *cookie;
20718{
Bram Moolenaar33570922005-01-25 22:26:29 +000020719 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720
20721 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20722 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020723 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 || fcp->returned);
20725}
20726
20727/*
20728 * return TRUE if cookie indicates a function which "abort"s on errors.
20729 */
20730 int
20731func_has_abort(cookie)
20732 void *cookie;
20733{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020734 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735}
20736
20737#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20738typedef enum
20739{
20740 VAR_FLAVOUR_DEFAULT,
20741 VAR_FLAVOUR_SESSION,
20742 VAR_FLAVOUR_VIMINFO
20743} var_flavour_T;
20744
20745static var_flavour_T var_flavour __ARGS((char_u *varname));
20746
20747 static var_flavour_T
20748var_flavour(varname)
20749 char_u *varname;
20750{
20751 char_u *p = varname;
20752
20753 if (ASCII_ISUPPER(*p))
20754 {
20755 while (*(++p))
20756 if (ASCII_ISLOWER(*p))
20757 return VAR_FLAVOUR_SESSION;
20758 return VAR_FLAVOUR_VIMINFO;
20759 }
20760 else
20761 return VAR_FLAVOUR_DEFAULT;
20762}
20763#endif
20764
20765#if defined(FEAT_VIMINFO) || defined(PROTO)
20766/*
20767 * Restore global vars that start with a capital from the viminfo file
20768 */
20769 int
20770read_viminfo_varlist(virp, writing)
20771 vir_T *virp;
20772 int writing;
20773{
20774 char_u *tab;
20775 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000020776 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777
20778 if (!writing && (find_viminfo_parameter('!') != NULL))
20779 {
20780 tab = vim_strchr(virp->vir_line + 1, '\t');
20781 if (tab != NULL)
20782 {
20783 *tab++ = '\0'; /* isolate the variable name */
20784 if (*tab == 'S') /* string var */
20785 is_string = TRUE;
20786
20787 tab = vim_strchr(tab, '\t');
20788 if (tab != NULL)
20789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 if (is_string)
20791 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020792 tv.v_type = VAR_STRING;
20793 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 }
20796 else
20797 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020798 tv.v_type = VAR_NUMBER;
20799 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020801 set_var(virp->vir_line + 1, &tv, FALSE);
20802 if (is_string)
20803 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 }
20805 }
20806 }
20807
20808 return viminfo_readline(virp);
20809}
20810
20811/*
20812 * Write global vars that start with a capital to the viminfo file
20813 */
20814 void
20815write_viminfo_varlist(fp)
20816 FILE *fp;
20817{
Bram Moolenaar33570922005-01-25 22:26:29 +000020818 hashitem_T *hi;
20819 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020820 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020821 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020822 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020823 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020824 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825
20826 if (find_viminfo_parameter('!') == NULL)
20827 return;
20828
20829 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000020830
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020831 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020832 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020834 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020836 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020837 this_var = HI2DI(hi);
20838 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020839 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020840 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000020841 {
20842 case VAR_STRING: s = "STR"; break;
20843 case VAR_NUMBER: s = "NUM"; break;
20844 default: continue;
20845 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000020847 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020848 if (p != NULL)
20849 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000020850 vim_free(tofree);
20851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 }
20853 }
20854}
20855#endif
20856
20857#if defined(FEAT_SESSION) || defined(PROTO)
20858 int
20859store_session_globals(fd)
20860 FILE *fd;
20861{
Bram Moolenaar33570922005-01-25 22:26:29 +000020862 hashitem_T *hi;
20863 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000020864 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 char_u *p, *t;
20866
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020867 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020870 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020872 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020873 this_var = HI2DI(hi);
20874 if ((this_var->di_tv.v_type == VAR_NUMBER
20875 || this_var->di_tv.v_type == VAR_STRING)
20876 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000020877 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020878 /* Escape special characters with a backslash. Turn a LF and
20879 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020880 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000020881 (char_u *)"\\\"\n\r");
20882 if (p == NULL) /* out of memory */
20883 break;
20884 for (t = p; *t != NUL; ++t)
20885 if (*t == '\n')
20886 *t = 'n';
20887 else if (*t == '\r')
20888 *t = 'r';
20889 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000020890 this_var->di_key,
20891 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20892 : ' ',
20893 p,
20894 (this_var->di_tv.v_type == VAR_STRING) ? '"'
20895 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000020896 || put_eol(fd) == FAIL)
20897 {
20898 vim_free(p);
20899 return FAIL;
20900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 vim_free(p);
20902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 }
20904 }
20905 return OK;
20906}
20907#endif
20908
Bram Moolenaar661b1822005-07-28 22:36:45 +000020909/*
20910 * Display script name where an item was last set.
20911 * Should only be invoked when 'verbose' is non-zero.
20912 */
20913 void
20914last_set_msg(scriptID)
20915 scid_T scriptID;
20916{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020917 char_u *p;
20918
Bram Moolenaar661b1822005-07-28 22:36:45 +000020919 if (scriptID != 0)
20920 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020921 p = home_replace_save(NULL, get_scriptname(scriptID));
20922 if (p != NULL)
20923 {
20924 verbose_enter();
20925 MSG_PUTS(_("\n\tLast set from "));
20926 MSG_PUTS(p);
20927 vim_free(p);
20928 verbose_leave();
20929 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000020930 }
20931}
20932
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933#endif /* FEAT_EVAL */
20934
20935#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20936
20937
20938#ifdef WIN3264
20939/*
20940 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20941 */
20942static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20943static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
20944static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
20945
20946/*
20947 * Get the short pathname of a file.
Bram Moolenaarbae0c162007-05-10 19:30:25 +000020948 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 */
20950 static int
20951get_short_pathname(fnamep, bufp, fnamelen)
20952 char_u **fnamep;
20953 char_u **bufp;
20954 int *fnamelen;
20955{
20956 int l,len;
20957 char_u *newbuf;
20958
20959 len = *fnamelen;
20960
20961 l = GetShortPathName(*fnamep, *fnamep, len);
20962 if (l > len - 1)
20963 {
20964 /* If that doesn't work (not enough space), then save the string
20965 * and try again with a new buffer big enough
20966 */
20967 newbuf = vim_strnsave(*fnamep, l);
20968 if (newbuf == NULL)
20969 return 0;
20970
20971 vim_free(*bufp);
20972 *fnamep = *bufp = newbuf;
20973
20974 l = GetShortPathName(*fnamep,*fnamep,l+1);
20975
20976 /* Really should always succeed, as the buffer is big enough */
20977 }
20978
20979 *fnamelen = l;
20980 return 1;
20981}
20982
20983/*
20984 * Create a short path name. Returns the length of the buffer it needs.
20985 * Doesn't copy over the end of the buffer passed in.
20986 */
20987 static int
20988shortpath_for_invalid_fname(fname, bufp, fnamelen)
20989 char_u **fname;
20990 char_u **bufp;
20991 int *fnamelen;
20992{
20993 char_u *s, *p, *pbuf2, *pbuf3;
20994 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020995 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996
20997 /* Make a copy */
20998 len2 = *fnamelen;
20999 pbuf2 = vim_strnsave(*fname, len2);
21000 pbuf3 = NULL;
21001
21002 s = pbuf2 + len2 - 1; /* Find the end */
21003 slen = 1;
21004 plen = len2;
21005
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021006 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 {
21008 --s;
21009 ++slen;
21010 --plen;
21011 }
21012
21013 do
21014 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021015 /* Go back one path-separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021016 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 {
21018 --s;
21019 ++slen;
21020 --plen;
21021 }
21022 if (s <= pbuf2)
21023 break;
21024
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021025 /* Remember the character that is about to be splatted */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 ch = *s;
21027 *s = 0; /* get_short_pathname requires a null-terminated string */
21028
21029 /* Try it in situ */
21030 p = pbuf2;
21031 if (!get_short_pathname(&p, &pbuf3, &plen))
21032 {
21033 vim_free(pbuf2);
21034 return -1;
21035 }
21036 *s = ch; /* Preserve the string */
21037 } while (plen == 0);
21038
21039 if (plen > 0)
21040 {
Bram Moolenaarbae0c162007-05-10 19:30:25 +000021041 /* Remember the length of the new string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 *fnamelen = len = plen + slen;
21043 vim_free(*bufp);
21044 if (len > len2)
21045 {
21046 /* If there's not enough space in the currently allocated string,
21047 * then copy it to a buffer big enough.
21048 */
21049 *fname= *bufp = vim_strnsave(p, len);
21050 if (*fname == NULL)
21051 return -1;
21052 }
21053 else
21054 {
21055 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21056 *fname = *bufp = pbuf2;
21057 if (p != pbuf2)
21058 strncpy(*fname, p, plen);
21059 pbuf2 = NULL;
21060 }
21061 /* Concat the next bit */
21062 strncpy(*fname + plen, s, slen);
21063 (*fname)[len] = '\0';
21064 }
21065 vim_free(pbuf3);
21066 vim_free(pbuf2);
21067 return 0;
21068}
21069
21070/*
21071 * Get a pathname for a partial path.
21072 */
21073 static int
21074shortpath_for_partial(fnamep, bufp, fnamelen)
21075 char_u **fnamep;
21076 char_u **bufp;
21077 int *fnamelen;
21078{
21079 int sepcount, len, tflen;
21080 char_u *p;
21081 char_u *pbuf, *tfname;
21082 int hasTilde;
21083
21084 /* Count up the path seperators from the RHS.. so we know which part
21085 * of the path to return.
21086 */
21087 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021088 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 if (vim_ispathsep(*p))
21090 ++sepcount;
21091
21092 /* Need full path first (use expand_env() to remove a "~/") */
21093 hasTilde = (**fnamep == '~');
21094 if (hasTilde)
21095 pbuf = tfname = expand_env_save(*fnamep);
21096 else
21097 pbuf = tfname = FullName_save(*fnamep, FALSE);
21098
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021099 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100
21101 if (!get_short_pathname(&tfname, &pbuf, &len))
21102 return -1;
21103
21104 if (len == 0)
21105 {
21106 /* Don't have a valid filename, so shorten the rest of the
21107 * path if we can. This CAN give us invalid 8.3 filenames, but
21108 * there's not a lot of point in guessing what it might be.
21109 */
21110 len = tflen;
21111 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
21112 return -1;
21113 }
21114
21115 /* Count the paths backward to find the beginning of the desired string. */
21116 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021117 {
21118#ifdef FEAT_MBYTE
21119 if (has_mbyte)
21120 p -= mb_head_off(tfname, p);
21121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 if (vim_ispathsep(*p))
21123 {
21124 if (sepcount == 0 || (hasTilde && sepcount == 1))
21125 break;
21126 else
21127 sepcount --;
21128 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 if (hasTilde)
21131 {
21132 --p;
21133 if (p >= tfname)
21134 *p = '~';
21135 else
21136 return -1;
21137 }
21138 else
21139 ++p;
21140
21141 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21142 vim_free(*bufp);
21143 *fnamelen = (int)STRLEN(p);
21144 *bufp = pbuf;
21145 *fnamep = p;
21146
21147 return 0;
21148}
21149#endif /* WIN3264 */
21150
21151/*
21152 * Adjust a filename, according to a string of modifiers.
21153 * *fnamep must be NUL terminated when called. When returning, the length is
21154 * determined by *fnamelen.
21155 * Returns valid flags.
21156 * When there is an error, *fnamep is set to NULL.
21157 */
21158 int
21159modify_fname(src, usedlen, fnamep, bufp, fnamelen)
21160 char_u *src; /* string with modifiers */
21161 int *usedlen; /* characters after src that are used */
21162 char_u **fnamep; /* file name so far */
21163 char_u **bufp; /* buffer for allocated file name or NULL */
21164 int *fnamelen; /* length of fnamep */
21165{
21166 int valid = 0;
21167 char_u *tail;
21168 char_u *s, *p, *pbuf;
21169 char_u dirname[MAXPATHL];
21170 int c;
21171 int has_fullname = 0;
21172#ifdef WIN3264
21173 int has_shortname = 0;
21174#endif
21175
21176repeat:
21177 /* ":p" - full path/file_name */
21178 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
21179 {
21180 has_fullname = 1;
21181
21182 valid |= VALID_PATH;
21183 *usedlen += 2;
21184
21185 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21186 if ((*fnamep)[0] == '~'
21187#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21188 && ((*fnamep)[1] == '/'
21189# ifdef BACKSLASH_IN_FILENAME
21190 || (*fnamep)[1] == '\\'
21191# endif
21192 || (*fnamep)[1] == NUL)
21193
21194#endif
21195 )
21196 {
21197 *fnamep = expand_env_save(*fnamep);
21198 vim_free(*bufp); /* free any allocated file name */
21199 *bufp = *fnamep;
21200 if (*fnamep == NULL)
21201 return -1;
21202 }
21203
21204 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021205 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 {
21207 if (vim_ispathsep(*p)
21208 && p[1] == '.'
21209 && (p[2] == NUL
21210 || vim_ispathsep(p[2])
21211 || (p[2] == '.'
21212 && (p[3] == NUL || vim_ispathsep(p[3])))))
21213 break;
21214 }
21215
21216 /* FullName_save() is slow, don't use it when not needed. */
21217 if (*p != NUL || !vim_isAbsName(*fnamep))
21218 {
21219 *fnamep = FullName_save(*fnamep, *p != NUL);
21220 vim_free(*bufp); /* free any allocated file name */
21221 *bufp = *fnamep;
21222 if (*fnamep == NULL)
21223 return -1;
21224 }
21225
21226 /* Append a path separator to a directory. */
21227 if (mch_isdir(*fnamep))
21228 {
21229 /* Make room for one or two extra characters. */
21230 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
21231 vim_free(*bufp); /* free any allocated file name */
21232 *bufp = *fnamep;
21233 if (*fnamep == NULL)
21234 return -1;
21235 add_pathsep(*fnamep);
21236 }
21237 }
21238
21239 /* ":." - path relative to the current directory */
21240 /* ":~" - path relative to the home directory */
21241 /* ":8" - shortname path - postponed till after */
21242 while (src[*usedlen] == ':'
21243 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
21244 {
21245 *usedlen += 2;
21246 if (c == '8')
21247 {
21248#ifdef WIN3264
21249 has_shortname = 1; /* Postpone this. */
21250#endif
21251 continue;
21252 }
21253 pbuf = NULL;
21254 /* Need full path first (use expand_env() to remove a "~/") */
21255 if (!has_fullname)
21256 {
21257 if (c == '.' && **fnamep == '~')
21258 p = pbuf = expand_env_save(*fnamep);
21259 else
21260 p = pbuf = FullName_save(*fnamep, FALSE);
21261 }
21262 else
21263 p = *fnamep;
21264
21265 has_fullname = 0;
21266
21267 if (p != NULL)
21268 {
21269 if (c == '.')
21270 {
21271 mch_dirname(dirname, MAXPATHL);
21272 s = shorten_fname(p, dirname);
21273 if (s != NULL)
21274 {
21275 *fnamep = s;
21276 if (pbuf != NULL)
21277 {
21278 vim_free(*bufp); /* free any allocated file name */
21279 *bufp = pbuf;
21280 pbuf = NULL;
21281 }
21282 }
21283 }
21284 else
21285 {
21286 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
21287 /* Only replace it when it starts with '~' */
21288 if (*dirname == '~')
21289 {
21290 s = vim_strsave(dirname);
21291 if (s != NULL)
21292 {
21293 *fnamep = s;
21294 vim_free(*bufp);
21295 *bufp = s;
21296 }
21297 }
21298 }
21299 vim_free(pbuf);
21300 }
21301 }
21302
21303 tail = gettail(*fnamep);
21304 *fnamelen = (int)STRLEN(*fnamep);
21305
21306 /* ":h" - head, remove "/file_name", can be repeated */
21307 /* Don't remove the first "/" or "c:\" */
21308 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
21309 {
21310 valid |= VALID_HEAD;
21311 *usedlen += 2;
21312 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000021313 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021314 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315 *fnamelen = (int)(tail - *fnamep);
21316#ifdef VMS
21317 if (*fnamelen > 0)
21318 *fnamelen += 1; /* the path separator is part of the path */
21319#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000021320 if (*fnamelen == 0)
21321 {
21322 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21323 p = vim_strsave((char_u *)".");
21324 if (p == NULL)
21325 return -1;
21326 vim_free(*bufp);
21327 *bufp = *fnamep = tail = p;
21328 *fnamelen = 1;
21329 }
21330 else
21331 {
21332 while (tail > s && !after_pathsep(s, tail))
21333 mb_ptr_back(*fnamep, tail);
21334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 }
21336
21337 /* ":8" - shortname */
21338 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
21339 {
21340 *usedlen += 2;
21341#ifdef WIN3264
21342 has_shortname = 1;
21343#endif
21344 }
21345
21346#ifdef WIN3264
21347 /* Check shortname after we have done 'heads' and before we do 'tails'
21348 */
21349 if (has_shortname)
21350 {
21351 pbuf = NULL;
21352 /* Copy the string if it is shortened by :h */
21353 if (*fnamelen < (int)STRLEN(*fnamep))
21354 {
21355 p = vim_strnsave(*fnamep, *fnamelen);
21356 if (p == 0)
21357 return -1;
21358 vim_free(*bufp);
21359 *bufp = *fnamep = p;
21360 }
21361
21362 /* Split into two implementations - makes it easier. First is where
21363 * there isn't a full name already, second is where there is.
21364 */
21365 if (!has_fullname && !vim_isAbsName(*fnamep))
21366 {
21367 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
21368 return -1;
21369 }
21370 else
21371 {
21372 int l;
21373
21374 /* Simple case, already have the full-name
21375 * Nearly always shorter, so try first time. */
21376 l = *fnamelen;
21377 if (!get_short_pathname(fnamep, bufp, &l))
21378 return -1;
21379
21380 if (l == 0)
21381 {
21382 /* Couldn't find the filename.. search the paths.
21383 */
21384 l = *fnamelen;
21385 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
21386 return -1;
21387 }
21388 *fnamelen = l;
21389 }
21390 }
21391#endif /* WIN3264 */
21392
21393 /* ":t" - tail, just the basename */
21394 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
21395 {
21396 *usedlen += 2;
21397 *fnamelen -= (int)(tail - *fnamep);
21398 *fnamep = tail;
21399 }
21400
21401 /* ":e" - extension, can be repeated */
21402 /* ":r" - root, without extension, can be repeated */
21403 while (src[*usedlen] == ':'
21404 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
21405 {
21406 /* find a '.' in the tail:
21407 * - for second :e: before the current fname
21408 * - otherwise: The last '.'
21409 */
21410 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
21411 s = *fnamep - 2;
21412 else
21413 s = *fnamep + *fnamelen - 1;
21414 for ( ; s > tail; --s)
21415 if (s[0] == '.')
21416 break;
21417 if (src[*usedlen + 1] == 'e') /* :e */
21418 {
21419 if (s > tail)
21420 {
21421 *fnamelen += (int)(*fnamep - (s + 1));
21422 *fnamep = s + 1;
21423#ifdef VMS
21424 /* cut version from the extension */
21425 s = *fnamep + *fnamelen - 1;
21426 for ( ; s > *fnamep; --s)
21427 if (s[0] == ';')
21428 break;
21429 if (s > *fnamep)
21430 *fnamelen = s - *fnamep;
21431#endif
21432 }
21433 else if (*fnamep <= tail)
21434 *fnamelen = 0;
21435 }
21436 else /* :r */
21437 {
21438 if (s > tail) /* remove one extension */
21439 *fnamelen = (int)(s - *fnamep);
21440 }
21441 *usedlen += 2;
21442 }
21443
21444 /* ":s?pat?foo?" - substitute */
21445 /* ":gs?pat?foo?" - global substitute */
21446 if (src[*usedlen] == ':'
21447 && (src[*usedlen + 1] == 's'
21448 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
21449 {
21450 char_u *str;
21451 char_u *pat;
21452 char_u *sub;
21453 int sep;
21454 char_u *flags;
21455 int didit = FALSE;
21456
21457 flags = (char_u *)"";
21458 s = src + *usedlen + 2;
21459 if (src[*usedlen + 1] == 'g')
21460 {
21461 flags = (char_u *)"g";
21462 ++s;
21463 }
21464
21465 sep = *s++;
21466 if (sep)
21467 {
21468 /* find end of pattern */
21469 p = vim_strchr(s, sep);
21470 if (p != NULL)
21471 {
21472 pat = vim_strnsave(s, (int)(p - s));
21473 if (pat != NULL)
21474 {
21475 s = p + 1;
21476 /* find end of substitution */
21477 p = vim_strchr(s, sep);
21478 if (p != NULL)
21479 {
21480 sub = vim_strnsave(s, (int)(p - s));
21481 str = vim_strnsave(*fnamep, *fnamelen);
21482 if (sub != NULL && str != NULL)
21483 {
21484 *usedlen = (int)(p + 1 - src);
21485 s = do_string_sub(str, pat, sub, flags);
21486 if (s != NULL)
21487 {
21488 *fnamep = s;
21489 *fnamelen = (int)STRLEN(s);
21490 vim_free(*bufp);
21491 *bufp = s;
21492 didit = TRUE;
21493 }
21494 }
21495 vim_free(sub);
21496 vim_free(str);
21497 }
21498 vim_free(pat);
21499 }
21500 }
21501 /* after using ":s", repeat all the modifiers */
21502 if (didit)
21503 goto repeat;
21504 }
21505 }
21506
21507 return valid;
21508}
21509
21510/*
21511 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21512 * "flags" can be "g" to do a global substitute.
21513 * Returns an allocated string, NULL for error.
21514 */
21515 char_u *
21516do_string_sub(str, pat, sub, flags)
21517 char_u *str;
21518 char_u *pat;
21519 char_u *sub;
21520 char_u *flags;
21521{
21522 int sublen;
21523 regmatch_T regmatch;
21524 int i;
21525 int do_all;
21526 char_u *tail;
21527 garray_T ga;
21528 char_u *ret;
21529 char_u *save_cpo;
21530
21531 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21532 save_cpo = p_cpo;
21533 p_cpo = (char_u *)"";
21534
21535 ga_init2(&ga, 1, 200);
21536
21537 do_all = (flags[0] == 'g');
21538
21539 regmatch.rm_ic = p_ic;
21540 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
21541 if (regmatch.regprog != NULL)
21542 {
21543 tail = str;
21544 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
21545 {
21546 /*
21547 * Get some space for a temporary buffer to do the substitution
21548 * into. It will contain:
21549 * - The text up to where the match is.
21550 * - The substituted text.
21551 * - The text after the match.
21552 */
21553 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
21554 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
21555 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
21556 {
21557 ga_clear(&ga);
21558 break;
21559 }
21560
21561 /* copy the text up to where the match is */
21562 i = (int)(regmatch.startp[0] - tail);
21563 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
21564 /* add the substituted text */
21565 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
21566 + ga.ga_len + i, TRUE, TRUE, FALSE);
21567 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 /* avoid getting stuck on a match with an empty string */
21569 if (tail == regmatch.endp[0])
21570 {
21571 if (*tail == NUL)
21572 break;
21573 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
21574 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 }
21576 else
21577 {
21578 tail = regmatch.endp[0];
21579 if (*tail == NUL)
21580 break;
21581 }
21582 if (!do_all)
21583 break;
21584 }
21585
21586 if (ga.ga_data != NULL)
21587 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
21588
21589 vim_free(regmatch.regprog);
21590 }
21591
21592 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
21593 ga_clear(&ga);
21594 p_cpo = save_cpo;
21595
21596 return ret;
21597}
21598
21599#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */